-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
154 lines (115 loc) · 4.61 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import logging
import os
from flask import (
Flask, request, render_template, redirect, jsonify,
session as flask_session
)
from requests_oauthlib import OAuth2Session
from oauthlib.oauth2 import BackendApplicationClient
LOG = logging.getLogger()
logging.basicConfig(level=logging.INFO)
CLIENT_ID = os.environ.get('CLIENT_ID')
CLIENT_SECRET = os.environ.get('CLIENT_SECRET')
SCOPES = ['hydra.consent']
TOKEN_ENDPOINT = os.environ.get('TOKEN_ENDPOINT')
CONSENT_ENDPOINT = os.environ.get('CONSENT_ENDPOINT')
app = Flask(__name__)
app.secret_key = 'super-secret'
@app.route('/')
def index():
return 'This is the consent app'
@app.route('/healthz')
def healthz():
return jsonify({'status': 'ok'})
@app.route('/logout')
def logout():
if 'subject' in flask_session:
del flask_session['subject']
return jsonify({'message': 'logged out'})
return jsonify({'message': 'no user logged in'})
@app.route('/consent', methods=['GET'])
def consent_get():
# Handle GET-requests to the consent endpoint. These are initiated when the
# User Agent (UA) navigates to the token request URL and Hydra redirects them
# here. We are passed an id for the consent request in the query string
# which we should validate, examine and accept or reject as we see fit.
#
# If no user interaction is needed to accept/reject the request, we
# immediately do so and redirect the UA back to Hydra by means of the
# redirect URI Hydra gives us. No interaction is needed if the user is
# logged in or the "prompt:none" scope is requested and there is no current
# logged in user.
#
# If user interaction *is* needed, we render a simple login form which will
# POST its content back to the '/consent' endpoint. This is handled by
# consent_post() below.
session = get_session()
error = request.args.get('error')
error_description = request.args.get('error_description')
if error is not None:
return render_template('error.html', error=error, error_description=error_description)
consent_id = request.args.get('consent')
if consent_id is None:
return render_template(
'error.html',
error='no consent id',
error_description='No consent ID was given for the request')
r = session.get(CONSENT_ENDPOINT + consent_id)
r.raise_for_status()
consent = r.json()
# Note: flask.session does not support .get()
try:
subject = flask_session['subject']
except KeyError:
subject = None
if 'prompt:none' in consent['requestedScopes'] and subject is None:
return _reject_request(session, consent, 'user not logged in')
if subject is not None:
return _accept_request(session, consent, subject)
return render_template('consent.html', consent=consent)
@app.route('/consent', methods=['POST'])
def consent_post():
# Handle POST requests to the consent endpoint. These are initiated after
# user interaction with the form rendered by consent_get(). The form
# provides us with the current Hydra consent id and the scheme and
# identifier for the user which should be logged in.
#
# We take the scheme and identifier from the form and use them to construct
# a subject for the consent request which we immediately grant.
session = get_session()
consent_id = request.args.get('consent')
if consent_id is None:
return 'no consent id'
r = session.get(CONSENT_ENDPOINT + consent_id)
r.raise_for_status()
consent = r.json()
scheme = request.form['scheme']
identifier = request.form['identifier']
subject = ':'.join([scheme, identifier])
flask_session['subject'] = subject
return _accept_request(session, consent, subject)
def _accept_request(session, consent, subject):
session.patch(
CONSENT_ENDPOINT + consent['id'] + '/accept', json={
'grantScopes': consent['requestedScopes'],
'subject': subject,
})
return redirect(consent['redirectUrl'])
def _reject_request(session, consent, reason):
session.patch(
CONSENT_ENDPOINT + consent['id'] + '/reject', json={
'reason': reason,
})
return redirect(consent['redirectUrl'])
def get_session():
LOG.info('Fetching initial token')
client = BackendApplicationClient(client_id=CLIENT_ID)
session = OAuth2Session(client=client)
access_token = session.fetch_token(
timeout=1, token_url=TOKEN_ENDPOINT,
client_id=CLIENT_ID,
client_secret=CLIENT_SECRET,
scope=SCOPES,
verify=False)
LOG.info('Got access token: %r', access_token)
return session