Search in sources :

Example 1 with ClaimTypeConverter

use of org.springframework.security.oauth2.core.converter.ClaimTypeConverter in project spring-security by spring-projects.

the class ReactiveOidcIdTokenDecoderFactoryTests method createDecoderWhenCustomClaimTypeConverterFactorySetThenApplied.

@Test
public void createDecoderWhenCustomClaimTypeConverterFactorySetThenApplied() {
    Function<ClientRegistration, Converter<Map<String, Object>, Map<String, Object>>> customClaimTypeConverterFactory = mock(Function.class);
    this.idTokenDecoderFactory.setClaimTypeConverterFactory(customClaimTypeConverterFactory);
    ClientRegistration clientRegistration = this.registration.build();
    given(customClaimTypeConverterFactory.apply(same(clientRegistration))).willReturn(new ClaimTypeConverter(OidcIdTokenDecoderFactory.createDefaultClaimTypeConverters()));
    this.idTokenDecoderFactory.createDecoder(clientRegistration);
    verify(customClaimTypeConverterFactory).apply(same(clientRegistration));
}
Also used : ClientRegistration(org.springframework.security.oauth2.client.registration.ClientRegistration) Converter(org.springframework.core.convert.converter.Converter) ClaimTypeConverter(org.springframework.security.oauth2.core.converter.ClaimTypeConverter) ClaimTypeConverter(org.springframework.security.oauth2.core.converter.ClaimTypeConverter) Test(org.junit.jupiter.api.Test)

Example 2 with ClaimTypeConverter

use of org.springframework.security.oauth2.core.converter.ClaimTypeConverter in project spring-security by spring-projects.

the class OidcIdTokenDecoderFactory method createDecoder.

@Override
public JwtDecoder createDecoder(ClientRegistration clientRegistration) {
    Assert.notNull(clientRegistration, "clientRegistration cannot be null");
    return this.jwtDecoders.computeIfAbsent(clientRegistration.getRegistrationId(), (key) -> {
        NimbusJwtDecoder jwtDecoder = buildDecoder(clientRegistration);
        jwtDecoder.setJwtValidator(this.jwtValidatorFactory.apply(clientRegistration));
        Converter<Map<String, Object>, Map<String, Object>> claimTypeConverter = this.claimTypeConverterFactory.apply(clientRegistration);
        if (claimTypeConverter != null) {
            jwtDecoder.setClaimSetConverter(claimTypeConverter);
        }
        return jwtDecoder;
    });
}
Also used : NimbusJwtDecoder(org.springframework.security.oauth2.jwt.NimbusJwtDecoder) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 3 with ClaimTypeConverter

use of org.springframework.security.oauth2.core.converter.ClaimTypeConverter in project spring-security by spring-projects.

the class ReactiveOidcIdTokenDecoderFactory method createDecoder.

@Override
public ReactiveJwtDecoder createDecoder(ClientRegistration clientRegistration) {
    Assert.notNull(clientRegistration, "clientRegistration cannot be null");
    return this.jwtDecoders.computeIfAbsent(clientRegistration.getRegistrationId(), (key) -> {
        NimbusReactiveJwtDecoder jwtDecoder = buildDecoder(clientRegistration);
        jwtDecoder.setJwtValidator(this.jwtValidatorFactory.apply(clientRegistration));
        Converter<Map<String, Object>, Map<String, Object>> claimTypeConverter = this.claimTypeConverterFactory.apply(clientRegistration);
        if (claimTypeConverter != null) {
            jwtDecoder.setClaimSetConverter(claimTypeConverter);
        }
        return jwtDecoder;
    });
}
Also used : NimbusReactiveJwtDecoder(org.springframework.security.oauth2.jwt.NimbusReactiveJwtDecoder) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 4 with ClaimTypeConverter

use of org.springframework.security.oauth2.core.converter.ClaimTypeConverter in project spring-security by spring-projects.

the class OidcUserServiceTests method loadUserWhenCustomClaimTypeConverterFactorySetThenApplied.

