Search in sources :

Example 1 with DefaultOidcUser

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

the class OidcReactiveOAuth2UserService method loadUser.

@Override
public Mono<OidcUser> loadUser(OidcUserRequest userRequest) throws OAuth2AuthenticationException {
    Assert.notNull(userRequest, "userRequest cannot be null");
    // @formatter:off
    return getUserInfo(userRequest).map((userInfo) -> new OidcUserAuthority(userRequest.getIdToken(), userInfo)).defaultIfEmpty(new OidcUserAuthority(userRequest.getIdToken(), null)).map((authority) -> {
        OidcUserInfo userInfo = authority.getUserInfo();
        Set<GrantedAuthority> authorities = new HashSet<>();
        authorities.add(authority);
        OAuth2AccessToken token = userRequest.getAccessToken();
        for (String scope : token.getScopes()) {
            authorities.add(new SimpleGrantedAuthority("SCOPE_" + scope));
        }
        String userNameAttributeName = userRequest.getClientRegistration().getProviderDetails().getUserInfoEndpoint().getUserNameAttributeName();
        if (StringUtils.hasText(userNameAttributeName)) {
            return new DefaultOidcUser(authorities, userRequest.getIdToken(), userInfo, userNameAttributeName);
        }
        return new DefaultOidcUser(authorities, userRequest.getIdToken(), userInfo);
    });
// @formatter:on
}
Also used : SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) OidcUserAuthority(org.springframework.security.oauth2.core.oidc.user.OidcUserAuthority) OAuth2AccessToken(org.springframework.security.oauth2.core.OAuth2AccessToken) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) GrantedAuthority(org.springframework.security.core.GrantedAuthority) OidcUserInfo(org.springframework.security.oauth2.core.oidc.OidcUserInfo) DefaultOidcUser(org.springframework.security.oauth2.core.oidc.user.DefaultOidcUser) HashSet(java.util.HashSet)

Example 2 with DefaultOidcUser

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

the class OidcUserService method getUser.

private OidcUser getUser(OidcUserRequest userRequest, OidcUserInfo userInfo, Set<GrantedAuthority> authorities) {
    ProviderDetails providerDetails = userRequest.getClientRegistration().getProviderDetails();
    String userNameAttributeName = providerDetails.getUserInfoEndpoint().getUserNameAttributeName();
    if (StringUtils.hasText(userNameAttributeName)) {
        return new DefaultOidcUser(authorities, userRequest.getIdToken(), userInfo, userNameAttributeName);
    }
    return new DefaultOidcUser(authorities, userRequest.getIdToken(), userInfo);
}
Also used : ProviderDetails(org.springframework.security.oauth2.client.registration.ClientRegistration.ProviderDetails) DefaultOidcUser(org.springframework.security.oauth2.core.oidc.user.DefaultOidcUser)

Example 3 with DefaultOidcUser

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

the class TestOidcUsers method create.

public static DefaultOidcUser create() {
    OidcIdToken idToken = idToken();
    OidcUserInfo userInfo = userInfo();
    return new DefaultOidcUser(authorities(idToken, userInfo), idToken, userInfo);
}
Also used : OidcIdToken(org.springframework.security.oauth2.core.oidc.OidcIdToken) OidcUserInfo(org.springframework.security.oauth2.core.oidc.OidcUserInfo)

Example 4 with DefaultOidcUser

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

the class SecurityMockMvcRequestPostProcessorsOidcLoginTests method oidcLoginWhenOidcUserSpecifiedThenLastCalledTakesPrecedence.

