Skip to content
This repository was archived by the owner on Jun 12, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 4 additions & 0 deletions src/oidcendpoint/exception.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ class UnAuthorizedClient(OidcEndpointError):
pass


class UnAuthorizedClientScope(OidcEndpointError):
pass


class InvalidCookieSign(Exception):
pass

Expand Down
21 changes: 21 additions & 0 deletions src/oidcendpoint/oauth2/authorization.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
from oidcendpoint.exception import ServiceError
from oidcendpoint.exception import TamperAllert
from oidcendpoint.exception import ToOld
from oidcendpoint.exception import UnAuthorizedClientScope
from oidcendpoint.exception import UnknownClient
from oidcendpoint.session import setup_session
from oidcendpoint.user_authn.authn_context import pick_auth
Expand Down Expand Up @@ -59,6 +60,21 @@ def re_authenticate(request, authn):
return False


def check_unknown_scopes_policy(request_info, cinfo, endpoint_context):
op_capabilities = endpoint_context.conf['capabilities']
client_allowed_scopes = cinfo.get('allowed_scopes') or \
op_capabilities['scopes_supported']

# this prevents that authz would be released for unavailable scopes
for scope in request_info['scope']:
if op_capabilities.get('deny_unknown_scopes') and \
scope not in client_allowed_scopes:
_msg = '{} requested an unauthorized scope ({})'
logger.warning(_msg.format(cinfo['client_id'],
scope))
raise UnAuthorizedClientScope()


class Authorization(Endpoint):
request_cls = oauth2.AuthorizationRequest
response_cls = oauth2.AuthorizationResponse
Expand Down Expand Up @@ -588,6 +604,11 @@ def process_request(self, request_info=None, **kwargs):
_cid = request_info["client_id"]
cinfo = self.endpoint_context.cdb[_cid]

logger.debug("client {}: {}".format(_cid, cinfo))

# this apply the default optionally deny_unknown_scopes policy
check_unknown_scopes_policy(request_info, cinfo, self.endpoint_context)

cookie = kwargs.get("cookie", "")
if cookie:
del kwargs["cookie"]
Expand Down
4 changes: 4 additions & 0 deletions src/oidcendpoint/oidc/authorization.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
from oidcendpoint.exception import TamperAllert
from oidcendpoint.exception import ToOld
from oidcendpoint.exception import UnknownClient
from oidcendpoint.oauth2.authorization import check_unknown_scopes_policy
from oidcendpoint.session import setup_session
from oidcendpoint.user_authn.authn_context import pick_auth

Expand Down Expand Up @@ -680,6 +681,9 @@ def process_request(self, request_info=None, **kwargs):
cinfo = self.endpoint_context.cdb[_cid]
logger.debug("client {}: {}".format(_cid, cinfo))

# this apply the default optionally deny_unknown_scopes policy
check_unknown_scopes_policy(request_info, cinfo, self.endpoint_context)

cookie = kwargs.get("cookie", "")
if cookie:
del kwargs["cookie"]
Expand Down
33 changes: 33 additions & 0 deletions tests/test_24_oauth2_authorization_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from oidcendpoint.exception import RedirectURIError
from oidcendpoint.exception import ToOld
from oidcendpoint.exception import UnknownClient
from oidcendpoint.exception import UnAuthorizedClientScope
from oidcendpoint.exception import UnAuthorizedClient
from oidcendpoint.id_token import IDToken
from oidcendpoint.oauth2.authorization import Authorization
Expand Down Expand Up @@ -464,6 +465,38 @@ def test_setup_auth_error(self):

item["method"].file = ""

def test_setup_auth_invalid_scope(self):
request = AuthorizationRequest(
client_id="client_id",
redirect_uri="https://rp.example.com/cb",
response_type=["id_token"],
state="state",
nonce="nonce",
scope="openid THAT-BLOODY_SCOPE",
)
redirect_uri = request["redirect_uri"]
cinfo = {
"client_id": "client_id",
"redirect_uris": [("https://rp.example.com/cb", {})],
"id_token_signed_response_alg": "RS256",
}

_ec = self.endpoint.endpoint_context
_ec.cdb["client_id"] = cinfo

kaka = self.endpoint.endpoint_context.cookie_dealer.create_cookie(
"value", "sso")

# force to 400 Http Error message if the release scope policy is heavy!
self.endpoint.endpoint_context.conf['capabilities']['deny_unknown_scopes'] = True
excp = None
try:
res = self.endpoint.process_request(request)
except UnAuthorizedClientScope as e:
excp = e
assert excp
assert isinstance(excp, UnAuthorizedClientScope)

def test_setup_auth_user(self):
request = AuthorizationRequest(
client_id="client_id",
Expand Down