use of org.springframework.security.oauth2.client.OAuth2AuthorizedClient in project spring-security by spring-projects.
the class SecurityMockServerConfigurersOAuth2ClientTests method oauth2ClientWhenClientRegistrationThenUses.
@Test
public void oauth2ClientWhenClientRegistrationThenUses() throws Exception {
ClientRegistration clientRegistration = TestClientRegistrations.clientRegistration().registrationId("registration-id").clientId("client-id").build();
this.client.mutateWith(SecurityMockServerConfigurers.mockOAuth2Client().clientRegistration(clientRegistration)).get().uri("/client").exchange().expectStatus().isOk();
OAuth2AuthorizedClient client = this.controller.authorizedClient;
assertThat(client).isNotNull();
assertThat(client.getClientRegistration().getRegistrationId()).isEqualTo("registration-id");
assertThat(client.getAccessToken().getTokenValue()).isEqualTo("access-token");
assertThat(client.getRefreshToken()).isNull();
}
use of org.springframework.security.oauth2.client.OAuth2AuthorizedClient in project spring-security by spring-projects.
the class SecurityMockServerConfigurersOAuth2ClientTests method oauth2ClientWhenUsedOnceThenDoesNotAffectRemainingTests.
@Test
public void oauth2ClientWhenUsedOnceThenDoesNotAffectRemainingTests() throws Exception {
this.client.mutateWith(SecurityMockServerConfigurers.mockOAuth2Client("registration-id")).get().uri("/client").exchange().expectStatus().isOk();
OAuth2AuthorizedClient client = this.controller.authorizedClient;
assertThat(client).isNotNull();
assertThat(client.getClientRegistration().getClientId()).isEqualTo("test-client");
client = new OAuth2AuthorizedClient(TestClientRegistrations.clientRegistration().build(), "sub", TestOAuth2AccessTokens.noScopes());
given(this.authorizedClientRepository.loadAuthorizedClient(eq("registration-id"), any(Authentication.class), any(ServerWebExchange.class))).willReturn(Mono.just(client));
this.client.get().uri("/client").exchange().expectStatus().isOk();
client = this.controller.authorizedClient;
assertThat(client).isNotNull();
assertThat(client.getClientRegistration().getClientId()).isEqualTo("client-id");
verify(this.authorizedClientRepository).loadAuthorizedClient(eq("registration-id"), any(Authentication.class), any(ServerWebExchange.class));
}
use of org.springframework.security.oauth2.client.OAuth2AuthorizedClient in project spring-security by spring-projects.
the class SecurityMockServerConfigurersOAuth2ClientTests method oauth2ClientWhenUsingRegistrationIdThenProducesAuthorizedClient.
@Test
public void oauth2ClientWhenUsingRegistrationIdThenProducesAuthorizedClient() throws Exception {
this.client.mutateWith(SecurityMockServerConfigurers.mockOAuth2Client("registration-id")).get().uri("/client").exchange().expectStatus().isOk();
OAuth2AuthorizedClient client = this.controller.authorizedClient;
assertThat(client).isNotNull();
assertThat(client.getClientRegistration().getRegistrationId()).isEqualTo("registration-id");
assertThat(client.getAccessToken().getTokenValue()).isEqualTo("access-token");
assertThat(client.getRefreshToken()).isNull();
}
use of org.springframework.security.oauth2.client.OAuth2AuthorizedClient in project spring-security by spring-projects.
the class SecurityMockServerConfigurersOAuth2ClientTests method oauth2ClientWhenClientRegistrationConsumerThenUses.
@Test
public void oauth2ClientWhenClientRegistrationConsumerThenUses() throws Exception {
this.client.mutateWith(SecurityMockServerConfigurers.mockOAuth2Client("registration-id").clientRegistration((c) -> c.clientId("client-id"))).get().uri("/client").exchange().expectStatus().isOk();
OAuth2AuthorizedClient client = this.controller.authorizedClient;
assertThat(client).isNotNull();
assertThat(client.getClientRegistration().getRegistrationId()).isEqualTo("registration-id");
assertThat(client.getClientRegistration().getClientId()).isEqualTo("client-id");
assertThat(client.getAccessToken().getTokenValue()).isEqualTo("access-token");
assertThat(client.getRefreshToken()).isNull();
}
use of org.springframework.security.oauth2.client.OAuth2AuthorizedClient 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");
}
Aggregations