def generate_request_token(self, oauth_request, oauth_consumer, oauth_callback=None): """Load the request token in the request :param oauth_request: oauth request :type oauth_request: :class:`oauth2.Request` :param oauth_consumer: oauth consumer :type oauth_consumer: :class:`oauth2.Consumer` :param oauth_callback: callback URL :type oauth_callback: :class:`string` :returns: :class:`oauth2.Token` with the new key and secret :raises: :class:`OAUTHException` if client key is invalid """ client = OAUTH_Client.get_by_key_name(oauth_consumer.key) if client is not None: if oauth_callback is not None: token = OAUTH_Token.generate(oauth_consumer = client, token_callback=oauth_callback) oauthToken = oauth2.Token(token.token_key, token.token_secret) oauthToken.set_callback(oauth_callback) elif client.callback is not None: token = OAUTH_Token.generate(oauth_consumer = client, token_callback=client.callback) oauthToken = oauth2.Token(token.token_key, token.token_secret) oauthToken.set_callback(client.callback) else: token = OAUTH_Token.generate(oauth_consumer = client) oauthToken = oauth2.Token(token.token_key, token.token_secret) return oauthToken raise OAUTHException("Client key invalid")
def generate_access_token(self, token, oauth_consumer): """Generate the new access token :param token: Request token authorized :type token: :class:`oauth2.Token` :param oauth_consumer: Application requesting the access token :type oauth_consumer: :class:`oauth2.Consumer` :param user: User for the authorized token :type user: :class:`georemindme.models.User` :returns: :class:`oauth2.Token` :raises: :class:`OAUTHException` if the token key or consumer key are invalid """ if token.verifier is None: raise OAUTHException('Unauthorized token') savedtoken = OAUTH_Token.get_by_key_name(token.key) client = OAUTH_Client.get_by_key_name(oauth_consumer.key) if savedtoken is not None and client is not None: if token.verifier != savedtoken.token_verifier: raise OAUTHException("Token verifier invalid") accessToken = OAUTH_Token.generate( oauth_consumer = client, oauth_user = savedtoken.oauth_user, access = True ) savedtoken.delete() oauthToken = oauth2.Token(accessToken.token_key, accessToken.token_secret) return oauthToken raise OAUTHException("Token or client key invalid")
def fetch_request_token(self, oauth_request, oauth_consumer, oauth_callback=None): """Load the token in the request :param oauth_request: oauth request :type oauth_request: :class:`oauth2.Request` :param oauth_consumer: oauth consumer :type oauth_consumer: :class:`oauth2.Consumer` :param oauth_callback: callback URL :type oauth_callback: :class:`string` """ client = OAUTH_Client.get_by_key_name(oauth_consumer.key) """ try: key = oauth_request.get_parameter('oauth_token') except: key = None #buscar el token en la BD if key is not None: token = self.fetch_token(oauth_request) if token.oauth_client.key != oauth_consumer.key: raise OAUTHException("Token key invalid") oauthToken = oauth2.Token(token.token_key, token.token_secret) if oauth_callback is not None: token.token_callback(oauth_callback) token.put() oauthToken.set_callback(oauth_callback) elif client.callback is not None: token.token_callback(client.callback) token.put() oauthToken.set_callback(oauth_callback) #el token no existe, crear uno nuevo """ if oauth_callback is not None: token = OAUTH_Token.generate(oauth_consumer = client, token_callback=oauth_callback) oauthToken = oauth2.Token(token.token_key, token.token_secret) oauthToken.set_callback(oauth_callback) elif client.callback is not None: token = OAUTH_Token.generate(oauth_consumer = client, token_callback=client.callback) oauthToken = oauth2.Token(token.token_key, token.token_secret) oauthToken.set_callback(client.callback) else: token = OAUTH_Token.generate(oauth_consumer = client) oauthToken = oauth2.Token(token.token_key, token.token_secret) return oauthToken
def fetch_access_token(self, token, oauth_consumer): """Generate the new access token :param token: Request token authorized :type token: :class:`oauth2.Token` :param oauth_consumer: Application requesting the access token :type oauth_consumer: :class:`oauth2.Consumer` :param user: User for the authorized token :type user: :class:`georemindme.models.User` """ if token.verifier is None: raise OAUTHException('Unauthorized token') token = OAUTH_Token.get_by_key_name(token.key) client = OAUTH_Client.get_by_key_name(oauth_consumer.key) #los token de acceso, tienen el mismo verifier accessToken = OAUTH_Token.generate( oauth_consumer = client, oauth_user = token.oauth_user, access = True ) token.delete() #el token a devolver oauthToken = oauth2.Token(accessToken.token_key, accessToken.token_secret) return oauthToken