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 __init__(self): from django.conf import settings self.consumer_key = settings.OAUTH['google']['app_key'] self.consumer_secret = settings.OAUTH['google']['app_secret'] self.token = oauth2.Token(settings.FUSIONTABLES['token_key'], settings.FUSIONTABLES['token_secret']) self.scope = "https://www.google.com/fusiontables/api/query"
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 _get_twitter_user_info(self, auth_info, key=None, secret=None): """Returns a dict of twitter user using https://api.twitter.com/1/account/verify_credentials.json """ token = oauth1.Token(key=auth_info['oauth_token'], secret=auth_info['oauth_token_secret']) client = self._oauth1_client(token, key, secret) resp, content = client.request( 'https://api.twitter.com/1/account/verify_credentials.json') uinfo = json.loads(content) uinfo.setdefault('link', 'http://twitter.com/%s' % uinfo['screen_name']) return uinfo
def _get_linkedin_user_info(self, auth_info, key=None, secret=None): """Returns a dict of currently logging in linkedin user LinkedIn user profile API endpoint: http://api.linkedin.com/v1/people/~ or http://api.linkedin.com/v1/people/~:(id,first-name,last-name,picture-url,public-profile-url,headline) """ token = oauth1.Token(key=auth_info['oauth_token'], secret=auth_info['oauth_token_secret']) client = self._oauth1_client(token, key, secret) resp, content = client.request( 'http://api.linkedin.com/v1/people/~:(id,first-name,last-name,picture-url,public-profile-url,headline)' ) person = etree.fromstring(content) uinfo = {} for e in person: uinfo.setdefault(e.tag, e.text) return uinfo
def fetch_token_db(self, oauth_request): """Get the token object in a request :param oauth_request: oauth request :type oauth_request: :class:`oauth2.Request` :returns: :class:`oauth2.Token` and :class:`geoauth.models.OAUTH_Access` :raises: :class:`OAUTHException` if token is invalid """ token = oauth_request.get_parameter('oauth_token') token = OAUTH_Token.get_by_key_name(token) if token is not None: oauthToken = oauth2.Token(token.token_key, token.token_secret) if token.token_callback is not None: oauthToken.set_callback(token.token_callback) if token.token_verifier is not None: verifier = oauth_request.get_parameter('oauth_verifier') if verifier != token.token_verifier: raise OAUTHException("Token key invalid") oauthToken.set_verifier(token.token_verifier) return oauthToken, token raise OAUTHException("Token key invalid")
def _oauth1_callback(self, provider, access_token_url): """Third step of OAuth 1.0 dance.""" request_token = self.session.pop('req_token', None) verifier = self.request.get('oauth_verifier', None) consumer_key, consumer_secret = self._get_consumer_info_for(provider) if not request_token: raise Exception("Couldn't find request token") if not verifier: raise Exception("No OAuth verifier was provided") token = oauth1.Token(request_token['oauth_token'], request_token['oauth_token_secret']) token.set_verifier(verifier) client = self._oauth1_client(token, consumer_key, consumer_secret) resp, content = client.request(access_token_url, "POST") auth_info = getattr(self, self.TOKEN_RESPONSE_PARSERS[provider])(content) user_data = getattr(self, '_get_%s_user_info' % provider)( auth_info, key=consumer_key, secret=consumer_secret) return (user_data, auth_info)
def client_access_request(request, provider, next=None): provider = provider.lower() try: if not request.session[provider]['request_token']['oauth_token'] == request.GET.get('oauth_token') \ and request.GET.get('oauth_verifier') : return HttpResponseRedirect( reverse('georemindme.views.login_panel')) except: return HttpResponseRedirect(reverse('georemindme.views.login_panel')) #lee el token recibido if 'denied' in request.GET: del request.session[provider] if 'cls' in request.GET: return HttpResponseRedirect(reverse('geouser.views.close_window')) else: return reverse('geouser.views.dashboard') token = oauth2.Token( request.GET.get('oauth_token'), request.session[provider]['request_token']['oauth_token_secret']) token.set_verifier(request.GET.get('oauth_verifier')) consumer = oauth2.Consumer(settings.OAUTH[provider]['app_key'], settings.OAUTH[provider]['app_secret']) client = oauth2.Client(consumer, token) #lo intercambia por un token de acceso response, content = client.request( settings.OAUTH[provider]['access_token_url'], method="GET") if response['status'] != 200: raise Exception("Invalid response from server.") params = parse_qs(content, keep_blank_values=False) token = { 'oauth_token_secret': params['oauth_token_secret'][0], 'oauth_token': params['oauth_token'][0], } if provider == 'twitter': from clients.twitter import TwitterClient client = TwitterClient(token=oauth2.Token(token['oauth_token'], token['oauth_token_secret'])) if request.user.is_authenticated( ): #usuario ya esta logeado, guardamos el token de su cuenta client.authorize(request.user, login=False if 'nologin' in request.GET else True) else: user = client.authenticate() init_user_session(request, user) elif provider == 'google': from clients.google import GoogleClient client = GoogleClient(token=oauth2.Token(token['oauth_token'], token['oauth_token_secret'])) if request.user.is_authenticated( ): #usuario ya esta logeado, guardamos el token de su cuenta client.authorize(request.user) else: raise OAUTHException() else: raise OAUTHException("Invalid server.") if 'cls' in request.GET: return HttpResponseRedirect(reverse('geouser.views.close_window')) if next is None: next = reverse('geouser.views.dashboard') return HttpResponseRedirect(next)