// gh-7794
@Test
public void oidcLoginWhenOidcUserSpecifiedThenLastCalledTakesPrecedence() throws Exception {
    OidcUser oidcUser = new DefaultOidcUser(AuthorityUtils.createAuthorityList("SCOPE_read"), TestOidcIdTokens.idToken().build());
    this.mvc.perform(get("/id-token/sub").with(oidcLogin().idToken((i) -> i.subject("foo")).oidcUser(oidcUser))).andExpect(status().isOk()).andExpect(content().string("subject"));
    this.mvc.perform(get("/id-token/sub").with(oidcLogin().oidcUser(oidcUser).idToken((i) -> i.subject("bar")))).andExpect(content().string("bar"));
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) PathVariable(org.springframework.web.bind.annotation.PathVariable) OidcUser(org.springframework.security.oauth2.core.oidc.user.OidcUser) RegisteredOAuth2AuthorizedClient(org.springframework.security.oauth2.client.annotation.RegisteredOAuth2AuthorizedClient) Autowired(org.springframework.beans.factory.annotation.Autowired) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) HttpSecurity(org.springframework.security.config.annotation.web.builders.HttpSecurity) MockMvcResultMatchers.content(org.springframework.test.web.servlet.result.MockMvcResultMatchers.content) MockMvc(org.springframework.test.web.servlet.MockMvc) WebSecurityConfigurerAdapter(org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) TestSecurityContextHolder(org.springframework.security.test.context.TestSecurityContextHolder) MockMvcResultMatchers.status(org.springframework.test.web.servlet.result.MockMvcResultMatchers.status) MockMvcBuilders(org.springframework.test.web.servlet.setup.MockMvcBuilders) GetMapping(org.springframework.web.bind.annotation.GetMapping) OidcIdToken(org.springframework.security.oauth2.core.oidc.OidcIdToken) WebAppConfiguration(org.springframework.test.context.web.WebAppConfiguration) TestOidcIdTokens(org.springframework.security.oauth2.core.oidc.TestOidcIdTokens) SpringExtension(org.springframework.test.context.junit.jupiter.SpringExtension) Collection(java.util.Collection) WebApplicationContext(org.springframework.web.context.WebApplicationContext) EnableWebMvc(org.springframework.web.servlet.config.annotation.EnableWebMvc) Collectors(java.util.stream.Collectors) RestController(org.springframework.web.bind.annotation.RestController) OAuth2AuthorizedClient(org.springframework.security.oauth2.client.OAuth2AuthorizedClient) DefaultOidcUser(org.springframework.security.oauth2.core.oidc.user.DefaultOidcUser) GrantedAuthority(org.springframework.security.core.GrantedAuthority) Test(org.junit.jupiter.api.Test) SecurityMockMvcConfigurers.springSecurity(org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity) List(java.util.List) AfterEach(org.junit.jupiter.api.AfterEach) OAuth2AuthorizedClientRepository(org.springframework.security.oauth2.client.web.OAuth2AuthorizedClientRepository) EnableWebSecurity(org.springframework.security.config.annotation.web.configuration.EnableWebSecurity) ContextConfiguration(org.springframework.test.context.ContextConfiguration) AuthenticationPrincipal(org.springframework.security.core.annotation.AuthenticationPrincipal) MockMvcRequestBuilders.get(org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get) Bean(org.springframework.context.annotation.Bean) SecurityMockMvcRequestPostProcessors.oidcLogin(org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.oidcLogin) AuthorityUtils(org.springframework.security.core.authority.AuthorityUtils) ClientRegistrationRepository(org.springframework.security.oauth2.client.registration.ClientRegistrationRepository) Mockito.mock(org.mockito.Mockito.mock) DefaultOidcUser(org.springframework.security.oauth2.core.oidc.user.DefaultOidcUser) OidcUser(org.springframework.security.oauth2.core.oidc.user.OidcUser) DefaultOidcUser(org.springframework.security.oauth2.core.oidc.user.DefaultOidcUser) Test(org.junit.jupiter.api.Test)

Example 5 with DefaultOidcUser

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

the class SecurityMockServerConfigurersOidcLoginTests method oidcUserWhenNameSpecifiedThenUserHasName.

@Test
public void oidcUserWhenNameSpecifiedThenUserHasName() throws Exception {
    OidcUser oidcUser = new DefaultOidcUser(AuthorityUtils.commaSeparatedStringToAuthorityList("SCOPE_read"), OidcIdToken.withTokenValue("id-token").claim("custom-attribute", "test-subject").build(), "custom-attribute");
    this.client.mutateWith(SecurityMockServerConfigurers.mockOAuth2Login().oauth2User(oidcUser)).get().uri("/token").exchange().expectStatus().isOk();
    OAuth2AuthenticationToken token = this.controller.token;
    assertThat(token.getPrincipal().getName()).isEqualTo("test-subject");
    this.client.mutateWith(SecurityMockServerConfigurers.mockOAuth2Login().oauth2User(oidcUser)).get().uri("/client").exchange().expectStatus().isOk();
    OAuth2AuthorizedClient client = this.controller.authorizedClient;
    assertThat(client.getPrincipalName()).isEqualTo("test-subject");
}
Also used : OAuth2AuthenticationToken(org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken) RegisteredOAuth2AuthorizedClient(org.springframework.security.oauth2.client.annotation.RegisteredOAuth2AuthorizedClient) OAuth2AuthorizedClient(org.springframework.security.oauth2.client.OAuth2AuthorizedClient) DefaultOidcUser(org.springframework.security.oauth2.core.oidc.user.DefaultOidcUser) OidcUser(org.springframework.security.oauth2.core.oidc.user.OidcUser) DefaultOidcUser(org.springframework.security.oauth2.core.oidc.user.DefaultOidcUser) Test(org.junit.jupiter.api.Test)

Aggregations

DefaultOidcUser (org.springframework.security.oauth2.core.oidc.user.DefaultOidcUser)14 Test (org.junit.jupiter.api.Test)11 OidcIdToken (org.springframework.security.oauth2.core.oidc.OidcIdToken)9 OidcUser (org.springframework.security.oauth2.core.oidc.user.OidcUser)8 GrantedAuthority (org.springframework.security.core.GrantedAuthority)7 BeforeEach (org.junit.jupiter.api.BeforeEach)6 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)6 AuthorityUtils (org.springframework.security.core.authority.AuthorityUtils)6 TestOidcIdTokens (org.springframework.security.oauth2.core.oidc.TestOidcIdTokens)6 List (java.util.List)5 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)5 Mock (org.mockito.Mock)5 Mockito.mock (org.mockito.Mockito.mock)5 MockitoExtension (org.mockito.junit.jupiter.MockitoExtension)5 OAuth2AuthenticationToken (org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken)5 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)4 Arrays (java.util.Arrays)4 Base64 (java.util.Base64)4 Collections (java.util.Collections)4 HashMap (java.util.HashMap)4