|
1 | 1 | """ |
2 | 2 | cdis_oauth2client.oauth2 |
3 | | -
|
4 | | -Provides :py:func:``authorize`` to perform the OAuth2 authorization. Note that |
5 | | -this module should be kept entirely Flask-agnostic, i.e. Flask should not be |
6 | | -imported here and :py:func:``authorize`` should accept an ``OAuth2Client`` with |
7 | | -a ``get_token`` method, in order to modularize the logic. |
8 | 3 | """ |
9 | 4 |
|
| 5 | +import flask |
10 | 6 | import requests |
11 | 7 |
|
12 | 8 | from .exceptions import OAuth2Error |
13 | 9 |
|
14 | 10 |
|
15 | | -def authorize(oauth_client, user_api, code): |
| 11 | +def get_username(user_api=None): |
16 | 12 | """ |
17 | | - Authorize an OAuth client. |
| 13 | + Given the URL for the user API, call user-api to get the username for the |
| 14 | + current flask session using the current access_token cookie. |
| 15 | +
|
| 16 | + For this function to get the username, the user must already be |
| 17 | + authenticated with an access_token cookie stored in the flask session: |
| 18 | +
|
| 19 | + flask.session['access_token'] |
| 20 | +
|
| 21 | + If the `user_api` argument is not provided, get the user API URL from: |
| 22 | +
|
| 23 | + flask.current_app.config['USER_API'] |
18 | 24 |
|
19 | | - :param oauth_client: the OAuth2 client to authorize |
20 | | - :type oauth_client: OAuth2Client |
21 | 25 | :param user_api: URL for the user API |
22 | 26 | :type user_api: str |
23 | | - :param code: will usually be flask.request.args.get('code') |
24 | | - :type code: str |
25 | | - :return: the username |
| 27 | + :return: the username: |
26 | 28 | :rtype: str |
27 | 29 | """ |
28 | | - if not code: |
29 | | - raise OAuth2Error('no authorization code provided') |
30 | | - |
31 | | - token_response = oauth_client.get_token(code) |
32 | | - access_token = token_response.get('access_token') |
33 | | - if not access_token: |
34 | | - raise OAuth2Error( |
35 | | - message='did not receive access token in response from client', |
36 | | - json=token_response |
37 | | - ) |
| 30 | + if user_api is None: |
| 31 | + try: |
| 32 | + user_api = flask.current_app.config['USER_API'] |
| 33 | + except KeyError as e: |
| 34 | + raise OAuth2Error("'USER_API' not set in flask.current_app.config") |
38 | 35 |
|
39 | 36 | try: |
40 | | - headers = {'Authorization': 'Bearer ' + access_token} |
41 | | - request = requests.get(user_api + 'user/', headers=headers) |
42 | | - user_api_response = request.json() |
| 37 | + access_token = flask.session['access_token'] |
| 38 | + except KeyError: |
| 39 | + code = flask.request.args.get('code') |
| 40 | + if code is None: |
| 41 | + raise OAuth2Error('could not obtain access token') |
| 42 | + access_token = flask.current_app.oauth2.get_access_token(code) |
| 43 | + |
| 44 | + url = user_api + 'user/' |
| 45 | + headers = {'Authorization': 'Bearer ' + access_token} |
| 46 | + try: |
| 47 | + response = requests.get(url, headers=headers).json() |
43 | 48 | except requests.RequestException as e: |
44 | | - raise OAuth2Error( |
45 | | - 'failed to get user info due to requests exception: {}'.format(e) |
46 | | - ) |
47 | | - except Exception as e: |
48 | | - raise OAuth2Error('failed due to unexpected exception: {}'.format(e)) |
49 | | - |
50 | | - username = user_api_response.get('username') |
51 | | - if not username: |
52 | | - raise OAuth2Error(json=user_api_response) |
| 49 | + msg = 'failed to get username due to requests exception: {}' |
| 50 | + raise OAuth2Error(msg.format(e)) |
| 51 | + username = response.get('username') |
| 52 | + if username is None: |
| 53 | + msg = 'username missing from response: {}' |
| 54 | + raise OAuth2Error(msg.format(response)) |
53 | 55 | return username |
0 commit comments