-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
40 lines (31 loc) · 1.11 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
import base64
import json
from flask import Flask, render_template, request
# Create a flask app
app = Flask(
__name__,
template_folder='templates',
static_folder='static'
)
@app.get('/')
def index():
return render_template('index.html')
# Extract the username for display from the base64 encoded header
# X-MS-CLIENT-PRINCIPAL from the 'name' claim.
#
# Fallback to `default_username` if the header is not present.
def extract_username(headers, default_username="You"):
if "X-MS-CLIENT-PRINCIPAL" not in headers:
return default_username
token = json.loads(base64.b64decode(headers.get("X-MS-CLIENT-PRINCIPAL")))
claims = {claim["typ"]: claim["val"] for claim in token["claims"]}
return claims.get("name", default_username)
@app.get('/hello')
def hello():
return render_template('hello.html', name=extract_username(request.headers))
@app.errorhandler(404)
def handle_404(e):
return '<h1>404</h1><p>File not found!</p><img src="https://httpcats.com/404.jpg" alt="cat in box" width=400>', 404
if __name__ == '__main__':
# Run the Flask app
app.run(host='0.0.0.0', debug=True, port=8080)