~ubuntu-branches/debian/sid/social-auth-core/sid

« back to all changes in this revision

Viewing changes to social_core/backends/mailru.py

  • Committer: Package Import Robot
  • Author(s): Andre Bianchi
  • Date: 2018-02-22 19:49:12 UTC
  • Revision ID: package-import@ubuntu.com-20180222194912-4lqv8mlhnqc4ncd3
Tags: upstream-1.7.0
ImportĀ upstreamĀ versionĀ 1.7.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""
 
2
Mail.ru OAuth2 backend, docs at:
 
3
    https://python-social-auth.readthedocs.io/en/latest/backends/mailru.html
 
4
"""
 
5
from hashlib import md5
 
6
 
 
7
from six.moves.urllib_parse import unquote
 
8
 
 
9
from .oauth import BaseOAuth2
 
10
 
 
11
 
 
12
class MailruOAuth2(BaseOAuth2):
 
13
    """Mail.ru authentication backend"""
 
14
    name = 'mailru-oauth2'
 
15
    ID_KEY = 'uid'
 
16
    AUTHORIZATION_URL = 'https://connect.mail.ru/oauth/authorize'
 
17
    ACCESS_TOKEN_URL = 'https://connect.mail.ru/oauth/token'
 
18
    ACCESS_TOKEN_METHOD = 'POST'
 
19
    EXTRA_DATA = [('refresh_token', 'refresh_token'),
 
20
                  ('expires_in', 'expires')]
 
21
 
 
22
    def get_user_details(self, response):
 
23
        """Return user details from Mail.ru request"""
 
24
        fullname, first_name, last_name = self.get_user_names(
 
25
            first_name=unquote(response['first_name']),
 
26
            last_name=unquote(response['last_name'])
 
27
        )
 
28
        return {'username': unquote(response['nick']),
 
29
                'email': unquote(response['email']),
 
30
                'fullname': fullname,
 
31
                'first_name': first_name,
 
32
                'last_name': last_name}
 
33
 
 
34
    def user_data(self, access_token, *args, **kwargs):
 
35
        """Return user data from Mail.ru REST API"""
 
36
        key, secret = self.get_key_and_secret()
 
37
        data = {'method': 'users.getInfo',
 
38
                'session_key': access_token,
 
39
                'app_id': key,
 
40
                'secure': '1'}
 
41
        param_list = sorted(list(item + '=' + data[item] for item in data))
 
42
        data['sig'] = md5(
 
43
            (''.join(param_list) + secret).encode('utf-8')
 
44
        ).hexdigest()
 
45
        return self.get_json('http://www.appsmail.ru/platform/api',
 
46
                             params=data)[0]