Skip to content

Commit 1c642b8

Browse files
committed
remove custom implementation of code challenge computation, reuse oic function instead. Remove nacl dependency
1 parent 173d3af commit 1c642b8

File tree

3 files changed

+5
-40
lines changed

3 files changed

+5
-40
lines changed

setup.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
description='OpenID Connect Provider (OP) library in Python.',
1313
install_requires=[
1414
'oic >= 1.2.1',
15-
'pynacl',
1615
'pymongo'
1716
]
1817
)

src/pyop/provider.py

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
from urllib.parse import parse_qsl
77
from urllib.parse import urlparse
88

9-
import nacl.hash
10-
from nacl.encoding import URLSafeBase64Encoder
119
from jwkest import jws
1210
from oic import rndstr
1311
from oic.exception import MessageException
@@ -23,6 +21,7 @@
2321
from oic.oic.message import RefreshAccessTokenRequest
2422
from oic.oic.message import RegistrationRequest
2523
from oic.oic.message import RegistrationResponse
24+
from oic.extension.provider import Provider as OICProviderExtensions
2625

2726
from .message import AuthorizationRequest
2827
from .message import AccessTokenRequest
@@ -330,23 +329,6 @@ def handle_token_request(self, request_body, # type: str
330329
raise InvalidTokenRequest('grant_type \'{}\' unknown'.format(token_request['grant_type']), token_request,
331330
oauth_error='unsupported_grant_type')
332331

333-
def _compute_code_challenge(self,
334-
code_verifier # type: str
335-
):
336-
# type: (...) -> str
337-
"""
338-
Given a code verifier compute the code_challenge. This code_challenge is computed as defined (https://datatracker.ietf.org/doc/html/rfc7636#section-4.2):
339-
340-
code_challenge = BASE64URL-ENCODE(SHA256(ASCII(code_verifier))).
341-
342-
This shows that the SHA256 of the ascii encoded code_verifier is URLSafe base64 encoded. We have adjusted the encoding to the ISO_8859_1 encoding,
343-
conform to the AppAuth SDK for Android and IOS. Moreover, we remove the base64 padding (=).
344-
345-
:param code_verifier: the code verifier to transform to the Code Challenge
346-
"""
347-
verifier_hash = nacl.hash.sha256(code_verifier.encode('ISO_8859_1'), encoder=URLSafeBase64Encoder)
348-
return verifier_hash.decode().replace('=', '')
349-
350332
def _PKCE_verify(self,
351333
token_request, # type: AccessTokenRequest
352334
authentication_request # type: AuthorizationRequest
@@ -368,12 +350,10 @@ def _PKCE_verify(self,
368350
raise InvalidTokenRequest("A code_challenge and code_verifier have been supplied"
369351
"but missing code_challenge_method in authentication_request", token_request)
370352

371-
code_challenge_method = authentication_request['code_challenge_method']
372-
if code_challenge_method == 'plain':
373-
return authentication_request['code_challenge'] == token_request['code_verifier']
374-
375-
code_challenge = self._compute_code_challenge(token_request['code_verifier'])
376-
return code_challenge == authentication_request['code_challenge']
353+
# OIC Provider extension returns either a boolean or Response object containing an error. To support
354+
# stricter typing guidelines, return if True. Error handling support should be in encapsulating function.
355+
return OICProviderExtensions.verify_code_challenge(token_request['code_verifier'],
356+
authentication_request['code_challenge'], authentication_request['code_challenge_method']) == True
377357

378358
def _verify_code_exchange_req(self,
379359
token_request, # type: AccessTokenRequest

tests/pyop/test_provider.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -333,20 +333,6 @@ def test_pkce_code_exchange_request(self):
333333
assert_id_token_base_claims(response['id_token'], self.provider.signing_key, self.provider,
334334
self.authn_request_args)
335335

336-
@patch('time.time', MOCK_TIME)
337-
def test_pkce_code_exchange_request_plaintext(self):
338-
self.authorization_code_exchange_request_args['code'] = self.create_authz_code(
339-
{
340-
"code_challenge": "SoOEDN-mZKNhw7Mc52VXxyiqTvFB3mod36MwPru253c",
341-
"code_challenge_method": "plain"
342-
}
343-
)
344-
self.authorization_code_exchange_request_args['code_verifier'] = "SoOEDN-mZKNhw7Mc52VXxyiqTvFB3mod36MwPru253c"
345-
response = self.provider._do_code_exchange(self.authorization_code_exchange_request_args, None)
346-
assert response['access_token'] in self.provider.authz_state.access_tokens
347-
assert_id_token_base_claims(response['id_token'], self.provider.signing_key, self.provider,
348-
self.authn_request_args)
349-
350336
@patch('time.time', MOCK_TIME)
351337
def test_code_exchange_request_with_claims_requested_in_id_token(self):
352338
claims_req = {'claims': ClaimsRequest(id_token=Claims(email=None))}

0 commit comments

Comments
 (0)