[Python-modules-commits] [python-social-auth] 149/322: added OAuth2 support to yahoo. Also, removed OAuth1 since yahoo will not be supporting it anymore

Wolfgang Borgert debacle at moszumanska.debian.org
Sat Dec 24 15:13:01 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 7094f806f31785899c1177f43895f48b5ab34a24
Author: Hassek <tomas at onereceipt.me>
Date:   Wed Feb 25 13:27:27 2015 -0430

    added OAuth2 support to yahoo. Also, removed OAuth1 since yahoo will not be supporting it anymore
---
 social/backends/yahoo.py | 83 ++++++++++++++++++++++++++++++++----------------
 1 file changed, 56 insertions(+), 27 deletions(-)

diff --git a/social/backends/yahoo.py b/social/backends/yahoo.py
index 206949e..872d8a8 100644
--- a/social/backends/yahoo.py
+++ b/social/backends/yahoo.py
@@ -1,9 +1,13 @@
 """
-Yahoo OpenId and OAuth1 backends, docs at:
+Yahoo OpenId, 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 BaseOAuth1
+from social.backends.oauth import BaseOAuth2
+from social.exceptions import AuthCanceled, AuthUnknownError
 
 
 class YahooOpenId(OpenIdAuth):
@@ -12,20 +16,26 @@ class YahooOpenId(OpenIdAuth):
     URL = 'http://me.yahoo.com'
 
 
-class YahooOAuth(BaseOAuth1):
-    """Yahoo OAuth authentication backend"""
-    name = 'yahoo-oauth'
+class YahooOAuth2(BaseOAuth2):
+    """Yahoo OAuth2 authentication backend"""
+    name = 'yahoo-oauth2'
     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'
+    AUTHORIZATION_URL = 'https://api.login.yahoo.com/oauth2/request_auth'
+    ACCESS_TOKEN_URL = 'https://api.login.yahoo.com/oauth2/get_token'
+    ACCESS_TOKEN_METHOD = 'POST'
     EXTRA_DATA = [
-        ('guid', 'id'),
+        ('xoauth_yahoo_guid', 'id'),
         ('access_token', 'access_token'),
-        ('expires', 'expires')
+        ('expires_in', 'expires'),
+        ('refresh_token', 'refresh_token'),
+        ('token_type', 'token_type'),
     ]
 
+    def get_user_names(self, first_name, last_name):
+        if first_name or last_name:
+            return " ".join((first_name, last_name)), first_name, last_name
+        return None, None, None
+
     def get_user_details(self, response):
         """Return user details from Yahoo Profile"""
         fullname, first_name, last_name = self.get_user_names(
@@ -36,25 +46,44 @@ class YahooOAuth(BaseOAuth1):
                         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 '',
+                'email': emails[0]['handle'] if emails else response.get('guid', ''),
                 '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']
+        url = 'https://social.yahooapis.com/v1/user/{0}/profile?format=json'.format(
+                kwargs['response']['xoauth_yahoo_guid'])
+        return self.get_json(url, headers={'Authorization': 'Bearer {0}'.format(
+            access_token)}, method='GET')['profile']
+
+    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),
+                data=self.auth_complete_params(self.validate_state()),
+                headers=self.auth_headers(),
+                method=self.ACCESS_TOKEN_METHOD
+            )
+        except HTTPError as err:
+            if err.response.status_code == 400:
+                raise AuthCanceled(self)
+            else:
+                raise
+        except KeyError:
+            raise AuthUnknownError(self)
+        self.process_error(response)
+        return self.do_auth(response['access_token'], response=response,
+                            *args, **kwargs)
+
+    def auth_complete_params(self, state=None):
+        return {
+            'grant_type': 'authorization_code',  # request auth code
+            'code': self.data.get('code', ''),  # server response code
+            'redirect_uri': self.get_redirect_uri(state)
+        }

-- 
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