|
13 | 13 | from satosa.backends.oauth import _OAuthBackend
|
14 | 14 | from satosa.internal import InternalData
|
15 | 15 | from satosa.internal import AuthenticationInformation
|
16 |
| -from satosa.response import Redirect |
| 16 | +from satosa.util import rndstr |
17 | 17 |
|
18 | 18 | logger = logging.getLogger(__name__)
|
19 | 19 |
|
@@ -45,50 +45,41 @@ def __init__(self, outgoing, internal_attributes, config, base_url, name):
|
45 | 45 | outgoing, internal_attributes, config, base_url, name, 'orcid',
|
46 | 46 | 'orcid')
|
47 | 47 |
|
48 |
| - def start_auth(self, context, internal_request, get_state=stateID): |
49 |
| - """ |
50 |
| - :param get_state: Generates a state to be used in authentication call |
51 |
| -
|
52 |
| - :type get_state: Callable[[str, bytes], str] |
53 |
| - :type context: satosa.context.Context |
54 |
| - :type internal_request: satosa.internal.InternalData |
55 |
| - :rtype satosa.response.Redirect |
56 |
| - """ |
57 |
| - request_args = dict( |
58 |
| - client_id=self.config['client_config']['client_id'], |
59 |
| - redirect_uri=self.redirect_url, |
60 |
| - scope=' '.join(self.config['scope']), ) |
61 |
| - cis = self.consumer.construct_AuthorizationRequest( |
62 |
| - request_args=request_args) |
63 |
| - return Redirect(cis.request(self.consumer.authorization_endpoint)) |
| 48 | + def get_request_args(self, get_state=stateID): |
| 49 | + oauth_state = get_state(self.config["base_url"], rndstr().encode()) |
| 50 | + request_args = { |
| 51 | + "client_id": self.config['client_config']['client_id'], |
| 52 | + "redirect_uri": self.redirect_url, |
| 53 | + "scope": ' '.join(self.config['scope']), |
| 54 | + "state": oauth_state, |
| 55 | + } |
| 56 | + return request_args |
64 | 57 |
|
65 | 58 | def auth_info(self, requrest):
|
66 | 59 | return AuthenticationInformation(
|
67 | 60 | UNSPECIFIED, None,
|
68 | 61 | self.config['server_info']['authorization_endpoint'])
|
69 | 62 |
|
70 | 63 | def _authn_response(self, context):
|
| 64 | + state_data = context.state[self.name] |
71 | 65 | aresp = self.consumer.parse_response(
|
72 | 66 | AuthorizationResponse, info=json.dumps(context.request))
|
73 |
| - url = self.config['server_info']['token_endpoint'] |
74 |
| - data = dict( |
75 |
| - grant_type='authorization_code', |
76 |
| - code=aresp['code'], |
77 |
| - redirect_uri=self.redirect_url, |
78 |
| - client_id=self.config['client_config']['client_id'], |
79 |
| - client_secret=self.config['client_secret'], ) |
80 |
| - headers = {'Accept': 'application/json'} |
| 67 | + self._verify_state(aresp, state_data, context.state) |
| 68 | + |
| 69 | + rargs = {"code": aresp["code"], "redirect_uri": self.redirect_url, |
| 70 | + "state": state_data["state"]} |
| 71 | + |
| 72 | + atresp = self.consumer.do_access_token_request( |
| 73 | + request_args=rargs, state=aresp['state']) |
81 | 74 |
|
82 |
| - r = requests.post(url, data=data, headers=headers) |
83 |
| - response = r.json() |
84 |
| - token = response['access_token'] |
85 |
| - orcid, name = response['orcid'], response['name'] |
86 |
| - user_info = self.user_information(token, orcid, name) |
87 |
| - auth_info = self.auth_info(context.request) |
88 |
| - internal_response = InternalData(auth_info=auth_info) |
| 75 | + user_info = self.user_information( |
| 76 | + atresp['access_token'], atresp['orcid'], atresp['name']) |
| 77 | + internal_response = InternalData( |
| 78 | + auth_info=self.auth_info(context.request)) |
89 | 79 | internal_response.attributes = self.converter.to_internal(
|
90 | 80 | self.external_type, user_info)
|
91 |
| - internal_response.subject_id = orcid |
| 81 | + internal_response.subject_id = user_info[self.user_id_attr] |
| 82 | + del context.state[self.name] |
92 | 83 | return self.auth_callback_func(context, internal_response)
|
93 | 84 |
|
94 | 85 | def user_information(self, access_token, orcid, name):
|
|
0 commit comments