Skip to content
This repository was archived by the owner on Jun 12, 2021. It is now read-only.

Commit d8439ac

Browse files
committed
Registration endpoint cosmesi
1 parent a8dee21 commit d8439ac

1 file changed

Lines changed: 46 additions & 53 deletions

File tree

src/oidcendpoint/oidc/registration.py

Lines changed: 46 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ class Registration(Endpoint):
128128
name = "registration"
129129

130130
# default
131-
# response_placement = 'body'
131+
# response_placement = 'body'dcfr
132132

133133
def match_client_request(self, request):
134134
_context = self.endpoint_context
@@ -193,7 +193,8 @@ def do_client_registration(self, request, client_id, ignore=None):
193193
] = self._verify_sector_identifier(request)
194194
except InvalidSectorIdentifier as err:
195195
return ResponseMessage(
196-
error="invalid_configuration_parameter", error_description=str(err)
196+
error="invalid_configuration_parameter",
197+
error_description=str(err)
197198
)
198199

199200
for item in ["policy_uri", "logo_uri", "tos_uri"]:
@@ -207,7 +208,8 @@ def do_client_registration(self, request, client_id, ignore=None):
207208
)
208209

209210
# Do I have the necessary keys
210-
for item in ["id_token_signed_response_alg", "userinfo_signed_response_alg"]:
211+
for item in ["id_token_signed_response_alg",
212+
"userinfo_signed_response_alg"]:
211213
if item in request:
212214
if request[item] in _context.provider_info[PREFERENCE2PROVIDER[item]]:
213215
ktyp = alg2keytype(request[item])
@@ -235,35 +237,24 @@ def do_client_registration(self, request, client_id, ignore=None):
235237
# if it can't load keys because the URL is false it will
236238
# just silently fail. Waiting for better times.
237239
_context.keyjar.load_keys(client_id, jwks_uri=t["jwks_uri"], jwks=t["jwks"])
238-
try:
239-
n_keys = 0
240-
for kb in _context.keyjar[client_id]:
241-
n_keys += len(kb.keys())
242-
msg = "found {} keys for client_id={}"
243-
logger.debug(msg.format(n_keys, client_id))
244-
except KeyError:
245-
pass
240+
n_keys = 0
241+
for kb in _context.keyjar.get(client_id, []):
242+
n_keys += len(kb.keys())
243+
msg = "found {} keys for client_id={}"
244+
logger.debug(msg.format(n_keys, client_id))
246245

247246
return _cinfo
248247

249248
@staticmethod
250249
def verify_redirect_uris(registration_request):
251250
verified_redirect_uris = []
252-
try:
253-
client_type = registration_request["application_type"]
254-
except KeyError: # default
255-
client_type = "web"
251+
client_type = registration_request.get("application_type", "web")
256252

253+
must_https = False
257254
if client_type == "web":
258-
try:
259-
if registration_request["response_types"] == ["code"]:
260-
must_https = False
261-
else: # one has to be implicit or hybrid
262-
must_https = True
263-
except KeyError:
264-
must_https = True
265-
else:
266-
must_https = False
255+
must_https = True
256+
if registration_request.get("response_types") == ["code"]:
257+
must_https = False
267258

268259
for uri in registration_request["redirect_uris"]:
269260
_custom = False
@@ -280,18 +271,21 @@ def verify_redirect_uris(registration_request):
280271
p.hostname,
281272
)
282273
raise InvalidRedirectURIError(
283-
"Redirect_uri must use custom scheme or http and " "localhost"
274+
"Redirect_uri must use custom "
275+
"scheme or http and localhost"
284276
)
285277
elif must_https and p.scheme != "https":
286-
raise InvalidRedirectURIError("None https redirect_uri not allowed")
287-
elif p.scheme not in ["http", "https"]: # Custom scheme
278+
msg = "None https redirect_uri not allowed"
279+
raise InvalidRedirectURIError(msg)
280+
elif p.scheme not in ["http", "https"]:
281+
# Custom scheme
288282
raise InvalidRedirectURIError(
289283
"Custom redirect_uri not allowed for web client"
290284
)
291285
elif p.fragment:
292286
raise InvalidRedirectURIError("redirect_uri contains fragment")
293287

