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

Merge of krvss fork #343

Merged
merged 121 commits into from
May 9, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
121 commits
Select commit Hold shift + click to select a range
4fe9b87
LiveJournal association display added to template.
Jan 14, 2011
9023d7c
Merge remote branch 'upstream/master'
Jan 17, 2011
9c39ba2
Reverted signal processing in user details update; removed django dep…
Jan 17, 2011
0614f67
VKontakte support - initial version.
Jan 19, 2011
0c4b22f
Merge remote branch 'upstream/master'
Jan 19, 2011
51a4a57
Fixed openid issue, switch to hashlib from md5 library.
Jan 19, 2011
aef5b15
Added expiration time check for VKontakte.
Jan 19, 2011
3ba9463
Yandex.ru support added; small change in default e-mail value in Open…
Jan 19, 2011
1dc833d
Merge remote branch 'upstream/master'
Jan 19, 2011
35151b7
Merging 'upstream/master'.
Jan 19, 2011
af82214
Merge remote branch 'upstream/master'
Jan 20, 2011
5c28055
Merging 'upstream/master'.
Jan 20, 2011
7d77363
Merge remote branch 'upstream/master'
Jan 25, 2011
c55b822
Updated LJ and Yandex backends to work with self.data, not self.POST.
Jan 25, 2011
96f1f8e
Update for Yandex to make sure all data passed into OpenID backend.
Jan 25, 2011
024761a
Merge remote branch 'upstream/master'
Feb 1, 2011
0577db6
Merge remote branch 'upstream/master'
Feb 14, 2011
cfdcab1
Merge remote branch 'upstream/master', updating to support latest cha…
Feb 18, 2011
374d26b
Merge remote branch 'upstream/master'
Feb 24, 2011
3055c4c
Merging of latest changes.
Feb 24, 2011
c040ba8
Merge remote branch 'upstream/master'
Mar 5, 2011
b76add6
Merge remote branch 'upstream/master'
Mar 10, 2011
d0c9462
Merge remote branch 'upstream/master'
Mar 18, 2011
f7f286c
Merge remote branch 'upstream/master'
Mar 25, 2011
d31eafd
Merge remote branch 'upstream/master'
Apr 4, 2011
63ffc7d
Merging with latest head; Yandex OpenID 2.0 support added - no need t…
Apr 4, 2011
c28571b
Merge remote branch 'upstream/master'
Apr 18, 2011
d70ce9a
Odnoklassniki OAuth 2.0 authentication support added.
Apr 18, 2011
4e15505
Merge remote branch 'upstream/master'
Apr 19, 2011
fdd379c
Minor updates of settings and html.
Apr 19, 2011
a2d788b
Merge remote branch 'upstream/master'
Apr 28, 2011
b6a3a92
Django dependency removed to allow development version usage.
Apr 28, 2011
d03ccd9
VKontakte OAuth2 authentication support added.
Apr 28, 2011
aabb618
Mail.ru OAuth 2.0 support added.
Apr 30, 2011
23bc1e2
Merge remote branch 'upstream/master'
Apr 30, 2011
5b96a5f
Merge remote branch 'upstream/master'
May 16, 2011
10e6297
Merge remote branch 'upstream/master'
May 20, 2011
61436a2
Merge remote branch 'upstream/master'
May 26, 2011
24e482a
Authentication using signed_request for Facebook. Useful when applica…
May 26, 2011
d53d4a7
Merge remote branch 'upstream/master'
Jun 10, 2011
30aad15
VKontakte: experimental authentication for applications added.
Jun 14, 2011
ea39a17
JS API Application authentication added ("session" authentication).
Jun 17, 2011
a7dbeae
Merge remote branch 'upstream/master'
Jul 4, 2011
1da4cad
Updating templates for latest syntax.
Jul 11, 2011
97b4fd3
Added safe conversion of UID in init in order to avoid possible conge…
Jul 11, 2011
f20cfdd
VKontakte error checking.
Jul 11, 2011
a3ab46b
Merge remote branch 'upstream/master'
Jul 13, 2011
4d4abc1
Mail.ru cancel authentication handled properly.
Jul 13, 2011
e4e5f7f
Adding exception handling for Value Error: Account already in use, in…
sidmitra Jul 14, 2011
d97dae1
Refactor repeated code. Refs gh-102
omab Jul 14, 2011
207d0fd
v0.4.2
omab Jul 14, 2011
bb6a1b5
Use details username only if it's non-blank. Closes gh-104
omab Jul 16, 2011
e125408
Avoid csrf check on complete views in case provider uses POST and doe…
omab Jul 17, 2011
742c8cc
Merge remote branch 'upstream/master'
Jul 26, 2011
1f5823c
VKontakte application authentication improvements.
Jul 27, 2011
c4efe99
VKontakte application authentication improvements.
Jul 27, 2011
db146a3
cosmetics
Jul 29, 2011
c4790a2
Merge branch 'master' of https://github.com/ryr/django-social-auth
ryr Jul 29, 2011
b81a22d
Merge branch 'master' of https://github.com/ryr/django-social-auth
ryr Jul 29, 2011
0d26e9d
VKontakte app auth improvements.
Aug 1, 2011
650be8d
VKontakte app auth improvements.
ryr Aug 8, 2011
567fb30
Merge branch 'master' of https://github.com/omab/django-social-auth
ryr Aug 8, 2011
0ffab23
Fixed bug in OpenIDBackend.extra_data()
bashmish Aug 13, 2011
1082920
Merge remote-tracking branch 'upstream/master'.
krvss Aug 16, 2011
69f1ccb
Merge remote-tracking branch 'upstream/master'
ryr Aug 18, 2011
391d329
Merge branch 'master' of https://github.com/ryr/django-social-auth
ryr Aug 18, 2011
14eed6e
fix missed line
ryr Aug 18, 2011
fb01102
revert to original file
ryr Aug 18, 2011
b0996f9
Merge pull request #1 from bashmish/master
krvss Aug 18, 2011
f223bcd
Merge pull request #2 from ryr/pull-request
krvss Aug 18, 2011
6236268
Merge remote-tracking branch 'upstream/master'; Fixing VKontakte bad …
krvss Aug 23, 2011
2def980
Merge branch 'master' of github.com:krvss/django-social-auth
krvss Aug 23, 2011
2b52132
Merge remote-tracking branch 'upstream/master'
krvss Aug 29, 2011
07b8b89
Fixing redirect url as found in pull request from Quard.
krvss Aug 29, 2011
e5767cb
Merge remote-tracking branch 'upstream/master'
krvss Sep 14, 2011
1b849e2
VKontakte OpenAPI fix: avoiding auto-close of popup window
krvss Sep 14, 2011
1698f9c
Merge remote-tracking branch 'upstream/master'
krvss Sep 20, 2011
808eff1
Adding logging to OAuth backends: log the api call errors
krvss Sep 20, 2011
d1514c6
Adding support of OAuth app login.
krvss Sep 20, 2011
6feae45
Merge remote-tracking branch 'upstream/master'
krvss Oct 1, 2011
15aba05
Merging with latest head; removal outdated FB authentication (sessions)
krvss Oct 1, 2011
2c6fbec
Merge remote-tracking branch 'upstream/master'
krvss Oct 17, 2011
2667211
Added link for Russian users.
krvss Oct 17, 2011
9517e1f
Merge remote-tracking branch 'upstream/master'
krvss Nov 8, 2011
dad8c88
Merge remote-tracking branch 'upstream/master'
krvss Nov 24, 2011
feb5ea0
Merge remote-tracking branch 'upstream/master'
krvss Dec 7, 2011
e7ac5f0
Merge remote-tracking branch 'upstream/master'
krvss Dec 21, 2011
84fe3b8
Merge remote-tracking branch 'upstream/master'
krvss Jan 4, 2012
e16f211
Yandex OAuth2 support for Ya.ru and Moi Krug added
krvss Jan 4, 2012
1ec6fd0
Yandex OAuth2: Token moved to Authorization header
krvss Jan 19, 2012
aa8e9d0
Merge remote-tracking branch 'upstream/master'
krvss Jan 19, 2012
2e54187
Yandex OAuth2: Userpic update
krvss Jan 19, 2012
c0d2222
Merge remote-tracking branch 'upstream/master'
krvss Feb 1, 2012
6465b04
Merging latest changes from omab
krvss Feb 16, 2012
85c568c
Facebook app auth update for the latest changes.
krvss Feb 16, 2012
595e76b
User-friendly errors, russian translation
gugu Feb 16, 2012
e13c36a
Account already in use exception
gugu Feb 17, 2012
48d0a96
removed debug
gugu Feb 19, 2012
2550cb5
changed "authentication" to "authorization"
gugu Feb 19, 2012
e177efd
changed "authentication" to "authorization"
gugu Feb 19, 2012
96bf35a
fix bug in facebook backend with partial pipeline
elmit Feb 20, 2012
abb41ed
Merge pull request #4 from Eksmo/master
krvss Feb 21, 2012
0bf1167
fixed twitter tests
gugu Feb 23, 2012
46c9a57
Merge pull request #5 from Eksmo/master
krvss Feb 24, 2012
c4b7e71
Merge remote-tracking branch 'upstream/master'
krvss Mar 9, 2012
3e95b2a
Merge remote-tracking branch 'origin/master'
krvss Mar 9, 2012
041354b
Merge remote-tracking branch 'upstream/master'
krvss Mar 30, 2012
cef9a80
replaced by except AuthFailed to except AuthException
oksana-slu Apr 4, 2012
c462d08
Merge pull request #6 from oksana-slu/master
krvss Apr 4, 2012
13adcf3
Update social_auth/backends/google.py
latheiere Apr 11, 2012
7729032
Merge remote-tracking branch 'upstream/master'
krvss Apr 13, 2012
d3e225f
Merge branch 'master' of github.com:krvss/django-social-auth
krvss Apr 13, 2012
f878011
Merge pull request #7 from latheiere/master
krvss Apr 13, 2012
29e1436
Merge remote-tracking branch 'upstream/master'
krvss May 2, 2012
e9d66c0
Merge branch 'master' of github.com:krvss/django-social-auth
krvss May 2, 2012
a43f8a4
Minor optimization and fixes
krvss May 4, 2012
a8a1539
Merging of vkontakte and yandex backends with bacher09 versions. Prep…
krvss May 7, 2012
bb86450
Merge remote-tracking branch 'upstream/master'
krvss May 9, 2012
c08dde0
Preparing for merge with main branch - cleaning up old merge differences
krvss May 9, 2012
d4a9707
Preparing for merge with main branch - more clean-ups and documents u…
krvss May 9, 2012
00bc3c4
Logging update to use log function from utils, Facebook documentation…
krvss May 9, 2012
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
45 changes: 45 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ credentials, some features are:
* `Skyrock OAuth`_
* `Yahoo OAuth`_
* `Evernote OAuth`_
* `Mail.ru OAuth`_
* `Odnoklassniki OAuth`_

