Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Yahoo OAuth2 support #536

Merged
merged 4 commits into from
Mar 5, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ or current ones extended):
* VK.com_ OpenAPI, OAuth2 and OAuth2 for Applications
* Weibo_ OAuth2
* Xing_ OAuth1
* Yahoo_ OpenId and OAuth1
* Yahoo_ OpenId and OAuth2
* Yammer_ OAuth2
* Yandex_ OAuth1, OAuth2 and OpenId
* Zotero_ OAuth1
Expand Down
13 changes: 5 additions & 8 deletions docs/backends/yahoo.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Yahoo
=====

Yahoo supports OpenId and OAuth1 for their auth flow.
Yahoo supports OpenId and OAuth2 for their auth flow.


Yahoo OpenId
Expand All @@ -17,17 +17,14 @@ in the ``AUTHENTICATION_BACKENDS`` setting::
)


Yahoo OAuth1
Yahoo OAuth2
------------

OAuth 1.0 workflow, useful if you are planning to use Yahoo's API.
OAuth 2.0 workflow, useful if you are planning to use Yahoo's API.

- Register a new application at `Yahoo Developer Center`_, set your app domain
and configure scopes (they can't be overriden by application).

- Fill ``Consumer Key`` and ``Consumer Secret`` values in the settings::

SOCIAL_AUTH_YAHOO_OAUTH_KEY = ''
SOCIAL_AUTH_YAHOO_OAUTH_SECRET = ''

.. _Yahoo Developer Center: https://developer.apps.yahoo.com/projects/
SOCIAL_AUTH_YAHOO_OAUTH2_KEY = ''
SOCIAL_AUTH_YAHOO_OAUTH2_SECRET = ''
103 changes: 100 additions & 3 deletions social/backends/yahoo.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
"""
Yahoo OpenId and OAuth1 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 BaseOAuth1
from social.backends.oauth import BaseOAuth2, BaseOAuth1
from social.exceptions import AuthCanceled, AuthUnknownError


class YahooOpenId(OpenIdAuth):
Expand All @@ -13,7 +17,7 @@ class YahooOpenId(OpenIdAuth):


class YahooOAuth(BaseOAuth1):
"""Yahoo OAuth authentication backend"""
"""Yahoo OAuth authentication backend. DEPRECATED"""
name = 'yahoo-oauth'
ID_KEY = 'guid'
AUTHORIZATION_URL = 'https://api.login.yahoo.com/oauth/v2/request_auth'
Expand Down Expand Up @@ -58,3 +62,96 @@ def _get_guid(self, access_token):
'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'
ID_KEY = 'guid'
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 = [
('xoauth_yahoo_guid', 'id'),
('access_token', 'access_token'),
('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.
To Get user email you need the profile private read permission.
"""
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 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'.format(
kwargs['response']['xoauth_yahoo_guid'])
return self.get_json(url, headers={'Authorization': 'Bearer {0}'.format(