-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathpyjwt_tutorial.py
76 lines (60 loc) · 1.96 KB
/
pyjwt_tutorial.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import jwt
import time
from Crypto.PublicKey import RSA
from datetime import datetime, timedelta, timezone
"""
https://pyjwt.readthedocs.io/en/stable/
pip3 install pyjwt
"""
def load_key(my_key):
with open(my_key, 'rb') as f:
key = RSA.import_key(f.read())
return key.export_key()
def ex1():
"""
base
"""
encoded_jwt = jwt.encode({"some": "payload"}, "secret", algorithm="HS256")
print(encoded_jwt)
decode_jwt = jwt.decode(encoded_jwt, "secret", algorithms=["HS256"])
print(decode_jwt)
def ex2():
"""
Encoding & Decoding Tokens with RS256 (RSA)
https://pyjwt.readthedocs.io/en/stable/usage.html#encoding-decoding-tokens-with-rs256-rsa
RSA 私鑰簽名, 公鑰驗證
"""
private_key = load_key("private_key.pem")
public_key = load_key("public_key.pem")
print("private_key:", private_key)
print("public_key:", public_key)
# 私鑰簽名
encoded = jwt.encode({"some": "payload"}, private_key, algorithm="RS256")
print("私鑰簽名:", encoded)
# 公鑰驗證
decoded = jwt.decode(encoded, public_key, algorithms=["RS256"])
print("公鑰 decoded:", decoded)
def ex3():
"""
Expiration Time Claim (exp)
"""
# token_exp = jwt.encode({"username":"joe", "exp": datetime.utcnow() + timedelta(minutes=1)}, "mykey123", algorithm='HS256')
jwt_payload = jwt.encode(
{"exp": datetime.now(tz=timezone.utc) + timedelta(seconds=30)},
"secret", algorithm="HS256"
)
decode_1 = jwt.decode(jwt_payload, "secret", algorithms=["HS256"])
print("decode_1:", decode_1)
time.sleep(32)
"""
JWT payload is now expired
But with some leeway(sec), it will still validate
"""
decode_leeway = jwt.decode(jwt_payload, "secret", leeway=10, algorithms=["HS256"])
print("decode_leeway:", decode_leeway)
# Signature has expired
jwt.decode(jwt_payload, "secret", algorithms=["HS256"])
if __name__ == '__main__':
ex1()
# ex2()
# ex3()