- Basic user data population and signaling, to allows custom fields values
from providers response
Expand Down Expand Up @@ -745,6 +747,9 @@ http://127.0.0.1:8000 or http://localhost:8000 because it won't work when
testing. Instead I define http://myapp.com and setup a mapping on /etc/hosts
or use dnsmasq_.

If you need to perform authentication from Facebook Canvas application, take a
look to `Facebook Canvas Application Authentication`_.

Orkut
^^^^^

Expand Down Expand Up @@ -952,6 +957,11 @@ Vkontakte uses OAuth v2 for Authentication

See the `names of the privileges VKontakte`_.

You can also use Vkontakte's own OpenAPI to log in, but you need to provide a HTML template
with JavaScript code to authenticate. See vkontakte.html in templates folder for details.

To support authentication for VKontakte applications see `authentication for VKontakte applications`_.

MSN Live Connect
^^^^^^^^^^^^^^^^

Expand Down Expand Up @@ -1009,6 +1019,33 @@ Evernote OAuth 1.0 workflow.
EVERNOTE_CONSUMER_KEY = ''
EVERNOTE_CONSUMER_SECRET = ''

Yandex OAuth and OpenID
^^^^^^^^^^^^^^^^^^^^^^^

Yandex uses OAuth 2.0 workflow, fill and YANDEX_APP_ID and YANDEX_API_SECRET settings to log in.
Currently there are 2 backends for Yandex, one is Yaru to log in using Ya.ru service and another one is
YandexOAuth that could use service API from settings. Use YANDEX_OAUTH2_API_URL to set up which service to use.
Tested with 'https://api-yaru.yandex.ru/me/' for Ya.ru and 'http://api.moikrug.ru/v1/my/' for Moi Krug.

