From 1ab7be27ea85b68226ed6aa84d7d961555b31e32 Mon Sep 17 00:00:00 2001 From: Florian Moga Date: Sat, 7 Jul 2012 17:39:14 +0300 Subject: [PATCH] Add Mixcloud backend --- README.rst | 38 ++++++++++++++++++ social_auth/backends/contrib/mixcloud.py | 49 ++++++++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 social_auth/backends/contrib/mixcloud.py diff --git a/README.rst b/README.rst index 791fd6e28..57fa1b0b7 100644 --- a/README.rst +++ b/README.rst @@ -51,6 +51,7 @@ credentials, some features are: * `Evernote OAuth`_ * `Mail.ru OAuth`_ * `Odnoklassniki OAuth`_ + * `Mixcloud OAuth2`_ - Basic user data population and signaling, to allows custom fields values from providers response @@ -131,6 +132,7 @@ Configuration 'social_auth.backends.contrib.yahoo.YahooOAuthBackend', 'social_auth.backends.OpenIDBackend', 'social_auth.backends.contrib.bitbucket.BitbucketBackend', + 'social_auth.backends.contrib.mixcloud.MixcloudBackend', 'social_auth.backends.contrib.live.LiveBackend', 'django.contrib.auth.backends.ModelBackend', ) @@ -176,6 +178,8 @@ Configuration SKYROCK_CONSUMER_SECRET = '' YAHOO_CONSUMER_KEY = '' YAHOO_CONSUMER_SECRET = '' + MIXCLOUD_CLIENT_ID = '' + MIXCLOUD_CLIENT_SECRET = '' - Setup login URLs:: @@ -1087,6 +1091,32 @@ Odnoklassniki.ru uses OAuth2 workflow, to use it fill in settings:: ODNOKLASSNIKI_OAUTH2_APP_KEY = '' ODNOKLASSNIKI_OAUTH2_CLIENT_SECRET = '' +Mixcloud OAuth2 +^^^^^^^^^^^^^^^ + +The `Mixcloud API`_ offers support for authorization. +To enable OAuth2 support: + +- Register a new application at `Mixcloud Developers`_ + +- Add Mixcloud backend to ``AUTHENTICATION_BACKENDS`` in settings:: + + AUTHENTICATION_BACKENDS = ( + ... + 'social_auth.backends.contrib.mixcloud.MixcloudBackend', + ) + +- Fill ``Client Id`` and ``Client Secret`` values in the settings:: + + MIXCLOUD_CLIENT_ID = '' + MIXCLOUD_CLIENT_SECRET = '' + +- Similar to the other OAuth backends you can define:: + + MIXCLOUD_EXTRA_DATA = [('username', 'username'), ('name', 'name'), ('pictures', 'pictures'), ('url', 'url')] + +as a list of tuples ``(response name, alias)`` to store user profile data on the UserSocialAuth model. + Testing ------- @@ -1289,6 +1319,10 @@ Attributions to whom deserves: - Evernote support +- fmoga_ (Florian Moga) + + - Mixcloud support + Copyrights ---------- @@ -1394,3 +1428,7 @@ Base work is copyrighted by: .. _Odnoklassniki OAuth: http://dev.odnoklassniki.ru/wiki/display/ok/The+OAuth+2.0+Protocol .. _authentication for VKontakte applications: http://www.ikrvss.ru/2011/11/08/django-social-auh-and-vkontakte-application/ .. _Facebook Canvas Application Authentication: http://www.ikrvss.ru/2011/09/22/django-social-auth-and-facebook-canvas-applications/ +.. _Mixcloud OAuth2: http://www.mixcloud.com/developers/documentation/#authorization +.. _Mixcloud API: http://www.mixcloud.com/developers/documentation +.. _Mixcloud Developers: http://www.mixcloud.com/developers +.. _fmoga: https://github.com/fmoga diff --git a/social_auth/backends/contrib/mixcloud.py b/social_auth/backends/contrib/mixcloud.py new file mode 100644 index 000000000..c2d352f7a --- /dev/null +++ b/social_auth/backends/contrib/mixcloud.py @@ -0,0 +1,49 @@ +""" +Mixcloud OAuth2 support +""" +from urllib import urlencode +from urllib2 import Request, urlopen +from oauth2 import Request as OAuthRequest +from django.utils import simplejson +from social_auth.utils import setting +from social_auth.backends import OpenIdAuth, ConsumerBasedOAuth, BaseOAuth2, \ + OAuthBackend, OpenIDBackend, USERNAME +from social_auth.backends.exceptions import AuthFailed +from pprint import pprint + +MIXCLOUD_PROFILE_URL = 'https://api.mixcloud.com/me/' + +class MixcloudBackend(OAuthBackend): + name = 'mixcloud' + + def get_user_id(self, details, response): + return response['username'] + + def get_user_details(self, response): + return {USERNAME: response['username'], + 'email': None, + 'fullname': response['name'], + 'first_name': None, + 'last_name': None } + +class MixcloudOAuth2(BaseOAuth2): + AUTH_BACKEND = MixcloudBackend + AUTHORIZATION_URL = 'https://www.mixcloud.com/oauth/authorize' + ACCESS_TOKEN_URL = 'https://www.mixcloud.com/oauth/access_token' + SETTINGS_KEY_NAME = 'MIXCLOUD_CLIENT_ID' + SETTINGS_SECRET_NAME = 'MIXCLOUD_CLIENT_SECRET' + + def user_data(self, access_token, *args, **kwargs): + return mixcloud_profile(access_token) + +def mixcloud_profile(access_token): + data = {'access_token': access_token, 'alt': 'json'} + request = Request(MIXCLOUD_PROFILE_URL + '?' + urlencode(data)) + try: + return simplejson.loads(urlopen(request).read()) + except (ValueError, KeyError, IOError): + return None + +BACKENDS = { + 'mixcloud': MixcloudOAuth2, +}