forked from ONSdigital/sdc-login-user
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jwt.py
34 lines (25 loc) · 729 Bytes
/
jwt.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
import os
import json
from jose import jwt
JWT_ALGORITHM = 'HS256'
JWT_SECRET = 'secret'
def encode(data):
return jwt.encode(data, JWT_SECRET, algorithm=JWT_ALGORITHM)
def decode(token):
return jwt.decode(token, JWT_SECRET, algorithms=[JWT_ALGORITHM])
def main():
global JWT_SECRET
data = {'key': 'value'}
print(json.dumps(data))
token = encode(data)
print(json.dumps(token))
#JWT_SECRET="wrong"
print(decode(token))
if __name__ == '__main__':
# Use configured secret if available:
if 'JWT_SECRET' in os.environ:
JWT_SECRET = os.environ['JWT_SECRET']
print("Info: using configured secret")
else:
print("Warn: using default JWT secret.")
main()