-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
58 lines (46 loc) · 1.24 KB
/
server.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
from datetime import datetime, timezone
from flask import Flask, request
from jwt import JWT, jwk_from_pem
TOKEN = ''
app = Flask(__name__)
@app.route('/')
def route_root():
return 'Welcome to the server!'
@app.route('/alive')
def route_alive():
return {
'status': 'alive',
'time': datetime.now(timezone.utc).isoformat()
}
@app.route('/token')
def route_token():
user_id = None
jwtoken = None
if 'user_id' in request.args:
user_id = request.args['user_id']
jwtoken = encode_token(user_id)
global TOKEN
TOKEN = jwtoken
return {
'token': jwtoken
}
@app.route('/secret')
def route_secret():
token = request.args['token']
if token == TOKEN:
return 'banana'
return 'deadbeef'
def encode_token(user_id):
if user_id is None:
return None
with open('./key.pem', 'rb') as f:
secret_key = jwk_from_pem(f.read())
token = ''
if user_id in ['hahsan', 'psrity', 'admin']:
payload = {
'user_id': user_id,
'message': 'Hey Pranav, this is a secret message!',
'exp': datetime.now(timezone.utc).timestamp() + 60 * 60
}
token = JWT().encode(payload, secret_key, alg='RS256')
return token