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

Commit 9c0f7db

Browse files
committed
Add pkce per client
1 parent 10058ed commit 9c0f7db

2 files changed

Lines changed: 45 additions & 7 deletions

File tree

src/oidcop/oidc/add_on/pkce.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@
33
from typing import Dict
44

55
from cryptojwt.utils import b64e
6-
from oidcmsg.oauth2 import (
7-
AuthorizationErrorResponse,
8-
RefreshAccessTokenRequest,
9-
TokenExchangeRequest,
10-
)
6+
from oidcmsg.oauth2 import AuthorizationErrorResponse
7+
from oidcmsg.oauth2 import RefreshAccessTokenRequest
8+
from oidcmsg.oauth2 import TokenExchangeRequest
119
from oidcmsg.oidc import TokenErrorResponse
1210

1311
from oidcop.endpoint import Endpoint
@@ -41,7 +39,12 @@ def post_authn_parse(request, client_id, endpoint_context, **kwargs):
4139
:param kwargs:
4240
:return:
4341
"""
44-
if endpoint_context.args["pkce"]["essential"] and "code_challenge" not in request:
42+
client = endpoint_context.cdb[client_id]
43+
if "pkce_essential" in client:
44+
essential = client["pkce_essential"]
45+
else:
46+
essential = endpoint_context.args["pkce"]["essential"]
47+
if essential and "code_challenge" not in request:
4548
return AuthorizationErrorResponse(
4649
error="invalid_request", error_description="Missing required code_challenge",
4750
)

tests/test_33_oauth2_pkce.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import secrets
55
import string
66

7-
from oidcop.configure import ASConfiguration
87
import pytest
98
import yaml
109
from oidcmsg.message import Message
@@ -15,6 +14,7 @@
1514
from oidcmsg.oidc import TokenErrorResponse
1615

1716
import oidcop.oauth2.introspection
17+
from oidcop.configure import ASConfiguration
1818
from oidcop.configure import OPConfiguration
1919
from oidcop.cookie_handler import CookieHandler
2020
from oidcop.endpoint import Endpoint
@@ -285,6 +285,41 @@ def test_not_essential(self, conf):
285285

286286
assert isinstance(_req, Message)
287287

288+
def test_essential_per_client(self, conf):
289+
conf["add_on"]["pkce"]["kwargs"]["essential"] = False
290+
server = create_server(conf)
291+
authn_endpoint = server.server_get("endpoint", "authorization")
292+
token_endpoint = server.server_get("endpoint", "token")
293+
_authn_req = AUTH_REQ.copy()
294+
endpoint_context = server.server_get("endpoint_context")
295+
endpoint_context.cdb[AUTH_REQ["client_id"]]["pkce_essential"] = True
296+
297+
_pr_resp = authn_endpoint.parse_request(_authn_req.to_dict())
298+
299+
assert isinstance(_pr_resp, AuthorizationErrorResponse)
300+
assert _pr_resp["error"] == "invalid_request"
301+
assert _pr_resp["error_description"] == "Missing required code_challenge"
302+
303+
def test_not_essential_per_client(self, conf):
304+
conf["add_on"]["pkce"]["kwargs"]["essential"] = True
305+
server = create_server(conf)
306+
authn_endpoint = server.server_get("endpoint", "authorization")
307+
token_endpoint = server.server_get("endpoint", "token")
308+
_authn_req = AUTH_REQ.copy()
309+
endpoint_context = server.server_get("endpoint_context")
310+
endpoint_context.cdb[AUTH_REQ["client_id"]]["pkce_essential"] = False
311+
312+
_pr_resp = authn_endpoint.parse_request(_authn_req.to_dict())
313+
resp = authn_endpoint.process_request(_pr_resp)
314+
315+
assert isinstance(resp["response_args"], AuthorizationResponse)
316+
317+
_token_request = TOKEN_REQ.copy()
318+
_token_request["code"] = resp["response_args"]["code"]
319+
_req = token_endpoint.parse_request(_token_request)
320+
321+
assert isinstance(_req, Message)
322+
288323
def test_unknown_code_challenge_method(self):
289324
_authn_req = AUTH_REQ.copy()
290325
_authn_req["code_challenge"] = "aba"

0 commit comments

Comments
 (0)