-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
192 lines (154 loc) · 5.22 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import traceback
from flask import Flask, request, redirect, url_for
import requests
import base64
import os
import clientconfig as cfg
app = Flask(__name__)
# here you can setup acr_values for scripts
ACR_VALUES = "passport_saml"
sub = ''
'''
STILL NOT WORKING - TO DO
@app.route('/endsession/<token>')
def end_session(token):
"""
Ends user session - uses logout redirect uri if string is not empty
:param token: Previously issued ID Token (id_token) passed to the logout endpoint as a hint
about the End-User's current authenticated session with the Client.
:return: redirect to login
"""
global sub
params = {
"id_token_hint": sub
#"state": base64.b64encode(os.urandom(18)).decode()
f
}
if cfg.LOGOUT_REDIRECT_URI is not "":
params.update({"post_logout_redirect_uri" : cfg.LOGOUT_REDIRECT_URI})
r = requests.get(url=cfg.ENDSESSION_URI, verify=cfg.SSL_VERIFY)
#r = requests.get(url=cfg.ENDSESSION_URI, params=params, verify=cfg.SSL_VERIFY)
print(r.json())
'''
@app.route('/login')
def login():
'''
Loads request object and shows page with Login button to authn
:return: html page with login button
'''
html = ""
html_line = '\t\t\t\t<input type="hidden" name="%s" value="%s" />\n'
request_object = {"scope": cfg.SCOPE,
"response_type": "code",
"client_id": cfg.CLIENT_ID,
"client_secret": cfg.CLIENT_SECRET,
"redirect_uri": cfg.REDIRECT_URI,
"acr_values": ACR_VALUES,
"state": base64.b64encode(os.urandom(18)).decode(),
"nonce": base64.b64encode(os.urandom(18)).decode()
}
for param in request_object.keys():
html = html + html_line % (param, request_object[param])
print("request_object = " + str(request_object))
print("html = " + html)
return '''
<h1>Welcome to OpenID Tester</H1>
<form action="%s" method="post">
%s
<input value="Login" type="submit" />
</form>
''' % (cfg.AUTH_URI, html)
@app.route('/callback')
def callback():
'''
- Receives callback from OP, including 'code'
- Get access token using the code
- redirects to /userinfo/<token> OR links to userinfo
:return: redirects to get_user_info url w/ access token OR links to userinfo
'''
if request.args.get('error_description'):
print("OP error: " + request.args.get('error_description'))
code = request.args.get('code')
session_id = request.args.get('session_id')
session_state = request.args.get('session_state')
print("CODE: " + code)
r = request.query_string
print("Query string: " + str(r))
tokens = get_tokens(code)
print("Access Token: " + str(tokens['access_token']))
return '''
<H1> Logged in </H1>
<a href="%s"> Get userinfo </a>
''' % (url_for('get_user_info', token=tokens['access_token']))
@app.route('/userinfo/<token>')
def get_user_info(token):
'''''''''''''''''''''''''''''
Shows user information scoped
:param token: client token
:return: all userinfo attribute s scoped
'''
print("Entered get_user_info")
print(token)
headers = {"Authorization": "Bearer %s" % token}
r = requests.post(url=cfg.USERINFO, headers=headers, verify=cfg.SSL_VERIFY)
print(r.json())
json_resp = r.json()
#global sub
#sub = json_resp['sub']
# lets create an HTML code while we don't use templates
html = ''
html_line = '\t\t\t\t<p><b>%s: </b>%s</p>\n'
for item in json_resp:
html = html + html_line % (item, json_resp[item])
print(item)
print(json_resp[item])
return'''
<H1>This is your userinfo</H1>
%s
''' % html
'''
<H1>This is your userinfo</H1>
<a href=%s>Logout</a>
%s
''' % (url_for('end_session', token=token), html)
'''
'''
#return r.json()
def get_tokens(code):
'''
Get tokens using the auth code
:param code: auth code
:return: {} tokens (dict)
'''
tokens = None
# this is for client_secret_basic auth method
credentials = requests.auth.HTTPBasicAuth(cfg.CLIENT_ID, cfg.CLIENT_SECRET)
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
params = {
"code": code,
"grant_type": "authorization_code",
"client_id": cfg.CLIENT_ID,
"redirect_uri": cfg.REDIRECT_URI
}
print("params = " + str(params))
try:
r = requests.post(url=cfg.TOKEN_ENDPOINT,
data=params,
headers=headers,
auth=credentials,
verify=cfg.SSL_VERIFY)
print(r)
if r.status_code != 200:
print("Token Error! Return Code %i" % r.status_code)
print(r)
print(r.json())
return None
tokens = r.json()
print("Tokens: %s\n" % str(tokens))
except:
print(traceback.format_exc())
print("Tokens = %s" % str(tokens))
return tokens
if __name__ == '__main__':
app.debug = True
app.run(host='0.0.0.0', ssl_context=('cert.pem', 'key.pem'))