Yandex also uses OpenID 2.0. You do not need to provide user's name because Yandex will do it for you.

Mail.ru OAuth
^^^^^^^^^^^^^

Mail.ru uses OAuth2 workflow, to use it fill in settings

MAILRU_OAUTH2_CLIENT_KEY = ''
MAILRU_OAUTH2_APP_KEY = ''
MAILRU_OAUTH2_CLIENT_SECRET = ''

Odnoklassniki.ru
^^^^^^^^^^^^^^^^

Odnoklassniki.ru uses OAuth2 workflow, to use it fill in settings

ODNOKLASSNIKI_OAUTH2_CLIENT_KEY = ''
ODNOKLASSNIKI_OAUTH2_APP_KEY = ''
ODNOKLASSNIKI_OAUTH2_CLIENT_SECRET = ''

Testing
-------
Expand Down Expand Up @@ -1156,6 +1193,10 @@ Attributions to whom deserves:
- krvss_ (Stas Kravets):

- Initial setup.py configuration
- LiveJournal support
- Mail.ru, Odnoklassniki support
- Yandex OpenID support
- VKontakte OpenAPI support

- jezdez_ (Jannis Leidel):

Expand Down Expand Up @@ -1309,3 +1350,7 @@ Base work is copyrighted by:
.. _Yahoo Developer Center: https://developer.apps.yahoo.com/projects/
.. _Evernote API Key form: http://dev.evernote.com/support/api_key.php
.. _hassek: https://github.com/hassek
.. _Mail.ru OAuth: http://api.mail.ru/docs/guides/oauth/
.. _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/
3 changes: 3 additions & 0 deletions doc/backends/facebook.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ http://127.0.0.1:8000 or http://localhost:8000 because it won't work when
testing. Instead I define http://myapp.com and setup a mapping on /etc/hosts
or use dnsmasq_.

