Search in sources :

Example 6 with OidcUserInfo

use of org.springframework.security.oauth2.core.oidc.OidcUserInfo in project spring-security by spring-projects.

the class OAuth2AuthenticationTokenMixinTests method deserializeWhenMixinRegisteredThenDeserializes.

@Test
public void deserializeWhenMixinRegisteredThenDeserializes() throws Exception {
    // OidcUser
    OAuth2AuthenticationToken expectedAuthentication = TestOAuth2AuthenticationTokens.oidcAuthenticated();
    String json = asJson(expectedAuthentication);
    OAuth2AuthenticationToken authentication = this.mapper.readValue(json, OAuth2AuthenticationToken.class);
    assertThat(authentication.getAuthorities()).containsExactlyElementsOf(expectedAuthentication.getAuthorities());
    assertThat(authentication.getDetails()).isEqualTo(expectedAuthentication.getDetails());
    assertThat(authentication.isAuthenticated()).isEqualTo(expectedAuthentication.isAuthenticated());
    assertThat(authentication.getAuthorizedClientRegistrationId()).isEqualTo(expectedAuthentication.getAuthorizedClientRegistrationId());
    DefaultOidcUser expectedOidcUser = (DefaultOidcUser) expectedAuthentication.getPrincipal();
    DefaultOidcUser oidcUser = (DefaultOidcUser) authentication.getPrincipal();
    assertThat(oidcUser.getAuthorities().containsAll(expectedOidcUser.getAuthorities())).isTrue();
    assertThat(oidcUser.getAttributes()).containsExactlyEntriesOf(expectedOidcUser.getAttributes());
    assertThat(oidcUser.getName()).isEqualTo(expectedOidcUser.getName());
    OidcIdToken expectedIdToken = expectedOidcUser.getIdToken();
    OidcIdToken idToken = oidcUser.getIdToken();
    assertThat(idToken.getTokenValue()).isEqualTo(expectedIdToken.getTokenValue());
    assertThat(idToken.getIssuedAt()).isEqualTo(expectedIdToken.getIssuedAt());
    assertThat(idToken.getExpiresAt()).isEqualTo(expectedIdToken.getExpiresAt());
    assertThat(idToken.getClaims()).containsExactlyEntriesOf(expectedIdToken.getClaims());
    OidcUserInfo expectedUserInfo = expectedOidcUser.getUserInfo();
    OidcUserInfo userInfo = oidcUser.getUserInfo();
    assertThat(userInfo.getClaims()).containsExactlyEntriesOf(expectedUserInfo.getClaims());
    // OAuth2User
    expectedAuthentication = TestOAuth2AuthenticationTokens.authenticated();
    json = asJson(expectedAuthentication);
    authentication = this.mapper.readValue(json, OAuth2AuthenticationToken.class);
    assertThat(authentication.getAuthorities()).containsExactlyElementsOf(expectedAuthentication.getAuthorities());
    assertThat(authentication.getDetails()).isEqualTo(expectedAuthentication.getDetails());
    assertThat(authentication.isAuthenticated()).isEqualTo(expectedAuthentication.isAuthenticated());
    assertThat(authentication.getAuthorizedClientRegistrationId()).isEqualTo(expectedAuthentication.getAuthorizedClientRegistrationId());
    DefaultOAuth2User expectedOauth2User = (DefaultOAuth2User) expectedAuthentication.getPrincipal();
    DefaultOAuth2User oauth2User = (DefaultOAuth2User) authentication.getPrincipal();
    assertThat(oauth2User.getAuthorities().containsAll(expectedOauth2User.getAuthorities())).isTrue();
    assertThat(oauth2User.getAttributes()).containsExactlyEntriesOf(expectedOauth2User.getAttributes());
    assertThat(oauth2User.getName()).isEqualTo(expectedOauth2User.getName());
}
Also used : OAuth2AuthenticationToken(org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken) OidcIdToken(org.springframework.security.oauth2.core.oidc.OidcIdToken) DefaultOAuth2User(org.springframework.security.oauth2.core.user.DefaultOAuth2User) OidcUserInfo(org.springframework.security.oauth2.core.oidc.OidcUserInfo) DefaultOidcUser(org.springframework.security.oauth2.core.oidc.user.DefaultOidcUser) Test(org.junit.jupiter.api.Test)

Aggregations

OidcUserInfo (org.springframework.security.oauth2.core.oidc.OidcUserInfo)5 DefaultOidcUser (org.springframework.security.oauth2.core.oidc.user.DefaultOidcUser)3 GrantedAuthority (org.springframework.security.core.GrantedAuthority)2 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)2 OAuth2AccessToken (org.springframework.security.oauth2.core.OAuth2AccessToken)2 OAuth2AuthenticationException (org.springframework.security.oauth2.core.OAuth2AuthenticationException)2 OAuth2Error (org.springframework.security.oauth2.core.OAuth2Error)2 OidcIdToken (org.springframework.security.oauth2.core.oidc.OidcIdToken)2 OidcUserAuthority (org.springframework.security.oauth2.core.oidc.user.OidcUserAuthority)2 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 User (org.hisp.dhis.user.User)1 Test (org.junit.jupiter.api.Test)1 OAuth2AuthenticationToken (org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken)1 ClientRegistration (org.springframework.security.oauth2.client.registration.ClientRegistration)1 ProviderDetails (org.springframework.security.oauth2.client.registration.ClientRegistration.ProviderDetails)1 OidcUser (org.springframework.security.oauth2.core.oidc.user.OidcUser)1 DefaultOAuth2User (org.springframework.security.oauth2.core.user.DefaultOAuth2User)1 OAuth2User (org.springframework.security.oauth2.core.user.OAuth2User)1