77import sys
88import time
99from colorama import Fore
10- from cryptography .hazmat .primitives import serialization
1110from cryptography .hazmat .backends import default_backend
11+ from cryptography .hazmat .primitives import serialization
1212from decouple import config
1313from icecream import ic
1414from pathlib import Path
4242# load private key
4343if isinstance (priv_key , pathlib .PosixPath ) and priv_key .exists ():
4444 with open (priv_key , 'rb' ) as f :
45- private_key = serialization .load_pem_private_key (
46- data = f .read (),
47- password = None ,
48- backend = default_backend ()
49- )
45+ private_key = serialization .load_pem_private_key (data = f .read (), password = None , backend = default_backend ())
5046else :
5147 # decode base64
5248 private_key = base64 .b64decode (priv_key )
5349 # load private key from env
54- private_key = serialization .load_pem_private_key (
55- data = private_key ,
56- password = None ,
57- backend = default_backend ()
58- )
50+ private_key = serialization .load_pem_private_key (data = private_key , password = None , backend = default_backend ())
5951
6052if isinstance (pub_key , pathlib .PosixPath ) and pub_key .exists ():
6153 with open (pub_key , 'rb' ) as f :
62- public_key = serialization .load_pem_public_key (
63- data = f .read (),
64- backend = default_backend ()
65- )
54+ public_key = serialization .load_pem_public_key (data = f .read (), backend = default_backend ())
6655else :
6756 # decode base64
6857 public_key = base64 .b64decode (pub_key )
6958 # load public key
70- public_key = serialization .load_pem_public_key (
71- data = public_key ,
72- backend = default_backend ()
73- )
59+ public_key = serialization .load_pem_public_key (data = public_key , backend = default_backend ())
7460
75- headers = {
76- "alg" : 'RS256' ,
77- "typ" : 'JWT' ,
78- "Accept" : 'application/json' ,
79- "Content-Type" : 'application/x-www-form-urlencoded'
80- }
61+ headers = {"alg" : 'RS256' , "typ" : 'JWT' , "Accept" : 'application/json' , "Content-Type" : 'application/x-www-form-urlencoded' }
8162
8263
8364# TODO: Fix `Signature has expired\n[ERROR] Exception in ASGI application`; scheduler.sh only works for ~7 tries / 1 hour
@@ -87,33 +68,19 @@ def gen_payload_data():
8768
8869 Avoids `invalid_grant` by getting a new `exp` value during signing
8970 """
90- payload_data = {
91- "sub" : SELF_ID ,
92- "iss" : CLIENT_ID ,
93- "aud" : "api.meetup.com" ,
94- "exp" : int (time .time () + JWT_LIFE_SPAN )
95- }
71+ payload_data = {"sub" : SELF_ID , "iss" : CLIENT_ID , "aud" : "api.meetup.com" , "exp" : int (time .time () + JWT_LIFE_SPAN )}
9672 return payload_data
9773
9874
9975def sign_token ():
10076 """Generate signed JWT"""
10177
10278 # Define headers exactly as specified in docs
103- jwt_headers = {
104- "kid" : SIGNING_KEY_ID ,
105- "typ" : "JWT" ,
106- "alg" : "RS256"
107- }
79+ jwt_headers = {"kid" : SIGNING_KEY_ID , "typ" : "JWT" , "alg" : "RS256" }
10880
10981 payload_data = gen_payload_data ()
11082
111- payload = jwt .encode (
112- headers = jwt_headers ,
113- payload = payload_data ,
114- key = private_key ,
115- algorithm = 'RS256'
116- )
83+ payload = jwt .encode (headers = jwt_headers , payload = payload_data , key = private_key , algorithm = 'RS256' )
11784
11885 return payload
11986
@@ -122,14 +89,7 @@ def verify_token(token):
12289 """Verify signed JWT against public key"""
12390
12491 try :
125- jwt .decode (
126- jwt = token ,
127- key = public_key ,
128- issuer = CLIENT_ID ,
129- audience = "api.meetup.com" ,
130- verify = True ,
131- algorithms = ['RS256' ]
132- )
92+ jwt .decode (jwt = token , key = public_key , issuer = CLIENT_ID , audience = "api.meetup.com" , verify = True , algorithms = ['RS256' ])
13393 print (f"{ Fore .GREEN } { info :<10} { Fore .RESET } Success! Token verified." )
13494 return True
13595 except jwt .exceptions .ExpiredSignatureError as e :
@@ -140,7 +100,7 @@ def verify_token(token):
140100 jwt .exceptions .InvalidSignatureError ,
141101 jwt .exceptions .InvalidIssuerError ,
142102 jwt .exceptions .InvalidAudienceError ,
143- ) as e :
103+ ) as e :
144104 print (f"{ Fore .RED } { error :<10} { Fore .RESET } { e } " )
145105 sys .exit (1 )
146106
@@ -149,16 +109,11 @@ def get_access_token(token):
149109 """Post token to auth server to get access token"""
150110
151111 # Headers for the token request
152- request_headers = {
153- "Content-Type" : "application/x-www-form-urlencoded"
154- }
112+ request_headers = {"Content-Type" : "application/x-www-form-urlencoded" }
155113
156114 # Payload exactly as specified in docs
157115 # https://www.meetup.com/api/authentication/#p04-jwt-flow-section
158- payload = {
159- "grant_type" : "urn:ietf:params:oauth:grant-type:jwt-bearer" ,
160- "assertion" : token
161- }
116+ payload = {"grant_type" : "urn:ietf:params:oauth:grant-type:jwt-bearer" , "assertion" : token }
162117 payload = urlencode (payload )
163118
164119 try :
0 commit comments