Skip to content

Commit bdc94b4

Browse files
authored
Merge pull request #11 from IMIO/error_no_plone
fix: skip OIDC settings configuration when Plone site or OIDC plugin is unavailable
2 parents 606381b + 6f73f1f commit bdc94b4

File tree

2 files changed

+39
-22
lines changed

2 files changed

+39
-22
lines changed

CHANGES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
## 1.2.1 (unreleased)
22

3+
- Skip OIDC settings configuration when Plone site or OIDC plugin is unavailable
4+
[remdub]
5+
36
- Set "came_from" session variable from HTTP_REFERER instead of came_from request.
47
[bsuttor]
58

src/pas/plugins/kimug/utils.py

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -91,28 +91,38 @@ def set_oidc_settings(context):
9191
logger.info("Site found with api.portal.get()")
9292
except api.exc.CannotGetPortalError:
9393
logger.info("Site not found with api.portal.get(), setting it with setSite()")
94-
setSite(context.database.open().root()["Application"]["Plone"])
95-
oidc = get_plugin()
96-
realm = os.environ.get("keycloak_realm", "plone")
97-
client_id = os.environ.get("keycloak_client_id", "plone")
98-
client_secret = os.environ.get("keycloak_client_secret", "12345678910")
99-
issuer = os.environ.get(
100-
"keycloak_issuer", f"http://keycloak.traefik.me/realms/{realm}/"
101-
)
102-
oidc.redirect_uris = get_redirect_uri()
103-
oidc.client_id = client_id
104-
oidc.client_secret = client_secret
105-
oidc.create_groups = True
106-
oidc.issuer = issuer
107-
oidc.scope = ("openid", "profile", "email")
108-
oidc.userinfo_endpoint_method = "GET"
109-
110-
api.portal.set_registry_record("plone.external_login_url", "acl_users/oidc/login")
111-
api.portal.set_registry_record("plone.external_logout_url", "acl_users/oidc/logout")
94+
try:
95+
site = context.database.open().root()["Application"]["Plone"]
96+
except KeyError:
97+
logger.warning("Could not find Plone site, not setting OIDC settings")
98+
return
99+
setSite(site)
100+
if oidc := get_plugin():
101+
realm = os.environ.get("keycloak_realm", "plone")
102+
client_id = os.environ.get("keycloak_client_id", "plone")
103+
client_secret = os.environ.get("keycloak_client_secret", "12345678910")
104+
issuer = os.environ.get(
105+
"keycloak_issuer", f"http://keycloak.traefik.me/realms/{realm}/"
106+
)
107+
oidc.redirect_uris = get_redirect_uri()
108+
oidc.client_id = client_id
109+
oidc.client_secret = client_secret
110+
oidc.create_groups = True
111+
oidc.issuer = issuer
112+
oidc.scope = ("openid", "profile", "email")
113+
oidc.userinfo_endpoint_method = "GET"
114+
115+
api.portal.set_registry_record(
116+
"plone.external_login_url", "acl_users/oidc/login"
117+
)
118+
api.portal.set_registry_record(
119+
"plone.external_logout_url", "acl_users/oidc/logout"
120+
)
112121

113-
transaction.commit()
114-
logger.info("OIDC settings set with set_oidc_settings()")
115-
# return site
122+
transaction.commit()
123+
logger.info("OIDC settings set with set_oidc_settings()")
124+
else:
125+
logger.warning("Could not find OIDC plugin, not setting OIDC settings")
116126

117127

118128
def get_admin_access_token(keycloak_url, username, password):
@@ -188,7 +198,11 @@ def get_client_access_token(
188198
def get_plugin():
189199
"""Get the OIDC plugin."""
190200
pas = api.portal.get_tool("acl_users")
191-
oidc = pas.oidc
201+
try:
202+
oidc = pas.oidc
203+
except AttributeError:
204+
logger.warning("Could not find OIDC plugin with get_plugin().")
205+
return None
192206
return oidc
193207

194208

0 commit comments

Comments
 (0)