Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[All] Add enable_pkce config, True by default #765

Merged
merged 5 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions oauthenticator/oauth2.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def get(self):

state = {"state_id": state_id, "next_url": next_url}

if self.authenticator.pkce:
if self.authenticator.require_pkce:
# https://datatracker.ietf.org/doc/html/rfc7636#section-4
code_verifier = secrets.token_urlsafe(43)
renan-r-santos marked this conversation as resolved.
Show resolved Hide resolved
code_challenge = hashlib.sha256(code_verifier.encode("utf-8")).digest()
Expand Down Expand Up @@ -680,13 +680,13 @@ def _allowed_scopes_validation(self, proposal):
""",
)

pkce = Bool(
require_pkce = Bool(
False,
config=True,
help="""
Whether to use PKCE (Proof Key for Code Exchange) for the OAuth2 flow.

Only the S256 method is supported.
Require Proof Key for Code Exchange (PKCE) for OAuth2 authorization code flow
consideRatio marked this conversation as resolved.
Show resolved Hide resolved
Only the S256 code challenge method is supported.
`RFC 7636 <https://datatracker.ietf.org/doc/html/rfc7636>`.
""",
)
Expand Down Expand Up @@ -1008,7 +1008,7 @@ def build_access_tokens_request_params(self, handler, data=None):
"data": data,
}

if self.pkce:
if self.require_pkce:
# https://datatracker.ietf.org/doc/html/rfc7636#section-4.5
cookie_state = handler.get_state_cookie()
if not cookie_state:
Expand Down
48 changes: 2 additions & 46 deletions oauthenticator/tests/test_oauth2.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,11 +210,8 @@ async def test_add_user_override(


async def test_login_handler_pkce():
authenticator = OAuthenticator(pkce=True)
authenticator = OAuthenticator(require_pkce=True)
login_handler = mock_handler(OAuthLoginHandler, authenticator=authenticator)

login_handler._generate_state_id = Mock(return_value=TEST_STATE_ID)
login_handler.set_state_cookie = Mock()
login_handler.authorize_redirect = Mock()

login_handler.get() # no await, since authorize_redirect is mocked
Expand Down Expand Up @@ -244,7 +241,7 @@ async def test_callback_handler_pkce():
}
)

authenticator = OAuthenticator(pkce=True)
authenticator = OAuthenticator(require_pkce=True)
callback_handler = mock_handler(
OAuthCallbackHandler,
uri=callback_request_uri,
Expand All @@ -261,44 +258,3 @@ async def test_callback_handler_pkce():
params = authenticator.build_access_tokens_request_params(callback_handler)

assert params['code_verifier'] == TEST_CODE_VERIFIER


async def test_pkce_not_supported(monkeypatch):
url_state = _serialize_state({'state_id': TEST_STATE_ID})
callback_request_uri = f"http://myhost/callback?code=123&state={url_state}"

cookie_state = _serialize_state(
{
'state_id': TEST_STATE_ID,
'next_url': TEST_NEXT_URL,
'code_verifier': TEST_CODE_VERIFIER,
}
)

authenticator = OAuthenticator(pkce=True)
callback_handler = mock_handler(
OAuthCallbackHandler,
uri=callback_request_uri,
authenticator=authenticator,
)

# Mock the statsd setting
monkeypatch.setitem(callback_handler.settings, 'statsd', Mock())
callback_handler.get_secure_cookie = Mock(return_value=cookie_state.encode('utf8'))

# Mock the token request to return an error indicating PKCE is not supported
error_response = {
"error": "invalid_request",
"error_description": "PKCE not supported",
}

async def mock_httpfetch(*args, **kwargs):
return error_response

authenticator.httpfetch = mock_httpfetch

with raises(HTTPError) as exc_info:
await callback_handler.get()

assert exc_info.value.status_code == 403
assert "An access token was not returned: PKCE not supported" in str(exc_info.value)