Search in sources :

Example 6 with OidcUserRequest

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

the class OidcReactiveOAuth2UserServiceTests method loadUserWhenTokenContainsScopesThenIndividualScopeAuthorities.

@Test
public void loadUserWhenTokenContainsScopesThenIndividualScopeAuthorities() {
    OidcReactiveOAuth2UserService userService = new OidcReactiveOAuth2UserService();
    OidcUserRequest request = new OidcUserRequest(TestClientRegistrations.clientRegistration().build(), TestOAuth2AccessTokens.scopes("message:read", "message:write"), TestOidcIdTokens.idToken().build());
    OidcUser user = userService.loadUser(request).block();
    assertThat(user.getAuthorities()).hasSize(3);
    Iterator<? extends GrantedAuthority> authorities = user.getAuthorities().iterator();
    assertThat(authorities.next()).isInstanceOf(OAuth2UserAuthority.class);
    assertThat(authorities.next()).isEqualTo(new SimpleGrantedAuthority("SCOPE_message:read"));
    assertThat(authorities.next()).isEqualTo(new SimpleGrantedAuthority("SCOPE_message:write"));
}
Also used : SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) OidcUser(org.springframework.security.oauth2.core.oidc.user.OidcUser) Test(org.junit.jupiter.api.Test)

Example 7 with OidcUserRequest

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

the class OidcUserServiceTests method loadUserWhenNonStandardScopesAuthorizedAndAccessibleScopesEmptyThenUserInfoEndpointRequested.

// gh-6886
@Test
public void loadUserWhenNonStandardScopesAuthorizedAndAccessibleScopesEmptyThenUserInfoEndpointRequested() {
    // @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();
    this.accessToken = TestOAuth2AccessTokens.scopes("scope1", "scope2");
    this.userService.setAccessibleScopes(Collections.emptySet());
    OidcUser user = this.userService.loadUser(new OidcUserRequest(clientRegistration, this.accessToken, this.idToken));
    assertThat(user.getUserInfo()).isNotNull();
}
Also used : ClientRegistration(org.springframework.security.oauth2.client.registration.ClientRegistration) OidcUser(org.springframework.security.oauth2.core.oidc.user.OidcUser) Test(org.junit.jupiter.api.Test)

Example 8 with OidcUserRequest

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

the class OidcUserServiceTests method loadUserWhenUserInfoSuccessResponseThenReturnUser.

@Test
public void loadUserWhenUserInfoSuccessResponseThenReturnUser() {
    // @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();
    OidcUser user = this.userService.loadUser(new OidcUserRequest(clientRegistration, this.accessToken, this.idToken));
    assertThat(user.getIdToken()).isNotNull();
    assertThat(user.getUserInfo()).isNotNull();
    assertThat(user.getUserInfo().getClaims().size()).isEqualTo(6);
    assertThat(user.getIdToken()).isEqualTo(this.idToken);
    assertThat(user.getName()).isEqualTo("subject1");
    assertThat(user.getUserInfo().getSubject()).isEqualTo("subject1");
    assertThat(user.getUserInfo().getFullName()).isEqualTo("first last");
    assertThat(user.getUserInfo().getGivenName()).isEqualTo("first");
    assertThat(user.getUserInfo().getFamilyName()).isEqualTo("last");
    assertThat(user.getUserInfo().getPreferredUsername()).isEqualTo("user1");
    assertThat(user.getUserInfo().getEmail()).isEqualTo("user1@example.com");
    assertThat(user.getAuthorities().size()).isEqualTo(3);
    assertThat(user.getAuthorities().iterator().next()).isInstanceOf(OidcUserAuthority.class);
    OidcUserAuthority userAuthority = (OidcUserAuthority) user.getAuthorities().iterator().next();
    assertThat(userAuthority.getAuthority()).isEqualTo("ROLE_USER");
    assertThat(userAuthority.getIdToken()).isEqualTo(user.getIdToken());
    assertThat(userAuthority.getUserInfo()).isEqualTo(user.getUserInfo());
}
Also used : OidcUserAuthority(org.springframework.security.oauth2.core.oidc.user.OidcUserAuthority) ClientRegistration(org.springframework.security.oauth2.client.registration.ClientRegistration) OidcUser(org.springframework.security.oauth2.core.oidc.user.OidcUser) Test(org.junit.jupiter.api.Test)

