Skip to content

Commit 03a8c3b

Browse files
djaymamico
andauthored
Changes to make compatible with OAM (#64)
* set client.allow["issuer_mismatch"] = True to make more compatible * add in changes to make work with OAM * fix ClientSettings import * fix missing init * fix dep * try test with domain set * fix setting key source * fix prepare_url * fix prepare_url * add property to interface to appear in control panel * fix control panel adapter for classic * fix label * change how jwik_uri is changed * fix how domain is set * fix formatting * tidy up code * fix flake8 --------- Co-authored-by: Mauro Amico <[email protected]>
1 parent 3169875 commit 03a8c3b

File tree

6 files changed

+62
-1
lines changed

6 files changed

+62
-1
lines changed

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
"plone.protect",
6262
"plone.restapi>=8.34.0",
6363
"oic",
64+
"requests",
6465
"z3c.form",
6566
],
6667
extras_require={

src/pas/plugins/oidc/controlpanel/classic.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,14 @@ def user_property_as_userid(self):
138138
def user_property_as_userid(self, value):
139139
self.settings.user_property_as_userid = value
140140

141+
@property
142+
def identity_domain_name(self):
143+
return self.settings.identity_domain_name
144+
145+
@identity_domain_name.setter
146+
def identity_domain_name(self, value):
147+
self.settings.identity_domain_name = value
148+
141149

142150
class OIDCSettingsForm(controlpanel.RegistryEditForm):
143151
schema = IOIDCSettings

src/pas/plugins/oidc/interfaces.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,13 @@ class IOIDCSettings(Interface):
119119
default="sub",
120120
)
121121

122+
identity_domain_name = schema.TextLine(
123+
title=_("Identity Domain Name"),
124+
description=_("Required for Oracle Authentication Manager only"),
125+
required=False,
126+
default="",
127+
)
128+
122129

123130
class IOIDCControlpanel(IControlpanel):
124131
"""OIDC Control panel"""

src/pas/plugins/oidc/plugins.py

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import itertools
2929
import plone.api as api
30+
import requests
3031
import string
3132

3233

@@ -69,6 +70,29 @@ class IOIDCPlugin(Interface):
6970
""" """
7071

7172

73+
class OAMClient(Client):
74+
"""Override so we can adjust the jwks_uri to add domain needed for OAM"""
75+
76+
def __init__(self, *args, domain=None, **xargs):
77+
super().__init__(self, *args, **xargs)
78+
self.domain = domain
79+
if domain:
80+
session = requests.Session()
81+
session.headers.update({"x-oauth-identity-domain-name": domain})
82+
self.settings.requests_session = session
83+
84+
def handle_provider_config(self, pcr, issuer, keys=True, endpoints=True):
85+
domain = self.domain
86+
if domain:
87+
# we need to modify jwks_uri in the provider_info to add the identityDomain for OAM
88+
# gets used in https://github.com/CZ-NIC/pyoidc/blob/0bd1eadcefc5ccb7ef6c69d9b631537a7d3cfe30/src/oic/oauth2/__init__.py#L1132
89+
url = pcr["jwks_uri"]
90+
req = requests.PreparedRequest()
91+
req.prepare_url(url, dict(identityDomainName=domain))
92+
pcr["jwks_uri"] = req.url
93+
return super().handle_provider_config(pcr, issuer, keys, endpoints)
94+
95+
7296
@implementer(IOIDCPlugin)
7397
class OIDCPlugin(BasePlugin):
7498
"""PAS Plugin OpenID Connect."""
@@ -92,6 +116,7 @@ class OIDCPlugin(BasePlugin):
92116
use_deprecated_redirect_uri_for_logout = False
93117
use_modified_openid_schema = False
94118
user_property_as_userid = "sub"
119+
identity_domain_name = ""
95120

96121
_properties = (
97122
dict(id="title", type="string", mode="w", label="Title"),
@@ -160,6 +185,12 @@ class OIDCPlugin(BasePlugin):
160185
mode="w",
161186
label="User info property used as userid, default 'sub'",
162187
),
188+
dict(
189+
id="identity_domain_name",
190+
type="string",
191+
mode="w",
192+
label="Identity Domain Name (required for Oracle Authentication Manager only)",
193+
),
163194
)
164195

165196
def __init__(self, id, title=None):
@@ -331,8 +362,19 @@ def _setupJWTTicket(self, user_id, user):
331362

332363
# TODO: memoize (?)
333364
def get_oauth2_client(self):
365+
domain = self.getProperty("identity_domain_name")
334366
try:
335-
client = Client(client_authn_method=CLIENT_AUTHN_METHOD)
367+
if domain:
368+
client = OAMClient(
369+
client_authn_method=CLIENT_AUTHN_METHOD,
370+
domain=domain,
371+
)
372+
else:
373+
client = Client(client_authn_method=CLIENT_AUTHN_METHOD)
374+
client.allow["issuer_mismatch"] = (
375+
True # Some providers aren't configured with configured and issuer urls the same even though they should.
376+
)
377+
336378
# registration_response = client.register(provider_info["registration_endpoint"], redirect_uris=...)
337379
# ... oic.exception.RegistrationError: {'error': 'insufficient_scope',
338380
# 'error_description': "Policy 'Trusted Hosts' rejected request to client-registration service. Details: Host not trusted."}

src/pas/plugins/oidc/utils.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ def authorization_flow_args(plugin: plugins.OIDCPlugin, session: Session) -> dic
127127
"nonce": session.get("nonce"),
128128
"redirect_uri": plugin.get_redirect_uris(),
129129
}
130+
if plugin.getProperty("identity_domain_name"):
131+
args["domain"] = plugin.getProperty("identity_domain_name", "")
130132
if plugin.getProperty("use_pkce"):
131133
# Build a random string of 43 to 128 characters
132134
# and send it in the request as a base64-encoded urlsafe string of the sha256 hash of that string

tests/services/conftest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ def keycloak(keycloak_service):
2323
"scope": ("openid", "profile", "email"),
2424
"redirect_uris": ("/login_oidc/oidc",),
2525
"create_restapi_ticket": True,
26+
"identity_domain_name": "blah", # ensure non OAM SP ignores extra params/header
2627
}
2728

2829

0 commit comments

Comments
 (0)