2
Chatwork OAuth2 backend
6
from .oauth import BaseOAuth2
9
class ChatworkOAuth2(BaseOAuth2):
10
"""Chatwork OAuth authentication backend"""
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'
17
DEFAULT_SCOPE = ['users.profile.me:read']
20
('expires_in', 'expires'),
21
('refresh_token', 'refresh_token')
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)
28
def auth_headers(self):
30
'Authorization': b'Basic ' + base64.b64encode(
31
'{0}:{1}'.format(*self.get_key_and_secret()).encode()
35
def auth_complete_params(self, state=None):
37
'grant_type': 'authorization_code',
38
'code': self.data.get('code', ''),
39
'redirect_uri': self.get_redirect_uri(state)
42
def get_user_details(self, response):
43
"""Return user details from Chatwork account"""
44
fullname, first_name, last_name = self.get_user_names(
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 \
57
'first_name': first_name,
58
'last_name': last_name
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)
66
def refresh_token_params(self, token, *args, **kwargs):
67
return {'refresh_token': token, 'grant_type': 'refresh_token'}