Skip to content

Commit

Permalink
User: Adds scope setting to OIDC authentication provider
Browse files Browse the repository at this point in the history
TYPE: Feature
LINK: OGC-1767
  • Loading branch information
Daverball committed Dec 19, 2024
1 parent 5859fb2 commit dc2ebc8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/onegov/user/auth/clients/oidc.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ class OIDCClient:

primary: bool = attrib()

# Required OAuth scope in addition to "openid"
scope: list[str] = attrib(factory=list)

# Override/amend discovered metadata
fixed_metadata: dict[str, Any] = attrib(factory=dict)

Expand All @@ -84,12 +87,13 @@ def session(
request: 'CoreRequest'
) -> OAuth2Session:
""" Returns a requests session tied to a OAuth2 client """
assert isinstance(self.scope, list), 'Invalid scope, expected list'
provider_cls = type(provider)
redirect_url = request.class_link(
provider_cls, {'name': provider.name}, name='redirect')
return OAuth2Session(
self.client_id,
scope=['openid'],
scope=['openid', *self.scope],
redirect_uri=redirect_url,
)

Expand Down Expand Up @@ -206,6 +210,7 @@ def from_cfg(cls, config: dict[str, Any]) -> Self:
issuer=cfg['issuer'],
client_id=cfg['client_id'],
client_secret=cfg['client_secret'],
scope=cfg.get('scope', []),
attributes=OIDCAttributes.from_cfg(
cfg.get('attributes', {})
),
Expand Down
7 changes: 6 additions & 1 deletion tests/onegov/user/test_oauth_oidc.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ def configure_provider(app, metadata=None, primary=False):
issuer: https://oidc.test/
client_id: test
client_secret: secret
scope:
- profile
- email
button_text: Login with OIDC
fixed_metadata: {json.dumps(metadata or {})}
roles:
Expand All @@ -72,6 +75,7 @@ def test_oidc_configuration(app):
assert client.attributes.last_name == 'family_name'
assert client.attributes.preferred_username == 'preferred_username'
assert client.primary is False
assert client.scope == ['profile', 'email']

assert provider.roles.app_specific(app) == {
'admins': 'ads', 'editors': 'eds', 'members': 'mems'
Expand All @@ -93,6 +97,7 @@ def test_oidc_configuration_primary(app):
assert client.attributes.last_name == 'family_name'
assert client.attributes.preferred_username == 'preferred_username'
assert client.primary is True
assert client.scope == ['profile', 'email']

assert provider.roles.app_specific(app) == {
'admins': 'ads', 'editors': 'eds', 'members': 'mems'
Expand Down Expand Up @@ -181,7 +186,7 @@ def test_oicd_authenticate_request(app):
location = response.headers['Location']
assert location.startswith('https://oidc.test/authorize')
assert 'state=oauth_state' in location
assert 'scope=openid' in location
assert 'scope=openid+profile+email' in location
assert browser_session['login_to'] == '/'


Expand Down

0 comments on commit dc2ebc8

Please sign in to comment.