@@ -26,26 +26,26 @@ class UserProfile:
2626 @classmethod
2727 def from_jwt_payload (cls , payload ):
2828 return cls (
29- user_id = payload .get (' urs-user-id' ),
30- token = payload .get (' urs-access-token' ),
31- groups = payload .get (' urs-groups' ),
32- first_name = payload .get (' first_name' ),
33- last_name = payload .get (' last_name' ),
34- email = payload .get (' email' ),
35- iat = payload .get (' iat' ),
36- exp = payload .get (' exp' )
29+ user_id = payload .get (" urs-user-id" ),
30+ token = payload .get (" urs-access-token" ),
31+ groups = payload .get (" urs-groups" ),
32+ first_name = payload .get (" first_name" ),
33+ last_name = payload .get (" last_name" ),
34+ email = payload .get (" email" ),
35+ iat = payload .get (" iat" ),
36+ exp = payload .get (" exp" ),
3737 )
3838
3939 def to_jwt_payload (self ):
4040 return {
41- ' urs-user-id' : self .user_id ,
42- ' urs-access-token' : self .token ,
43- ' urs-groups' : self .groups ,
44- ' first_name' : self .first_name ,
45- ' last_name' : self .last_name ,
46- ' email' : self .email ,
47- ' iat' : self .iat ,
48- ' exp' : self .exp ,
41+ " urs-user-id" : self .user_id ,
42+ " urs-access-token" : self .token ,
43+ " urs-groups" : self .groups ,
44+ " first_name" : self .first_name ,
45+ " last_name" : self .last_name ,
46+ " email" : self .email ,
47+ " iat" : self .iat ,
48+ " exp" : self .exp ,
4949 }
5050
5151
@@ -57,7 +57,7 @@ def __init__(
5757 private_key : str ,
5858 cookie_name : str ,
5959 blacklist = {},
60- session_ttl_in_hours : float = 7 * 24
60+ session_ttl_in_hours : float = 7 * 24 ,
6161 ):
6262 self .algorithm = algorithm
6363 self .public_key = public_key
@@ -67,7 +67,9 @@ def __init__(
6767 self .black_list = blacklist
6868
6969 def _get_auth_cookie (self , headers : Mapping [str , str ]):
70- cookie_string = headers .get ('cookie' ) or headers .get ('Cookie' ) or headers .get ('COOKIE' )
70+ cookie_string = (
71+ headers .get ("cookie" ) or headers .get ("Cookie" ) or headers .get ("COOKIE" )
72+ )
7173 if not cookie_string :
7274 return {}
7375
@@ -80,32 +82,32 @@ def _decode_jwt(self, token: str):
8082 try :
8183 return jwt .decode (token .encode (), self .public_key , [self .algorithm ])
8284 except jwt .ExpiredSignatureError :
83- log .info (' JWT has expired' )
85+ log .info (" JWT has expired" )
8486 except jwt .InvalidSignatureError :
85- log .info (' JWT has failed verification' )
87+ log .info (" JWT has failed verification" )
8688 return None
8789
8890 def _encode_jwt (self , payload : Mapping [str , str ]) -> str :
8991 try :
9092 encoded = jwt .encode (payload , self .private_key , self .algorithm )
9193 except TypeError :
92- log .error (' unable to encode jwt cookie' )
93- return ''
94+ log .error (" unable to encode jwt cookie" )
95+ return ""
9496 return encoded
9597
9698 def _jwt_payload_from_user_profile (self , user_profile : Optional [UserProfile ]):
9799 if user_profile is None :
98100 return {}
99101 now = int (time ())
100102 return {
101- ' urs-user-id' : user_profile .user_id ,
102- ' first_name' : user_profile .first_name ,
103- ' last_name' : user_profile .last_name ,
104- ' email' : user_profile .email ,
105- ' urs-access-token' : user_profile .token ,
106- ' urs-groups' : user_profile .groups ,
107- ' iat' : now ,
108- ' exp' : now + self .session_ttl
103+ " urs-user-id" : user_profile .user_id ,
104+ " first_name" : user_profile .first_name ,
105+ " last_name" : user_profile .last_name ,
106+ " email" : user_profile .email ,
107+ " urs-access-token" : user_profile .token ,
108+ " urs-groups" : user_profile .groups ,
109+ " iat" : now ,
110+ " exp" : now + self .session_ttl ,
109111 }
110112
111113 def _in_blacklist (self , user_profile : UserProfile ):
@@ -117,7 +119,8 @@ def _in_blacklist(self, user_profile: UserProfile):
117119 return False
118120
119121 def get_profile_from_headers (
120- self , headers : Mapping [str , str ],
122+ self ,
123+ headers : Mapping [str , str ],
121124 ) -> Optional [UserProfile ]:
122125 """Inspects headers for auth cookie and return user_profile if authenticated, None otherwise"""
123126 auth_cookie = self ._get_auth_cookie (headers )
@@ -133,22 +136,26 @@ def get_profile_from_headers(
133136 return None
134137 return user_profile
135138
136- def get_header_to_set_auth_cookie (self , user_profile : Optional [UserProfile ], cookie_domain : str = '' ):
137- """ Gets a header to set auth-cookie
139+ def get_header_to_set_auth_cookie (
140+ self ,
141+ user_profile : Optional [UserProfile ],
142+ cookie_domain : str = "" ,
143+ ):
144+ """Gets a header to set auth-cookie
138145
139146 Parameters:
140147 UserProfile: UserProfile to use in construction of a cookie, if none will return header to unset/logout
141148 """
142149 payload = self ._jwt_payload_from_user_profile (user_profile )
143- cookie_value = self ._encode_jwt (payload ) if payload else ' expired'
144- cookie_domain = f' ; Domain={ cookie_domain } ' if cookie_domain else ''
150+ cookie_value = self ._encode_jwt (payload ) if payload else " expired"
151+ cookie_domain = f" ; Domain={ cookie_domain } " if cookie_domain else ""
145152 if payload :
146- expire_date = format_7231_date (payload [' exp' ])
153+ expire_date = format_7231_date (payload [" exp" ])
147154 else :
148- expire_date = ' Thu, 01 Jan 1970 00:00:00 GMT'
155+ expire_date = " Thu, 01 Jan 1970 00:00:00 GMT"
149156 return {
150- ' SET-COOKIE' : (
151- f' { self .cookie_name } ={ cookie_value } ; Expires={ expire_date } ; Path=/{ cookie_domain } ; Secure; '
152- ' HttpOnly; SameSite=Lax'
157+ " SET-COOKIE" : (
158+ f" { self .cookie_name } ={ cookie_value } ; Expires={ expire_date } ; Path=/{ cookie_domain } ; Secure; "
159+ " HttpOnly; SameSite=Lax"
153160 )
154161 }
0 commit comments