If you need to perform authentication from Facebook Canvas application, take a
look to `Facebook Canvas Application Authentication`_.

.. _dnsmasq: http://www.thekelleys.org.uk/dnsmasq/doc.html
.. _Facebook development resources: http://developers.facebook.com/docs/authentication/
.. _Facebook App Creation: http://developers.facebook.com/setup/
.. _Facebook Canvas Application Authentication: http://www.ikrvss.ru/2011/09/22/django-social-auth-and-facebook-canvas-applications/
6 changes: 6 additions & 0 deletions doc/backends/vkontakte.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ Vkontakte uses OAuth v2 for Authentication

See the `names of the privileges VKontakte`_.

You can also use Vkontakte's own OpenAPI to log in, but you need to provide HTML template
with JavaScript code to authenticate. See vkontakte.html in templates folder for details.

To support authentication for VKontakte applications see `authentication for VKontakte applications`_

.. _Vkontakte OAuth: http://vk.com/developers.php?oid=-1&p=%D0%90%D0%B2%D1%82%D0%BE%D1%80%D0%B8%D0%B7%D0%B0%D1%86%D0%B8%D1%8F_%D1%81%D0%B0%D0%B9%D1%82%D0%BE%D0%B2
.. _names of the privileges VKontakte: http://vk.com/developers.php?oid=-1&p=%D0%9F%D1%80%D0%B0%D0%B2%D0%B0_%D0%B4%D0%BE%D1%81%D1%82%D1%83%D0%BF%D0%B0_%D0%BF%D1%80%D0%B8%D0%BB%D0%BE%D0%B6%D0%B5%D0%BD%D0%B8%D0%B9
.. _Vkontakte API: http://vk.com/developers.php
.. _authentication for VKontakte applications: http://www.ikrvss.ru/2011/11/08/django-social-auh-and-vkontakte-application/
24 changes: 22 additions & 2 deletions example/local_settings.py.template
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,34 @@ SOCIAL_AUTH_FORCE_RANDOM_USERNAME = False
SOCIAL_AUTH_DEFAULT_USERNAME = 'socialauth_user'
SOCIAL_AUTH_COMPLETE_URL_NAME = 'socialauth_complete'
LOGIN_ERROR_URL = '/login/error/'
VKONTAKTE_APP_ID = ''
VKONTAKTE_APP_SECRET = ''
# Usage for applications auth: {'key': application_key, 'user_mode': 0 (default) | 1 (check) | 2 (online check) }
# 0 means is_app_user request parameter is ignored, 1 - must be = 1, 2 - checked via VK API request (useful when user
# connects to your application on app page and you reload the iframe)
VKONTAKTE_APP_AUTH = None
ODNOKLASSNIKI_OAUTH2_CLIENT_KEY = ''
ODNOKLASSNIKI_OAUTH2_APP_KEY = ''
ODNOKLASSNIKI_OAUTH2_CLIENT_SECRET = ''
MAILRU_OAUTH2_CLIENT_KEY = ''
MAILRU_OAUTH2_APP_KEY = ''
MAILRU_OAUTH2_CLIENT_SECRET = ''
#SOCIAL_AUTH_USER_MODEL = 'app.CustomUser'
SOCIAL_AUTH_ERROR_KEY = 'socialauth_error'
GITHUB_APP_ID = ''
GITHUB_API_SECRET = ''
FOURSQUARE_CONSUMER_KEY = ''
FOURSQUARE_CONSUMER_SECRET = ''
VK_APP_ID = ''
VK_API_SECRET = ''
YANDEX_OAUTH2_CLIENT_KEY = ''
YANDEX_OAUTH2_CLIENT_SECRET = ''
YANDEX_OAUTH2_API_URL = 'https://api-yaru.yandex.ru/me/' # http://api.moikrug.ru/v1/my/ for Moi Krug

