1
from requests.auth import HTTPBasicAuth
2
from ..utils import handle_http_errors
3
from .oauth import BaseOAuth2
6
class JustGivingOAuth2(BaseOAuth2):
7
"""Just Giving OAuth authentication backend"""
10
AUTHORIZATION_URL = 'https://identity.justgiving.com/connect/authorize'
11
ACCESS_TOKEN_URL = 'https://identity.justgiving.com/connect/token'
12
ACCESS_TOKEN_METHOD = 'POST'
13
USER_DATA_URL = 'https://api.justgiving.com/v1/account'
14
DEFAULT_SCOPE = ['openid', 'account', 'profile', 'email', 'fundraise']
16
def get_user_details(self, response):
17
"""Return user details from Just Giving account"""
18
fullname, first_name, last_name = self.get_user_names(
20
response.get('firstName'),
21
response.get('lastName'))
23
'username': response.get('email'),
24
'email': response.get('email'),
26
'first_name': first_name,
27
'last_name': last_name
30
def user_data(self, access_token, *args, **kwargs):
31
"""Loads user data from service"""
32
key, secret = self.get_key_and_secret()
33
return self.get_json(self.USER_DATA_URL, headers={
34
'Authorization': 'Bearer {0}'.format(access_token),
35
'Content-Type': 'application/json',
36
'x-application-key': secret,
41
def auth_complete(self, *args, **kwargs):
42
"""Completes loging process, must return user instance"""
43
state = self.validate_state()
44
self.process_error(self.data)
46
key, secret = self.get_key_and_secret()
47
response = self.request_access_token(
48
self.access_token_url(),
49
data=self.auth_complete_params(state),
50
headers=self.auth_headers(),
51
auth=HTTPBasicAuth(key, secret),
52
method=self.ACCESS_TOKEN_METHOD
54
self.process_error(response)
55
return self.do_auth(response['access_token'], response=response,