[Python-modules-commits] [python-social-auth] 150/322: fixed refresh tokens for yahoo

Wolfgang Borgert debacle at moszumanska.debian.org
Sat Dec 24 15:13:02 UTC 2016


This is an automated email from the git hooks/post-receive script.

debacle pushed a commit to tag v0.2.10
in repository python-social-auth.

commit d9293d19b0ceab417a66c31fa6fb41d8c9c4d4cf
Author: Hassek <tomas at onereceipt.me>
Date:   Wed Feb 25 14:49:33 2015 -0430

    fixed refresh tokens for yahoo
---
 social/backends/yahoo.py | 73 +++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 69 insertions(+), 4 deletions(-)

diff --git a/social/backends/yahoo.py b/social/backends/yahoo.py
index 872d8a8..242b096 100644
--- a/social/backends/yahoo.py
+++ b/social/backends/yahoo.py
@@ -1,12 +1,12 @@
 """
-Yahoo OpenId, OAuth2 backends, docs at:
+Yahoo OpenId, OAuth1 and OAuth2 backends, docs at:
     http://psa.matiasaguirre.net/docs/backends/yahoo.html
 """
 from requests import HTTPError
 from requests.auth import HTTPBasicAuth
 
 from social.backends.open_id import OpenIdAuth
-from social.backends.oauth import BaseOAuth2
+from social.backends.oauth import BaseOAuth2, BaseOAuth1
 from social.exceptions import AuthCanceled, AuthUnknownError
 
 
@@ -16,6 +16,54 @@ class YahooOpenId(OpenIdAuth):
     URL = 'http://me.yahoo.com'
 
 
+class YahooOAuth(BaseOAuth1):
+    """Yahoo OAuth authentication backend"""
+    name = 'yahoo-oauth'
+    ID_KEY = 'guid'
+    AUTHORIZATION_URL = 'https://api.login.yahoo.com/oauth/v2/request_auth'
+    REQUEST_TOKEN_URL = \
+        'https://api.login.yahoo.com/oauth/v2/get_request_token'
+    ACCESS_TOKEN_URL = 'https://api.login.yahoo.com/oauth/v2/get_token'
+    EXTRA_DATA = [
+        ('guid', 'id'),
+        ('access_token', 'access_token'),
+        ('expires', 'expires')
+    ]
+
+    def get_user_details(self, response):
+        """Return user details from Yahoo Profile"""
+        fullname, first_name, last_name = self.get_user_names(
+            first_name=response.get('givenName'),
+            last_name=response.get('familyName')
+        )
+        emails = [email for email in response.get('emails', [])
+                        if email.get('handle')]
+        emails.sort(key=lambda e: e.get('primary', False), reverse=True)
+        return {'username': response.get('nickname'),
+                'email': emails[0]['handle'] if emails else '',
+                'fullname': fullname,
+                'first_name': first_name,
+                'last_name': last_name}
+
+    def user_data(self, access_token, *args, **kwargs):
+        """Loads user data from service"""
+        url = 'https://social.yahooapis.com/v1/user/{0}/profile?format=json'
+        return self.get_json(
+            url.format(self._get_guid(access_token)),
+            auth=self.oauth_auth(access_token)
+        )['profile']
+
+    def _get_guid(self, access_token):
+        """
+            Beause you have to provide GUID for every API request
+            it's also returned during one of OAuth calls
+        """
+        return self.get_json(
+            'https://social.yahooapis.com/v1/me/guid?format=json',
+            auth=self.oauth_auth(access_token)
+        )['guid']['value']
+
+
 class YahooOAuth2(BaseOAuth2):
     """Yahoo OAuth2 authentication backend"""
     name = 'yahoo-oauth2'
@@ -61,11 +109,10 @@ class YahooOAuth2(BaseOAuth2):
     def auth_complete(self, *args, **kwargs):
         """Completes loging process, must return user instance"""
         self.process_error(self.data)
-        client_id, client_secret = self.get_key_and_secret()
         try:
             response = self.request_access_token(
                 self.ACCESS_TOKEN_URL,
-                auth=HTTPBasicAuth(client_id, client_secret),
+                auth=HTTPBasicAuth(*self.get_key_and_secret()),
                 data=self.auth_complete_params(self.validate_state()),
                 headers=self.auth_headers(),
                 method=self.ACCESS_TOKEN_METHOD
@@ -81,6 +128,24 @@ class YahooOAuth2(BaseOAuth2):
         return self.do_auth(response['access_token'], response=response,
                             *args, **kwargs)
 
+    def refresh_token_params(self, token, *args, **kwargs):
+        return {
+            'refresh_token': token,
+            'grant_type': 'refresh_token',
+            'redirect_uri': 'oob',  # out of bounds
+        }
+
+    def refresh_token(self, token, *args, **kwargs):
+        params = self.refresh_token_params(token, *args, **kwargs)
+        url = self.REFRESH_TOKEN_URL or self.ACCESS_TOKEN_URL
+        method = self.REFRESH_TOKEN_METHOD
+        key = 'params' if method == 'GET' else 'data'
+        request_args = {'headers': self.auth_headers(),
+                        'method': method,
+                        key: params}
+        request = self.request(url, auth=HTTPBasicAuth(*self.get_key_and_secret()), **request_args)
+        return self.process_refresh_token_response(request, *args, **kwargs)
+
     def auth_complete_params(self, state=None):
         return {
             'grant_type': 'authorization_code',  # request auth code

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/python-modules/packages/python-social-auth.git



More information about the Python-modules-commits mailing list