# Backward compatibility
YANDEX_APP_ID = YANDEX_OAUTH2_CLIENT_KEY
YANDEX_API_SECRET = YANDEX_OAUTH2_CLIENT_SECRET

VK_APP_ID = VKONTAKTE_APP_ID
VK_API_SECRET = VKONTAKTE_APP_SECRET

SOCIAL_AUTH_PIPELINE = (
'social_auth.backends.pipeline.social.social_auth_user',
Expand Down
8 changes: 6 additions & 2 deletions example/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,17 @@
'social_auth.backends.contrib.flickr.FlickrBackend',
'social_auth.backends.contrib.instagram.InstagramBackend',
'social_auth.backends.contrib.github.GithubBackend',
'social_auth.backends.contrib.vkontakte.VkontakteBackend',
'social_auth.backends.contrib.yandex.YandexBackend',
'social_auth.backends.contrib.yandex.YaruBackend',
'social_auth.backends.contrib.yahoo.YahooOAuthBackend',
'social_auth.backends.OpenIDBackend',
'social_auth.backends.contrib.livejournal.LiveJournalBackend',
'social_auth.backends.browserid.BrowserIDBackend',
'social_auth.backends.contrib.vkontakte.VKontakteBackend',
'social_auth.backends.contrib.yandex.YandexOAuth2Backend',
'social_auth.backends.contrib.yandex.YaruBackend',
'social_auth.backends.contrib.odnoklassniki.OdnoklassnikiBackend',
'social_auth.backends.contrib.vkontakte.VKontakteOAuth2Backend',
'social_auth.backends.contrib.mailru.MailruBackend',
'django.contrib.auth.backends.ModelBackend',
)

Expand Down
7 changes: 7 additions & 0 deletions example/templates/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,11 @@ <h3>Login using <a href="https://browserid.org/" title="BrowserID">BrowserID</a>
</script>
</form>
</div>

<div>
<h3>Login using other authentication systems:</h3>
<ul>
<li><a rel="nofollow" href="{% url socialauth_begin "vkontakte" %}">VKontakte OpenAPI</a></li>
</ul>
</div>
{% endblock %}
39 changes: 39 additions & 0 deletions example/templates/vkontakte.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{% extends "base.html" %}

{% block heading %}<a href="http://www.vkontakte.ru">VKontakte</a> login{% endblock %}

{% block content %}

<script src="http://vkontakte.ru/js/api/openapi.js" type="text/javascript"></script>

<script type="text/javascript">
var vkAppId = {{ VK_APP_ID|default:"null" }};

if (vkAppId)
VK.init({ apiId: vkAppId });

function authVK() {
if (!vkAppId) {
alert ("Please specify VKontakte APP ID in your local settings file");
return false;
}

VK.Auth.login(function(response) {
var params = "";
if (response.session) {
params = "first_name=" + encodeURI(response.session.user.first_name) + "&last_name=" + encodeURI(response.session.user.last_name);
params += "&nickname=" + encodeURI(response.session.user.nickname) + "&id=" + encodeURI(response.session.user.id);
}
window.location = "{{ VK_COMPLETE_URL }}?" + params;

});

return false;
}

