Skip to content

Commit 16bb922

Browse files
committed
Generate a signed jwt
- Encoding using RSA private key, payload, and RS256 algorithm - (Optional) decode_and_validate_jwt() for validate
1 parent e64ac86 commit 16bb922

File tree

1 file changed

+33
-2
lines changed

1 file changed

+33
-2
lines changed

tools/events-automation/jwt_auth.py

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,40 @@ def get_RSA_public_key():
2727
return public_key
2828

2929
def get_PEM_public_key():
30-
# Serialize the public key from RSA object to PEM format, to verify digital signatures
30+
#git, to verify digital signatures
3131
pem_bytes = (get_RSA_public_key().public_bytes(
3232
encoding=serialization.Encoding.PEM,
3333
format=serialization.PublicFormat.SubjectPublicKeyInfo
3434
)).decode()
35-
return pem_bytes
35+
return pem_bytes
36+
37+
def generate_signed_jwt():
38+
AUTHORIZED_MEMBER_ID = os.getenv('AUTHORIZED_MEMBER_ID', "") # the member id that owns the OAuth Client
39+
CLIENT_KEY = os.getenv('CLIENT_KEY', "")
40+
private_key = get_RSA_private_key()
41+
payload = {
42+
"sub": AUTHORIZED_MEMBER_ID,
43+
"iss": CLIENT_KEY,
44+
"aud": "api.meetup.com",
45+
"exp": (datetime.datetime.utcnow() + datetime.timedelta(hours=24)).timestamp()
46+
}
47+
# Generates a JWT: Encodes and signs the payload using RS256 and the private RSA key, forming a base64-url encoded header, payload, and signature. Then return it.
48+
return jwt.encode(
49+
payload=payload,
50+
key=private_key,
51+
algorithm="RS256"
52+
)
53+
54+
def decode_and_validate_token(): #get_token_payload/claims
55+
token = generate_signed_jwt()
56+
pem_public_key = get_PEM_public_key()
57+
try:
58+
payload = jwt.decode(
59+
token,
60+
key=pem_public_key,
61+
algorithms="RS256",
62+
audience="api.meetup.com"
63+
)
64+
return payload
65+
except ExpiredSignatureError as error:
66+
print(f'Unable to decode the token, error: {error}')

0 commit comments

Comments
 (0)