Search in sources :

Example 21 with OAuth2AuthenticationToken

use of org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken in project books by aidanwhiteley.

the class Oauth2AuthenticationUtils method getUserIfExists.

public Optional<User> getUserIfExists(OAuth2AuthenticationToken authentication) {
    OAuth2AuthorizedClient authorizedClient = this.getAuthorizedClient(authentication);
    String authenticationProviderId = authorizedClient.getPrincipalName();
    List<User> users = userRepository.findAllByAuthenticationServiceIdAndAuthProvider(authenticationProviderId, this.getAuthenticationProvider(authentication).toString());
    User user;
    switch(users.size()) {
        case 0:
            user = null;
            break;
        case 1:
            user = users.get(0);
            break;
        default:
            LOGGER.error("More than one user found for Authentication: {}", authentication);
            throw new IllegalStateException("More that one user found for a given Authentication");
    }
    return Optional.ofNullable(user);
}
Also used : User(com.aidanwhiteley.books.domain.User) OAuth2AuthorizedClient(org.springframework.security.oauth2.client.OAuth2AuthorizedClient)

Example 22 with OAuth2AuthenticationToken

use of org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken in project spring-security by spring-projects.

the class OAuth2LoginConfigurerTests method logoutWhenUsingOidcLogoutHandlerThenRedirects.

@Test
public void logoutWhenUsingOidcLogoutHandlerThenRedirects() throws Exception {
    this.spring.register(OAuth2LoginConfigWithOidcLogoutSuccessHandler.class).autowire();
    OAuth2AuthenticationToken token = new OAuth2AuthenticationToken(TestOidcUsers.create(), AuthorityUtils.NO_AUTHORITIES, "registration-id");
    this.mvc.perform(post("/logout").with(authentication(token)).with(csrf())).andExpect(redirectedUrl("https://logout?id_token_hint=id-token"));
}
Also used : OAuth2AuthenticationToken(org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken) Test(org.junit.jupiter.api.Test)

Example 23 with OAuth2AuthenticationToken

use of org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken in project spring-security by spring-projects.

the class OAuth2LoginTests method logoutWhenUsingOidcLogoutHandlerThenRedirects.

@Test
public void logoutWhenUsingOidcLogoutHandlerThenRedirects() {
    this.spring.register(OAuth2LoginConfigWithOidcLogoutSuccessHandler.class).autowire();
    OAuth2AuthenticationToken token = new OAuth2AuthenticationToken(TestOidcUsers.create(), AuthorityUtils.NO_AUTHORITIES, getBean(ClientRegistration.class).getRegistrationId());
    ServerSecurityContextRepository repository = getBean(ServerSecurityContextRepository.class);
    given(repository.load(any())).willReturn(authentication(token));
    // @formatter:off
    this.client.post().uri("/logout").exchange().expectHeader().valueEquals("Location", "https://logout?id_token_hint=id-token");
// @formatter:on
}
Also used : OAuth2AuthenticationToken(org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken) ServerSecurityContextRepository(org.springframework.security.web.server.context.ServerSecurityContextRepository) Test(org.junit.jupiter.api.Test)

Example 24 with OAuth2AuthenticationToken

use of org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken in project spring-security by spring-projects.

the class TestOAuth2AuthenticationTokens method authenticated.

public static OAuth2AuthenticationToken authenticated() {
    DefaultOAuth2User principal = TestOAuth2Users.create();
    String registrationId = "registration-id";
    return new OAuth2AuthenticationToken(principal, principal.getAuthorities(), registrationId);
}
Also used : DefaultOAuth2User(org.springframework.security.oauth2.core.user.DefaultOAuth2User)

Example 25 with OAuth2AuthenticationToken

use of org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken in project spring-security by spring-projects.

the class TestOAuth2AuthenticationTokens method oidcAuthenticated.

public static OAuth2AuthenticationToken oidcAuthenticated() {
    DefaultOidcUser principal = TestOidcUsers.create();
    String registrationId = "registration-id";
    return new OAuth2AuthenticationToken(principal, principal.getAuthorities(), registrationId);
}
Also used : DefaultOidcUser(org.springframework.security.oauth2.core.oidc.user.DefaultOidcUser)

Aggregations

OAuth2AuthenticationToken (org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken)41 Test (org.junit.jupiter.api.Test)34 OAuth2AuthorizedClient (org.springframework.security.oauth2.client.OAuth2AuthorizedClient)11 Collection (java.util.Collection)6 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)6 DefaultOidcUser (org.springframework.security.oauth2.core.oidc.user.DefaultOidcUser)6 DefaultOAuth2User (org.springframework.security.oauth2.core.user.DefaultOAuth2User)6 RegisteredOAuth2AuthorizedClient (org.springframework.security.oauth2.client.annotation.RegisteredOAuth2AuthorizedClient)5 ClientRegistration (org.springframework.security.oauth2.client.registration.ClientRegistration)5 OAuth2User (org.springframework.security.oauth2.core.user.OAuth2User)5 WebFilterExchange (org.springframework.security.web.server.WebFilterExchange)5 HttpHeaders (org.springframework.http.HttpHeaders)4 URI (java.net.URI)3 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)3 BeforeEach (org.junit.jupiter.api.BeforeEach)3 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)3 Mock (org.mockito.Mock)3 MockitoExtension (org.mockito.junit.jupiter.MockitoExtension)3 GrantedAuthority (org.springframework.security.core.GrantedAuthority)3 AuthorityUtils (org.springframework.security.core.authority.AuthorityUtils)3