</script>

<p>
<a href="javascript:void(0);" onclick="authVK();">Click to authorize</a>
</p>
{% endblock %}
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def long_description():
'social_auth.backends',
'social_auth.backends.contrib',
'social_auth.backends.pipeline'],
package_data={'social_auth':['locale/*/LC_MESSAGES/*']},
long_description=long_description(),
install_requires=['django>=1.2.5',
'oauth2>=1.5.167',
Expand Down
98 changes: 98 additions & 0 deletions social_auth/backends/contrib/mailru.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
"""
Mail.ru OAuth2 support

Take a look to http://api.mail.ru/docs/guides/oauth/

You need to register OAuth site here:
http://api.mail.ru/sites/my/add

Then update your settings values using registration information

"""

from django.conf import settings
from django.utils import simplejson

from urllib import urlencode, unquote
from urllib2 import Request, urlopen, HTTPError
from hashlib import md5

from social_auth.backends import OAuthBackend, BaseOAuth2, USERNAME
from social_auth.utils import setting, log

MAILRU_API_URL = 'http://www.appsmail.ru/platform/api'
MAILRU_OAUTH2_SCOPE = ['']

EXPIRES_NAME = getattr(settings, 'SOCIAL_AUTH_EXPIRATION', 'expires')

class MailruBackend(OAuthBackend):
"""Mail.ru authentication backend"""
name = 'mailru-oauth2'
EXTRA_DATA = [('refresh_token', 'refresh_token'),
('expires_in', EXPIRES_NAME)]

def get_user_id(self, details, response):
"""Return user unique id provided by Mail.ru"""
return response['uid']

def get_user_details(self, response):
"""Return user details from Mail.ru request"""
values = { USERNAME: unquote(response['nick']), 'email': unquote(response['email']),
'first_name': unquote(response['first_name']), 'last_name': unquote(response['last_name'])}

if values['first_name'] and values['last_name']:
values['fullname'] = "%s %s" % (values['first_name'], values['last_name'])
return values


class MailruOAuth2(BaseOAuth2):
"""Mail.ru OAuth2 support"""
AUTH_BACKEND = MailruBackend
AUTHORIZATION_URL = 'https://connect.mail.ru/oauth/authorize'
ACCESS_TOKEN_URL = 'https://connect.mail.ru/oauth/token'
SETTINGS_KEY_NAME = 'MAILRU_OAUTH2_CLIENT_KEY'
SETTINGS_SECRET_NAME = 'MAILRU_OAUTH2_CLIENT_SECRET'

def get_scope(self):
return setting('MAILRU_OAUTH2_EXTRA_SCOPE', [])

def auth_complete(self, *args, **kwargs):
try:
auth_result = super(MailruOAuth2, self).auth_complete(*args, **kwargs)
except HTTPError: # Mail.ru returns HTTPError 400 if cancelled
raise ValueError('Authentication cancelled')

return auth_result

def user_data(self, access_token, *args, **kwargs):
"""Return user data from Mail.ru REST API"""
data = {'method': 'users.getInfo', 'session_key': access_token}
return mailru_api(data)[0]

def mailru_sig(data):
""" Calculates signature of request data """

param_list = sorted(list(item + '=' + data[item] for item in data))

return md5(''.join(param_list) + settings.MAILRU_OAUTH2_CLIENT_SECRET).hexdigest()

def mailru_api(data):
""" Calls Mail.ru REST API method
http://api.mail.ru/docs/guides/restapi/
"""
data.update({'app_id': settings.MAILRU_OAUTH2_CLIENT_KEY, 'secure': '1'})
data['sig'] = mailru_sig(data)

params = urlencode(data)
request = Request(MAILRU_API_URL, params)
try:
return simplejson.loads(urlopen(request).read())
except (TypeError, KeyError, IOError, ValueError, IndexError):
log('error', 'Could not load data from Mail.ru.', exc_info=True, extra=dict(data=params))
return None


# Backend definition
BACKENDS = {
'mailru-oauth2': MailruOAuth2
}
Loading