-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathserver.py
More file actions
57 lines (48 loc) · 1.52 KB
/
server.py
File metadata and controls
57 lines (48 loc) · 1.52 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
55
56
57
import pybase64
import secrets
import traceback
import asyncio
import os
from dotenv import load_dotenv
from flask import Flask, make_response, request, jsonify
from flask_cors import CORS
from okta_jwt_verifier import BaseJWTVerifier
app = Flask(__name__)
app.config.update({'SECRET_KEY': secrets.token_urlsafe()})
CORS(app)
load_dotenv('.okta.env')
ISSUER = os.getenv('ISSUER')
async def verify_token_async(token, issuer):
print(token)
"""Verify access token."""
jwt_verifier = BaseJWTVerifier(issuer=issuer, audience='api://default')
try:
await jwt_verifier.verify_access_token(token)
print(token)
headers, claims, signing_input, signature = jwt_verifier.parse_token(token)
return claims
except Exception as e:
print(f"An error occurred while verifying the token: {e}")
traceback.print_exc()
return None
def is_authorized(request):
"""Get verify and get claims from access token."""
try:
token = request.headers.get("Authorization").split("Bearer ")[1]
claims = asyncio.run(verify_token_async(token, f'{ISSUER}'))
return claims
except Exception:
return None
@app.route("/api/whoami")
def whoami():
claims = is_authorized(request)
print(claims)
if not claims:
return "Unauthorized", 401
else:
return make_response(jsonify(claims), 200)
@app.route("/api/hello")
def get_anonymous():
return "You are anonymous."
if __name__ == '__main__':
app.run(host="localhost", port=5000, debug=True)