1717
1818logger = logging .getLogger (__name__ )
1919
20- __author__ = ' roland hedberg'
20+ __author__ = " roland hedberg"
2121
2222
2323class AuthnFailure (Exception ):
@@ -62,9 +62,9 @@ def basic_authn(authn):
6262 _tok = base64 .b64decode (_tok )
6363 part = as_unicode (_tok ).split (":" )
6464 if len (part ) == 2 :
65- return dict (zip (['id' , ' secret' ], part ))
65+ return dict (zip (["id" , " secret" ], part ))
6666 else :
67- raise ValueError (' Illegal token' )
67+ raise ValueError (" Illegal token" )
6868
6969
7070class ClientSecretBasic (ClientAuthnMethod ):
@@ -77,9 +77,11 @@ class ClientSecretBasic(ClientAuthnMethod):
7777 def verify (self , request , authorization_info , ** kwargs ):
7878 client_info = basic_authn (authorization_info )
7979
80- if self .endpoint_context .cdb [
81- client_info ['id' ]]["client_secret" ] == client_info ['secret' ]:
82- return {'client_id' : client_info ['id' ]}
80+ if (
81+ self .endpoint_context .cdb [client_info ["id" ]]["client_secret" ]
82+ == client_info ["secret" ]
83+ ):
84+ return {"client_id" : client_info ["id" ]}
8385 else :
8486 raise AuthnFailure ()
8587
@@ -93,10 +95,11 @@ class ClientSecretPost(ClientSecretBasic):
9395 """
9496
9597 def verify (self , request , ** kwargs ):
96- if self .endpoint_context .cdb [
97- request [
98- 'client_id' ]]["client_secret" ] == request ['client_secret' ]:
99- return {'client_id' : request ['client_id' ]}
98+ if (
99+ self .endpoint_context .cdb [request ["client_id" ]]["client_secret" ]
100+ == request ["client_secret" ]
101+ ):
102+ return {"client_id" : request ["client_id" ]}
100103 else :
101104 raise AuthnFailure ("secrets doesn't match" )
102105
@@ -109,7 +112,7 @@ def verify(self, request, authorization_info, **kwargs):
109112 if not authorization_info .startswith ("Bearer " ):
110113 raise AuthnFailure ("Wrong type of authorization token" )
111114
112- return {' token' : authorization_info .split (' ' , 1 )[1 ]}
115+ return {" token" : authorization_info .split (" " , 1 )[1 ]}
113116
114117
115118class BearerBody (ClientSecretPost ):
@@ -119,13 +122,12 @@ class BearerBody(ClientSecretPost):
119122
120123 def verify (self , request , ** kwargs ):
121124 try :
122- return {' token' : request [' access_token' ]}
125+ return {" token" : request [" access_token" ]}
123126 except KeyError :
124- raise AuthnFailure (' No access token' )
127+ raise AuthnFailure (" No access token" )
125128
126129
127130class JWSAuthnMethod (ClientAuthnMethod ):
128-
129131 def verify (self , request , ** kwargs ):
130132 _jwt = JWT (self .endpoint_context .keyjar )
131133 try :
@@ -139,7 +141,7 @@ def verify(self, request, **kwargs):
139141 except AttributeError :
140142 logger .debug ("authntoken: %s" % sanitize (ca_jwt ))
141143
142- request [verified_claim_name (' client_assertion' )] = ca_jwt
144+ request [verified_claim_name (" client_assertion" )] = ca_jwt
143145
144146 try :
145147 client_id = kwargs ["client_id" ]
@@ -150,12 +152,12 @@ def verify(self, request, **kwargs):
150152 # could be either my issuer id or the token endpoint
151153 if self .endpoint_context .issuer in ca_jwt ["aud" ]:
152154 pass
153- elif self .endpoint_context .endpoint [' token' ].full_path in ca_jwt [' aud' ]:
155+ elif self .endpoint_context .endpoint [" token" ].full_path in ca_jwt [" aud" ]:
154156 pass
155157 else :
156158 raise NotForMe ("Not for me!" )
157159
158- return {' client_id' : client_id , ' jwt' : ca_jwt }
160+ return {" client_id" : client_id , " jwt" : ca_jwt }
159161
160162
161163class ClientSecretJWT (JWSAuthnMethod ):
@@ -180,21 +182,22 @@ class PrivateKeyJWT(JWSAuthnMethod):
180182 "bearer_body" : BearerBody ,
181183 "client_secret_jwt" : ClientSecretJWT ,
182184 "private_key_jwt" : PrivateKeyJWT ,
183- "none" : None
185+ "none" : None ,
184186}
185187
186188TYPE_METHOD = [(JWT_BEARER , JWSAuthnMethod )]
187189
188190
189191def valid_client_info (cinfo ):
190- eta = cinfo .get (' client_secret_expires_at' , 0 )
192+ eta = cinfo .get (" client_secret_expires_at" , 0 )
191193 if eta != 0 and eta < utc_time_sans_frac ():
192194 return False
193195 return True
194196
195197
196- def verify_client (endpoint_context , request , authorization_info = None ,
197- get_client_id_from_token = None ):
198+ def verify_client (
199+ endpoint_context , request , authorization_info = None , get_client_id_from_token = None
200+ ):
198201 """
199202 Initiated Guessing !
200203
@@ -209,78 +212,81 @@ def verify_client(endpoint_context, request, authorization_info=None,
209212 # fixes request = {} instead of str
210213 # "AttributeError: 'dict' object has no attribute 'startswith'" in oidcendpoint/endpoint.py(158)client_authentication()
211214 if isinstance (authorization_info , dict ):
212- strings_parade = (' {} {}' .format (k ,v ) for k ,v in authorization_info .items ())
213- authorization_info = ' ' .join (strings_parade )
215+ strings_parade = (" {} {}" .format (k , v ) for k , v in authorization_info .items ())
216+ authorization_info = " " .join (strings_parade )
214217
215218 if authorization_info is None :
216- if ' client_id' in request and ' client_secret' in request :
219+ if " client_id" in request and " client_secret" in request :
217220 auth_info = ClientSecretPost (endpoint_context ).verify (request )
218- auth_info [' method' ] = ' client_secret_post'
219- elif ' client_assertion' in request :
221+ auth_info [" method" ] = " client_secret_post"
222+ elif " client_assertion" in request :
220223 auth_info = JWSAuthnMethod (endpoint_context ).verify (request )
221224 # If symmetric key was used
222225 # auth_method = 'client_secret_jwt'
223226 # If asymmetric key was used
224- auth_info [' method' ] = ' private_key_jwt'
225- elif ' access_token' in request :
227+ auth_info [" method" ] = " private_key_jwt"
228+ elif " access_token" in request :
226229 auth_info = BearerBody (endpoint_context ).verify (request )
227- auth_info [' method' ] = ' bearer_body'
230+ auth_info [" method" ] = " bearer_body"
228231 else :
229232 raise UnknownOrNoAuthnMethod ()
230233 else :
231- if authorization_info .startswith (' Basic ' ):
234+ if authorization_info .startswith (" Basic " ):
232235 auth_info = ClientSecretBasic (endpoint_context ).verify (
233- request , authorization_info )
234- auth_info ['method' ] = 'client_secret_basic'
235- elif authorization_info .startswith ('Bearer ' ):
236+ request , authorization_info
237+ )
238+ auth_info ["method" ] = "client_secret_basic"
239+ elif authorization_info .startswith ("Bearer " ):
236240 auth_info = BearerHeader (endpoint_context ).verify (
237- request , authorization_info )
238- auth_info ['method' ] = 'bearer_header'
241+ request , authorization_info
242+ )
243+ auth_info ["method" ] = "bearer_header"
239244 else :
240245 raise UnknownOrNoAuthnMethod (authorization_info )
241246
242247 try :
243- client_id = auth_info [' client_id' ]
248+ client_id = auth_info [" client_id" ]
244249 except KeyError :
245250 try :
246- _token = auth_info [' token' ]
251+ _token = auth_info [" token" ]
247252 except KeyError :
248- logger .warning (' No token' )
253+ logger .warning (" No token" )
249254 else :
250255 if get_client_id_from_token :
251256 try :
252257 _id = get_client_id_from_token (endpoint_context , _token , request )
253258 except KeyError :
254- raise ValueError (' Unknown token' )
259+ raise ValueError (" Unknown token" )
255260
256261 if _id :
257- auth_info [' client_id' ] = _id
262+ auth_info [" client_id" ] = _id
258263 else :
259264 try :
260265 _cinfo = endpoint_context .cdb [client_id ]
261266 except KeyError :
262- raise ValueError (' Unknown Client ID' )
267+ raise ValueError (" Unknown Client ID" )
263268 else :
264269 if isinstance (_cinfo , str ):
265270 try :
266271 _cinfo = endpoint_context .cdb [_cinfo ]
267272 except KeyError :
268- raise ValueError (' Unknown Client ID' )
273+ raise ValueError (" Unknown Client ID" )
269274
270275 try :
271276 valid_client_info (_cinfo )
272277 except KeyError :
273- logger .warning (' Client registration has timed out' )
274- raise ValueError (' Not valid client' )
278+ logger .warning (" Client registration has timed out" )
279+ raise ValueError (" Not valid client" )
275280 else :
276281 # store what authn method was used
277282 try :
278- endpoint_context .cdb [client_id ]['auth_method' ][
279- request .__class__ .__name__ ] = auth_info ['method' ]
283+ endpoint_context .cdb [client_id ]["auth_method" ][
284+ request .__class__ .__name__
285+ ] = auth_info ["method" ]
280286 except KeyError :
281287 try :
282- endpoint_context .cdb [client_id ][' auth_method' ] = {
283- request .__class__ .__name__ : auth_info [' method' ]
288+ endpoint_context .cdb [client_id ][" auth_method" ] = {
289+ request .__class__ .__name__ : auth_info [" method" ]
284290 }
285291 except KeyError :
286292 pass
0 commit comments