2828import java .util .Date ;
2929import java .util .List ;
3030import java .util .Map ;
31+ import java .util .Optional ;
3132
3233import io .jsonwebtoken .Jwts ;
34+ import org .georchestra .gateway .security .GeorchestraGatewaySecurityConfigProperties ;
3335import org .georchestra .security .model .GeorchestraUser ;
3436import org .junit .jupiter .api .BeforeEach ;
3537import org .junit .jupiter .api .Test ;
38+ import org .springframework .security .core .authority .AuthorityUtils ;
39+ import org .springframework .security .oauth2 .client .authentication .OAuth2AuthenticationToken ;
3640import org .springframework .security .oauth2 .client .registration .ClientRegistration ;
3741import org .springframework .security .oauth2 .core .AuthorizationGrantType ;
3842import org .springframework .security .oauth2 .core .oidc .AddressStandardClaim ;
43+ import org .springframework .security .oauth2 .core .oidc .user .OidcUser ;
3944import org .springframework .security .oauth2 .core .oidc .StandardClaimAccessor ;
4045
4146import com .nimbusds .oauth2 .sdk .ParseException ;
@@ -52,6 +57,7 @@ class OpenIdConnectUserMapperTest {
5257
5358 OpenIdConnectUserMapper mapper ;
5459 OpenIdConnectCustomClaimsConfigProperties nonStandardClaimsConfig ;
60+ GeorchestraGatewaySecurityConfigProperties securityConfigProperties ;
5561 ExtendedOAuth2ClientProperties properties ;
5662
5763 /**
@@ -60,7 +66,8 @@ class OpenIdConnectUserMapperTest {
6066 @ BeforeEach
6167 void setUp () throws Exception {
6268 nonStandardClaimsConfig = new OpenIdConnectCustomClaimsConfigProperties ();
63- mapper = new OpenIdConnectUserMapper (nonStandardClaimsConfig );
69+ securityConfigProperties = new GeorchestraGatewaySecurityConfigProperties ();
70+ mapper = new OpenIdConnectUserMapper (nonStandardClaimsConfig , securityConfigProperties );
6471 }
6572
6673 @ Test
@@ -328,6 +335,90 @@ public void customProviderValuesMapper() {
328335 assertThat (georchestraUser .getFirstName ()).isEqualTo ("given_name" );
329336 }
330337
338+ @ Test
339+ void map_shouldTransformUsernameWhenDisableUidTransformationIsEmpty () {
340+ OpenIdConnectUserMapper mapper = newMapper ("" );
341+
342+ OAuth2AuthenticationToken token = mock (OAuth2AuthenticationToken .class );
343+ OidcUser oidcUser = mock (OidcUser .class );
344+
345+ when (token .getPrincipal ()).thenReturn (oidcUser );
346+ when (token .getAuthorizedClientRegistrationId ()).thenReturn ("google" );
347+ when (token .getAuthorities ()).thenReturn (AuthorityUtils .NO_AUTHORITIES );
348+
349+ when (oidcUser .getSubject ()).thenReturn ("b7f3dd13-f9cc-4573-8482-b4fccf8e1977" );
350+ when (oidcUser .getPreferredUsername ()).thenReturn ("John.Doe@test.com" );
351+ when (oidcUser .getGivenName ()).thenReturn ("John" );
352+ when (oidcUser .getFamilyName ()).thenReturn ("Doe" );
353+ when (oidcUser .getEmail ()).thenReturn ("jdoe@test.com" );
354+ when (oidcUser .getPhoneNumber ()).thenReturn ("+123" );
355+ when (oidcUser .getAddress ()).thenReturn (null );
356+ when (oidcUser .getClaims ()).thenReturn (Map .of ());
357+
358+ Optional <GeorchestraUser > result = mapper .map (token );
359+
360+ assertThat (result ).isPresent ();
361+ assertThat (result .orElseThrow ().getUsername ()).isEqualTo ("google_john_doe_test_com" );
362+ }
363+
364+ @ Test
365+ void map_shouldTransformUsernameWhenDisableUidTransformationDoesNotMatchProvider () {
366+ OpenIdConnectUserMapper mapper = newMapper ("github" );
367+
368+ OAuth2AuthenticationToken token = mock (OAuth2AuthenticationToken .class );
369+ OidcUser oidcUser = mock (OidcUser .class );
370+
371+ when (token .getPrincipal ()).thenReturn (oidcUser );
372+ when (token .getAuthorizedClientRegistrationId ()).thenReturn ("google" );
373+ when (token .getAuthorities ()).thenReturn (AuthorityUtils .NO_AUTHORITIES );
374+
375+ when (oidcUser .getSubject ()).thenReturn ("b7f3dd13-f9cc-4573-8482-b4fccf8e1977" );
376+ when (oidcUser .getPreferredUsername ()).thenReturn ("John.Doe@test.com" );
377+ when (oidcUser .getGivenName ()).thenReturn ("John" );
378+ when (oidcUser .getFamilyName ()).thenReturn ("Doe" );
379+ when (oidcUser .getEmail ()).thenReturn ("jdoe@test.com" );
380+ when (oidcUser .getPhoneNumber ()).thenReturn ("+123" );
381+ when (oidcUser .getAddress ()).thenReturn (null );
382+ when (oidcUser .getClaims ()).thenReturn (Map .of ());
383+
384+ Optional <GeorchestraUser > result = mapper .map (token );
385+
386+ assertThat (result ).isPresent ();
387+ assertThat (result .orElseThrow ().getUsername ()).isEqualTo ("google_john_doe_test_com" );
388+ }
389+
390+ @ Test
391+ void map_shouldNotTransformUsernameWhenDisableUidTransformationMatchesProvider () {
392+ OpenIdConnectUserMapper mapper = newMapper ("google" );
393+
394+ OAuth2AuthenticationToken token = mock (OAuth2AuthenticationToken .class );
395+ OidcUser oidcUser = mock (OidcUser .class );
396+
397+ when (token .getPrincipal ()).thenReturn (oidcUser );
398+ when (token .getAuthorizedClientRegistrationId ()).thenReturn ("google" );
399+ when (token .getAuthorities ()).thenReturn (AuthorityUtils .NO_AUTHORITIES );
400+
401+ when (oidcUser .getSubject ()).thenReturn ("b7f3dd13-f9cc-4573-8482-b4fccf8e1977" );
402+ when (oidcUser .getPreferredUsername ()).thenReturn ("John.Doe@test.com" );
403+ when (oidcUser .getGivenName ()).thenReturn ("John" );
404+ when (oidcUser .getFamilyName ()).thenReturn ("Doe" );
405+ when (oidcUser .getEmail ()).thenReturn ("jdoe@test.com" );
406+ when (oidcUser .getPhoneNumber ()).thenReturn ("+123" );
407+ when (oidcUser .getAddress ()).thenReturn (null );
408+ when (oidcUser .getClaims ()).thenReturn (Map .of ());
409+
410+ Optional <GeorchestraUser > result = mapper .map (token );
411+
412+ assertThat (result ).isPresent ();
413+ assertThat (result .orElseThrow ().getUsername ()).isEqualTo ("John.Doe@test.com" );
414+ }
415+
416+ private OpenIdConnectUserMapper newMapper (String disableUidTransformation ) {
417+ GeorchestraGatewaySecurityConfigProperties securityConfigProperties = new GeorchestraGatewaySecurityConfigProperties ();
418+ securityConfigProperties .setDisableUidTransformation (disableUidTransformation );
419+ return new OpenIdConnectUserMapper (nonStandardClaimsConfig , securityConfigProperties );
420+ }
421+
331422 private Map <String , Object > sampleClaims () throws ParseException {
332423 String json = SAMPLE_CLAIMS ;
333424 return sampleClaims (json );
0 commit comments