Skip to content

Commit 4bfcf71

Browse files
committed
Implement OIDC groups mapping
1 parent a008a00 commit 4bfcf71

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

dojo/pipeline.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from django.conf import settings
88
from social_core.backends.azuread_tenant import AzureADTenantOAuth2
99
from social_core.backends.google import GoogleOAuth2
10+
from social_core.backends.open_id_connect import OpenIdConnectAuth
1011

1112
from dojo.authorization.roles_permissions import Permissions, Roles
1213
from dojo.models import Dojo_Group, Dojo_Group_Member, Product, Product_Member, Product_Type, Role
@@ -106,6 +107,31 @@ def update_azure_groups(backend, uid, user=None, social=None, *args, **kwargs):
106107
cleanup_old_groups_for_user(user, group_names)
107108

108109

110+
def update_oidc_groups(backend, uid, user=None, social=None, *args, **kwargs):
111+
if settings.OIDC_AUTH_ENABLED and settings.DD_SOCIAL_AUTH_OIDC_GET_GROUPS and isinstance(backend, OpenIdConnectAuth):
112+
response = kwargs.get("response", {})
113+
group_names = response.get("groups", [])
114+
115+
if not group_names:
116+
logger.warning("No 'groups' claim found in Dex OIDC response. Skipping group assignment.")
117+
return
118+
logger.debug(f"Dex OIDC groups received: {group_names}")
119+
filtered_group_names = []
120+
group_filter = getattr(settings, "OIDC_GROUPS_FILTER", None)
121+
for group_name in group_names:
122+
try:
123+
if group_filter and not re.search(group_filter, group_name):
124+
logger.debug(f"Skipping group '{group_name}' due to OIDC_GROUPS_FILTER: {group_filter}")
125+
continue
126+
filtered_group_names.append(group_name)
127+
except Exception as e:
128+
logger.error(f"Error processing group '{group_name}': {e}")
129+
if filtered_group_names:
130+
assign_user_to_groups(user, filtered_group_names, Dojo_Group.OIDC)
131+
if getattr(settings, "OIDC_CLEANUP_GROUPS", False):
132+
cleanup_old_groups_for_user(user, filtered_group_names)
133+
134+
109135
def is_group_id(group):
110136
return bool(re.search(r"^[a-zA-Z0-9]{8,}-[a-zA-Z0-9]{4,}-[a-zA-Z0-9]{4,}-[a-zA-Z0-9]{4,}-[a-zA-Z0-9]{12,}$", group))
111137

dojo/settings/settings.dist.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,9 @@
116116
DD_SOCIAL_LOGIN_AUTO_REDIRECT=(bool, False), # auto-redirect if there is only one social login method
117117
DD_SOCIAL_AUTH_TRAILING_SLASH=(bool, True),
118118
DD_SOCIAL_AUTH_OIDC_AUTH_ENABLED=(bool, False),
119+
DD_SOCIAL_AUTH_OIDC_GET_GROUPS=(bool, False),
120+
DD_SOCIAL_AUTH_OIDC_GROUPS_FILTER=(str, ""),
121+
DD_SOCIAL_AUTH_OIDC_CLEANUP_GROUPS=(bool, True),
119122
DD_SOCIAL_AUTH_OIDC_OIDC_ENDPOINT=(str, ""),
120123
DD_SOCIAL_AUTH_OIDC_ID_KEY=(str, ""),
121124
DD_SOCIAL_AUTH_OIDC_KEY=(str, ""),
@@ -562,6 +565,7 @@ def generate_url(scheme, double_slashes, user, password, host, port, path, param
562565
"social_core.pipeline.social_auth.load_extra_data",
563566
"social_core.pipeline.user.user_details",
564567
"dojo.pipeline.update_azure_groups",
568+
"dojo.pipeline.update_oidc_groups",
565569
"dojo.pipeline.update_product_access",
566570
)
567571

@@ -618,6 +622,9 @@ def generate_url(scheme, double_slashes, user, password, host, port, path, param
618622

619623
# Mandatory settings
620624
OIDC_AUTH_ENABLED = env("DD_SOCIAL_AUTH_OIDC_AUTH_ENABLED")
625+
OIDC_GET_GROUPS = env("DD_SOCIAL_AUTH_OIDC_GET_GROUPS")
626+
OIDC_GROUPS_FILTER = env("DD_SOCIAL_AUTH_OIDC_GROUPS_FILTER")
627+
OIDC_CLEANUP_GROUPS = env("DD_SOCIAL_AUTH_OIDC_CLEANUP_GROUPS")
621628
SOCIAL_AUTH_OIDC_OIDC_ENDPOINT = env("DD_SOCIAL_AUTH_OIDC_OIDC_ENDPOINT")
622629
SOCIAL_AUTH_OIDC_KEY = env("DD_SOCIAL_AUTH_OIDC_KEY")
623630
SOCIAL_AUTH_OIDC_SECRET = env("DD_SOCIAL_AUTH_OIDC_SECRET")

0 commit comments

Comments
 (0)