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

« back to all changes in this revision

Viewing changes to social_core/backends/chatwork.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
Chatwork OAuth2 backend
 
3
"""
 
4
import base64
 
5
 
 
6
from .oauth import BaseOAuth2
 
7
 
 
8
 
 
9
class ChatworkOAuth2(BaseOAuth2):
 
10
    """Chatwork OAuth authentication backend"""
 
11
    name = 'chatwork'
 
12
    API_URL = 'https://api.chatwork.com/v2'
 
13
    AUTHORIZATION_URL = 'https://www.chatwork.com/packages/oauth2/login.php'
 
14
    ACCESS_TOKEN_URL = 'https://oauth.chatwork.com/token'
 
15
    ACCESS_TOKEN_METHOD = 'POST'
 
16
    REDIRECT_STATE = True
 
17
    DEFAULT_SCOPE = ['users.profile.me:read']
 
18
    ID_KEY = 'account_id'
 
19
    EXTRA_DATA = [
 
20
        ('expires_in', 'expires'),
 
21
        ('refresh_token', 'refresh_token')
 
22
    ]
 
23
 
 
24
    def api_url(self, path):
 
25
        api_url = self.setting('API_URL') or self.API_URL
 
26
        return '{0}{1}'.format(api_url.rstrip('/'), path)
 
27
 
 
28
    def auth_headers(self):
 
29
        return {
 
30
            'Authorization': b'Basic ' + base64.b64encode(
 
31
                '{0}:{1}'.format(*self.get_key_and_secret()).encode()
 
32
            )
 
33
        }
 
34
 
 
35
    def auth_complete_params(self, state=None):
 
36
        return {
 
37
            'grant_type': 'authorization_code',
 
38
            'code': self.data.get('code', ''),
 
39
            'redirect_uri': self.get_redirect_uri(state)
 
40
        }
 
41
 
 
42
    def get_user_details(self, response):
 
43
        """Return user details from Chatwork account"""
 
44
        fullname, first_name, last_name = self.get_user_names(
 
45
            response.get('name')
 
46
        )
 
47
        username = response.get('chatwork_id') or \
 
48
                   response.get('login_mail') or \
 
49
                   response.get('account_id')
 
50
        email = response.get('mail') or \
 
51
                response.get('login_mail') or \
 
52
                ''
 
53
        return {
 
54
            'username': username,
 
55
            'email': email,
 
56
            'fullname': fullname,
 
57
            'first_name': first_name,
 
58
            'last_name': last_name
 
59
        }
 
60
 
 
61
    def user_data(self, access_token, *args, **kwargs):
 
62
        """Loads user data from service"""
 
63
        headers = {'Authorization': 'Bearer ' + access_token}
 
64
        return self.get_json(self.api_url('/me'), headers=headers)
 
65
 
 
66
    def refresh_token_params(self, token, *args, **kwargs):
 
67
        return {'refresh_token': token, 'grant_type': 'refresh_token'}