Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

♻️ [#76] Make groups_claim optional #77

Merged
merged 2 commits into from
Jan 11, 2024
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 3.2.23 on 2024-01-09 10:38

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("mozilla_django_oidc_db", "0013_merge_20231221_1529"),
]

operations = [
migrations.AlterField(
model_name="openidconnectconfig",
name="groups_claim",
field=models.CharField(
blank=True,
default="roles",
help_text="The name of the OIDC claim that holds the values to map to local user groups.",
max_length=50,
verbose_name="groups claim",
),
),
]
1 change: 1 addition & 0 deletions mozilla_django_oidc_db/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ class OpenIDConnectConfig(CachingMixin, OpenIDConnectConfigBase):
help_text=_(
"The name of the OIDC claim that holds the values to map to local user groups."
),
blank=True,
)
sync_groups = models.BooleanField(
_("Create local user groups if they do not exist yet"),
Expand Down
42 changes: 42 additions & 0 deletions tests/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,48 @@ def test_backend_create_user_sync_all_groups(mock_get_solo):
]


@pytest.mark.django_db
@patch("mozilla_django_oidc_db.models.OpenIDConnectConfig.get_solo")
def test_backend_create_user_no_groups_sync_without_groups_claim(mock_get_solo):
Group.objects.create(name="group1")
Group.objects.create(name="group2")

oidc_config = OpenIDConnectConfig(
id=1,
enabled=True,
oidc_rp_client_id="testid",
oidc_rp_client_secret="secret",
oidc_rp_sign_algo="HS256",
oidc_rp_scopes_list=["openid", "email"],
oidc_op_jwks_endpoint="http://some.endpoint/v1/jwks",
oidc_op_authorization_endpoint="http://some.endpoint/v1/auth",
oidc_op_token_endpoint="http://some.endpoint/v1/token",
oidc_op_user_endpoint="http://some.endpoint/v1/user",
groups_claim="",
sync_groups=True,
sync_groups_glob_pattern="*",
)
# Explicitly set to none
oidc_config.default_groups.set(Group.objects.none())
mock_get_solo.return_value = oidc_config

claims = {
"sub": "123456",
"roles": ["group1", "newgroup"],
}

backend = OIDCAuthenticationBackend()

user = backend.create_user(claims)

# Verify that no groups were created
assert Group.objects.count() == 2

# Verify that a user is created with the correct values
assert user.username == "123456"
assert list(user.groups.values_list("name", flat=True)) == []


@pytest.mark.django_db
@patch("mozilla_django_oidc_db.models.OpenIDConnectConfig.get_solo")
def test_backend_create_user_sync_groups_according_to_pattern(mock_get_solo):
Expand Down
Loading