Search in sources :

Example 1 with DelegatingOAuth2UserService

use of org.springframework.security.oauth2.client.userinfo.DelegatingOAuth2UserService in project spring-security by spring-projects.

the class OAuth2LoginConfigurer method getOAuth2UserService.

private OAuth2UserService<OAuth2UserRequest, OAuth2User> getOAuth2UserService() {
    if (this.userInfoEndpointConfig.userService != null) {
        return this.userInfoEndpointConfig.userService;
    }
    ResolvableType type = ResolvableType.forClassWithGenerics(OAuth2UserService.class, OAuth2UserRequest.class, OAuth2User.class);
    OAuth2UserService<OAuth2UserRequest, OAuth2User> bean = getBeanOrNull(type);
    if (bean != null) {
        return bean;
    }
    if (this.userInfoEndpointConfig.customUserTypes.isEmpty()) {
        return new DefaultOAuth2UserService();
    }
    List<OAuth2UserService<OAuth2UserRequest, OAuth2User>> userServices = new ArrayList<>();
    userServices.add(new CustomUserTypesOAuth2UserService(this.userInfoEndpointConfig.customUserTypes));
    userServices.add(new DefaultOAuth2UserService());
    return new DelegatingOAuth2UserService<>(userServices);
}
Also used : CustomUserTypesOAuth2UserService(org.springframework.security.oauth2.client.userinfo.CustomUserTypesOAuth2UserService) OAuth2User(org.springframework.security.oauth2.core.user.OAuth2User) DelegatingOAuth2UserService(org.springframework.security.oauth2.client.userinfo.DelegatingOAuth2UserService) DefaultOAuth2UserService(org.springframework.security.oauth2.client.userinfo.DefaultOAuth2UserService) CustomUserTypesOAuth2UserService(org.springframework.security.oauth2.client.userinfo.CustomUserTypesOAuth2UserService) OAuth2UserService(org.springframework.security.oauth2.client.userinfo.OAuth2UserService) ArrayList(java.util.ArrayList) DelegatingOAuth2UserService(org.springframework.security.oauth2.client.userinfo.DelegatingOAuth2UserService) OAuth2UserRequest(org.springframework.security.oauth2.client.userinfo.OAuth2UserRequest) ResolvableType(org.springframework.core.ResolvableType) DefaultOAuth2UserService(org.springframework.security.oauth2.client.userinfo.DefaultOAuth2UserService)

Example 2 with DelegatingOAuth2UserService

use of org.springframework.security.oauth2.client.userinfo.DelegatingOAuth2UserService in project spring-security by spring-projects.

the class DelegatingOAuth2UserServiceTests method loadUserWhenUserServiceCannotLoadThenReturnNull.

@Test
@SuppressWarnings("unchecked")
public void loadUserWhenUserServiceCannotLoadThenReturnNull() {
    OAuth2UserService<OAuth2UserRequest, OAuth2User> userService1 = mock(OAuth2UserService.class);
    OAuth2UserService<OAuth2UserRequest, OAuth2User> userService2 = mock(OAuth2UserService.class);
    OAuth2UserService<OAuth2UserRequest, OAuth2User> userService3 = mock(OAuth2UserService.class);
    DelegatingOAuth2UserService<OAuth2UserRequest, OAuth2User> delegatingUserService = new DelegatingOAuth2UserService<>(Arrays.asList(userService1, userService2, userService3));
    OAuth2User loadedUser = delegatingUserService.loadUser(mock(OAuth2UserRequest.class));
    assertThat(loadedUser).isNull();
}
Also used : OAuth2User(org.springframework.security.oauth2.core.user.OAuth2User) Test(org.junit.jupiter.api.Test)

Example 3 with DelegatingOAuth2UserService

use of org.springframework.security.oauth2.client.userinfo.DelegatingOAuth2UserService in project spring-security by spring-projects.

the class DelegatingOAuth2UserServiceTests method loadUserWhenUserServiceCanLoadThenReturnUser.

@Test
@SuppressWarnings("unchecked")
public void loadUserWhenUserServiceCanLoadThenReturnUser() {
    OAuth2UserService<OAuth2UserRequest, OAuth2User> userService1 = mock(OAuth2UserService.class);
    OAuth2UserService<OAuth2UserRequest, OAuth2User> userService2 = mock(OAuth2UserService.class);
    OAuth2UserService<OAuth2UserRequest, OAuth2User> userService3 = mock(OAuth2UserService.class);
    OAuth2User mockUser = mock(OAuth2User.class);
    given(userService3.loadUser(any(OAuth2UserRequest.class))).willReturn(mockUser);
    DelegatingOAuth2UserService<OAuth2UserRequest, OAuth2User> delegatingUserService = new DelegatingOAuth2UserService<>(Arrays.asList(userService1, userService2, userService3));
    OAuth2User loadedUser = delegatingUserService.loadUser(mock(OAuth2UserRequest.class));
    assertThat(loadedUser).isEqualTo(mockUser);
}
Also used : OAuth2User(org.springframework.security.oauth2.core.user.OAuth2User) Test(org.junit.jupiter.api.Test)

Aggregations

OAuth2User (org.springframework.security.oauth2.core.user.OAuth2User)3 Test (org.junit.jupiter.api.Test)2 ArrayList (java.util.ArrayList)1 ResolvableType (org.springframework.core.ResolvableType)1 CustomUserTypesOAuth2UserService (org.springframework.security.oauth2.client.userinfo.CustomUserTypesOAuth2UserService)1 DefaultOAuth2UserService (org.springframework.security.oauth2.client.userinfo.DefaultOAuth2UserService)1 DelegatingOAuth2UserService (org.springframework.security.oauth2.client.userinfo.DelegatingOAuth2UserService)1 OAuth2UserRequest (org.springframework.security.oauth2.client.userinfo.OAuth2UserRequest)1 OAuth2UserService (org.springframework.security.oauth2.client.userinfo.OAuth2UserService)1