forked from okta/samples-python-flask
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
54 lines (40 loc) · 1.17 KB
/
main.py
File metadata and controls
54 lines (40 loc) · 1.17 KB
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
import json
import time
from flask import Flask, request
from flask_cors import CORS
from helpers import is_access_token_valid, config
app = Flask(__name__)
CORS(app)
app.config.update({
'SECRET_KEY': 'SomethingNotEntirelySecret',
})
def is_authorized(request):
"""Get access token from authorization header."""
try:
token = request.headers.get("Authorization").split("Bearer ")[1]
return is_access_token_valid(token, config["issuer"])
except Exception:
return False
@app.route("/")
def home():
return "Hello! There's not much to see here." \
"Please grab one of our front-end samples for use with this sample resource server"
@app.route("/api/messages")
def messages():
if not is_authorized(request):
return "Unauthorized", 401
response = {
'messages': [
{
'date': time.time(),
'text': 'I am a robot.'
},
{
'date': time.time()-3600,
'text': 'Hello, World!'
}
]
}
return json.dumps(response)
if __name__ == '__main__':
app.run(host="localhost", port=8000, debug=True)