294-
if _custom is True: # Can not verify a custom scheme
288+
if _custom: # Can not verify a custom scheme
295289
verified_redirect_uris.append((uri, {}))
296290
else:
297291
base, query = splitquery(uri)
@@ -314,15 +308,12 @@ def _verify_sector_identifier(self, request):
314308
si_url = request["sector_identifier_uri"]
315309
try:
316310
res = self.endpoint_context.httpc.get(si_url)
311+
logger.debug("sector_identifier_uri => %s", sanitize(res.text))
317312
except Exception as err:
318313
logger.error(err)
319-
res = None
320-
321-
if not res:
314+
#res = None
322315
raise InvalidSectorIdentifier("Couldn't read from sector_identifier_uri")
323316

324-
logger.debug("sector_identifier_uri => %s", sanitize(res.text))
325-
326317
try:
327318
si_redirects = json.loads(res.text)
328319
except ValueError:
@@ -351,10 +342,10 @@ def add_registration_api(self, cinfo, client_id, context):
351342
context.registration_access_token[_rat] = client_id
352343

353344
def add_client_secret(self, cinfo, client_id, context):
354-
try:
355-
args = {"delta": int(self.kwargs["client_secret_expiration_time"])}
356-
except KeyError:
357-
args = {}
345+
delta_int = int(self.kwargs.get("client_secret_expiration_time",
346+
0))
347+
if delta_int:
348+
args = {"delta": delta_int} if delta_int else {}
358349

359350
client_secret = secret(context.seed, client_id)
360351
cinfo.update(
@@ -386,13 +377,13 @@ def client_registration_setup(self, request, new_id=True, set_secret=True):
386377
_context = self.endpoint_context
387378
if new_id:
388379
# create new id och secret
389-
client_id = rndstr(12)
380+
#client_id = rndstr(12)
381+
# cdb client_id MUT be unique!
390382
while client_id in _context.cdb:
391383
client_id = rndstr(12)
392384
else:
393-
try:
394-
client_id = request["client_id"]
395-
except KeyError:
385+
client_id = request.get("client_id")
386+
if not client_id:
396387
raise ValueError("Missing client_id")
397388

398389
_cinfo = {"client_id": client_id, "client_salt": rndstr(8)}
@@ -403,13 +394,12 @@ def client_registration_setup(self, request, new_id=True, set_secret=True):
403394
if new_id:
404395
_cinfo["client_id_issued_at"] = utc_time_sans_frac()
405396

397+
client_secret = ""
406398
if set_secret:
407-
client_secret = self.add_client_secret(_cinfo, client_id, _context)
408-
else:
409-
client_secret = ""
399+
client_secret = self.add_client_secret(_cinfo, client_id,
400+
_context)
410401

411402
_context.cdb[client_id] = _cinfo
412-
413403
_cinfo = self.do_client_registration(
414404
request,
415405
client_id,
@@ -419,7 +409,8 @@ def client_registration_setup(self, request, new_id=True, set_secret=True):
419409
return _cinfo
420410

421411
args = dict(
422-
[(k, v) for k, v in _cinfo.items() if k in RegistrationResponse.c_param]
412+
[(k, v) for k, v in _cinfo.items()
413+
if k in RegistrationResponse.c_param]
423414
)
424415

425416
comb_uri(args)
@@ -431,21 +422,23 @@ def client_registration_setup(self, request, new_id=True, set_secret=True):
431422

432423
_context.cdb[client_id] = _cinfo
433424

434-
try:
425+
# Not all databases can be sync'ed
426+
if hasattr(_context.cdb, 'sync') and callable(_context.cdb.sync):
435427
_context.cdb.sync()
436-
except AttributeError: # Not all databases can be sync'ed
437-
pass
438428

439-
logger.info("registration_response: %s" % sanitize(response.to_dict()))
429+
msg = "registration_response: {}"
430+
logger.info(msg.format(sanitize(response.to_dict())))
440431

441432
return response
442433

443-
def process_request(self, request=None, new_id=True, set_secret=True, **kwargs):
434+
def process_request(self, request=None, new_id=True,
435+
set_secret=True, **kwargs):
444436
try:
445437
reg_resp = self.client_registration_setup(request, new_id, set_secret)
446438
except Exception as err:
447439
return ResponseMessage(
448-
error="invalid_configuration_request", error_description="%s" % err
440+
error="invalid_configuration_request",
441+
error_description="%s" % err
449442
)
450443

451444
if "error" in reg_resp:
@@ -454,7 +447,7 @@ def process_request(self, request=None, new_id=True, set_secret=True, **kwargs):
454447
_cookie = new_cookie(
455448
self.endpoint_context,
456449
cookie_name="oidc_op_rp",
457-
client_id=reg_resp["client_id"],
450+
client_id=reg_resp["client_id"]
458451
)
459452

460453
return {"response_args": reg_resp, "cookie": _cookie}

0 commit comments

Comments
 (0)