|
| 1 | +import functools |
| 2 | + |
| 3 | +import flask |
| 4 | +from flask.helpers import url_for |
| 5 | +from oic.oauth2 import rndstr |
| 6 | +from oic.oic import Client |
| 7 | +from oic.oic.message import ProviderConfigurationResponse, RegistrationRequest, \ |
| 8 | + AuthorizationResponse |
| 9 | +from oic.utils.authn.client import CLIENT_AUTHN_METHOD |
| 10 | +from werkzeug.utils import redirect |
| 11 | + |
| 12 | + |
| 13 | +class OIDCAuthentication(object): |
| 14 | + def __init__(self, flask_app, client_registration_info=None, issuer=None, |
| 15 | + provider_configuration_info=None): |
| 16 | + self.app = flask_app |
| 17 | + |
| 18 | + self.client = Client(client_authn_method=CLIENT_AUTHN_METHOD) |
| 19 | + if not issuer and not provider_configuration_info: |
| 20 | + raise ValueError( |
| 21 | + 'Either \'issuer\' (for dynamic discovery) or \'provider_configuration_info\' (for static configuration must be specified.') |
| 22 | + if issuer and not provider_configuration_info: |
| 23 | + self.client.provider_config(issuer) |
| 24 | + else: |
| 25 | + self.client.handle_provider_config( |
| 26 | + ProviderConfigurationResponse(**provider_configuration_info), |
| 27 | + provider_configuration_info['issuer']) |
| 28 | + |
| 29 | + self.client_registration_info = client_registration_info or {} |
| 30 | + if client_registration_info and 'client_id' in client_registration_info: |
| 31 | + # static client info provided |
| 32 | + self.client.store_registration_info(RegistrationRequest(**client_registration_info)) |
| 33 | + else: |
| 34 | + # do dynamic registration |
| 35 | + self.app.add_url_rule('/redirect_uri', 'redirect_uri', |
| 36 | + self._handle_authentication_response) |
| 37 | + with self.app.app_context(): |
| 38 | + self.client_registration_info['redirect_uris'] = url_for('redirect_uri') |
| 39 | + self.client.register(self.client.provider_info['registration_endpoint'], |
| 40 | + **self.client_registration_info) |
| 41 | + |
| 42 | + self.callback = None |
| 43 | + |
| 44 | + def _authenticate(self): |
| 45 | + if flask.g.get('userinfo', None): |
| 46 | + return self.callback() |
| 47 | + |
| 48 | + flask.session['state'] = rndstr() |
| 49 | + flask.session['nonce'] = rndstr() |
| 50 | + args = { |
| 51 | + 'client_id': self.client.client_id, |
| 52 | + 'response_type': 'code', |
| 53 | + 'scope': ['openid'], |
| 54 | + 'redirect_uri': self.client.registration_response['redirect_uris'][0], |
| 55 | + 'state': flask.session['state'], |
| 56 | + 'nonce': flask.session['nonce'], |
| 57 | + } |
| 58 | + |
| 59 | + auth_req = self.client.construct_AuthorizationRequest(request_args=args) |
| 60 | + login_url = auth_req.request(self.client.authorization_endpoint) |
| 61 | + return redirect(login_url) |
| 62 | + |
| 63 | + def _handle_authentication_response(self): |
| 64 | + # parse authentication response |
| 65 | + query_string = flask.request.query_string.decode('utf-8') |
| 66 | + authn_resp = self.client.parse_response(AuthorizationResponse, info=query_string, |
| 67 | + sformat='urlencoded') |
| 68 | + |
| 69 | + if authn_resp['state'] != flask.session['state']: |
| 70 | + raise ValueError('The \'state\' parameter does not match.') |
| 71 | + |
| 72 | + # do token request |
| 73 | + args = { |
| 74 | + 'code': authn_resp['code'], |
| 75 | + 'redirect_uri': self.client.registration_response['redirect_uris'][0], |
| 76 | + 'client_id': self.client.client_id, |
| 77 | + 'client_secret': self.client.client_secret |
| 78 | + } |
| 79 | + token_resp = self.client.do_access_token_request(scope='openid', state=authn_resp['state'], |
| 80 | + request_args=args, |
| 81 | + authn_method='client_secret_basic') |
| 82 | + id_token = token_resp['id_token'] |
| 83 | + if id_token['nonce'] != flask.session['nonce']: |
| 84 | + raise ValueError('The \'nonce\' parameter does not match.') |
| 85 | + access_token = token_resp['access_token'] |
| 86 | + |
| 87 | + # do userinfo request |
| 88 | + userinfo = self.client.do_user_info_request(state=authn_resp['state']) |
| 89 | + if userinfo['sub'] != id_token['sub']: |
| 90 | + raise ValueError('The \'sub\' of userinfo does not match \'sub\' of ID Token.') |
| 91 | + |
| 92 | + # store the current user |
| 93 | + flask.g.id_token = id_token |
| 94 | + flask.g.access_token = access_token |
| 95 | + flask.g.userinfo = userinfo |
| 96 | + |
| 97 | + return self.callback() |
| 98 | + |
| 99 | + def oidc_auth(self, f): |
| 100 | + self.callback = f |
| 101 | + |
| 102 | + @functools.wraps(f) |
| 103 | + def wrapper(): |
| 104 | + return self._authenticate() |
| 105 | + |
| 106 | + return wrapper |
0 commit comments