Example 9 with OidcUserRequest

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

the class OidcUserServiceTests method loadUserWhenNonStandardScopesAuthorizedThenUserInfoEndpointNotRequested.

@Test
public void loadUserWhenNonStandardScopesAuthorizedThenUserInfoEndpointNotRequested() {
    ClientRegistration clientRegistration = this.clientRegistrationBuilder.userInfoUri("https://provider.com/user").build();
    this.accessToken = TestOAuth2AccessTokens.scopes("scope1", "scope2");
    OidcUser user = this.userService.loadUser(new OidcUserRequest(clientRegistration, this.accessToken, this.idToken));
    assertThat(user.getUserInfo()).isNull();
}
Also used : ClientRegistration(org.springframework.security.oauth2.client.registration.ClientRegistration) OidcUser(org.springframework.security.oauth2.core.oidc.user.OidcUser) Test(org.junit.jupiter.api.Test)

Example 10 with OidcUserRequest

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

the class OidcUserServiceTests method loadUserWhenUserInfoSuccessResponseAndUserInfoSubjectNotSameAsIdTokenSubjectThenThrowOAuth2AuthenticationException.

@Test
public void loadUserWhenUserInfoSuccessResponseAndUserInfoSubjectNotSameAsIdTokenSubjectThenThrowOAuth2AuthenticationException() {
    String userInfoResponse = "{\n" + "	\"sub\": \"other-subject\"\n" + "}\n";
    this.server.enqueue(jsonResponse(userInfoResponse));
    String userInfoUri = this.server.url("/user").toString();
    ClientRegistration clientRegistration = this.clientRegistrationBuilder.userInfoUri(userInfoUri).build();
    assertThatExceptionOfType(OAuth2AuthenticationException.class).isThrownBy(() -> this.userService.loadUser(new OidcUserRequest(clientRegistration, this.accessToken, this.idToken))).withMessageContaining("invalid_user_info_response");
}
Also used : ClientRegistration(org.springframework.security.oauth2.client.registration.ClientRegistration) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)25 ClientRegistration (org.springframework.security.oauth2.client.registration.ClientRegistration)23 OidcUser (org.springframework.security.oauth2.core.oidc.user.OidcUser)20 OidcUserRequest (org.springframework.security.oauth2.client.oidc.userinfo.OidcUserRequest)8 OAuth2AccessToken (org.springframework.security.oauth2.core.OAuth2AccessToken)7 OAuth2AuthenticationException (org.springframework.security.oauth2.core.OAuth2AuthenticationException)7 GrantedAuthority (org.springframework.security.core.GrantedAuthority)6 OAuth2AccessTokenResponse (org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse)6 OAuth2AuthorizationRequest (org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest)6 OAuth2AuthorizationResponse (org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationResponse)6 HashMap (java.util.HashMap)5 OAuth2LoginAuthenticationToken (org.springframework.security.oauth2.client.authentication.OAuth2LoginAuthenticationToken)5 OAuth2AuthorizationCodeGrantRequest (org.springframework.security.oauth2.client.endpoint.OAuth2AuthorizationCodeGrantRequest)5 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)4 Base64 (java.util.Base64)4 Map (java.util.Map)4 GrantedAuthoritiesMapper (org.springframework.security.core.authority.mapping.GrantedAuthoritiesMapper)4 OAuth2User (org.springframework.security.oauth2.core.user.OAuth2User)4 Arrays (java.util.Arrays)3 Collections (java.util.Collections)3