@@ -25,26 +25,26 @@ class UserProfile:
2525 @classmethod
2626 def from_jwt_payload (cls , payload ):
2727 return cls (
28- user_id = payload .get (' urs-user-id' ),
29- token = payload .get (' urs-access-token' ),
30- groups = payload .get (' urs-groups' ),
31- first_name = payload .get (' first_name' ),
32- last_name = payload .get (' last_name' ),
33- email = payload .get (' email' ),
34- iat = payload .get (' iat' ),
35- exp = payload .get (' exp' )
28+ user_id = payload .get (" urs-user-id" ),
29+ token = payload .get (" urs-access-token" ),
30+ groups = payload .get (" urs-groups" ),
31+ first_name = payload .get (" first_name" ),
32+ last_name = payload .get (" last_name" ),
33+ email = payload .get (" email" ),
34+ iat = payload .get (" iat" ),
35+ exp = payload .get (" exp" ),
3636 )
3737
3838 def to_jwt_payload (self ):
3939 return {
40- ' urs-user-id' : self .user_id ,
41- ' urs-access-token' : self .token ,
42- ' urs-groups' : self .groups ,
43- ' first_name' : self .first_name ,
44- ' last_name' : self .last_name ,
45- ' email' : self .email ,
46- ' iat' : self .iat ,
47- ' exp' : self .exp ,
40+ " urs-user-id" : self .user_id ,
41+ " urs-access-token" : self .token ,
42+ " urs-groups" : self .groups ,
43+ " first_name" : self .first_name ,
44+ " last_name" : self .last_name ,
45+ " email" : self .email ,
46+ " iat" : self .iat ,
47+ " exp" : self .exp ,
4848 }
4949
5050
@@ -56,7 +56,7 @@ def __init__(
5656 private_key : str ,
5757 cookie_name : str ,
5858 blacklist = {},
59- session_ttl_in_hours : float = 7 * 24
59+ session_ttl_in_hours : float = 7 * 24 ,
6060 ):
6161 self .algorithm = algorithm
6262 self .public_key = public_key
@@ -66,7 +66,9 @@ def __init__(
6666 self .black_list = blacklist
6767
6868 def _get_auth_cookie (self , headers : Mapping [str , str ]):
69- cookie_string = headers .get ('cookie' ) or headers .get ('Cookie' ) or headers .get ('COOKIE' )
69+ cookie_string = (
70+ headers .get ("cookie" ) or headers .get ("Cookie" ) or headers .get ("COOKIE" )
71+ )
7072 if not cookie_string :
7173 return {}
7274
@@ -79,32 +81,32 @@ def _decode_jwt(self, token: str):
7981 try :
8082 return jwt .decode (token .encode (), self .public_key , [self .algorithm ])
8183 except jwt .ExpiredSignatureError :
82- log .info (' JWT has expired' )
84+ log .info (" JWT has expired" )
8385 except jwt .InvalidSignatureError :
84- log .info (' JWT has failed verification' )
86+ log .info (" JWT has failed verification" )
8587 return None
8688
8789 def _encode_jwt (self , payload : Mapping [str , str ]) -> str :
8890 try :
8991 encoded = jwt .encode (payload , self .private_key , self .algorithm )
9092 except TypeError :
91- log .error (' unable to encode jwt cookie' )
92- return ''
93+ log .error (" unable to encode jwt cookie" )
94+ return ""
9395 return encoded
9496
9597 def _jwt_payload_from_user_profile (self , user_profile : Optional [UserProfile ]):
9698 if user_profile is None :
9799 return {}
98100 now = int (time ())
99101 return {
100- ' urs-user-id' : user_profile .user_id ,
101- ' first_name' : user_profile .first_name ,
102- ' last_name' : user_profile .last_name ,
103- ' email' : user_profile .email ,
104- ' urs-access-token' : user_profile .token ,
105- ' urs-groups' : user_profile .groups ,
106- ' iat' : now ,
107- ' exp' : now + self .session_ttl
102+ " urs-user-id" : user_profile .user_id ,
103+ " first_name" : user_profile .first_name ,
104+ " last_name" : user_profile .last_name ,
105+ " email" : user_profile .email ,
106+ " urs-access-token" : user_profile .token ,
107+ " urs-groups" : user_profile .groups ,
108+ " iat" : now ,
109+ " exp" : now + self .session_ttl ,
108110 }
109111
110112 def _in_blacklist (self , user_profile : UserProfile ):
@@ -130,22 +132,24 @@ def get_profile_from_headers(self, headers) -> Optional[UserProfile]:
130132 return None
131133 return user_profile
132134
133- def get_header_to_set_auth_cookie (self , user_profile : Optional [UserProfile ], cookie_domain = '' ):
134- """ Gets a header to set auth-cookie
135+ def get_header_to_set_auth_cookie (
136+ self , user_profile : Optional [UserProfile ], cookie_domain = ""
137+ ):
138+ """Gets a header to set auth-cookie
135139
136140 Parameters:
137141 UserProfile: UserProfile to use in construction of a cookie, if none will return header to unset/logout
138142 """
139143 payload = self ._jwt_payload_from_user_profile (user_profile )
140- cookie_value = self ._encode_jwt (payload ) if payload else ' expired'
141- cookie_domain = f' ; Domain={ cookie_domain } ' if cookie_domain else ''
144+ cookie_value = self ._encode_jwt (payload ) if payload else " expired"
145+ cookie_domain = f" ; Domain={ cookie_domain } " if cookie_domain else ""
142146 if payload :
143- expire_date = format_7231_date (payload [' exp' ])
147+ expire_date = format_7231_date (payload [" exp" ])
144148 else :
145- expire_date = ' Thu, 01 Jan 1970 00:00:00 GMT'
149+ expire_date = " Thu, 01 Jan 1970 00:00:00 GMT"
146150 return {
147- ' SET-COOKIE' : (
148- f' { self .cookie_name } ={ cookie_value } ; Expires={ expire_date } ; Path=/{ cookie_domain } ; Secure; '
149- ' HttpOnly; SameSite=Lax'
151+ " SET-COOKIE" : (
152+ f" { self .cookie_name } ={ cookie_value } ; Expires={ expire_date } ; Path=/{ cookie_domain } ; Secure; "
153+ " HttpOnly; SameSite=Lax"
150154 )
151155 }
0 commit comments