Search in sources :

Example 56 with OAuth2AuthorizedClient

use of org.springframework.security.oauth2.client.OAuth2AuthorizedClient in project spring-security by spring-projects.

the class SecurityMockServerConfigurersOidcLoginTests method oidcLoginWhenUsingDefaultsThenProducesDefaultAuthorizedClient.

@Test
public void oidcLoginWhenUsingDefaultsThenProducesDefaultAuthorizedClient() {
    this.client.mutateWith(SecurityMockServerConfigurers.mockOidcLogin()).get().uri("/client").exchange().expectStatus().isOk();
    OAuth2AuthorizedClient client = this.controller.authorizedClient;
    assertThat(client).isNotNull();
    assertThat(client.getClientRegistration().getRegistrationId()).isEqualTo("test");
    assertThat(client.getAccessToken().getTokenValue()).isEqualTo("access-token");
    assertThat(client.getRefreshToken()).isNull();
}
Also used : RegisteredOAuth2AuthorizedClient(org.springframework.security.oauth2.client.annotation.RegisteredOAuth2AuthorizedClient) OAuth2AuthorizedClient(org.springframework.security.oauth2.client.OAuth2AuthorizedClient) Test(org.junit.jupiter.api.Test)

Example 57 with OAuth2AuthorizedClient

use of org.springframework.security.oauth2.client.OAuth2AuthorizedClient in project spring-security by spring-projects.

the class SecurityMockServerConfigurersOAuth2LoginTests method oauth2LoginWhenUsingDefaultsThenProducesDefaultAuthorizedClient.

@Test
public void oauth2LoginWhenUsingDefaultsThenProducesDefaultAuthorizedClient() {
    this.client.mutateWith(SecurityMockServerConfigurers.mockOAuth2Login()).get().uri("/client").exchange().expectStatus().isOk();
    OAuth2AuthorizedClient client = this.controller.authorizedClient;
    assertThat(client).isNotNull();
    assertThat(client.getClientRegistration().getRegistrationId()).isEqualTo("test");
    assertThat(client.getAccessToken().getTokenValue()).isEqualTo("access-token");
    assertThat(client.getRefreshToken()).isNull();
}
Also used : RegisteredOAuth2AuthorizedClient(org.springframework.security.oauth2.client.annotation.RegisteredOAuth2AuthorizedClient) OAuth2AuthorizedClient(org.springframework.security.oauth2.client.OAuth2AuthorizedClient) Test(org.junit.jupiter.api.Test)

Example 58 with OAuth2AuthorizedClient

use of org.springframework.security.oauth2.client.OAuth2AuthorizedClient in project spring-security by spring-projects.

the class DefaultReactiveOAuth2AuthorizedClientManagerTests method setup.

@SuppressWarnings("unchecked")
@BeforeEach
public void setup() {
    this.clientRegistrationRepository = mock(ReactiveClientRegistrationRepository.class);
    given(this.clientRegistrationRepository.findByRegistrationId(anyString())).willReturn(Mono.empty());
    this.authorizedClientRepository = mock(ServerOAuth2AuthorizedClientRepository.class);
    this.loadAuthorizedClientProbe = PublisherProbe.empty();
    given(this.authorizedClientRepository.loadAuthorizedClient(anyString(), any(Authentication.class), any(ServerWebExchange.class))).willReturn(this.loadAuthorizedClientProbe.mono());
    this.saveAuthorizedClientProbe = PublisherProbe.empty();
    given(this.authorizedClientRepository.saveAuthorizedClient(any(OAuth2AuthorizedClient.class), any(Authentication.class), any(ServerWebExchange.class))).willReturn(this.saveAuthorizedClientProbe.mono());
    this.removeAuthorizedClientProbe = PublisherProbe.empty();
    given(this.authorizedClientRepository.removeAuthorizedClient(any(String.class), any(Authentication.class), any(ServerWebExchange.class))).willReturn(this.removeAuthorizedClientProbe.mono());
    this.authorizedClientProvider = mock(ReactiveOAuth2AuthorizedClientProvider.class);
    given(this.authorizedClientProvider.authorize(any(OAuth2AuthorizationContext.class))).willReturn(Mono.empty());
    this.contextAttributesMapper = mock(Function.class);
    given(this.contextAttributesMapper.apply(any())).willReturn(Mono.just(Collections.emptyMap()));
    this.authorizedClientManager = new DefaultReactiveOAuth2AuthorizedClientManager(this.clientRegistrationRepository, this.authorizedClientRepository);
    this.authorizedClientManager.setAuthorizedClientProvider(this.authorizedClientProvider);
    this.authorizedClientManager.setContextAttributesMapper(this.contextAttributesMapper);
    this.clientRegistration = TestClientRegistrations.clientRegistration().build();
    this.principal = new TestingAuthenticationToken("principal", "password");
    this.authorizedClient = new OAuth2AuthorizedClient(this.clientRegistration, this.principal.getName(), TestOAuth2AccessTokens.scopes("read", "write"), TestOAuth2RefreshTokens.refreshToken());
    this.serverWebExchange = MockServerWebExchange.builder(MockServerHttpRequest.get("/")).build();
    this.context = Context.of(ServerWebExchange.class, this.serverWebExchange);
    this.authorizationContextCaptor = ArgumentCaptor.forClass(OAuth2AuthorizationContext.class);
}
Also used : ServerWebExchange(org.springframework.web.server.ServerWebExchange) MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) Function(java.util.function.Function) ReactiveClientRegistrationRepository(org.springframework.security.oauth2.client.registration.ReactiveClientRegistrationRepository) ServerOAuth2AuthorizedClientRepository(org.springframework.security.oauth2.client.web.server.ServerOAuth2AuthorizedClientRepository) OAuth2AuthorizationContext(org.springframework.security.oauth2.client.OAuth2AuthorizationContext) Authentication(org.springframework.security.core.Authentication) OAuth2AuthorizedClient(org.springframework.security.oauth2.client.OAuth2AuthorizedClient) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) TestingAuthenticationToken(org.springframework.security.authentication.TestingAuthenticationToken) ReactiveOAuth2AuthorizedClientProvider(org.springframework.security.oauth2.client.ReactiveOAuth2AuthorizedClientProvider) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 59 with OAuth2AuthorizedClient