@Test
public void loadUserWhenCustomClaimTypeConverterFactorySetThenApplied() {
    // @formatter:off
    String userInfoResponse = "{\n" + "   \"sub\": \"subject1\",\n" + "   \"name\": \"first last\",\n" + "   \"given_name\": \"first\",\n" + "   \"family_name\": \"last\",\n" + "   \"preferred_username\": \"user1\",\n" + "   \"email\": \"user1@example.com\"\n" + "}\n";
    // @formatter:on
    this.server.enqueue(jsonResponse(userInfoResponse));
    String userInfoUri = this.server.url("/user").toString();
    ClientRegistration clientRegistration = this.clientRegistrationBuilder.userInfoUri(userInfoUri).build();
    Function<ClientRegistration, Converter<Map<String, Object>, Map<String, Object>>> customClaimTypeConverterFactory = mock(Function.class);
    this.userService.setClaimTypeConverterFactory(customClaimTypeConverterFactory);
    given(customClaimTypeConverterFactory.apply(same(clientRegistration))).willReturn(new ClaimTypeConverter(OidcUserService.createDefaultClaimTypeConverters()));
    this.userService.loadUser(new OidcUserRequest(clientRegistration, this.accessToken, this.idToken));
    verify(customClaimTypeConverterFactory).apply(same(clientRegistration));
}
Also used : ClientRegistration(org.springframework.security.oauth2.client.registration.ClientRegistration) ClaimTypeConverter(org.springframework.security.oauth2.core.converter.ClaimTypeConverter) Converter(org.springframework.core.convert.converter.Converter) ClaimTypeConverter(org.springframework.security.oauth2.core.converter.ClaimTypeConverter) Test(org.junit.jupiter.api.Test)

Example 5 with ClaimTypeConverter

use of org.springframework.security.oauth2.core.converter.ClaimTypeConverter in project spring-security by spring-projects.

the class OidcIdTokenDecoderFactoryTests method createDecoderWhenCustomClaimTypeConverterFactorySetThenApplied.

@Test
public void createDecoderWhenCustomClaimTypeConverterFactorySetThenApplied() {
    Function<ClientRegistration, Converter<Map<String, Object>, Map<String, Object>>> customClaimTypeConverterFactory = mock(Function.class);
    this.idTokenDecoderFactory.setClaimTypeConverterFactory(customClaimTypeConverterFactory);
    ClientRegistration clientRegistration = this.registration.build();
    given(customClaimTypeConverterFactory.apply(same(clientRegistration))).willReturn(new ClaimTypeConverter(OidcIdTokenDecoderFactory.createDefaultClaimTypeConverters()));
    this.idTokenDecoderFactory.createDecoder(clientRegistration);
    verify(customClaimTypeConverterFactory).apply(same(clientRegistration));
}
Also used : ClientRegistration(org.springframework.security.oauth2.client.registration.ClientRegistration) Converter(org.springframework.core.convert.converter.Converter) ClaimTypeConverter(org.springframework.security.oauth2.core.converter.ClaimTypeConverter) ClaimTypeConverter(org.springframework.security.oauth2.core.converter.ClaimTypeConverter) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)4 Converter (org.springframework.core.convert.converter.Converter)4 ClientRegistration (org.springframework.security.oauth2.client.registration.ClientRegistration)4 ClaimTypeConverter (org.springframework.security.oauth2.core.converter.ClaimTypeConverter)4 HashMap (java.util.HashMap)3 Map (java.util.Map)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 DefaultOAuth2User (org.springframework.security.oauth2.core.user.DefaultOAuth2User)1 OAuth2User (org.springframework.security.oauth2.core.user.OAuth2User)1 NimbusJwtDecoder (org.springframework.security.oauth2.jwt.NimbusJwtDecoder)1 NimbusReactiveJwtDecoder (org.springframework.security.oauth2.jwt.NimbusReactiveJwtDecoder)1