use of org.springframework.security.oauth2.client.OAuth2AuthorizedClient in project spring-security by spring-projects.
the class AuthenticatedPrincipalOAuth2AuthorizedClientRepositoryTests method saveAuthorizedClientWhenAnonymousPrincipalThenSaveToAnonymousRepository.
@Test
public void saveAuthorizedClientWhenAnonymousPrincipalThenSaveToAnonymousRepository() {
Authentication authentication = this.createAnonymousPrincipal();
OAuth2AuthorizedClient authorizedClient = mock(OAuth2AuthorizedClient.class);
this.authorizedClientRepository.saveAuthorizedClient(authorizedClient, authentication, this.request, this.response);
verify(this.anonymousAuthorizedClientRepository).saveAuthorizedClient(authorizedClient, authentication, this.request, this.response);
}
use of org.springframework.security.oauth2.client.OAuth2AuthorizedClient in project books by aidanwhiteley.
the class Oauth2AuthenticationUtils method getAuthenticationProvider.
public User.AuthenticationProvider getAuthenticationProvider(OAuth2AuthenticationToken auth) {
OAuth2AuthorizedClient authorizedClient = this.getAuthorizedClient(auth);
String clientId = authorizedClient.getClientRegistration().getClientId();
if (clientId.equals(googleClientClientId)) {
return GOOGLE;
} else if (clientId.equals(facebookClientClientId)) {
return FACEBOOK;
} else {
LOGGER.error("Unknown clientId specified of {} so cant determine authentication provider.", clientId);
throw new IllegalArgumentException("Uknown client id specified");
}
}
use of org.springframework.security.oauth2.client.OAuth2AuthorizedClient 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.OAuth2AuthorizedClient in project books by aidanwhiteley.
the class UserServiceTest method configureOauth.
private void configureOauth(String clientId, String name) {
Map<String, Object> details = new LinkedHashMap<>();
details.put("name", name);
details.put(name, name);
Set<GrantedAuthority> authorities = new HashSet<>();
authorities.add(new SimpleGrantedAuthority("USER"));
OAuth2User oauth2User = new DefaultOAuth2User(authorities, details, name);
when(oauthToken.getName()).thenReturn(DUMMY);
when(oauthToken.getAuthorizedClientRegistrationId()).thenReturn(DUMMY);
when(oauthToken.getPrincipal()).thenReturn(oauth2User);
OAuth2AuthorizedClient client = Mockito.mock(OAuth2AuthorizedClient.class);
ClientRegistration.Builder builder = ClientRegistration.withRegistrationId(DUMMY);
builder.clientId(clientId).authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE).clientSecret(DUMMY).redirectUriTemplate(DUMMY).scope(DUMMY).authorizationUri(DUMMY).tokenUri(DUMMY).clientName(DUMMY);
ClientRegistration clientReg = builder.build();
when(client.getClientRegistration()).thenReturn(clientReg);
when(authorisedClientService.loadAuthorizedClient(any(String.class), any(String.class))).thenReturn(client);
}
use of org.springframework.security.oauth2.client.OAuth2AuthorizedClient in project spring-security by spring-projects.
the class OAuth2ClientConfigurerTests method configureWhenAuthorizationCodeResponseSuccessThenAuthorizedClientSaved.
@Test
public void configureWhenAuthorizationCodeResponseSuccessThenAuthorizedClientSaved() throws Exception {
this.spring.register(OAuth2ClientConfig.class).autowire();
// Setup the Authorization Request in the session
Map<String, Object> attributes = new HashMap<>();
attributes.put(OAuth2ParameterNames.REGISTRATION_ID, this.registration1.getRegistrationId());
// @formatter:off
OAuth2AuthorizationRequest authorizationRequest = OAuth2AuthorizationRequest.authorizationCode().authorizationUri(this.registration1.getProviderDetails().getAuthorizationUri()).clientId(this.registration1.getClientId()).redirectUri("http://localhost/client-1").state("state").attributes(attributes).build();
// @formatter:on
AuthorizationRequestRepository<OAuth2AuthorizationRequest> authorizationRequestRepository = new HttpSessionOAuth2AuthorizationRequestRepository();
MockHttpServletRequest request = new MockHttpServletRequest("GET", "");
MockHttpServletResponse response = new MockHttpServletResponse();
authorizationRequestRepository.saveAuthorizationRequest(authorizationRequest, request, response);
MockHttpSession session = (MockHttpSession) request.getSession();
String principalName = "user1";
TestingAuthenticationToken authentication = new TestingAuthenticationToken(principalName, "password");
// @formatter:off
MockHttpServletRequestBuilder clientRequest = get("/client-1").param(OAuth2ParameterNames.CODE, "code").param(OAuth2ParameterNames.STATE, "state").with(authentication(authentication)).session(session);
this.mockMvc.perform(clientRequest).andExpect(status().is3xxRedirection()).andExpect(redirectedUrl("http://localhost/client-1"));
// @formatter:on
OAuth2AuthorizedClient authorizedClient = authorizedClientRepository.loadAuthorizedClient(this.registration1.getRegistrationId(), authentication, request);
assertThat(authorizedClient).isNotNull();
}
Aggregations