Skip to content

Commit

Permalink
feat: clientA_ssertion validation on token endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
peppelinux committed Mar 7, 2022
1 parent 5e5032e commit f233568
Showing 1 changed file with 34 additions and 2 deletions.
36 changes: 34 additions & 2 deletions spid_cie_oidc/provider/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,18 @@ def get_issuer(self):
entity_type="openid_provider"
).first()

def check_client_assertion(self, client_id: str, client_assertion: str) -> bool:
head = unpad_jwt_head(client_assertion)
payload = unpad_jwt_payload(client_assertion)
if payload['sub'] != client_id:
# TODO Specialize exceptions
raise Exception()

tc = TrustChain.objects.get(sub=client_id, is_active=True)
jwk = self.find_jwk(head, tc.metadata['jwks']['keys'])
verify_jws(client_assertion, jwk)

return True

class AuthzRequestView(OpBase, View):
"""
Expand Down Expand Up @@ -544,12 +556,30 @@ def post(self, request, *args, **kwargs):
self.issuer = self.get_issuer()

self.authz = OidcSession.objects.filter(
auth_code=request.POST["code"], revoked=False
auth_code=request.POST["code"],
revoked=False
).first()

if not self.authz:
return HttpResponseBadRequest()

# check client_assertion and client ownership
try:
self.check_client_assertion(
request.POST['client_id'],
request.POST['client_assertion']
)
except Exception:
# TODO: coverage test
return JsonResponse(
# TODO: error message here
{
'error': "...",
'error_description': "..."

}, status = 403
)

if request.POST.get("grant_type") == 'authorization_code':
return self.grant_auth_code(request)
elif request.POST.get("grant_type") == 'refresh_token':
Expand All @@ -576,7 +606,9 @@ def get(self, request, *args, **kwargs):
return HttpResponseForbidden()

rp_tc = TrustChain.objects.filter(
sub=token.session.client_id, type="openid_relying_party", is_active=True
sub=token.session.client_id,
type="openid_relying_party",
is_active=True
).first()
if not rp_tc:
return HttpResponseForbidden()
Expand Down

0 comments on commit f233568

Please sign in to comment.