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);
}
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"));
}
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
}
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);
}
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);
}
Aggregations