Skip to content

fix: Fix JIT userGroups provisioning #69

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

Merged
merged 1 commit into from
Apr 16, 2025
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
Expand Up @@ -73,19 +73,23 @@ class JitProvisioningAuthenticationSuccessHandler(
val emailClaim = authenticationToken.getClaim(EMAIL)

val userGroupsClaimName = jitSetting.userGroupsClaimName ?: GD_USER_GROUPS
val userGroupsClaim = authenticationToken.getClaimList(userGroupsClaimName) ?: jitSetting.userGroupsDefaults
val userGroups =
if (jitSetting.userGroupsScopeEnabled) {
authenticationToken.getClaimList(userGroupsClaimName) ?: jitSetting.userGroupsDefaults
} else {
jitSetting.userGroupsDefaults
}
val shouldApplyUserGroups = (jitSetting.userGroupsScopeEnabled || jitSetting.userGroupsDefaults != null)

return client.getUserByAuthenticationId(organization.id, subClaim)
.flatMap { user ->
logMessage("Checking for user update", "running", organization.id)
if (userDetailsChanged(user, firstnameClaim, lastnameClaim, emailClaim, userGroupsClaim)) {
if (userDetailsChanged(user, firstnameClaim, lastnameClaim, emailClaim, userGroups)) {
logMessage("User details changed, patching", "running", organization.id)
user.firstname = firstnameClaim
user.lastname = lastnameClaim
user.email = emailClaim
if (jitSetting.userGroupsScopeEnabled && userGroupsClaim != null) {
user.userGroups = userGroupsClaim
}
if (shouldApplyUserGroups) user.userGroups = userGroups
client.patchUser(organization.id, user)
} else {
logMessage("User not changed, skipping update", "finished", organization.id)
Expand All @@ -99,7 +103,7 @@ class JitProvisioningAuthenticationSuccessHandler(
firstnameClaim,
lastnameClaim,
emailClaim,
userGroupsClaim ?: emptyList()
if (shouldApplyUserGroups) userGroups ?: emptyList() else emptyList()
).doOnSuccess { provisionedUser ->
logMessage("User ${provisionedUser.id} created in organization", "finished", organization.id)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import strikt.api.expectThrows
import strikt.assertions.isEqualTo
import strikt.assertions.isNull

@SuppressWarnings("LongParameterList")
class JitProvisioningAuthenticationSuccessHandlerTest {

private val client: AuthenticationStoreClient = mockk()
Expand Down Expand Up @@ -102,7 +103,8 @@ class JitProvisioningAuthenticationSuccessHandlerTest {
@MethodSource("jitOptions")
fun `should perform JIT provisioning when JIT enabled and user does not exist`(
case: String,
userGroupsClaimName: String,
userGroupsScopeEnabled: Boolean,
userGroupsClaimName: String?,
userGroupsInAuthToken: List<String>?,
userGroupsInOrgSetting: List<String>?,
expectedUserGroups: List<String>
Expand All @@ -116,7 +118,7 @@ class JitProvisioningAuthenticationSuccessHandlerTest {
EMAIL to EMAIL,
GIVEN_NAME to GIVEN_NAME,
FAMILY_NAME to FAMILY_NAME,
userGroupsClaimName to userGroupsInAuthToken
userGroupsClaimName!! to userGroupsInAuthToken
)
} else {
mapOf(
Expand All @@ -139,6 +141,7 @@ class JitProvisioningAuthenticationSuccessHandlerTest {
Mono.just(
JitProvisioningSetting(
enabled = true,
userGroupsScopeEnabled = userGroupsScopeEnabled,
userGroupsClaimName = userGroupsClaimName,
userGroupsDefaults = userGroupsInOrgSetting
)
Expand All @@ -164,6 +167,7 @@ class JitProvisioningAuthenticationSuccessHandlerTest {
fun `should test user patching`(
case: String,
user: User,
userGroupsScopeEnabled: Boolean,
userGroupsInAuthToken: List<String>?,
patchCount: Int
) {
Expand Down Expand Up @@ -201,7 +205,8 @@ class JitProvisioningAuthenticationSuccessHandlerTest {
Mono.just(
JitProvisioningSetting(
enabled = true,
userGroupsScopeEnabled = true
userGroupsScopeEnabled = userGroupsScopeEnabled,
userGroupsDefaults = listOf("defaultUserGroup")
)
)
mockUserByAuthId(client, ORG_ID, SUB, user)
Expand All @@ -219,7 +224,7 @@ class JitProvisioningAuthenticationSuccessHandlerTest {
coVerify(exactly = patchCount) { client.patchUser(ORG_ID, any()) }

if (patchCount != 0) {
if (userGroupsInAuthToken != null) {
if (userGroupsScopeEnabled && userGroupsInAuthToken != null) {
expectThat(userSlot.captured.userGroups).isEqualTo(userGroupsInAuthToken)
} else {
expectThat(userSlot.captured.userGroups).isEqualTo(usersCurrentUserGroups)
Expand All @@ -238,31 +243,59 @@ class JitProvisioningAuthenticationSuccessHandlerTest {
fun jitOptions() = Stream.of(
Arguments.of(
"without user groups",
true,
GD_USER_GROUPS,
emptyList<String>(),
emptyList<String>(),
emptyList<String>(),
),
Arguments.of(
"with default user groups",
true,
GD_USER_GROUPS,
null,
listOf("defaultUserGroup"),
listOf("defaultUserGroup"),
),
Arguments.of(
"with default user groups and user groups in token",
true,
GD_USER_GROUPS,
listOf("adminUserGroup", "secondUserGroup"),
listOf("defaultUserGroup"),
listOf("adminUserGroup", "secondUserGroup"),
),
Arguments.of(
"with user groups in token with custom claim name",
true,
"custom_user_groups_claim_name",
listOf("adminUserGroup", "secondUserGroup"),
null,
listOf("adminUserGroup", "secondUserGroup"),
),
Arguments.of(
"with default user groups and userGroups scope disabled",
false,
null,
null,
listOf("adminUserGroup", "secondUserGroup"),
listOf("adminUserGroup", "secondUserGroup"),
),
Arguments.of(
"with no default user groups, userGroupsScope disabled, but with user groups claims present in token",
false,
GD_USER_GROUPS,
listOf("adminUserGroup", "secondUserGroup"),
null,
emptyList<String>(),
),
Arguments.of(
"with default user groups, userGroupsScope disabled, but with user groups claims present in token",
false,
GD_USER_GROUPS,
listOf("adminUserGroup", "secondUserGroup"),
listOf("secondUserGroup"),
listOf("secondUserGroup"),
)
)

Expand All @@ -278,6 +311,7 @@ class JitProvisioningAuthenticationSuccessHandlerTest {
email = EMAIL,
userGroups = listOf("defaultUserGroup")
),
true,
null,
1
),
Expand All @@ -291,6 +325,7 @@ class JitProvisioningAuthenticationSuccessHandlerTest {
email = EMAIL,
userGroups = listOf("defaultUserGroup")
),
true,
emptyList<String>(),
1
),
Expand All @@ -304,6 +339,7 @@ class JitProvisioningAuthenticationSuccessHandlerTest {
email = EMAIL,
userGroups = listOf("defaultUserGroup")
),
true,
listOf("NewUserGroup"),
1
),
Expand All @@ -317,8 +353,23 @@ class JitProvisioningAuthenticationSuccessHandlerTest {
email = EMAIL,
userGroups = listOf("defaultUserGroup")
),
true,
listOf("defaultUserGroup"),
0
),
Arguments.of(
"should not update user when user details are not changed",
User(
USER_ID,
null,
firstname = GIVEN_NAME,
lastname = FAMILY_NAME,
email = EMAIL,
userGroups = listOf("defaultUserGroup")
),
false,
listOf("NewUserGroup"),
0
)
)
}
Expand Down