use of org.springframework.security.oauth2.client.OAuth2AuthorizedClient in project spring-security by spring-projects.

the class OAuth2AuthorizedClientMixinTests method deserializeWhenMixinNotRegisteredThenThrowJsonProcessingException.

@Test
public void deserializeWhenMixinNotRegisteredThenThrowJsonProcessingException() {
    OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient(this.clientRegistrationBuilder.build(), this.principalName, this.accessToken);
    String json = asJson(authorizedClient);
    assertThatExceptionOfType(JsonProcessingException.class).isThrownBy(() -> new ObjectMapper().readValue(json, OAuth2AuthorizedClient.class));
}
Also used : OAuth2AuthorizedClient(org.springframework.security.oauth2.client.OAuth2AuthorizedClient) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.jupiter.api.Test)

Example 60 with OAuth2AuthorizedClient

use of org.springframework.security.oauth2.client.OAuth2AuthorizedClient in project spring-security by spring-projects.

the class OAuth2AuthorizedClientMixinTests method serializeWhenMixinRegisteredThenSerializes.

@Test
public void serializeWhenMixinRegisteredThenSerializes() throws Exception {
    OAuth2AuthorizedClient authorizedClient = new OAuth2AuthorizedClient(this.clientRegistrationBuilder.build(), this.principalName, this.accessToken, this.refreshToken);
    String expectedJson = asJson(authorizedClient);
    String json = this.mapper.writeValueAsString(authorizedClient);
    JSONAssert.assertEquals(expectedJson, json, true);
}
Also used : OAuth2AuthorizedClient(org.springframework.security.oauth2.client.OAuth2AuthorizedClient) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)140 OAuth2AuthorizedClient (org.springframework.security.oauth2.client.OAuth2AuthorizedClient)123 OAuth2AccessToken (org.springframework.security.oauth2.core.OAuth2AccessToken)66 ClientRegistration (org.springframework.security.oauth2.client.registration.ClientRegistration)51 OAuth2AccessTokenResponse (org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse)45 Instant (java.time.Instant)43 Authentication (org.springframework.security.core.Authentication)41 TestingAuthenticationToken (org.springframework.security.authentication.TestingAuthenticationToken)36 ClientRequest (org.springframework.web.reactive.function.client.ClientRequest)34 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)32 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)31 OAuth2RefreshToken (org.springframework.security.oauth2.core.OAuth2RefreshToken)31 BeforeEach (org.junit.jupiter.api.BeforeEach)28 OAuth2AuthorizationContext (org.springframework.security.oauth2.client.OAuth2AuthorizationContext)23 Map (java.util.Map)21 HashMap (java.util.HashMap)20 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)19 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)17 Assertions.assertThatExceptionOfType (org.assertj.core.api.Assertions.assertThatExceptionOfType)17 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)17