use of org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken in project spring-security by spring-projects.
the class SecurityMockServerConfigurersOAuth2LoginTests method oauth2LoginWhenNameSpecifiedThenUserHasName.
@Test
public void oauth2LoginWhenNameSpecifiedThenUserHasName() throws Exception {
OAuth2User oauth2User = new DefaultOAuth2User(AuthorityUtils.commaSeparatedStringToAuthorityList("SCOPE_read"), Collections.singletonMap("custom-attribute", "test-subject"), "custom-attribute");
this.client.mutateWith(SecurityMockServerConfigurers.mockOAuth2Login().oauth2User(oauth2User)).get().uri("/token").exchange().expectStatus().isOk();
OAuth2AuthenticationToken token = this.controller.token;
assertThat(token.getPrincipal().getName()).isEqualTo("test-subject");
this.client.mutateWith(SecurityMockServerConfigurers.mockOAuth2Login().oauth2User(oauth2User)).get().uri("/client").exchange().expectStatus().isOk();
OAuth2AuthorizedClient client = this.controller.authorizedClient;
assertThat(client.getPrincipalName()).isEqualTo("test-subject");
}
use of org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken in project spring-security by spring-projects.
the class SecurityMockServerConfigurersOAuth2LoginTests method oauth2LoginWhenAttributeSpecifiedThenUserHasAttribute.
@Test
public void oauth2LoginWhenAttributeSpecifiedThenUserHasAttribute() {
this.client.mutateWith(SecurityMockServerConfigurers.mockOAuth2Login().attributes((a) -> a.put("iss", "https://idp.example.org"))).get().uri("/token").exchange().expectStatus().isOk();
OAuth2AuthenticationToken token = this.controller.token;
assertThat(token.getPrincipal().getAttributes()).containsEntry("iss", "https://idp.example.org");
}
use of org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken in project spring-security by spring-projects.
the class SecurityMockServerConfigurersOAuth2LoginTests method oauth2LoginWhenUsingDefaultsThenProducesDefaultAuthentication.
@Test
public void oauth2LoginWhenUsingDefaultsThenProducesDefaultAuthentication() {
this.client.mutateWith(SecurityMockServerConfigurers.mockOAuth2Login()).get().uri("/token").exchange().expectStatus().isOk();
OAuth2AuthenticationToken token = this.controller.token;
assertThat(token).isNotNull();
assertThat(token.getAuthorizedClientRegistrationId()).isEqualTo("test");
assertThat(token.getPrincipal()).isInstanceOf(OAuth2User.class);
assertThat(token.getPrincipal().getAttributes()).containsEntry("sub", "user");
assertThat((Collection<GrantedAuthority>) token.getPrincipal().getAuthorities()).contains(new SimpleGrantedAuthority("SCOPE_read"));
}
use of org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken in project spring-security by spring-projects.
the class SecurityMockServerConfigurersOidcLoginTests method oidcLoginWhenOidcUserSpecifiedThenLastCalledTakesPrecedence.
// gh-7794
@Test
public void oidcLoginWhenOidcUserSpecifiedThenLastCalledTakesPrecedence() throws Exception {
OidcUser oidcUser = new DefaultOidcUser(AuthorityUtils.createAuthorityList("SCOPE_read"), TestOidcIdTokens.idToken().build());
this.client.mutateWith(SecurityMockServerConfigurers.mockOidcLogin().idToken((i) -> i.subject("foo")).oidcUser(oidcUser)).get().uri("/token").exchange().expectStatus().isOk();
OAuth2AuthenticationToken token = this.controller.token;
assertThat(token.getPrincipal().getAttributes()).containsEntry("sub", "subject");
this.client.mutateWith(SecurityMockServerConfigurers.mockOidcLogin().oidcUser(oidcUser).idToken((i) -> i.subject("bar"))).get().uri("/token").exchange().expectStatus().isOk();
token = this.controller.token;
assertThat(token.getPrincipal().getAttributes()).containsEntry("sub", "bar");
}
use of org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken in project spring-security by spring-projects.
the class SecurityMockServerConfigurersOidcLoginTests method oidcLoginWhenUsingDefaultsThenProducesDefaultAuthentication.
@Test
public void oidcLoginWhenUsingDefaultsThenProducesDefaultAuthentication() {
this.client.mutateWith(SecurityMockServerConfigurers.mockOidcLogin()).get().uri("/token").exchange().expectStatus().isOk();
OAuth2AuthenticationToken token = this.controller.token;
assertThat(token).isNotNull();
assertThat(token.getAuthorizedClientRegistrationId()).isEqualTo("test");
assertThat(token.getPrincipal()).isInstanceOf(OidcUser.class);
assertThat(token.getPrincipal().getAttributes()).containsEntry("sub", "user");
assertThat((Collection<GrantedAuthority>) token.getPrincipal().getAuthorities()).contains(new SimpleGrantedAuthority("SCOPE_read"));
assertThat(((OidcUser) token.getPrincipal()).getIdToken().getTokenValue()).isEqualTo("id-token");
}
Aggregations