Search in sources :

Example 21 with MockServerHttpRequest

use of org.springframework.mock.http.server.reactive.MockServerHttpRequest in project spring-security by spring-projects.

the class OAuth2AuthorizationCodeGrantWebFilterTests method filterWhenAuthenticationManagerThrowsOAuth2AuthorizationExceptionThenMappedToOAuth2AuthenticationException.

// gh-8609
@Test
public void filterWhenAuthenticationManagerThrowsOAuth2AuthorizationExceptionThenMappedToOAuth2AuthenticationException() {
    ClientRegistration clientRegistration = TestClientRegistrations.clientRegistration().build();
    given(this.clientRegistrationRepository.findByRegistrationId(any())).willReturn(Mono.just(clientRegistration));
    MockServerHttpRequest authorizationRequest = createAuthorizationRequest("/authorization/callback");
    OAuth2AuthorizationRequest oauth2AuthorizationRequest = createOAuth2AuthorizationRequest(authorizationRequest, clientRegistration);
    given(this.authorizationRequestRepository.loadAuthorizationRequest(any())).willReturn(Mono.just(oauth2AuthorizationRequest));
    given(this.authorizationRequestRepository.removeAuthorizationRequest(any())).willReturn(Mono.just(oauth2AuthorizationRequest));
    given(this.authenticationManager.authenticate(any())).willReturn(Mono.error(new OAuth2AuthorizationException(new OAuth2Error("authorization_error"))));
    MockServerHttpRequest authorizationResponse = createAuthorizationResponse(authorizationRequest);
    MockServerWebExchange exchange = MockServerWebExchange.from(authorizationResponse);
    DefaultWebFilterChain chain = new DefaultWebFilterChain((e) -> e.getResponse().setComplete(), Collections.emptyList());
    assertThatExceptionOfType(OAuth2AuthenticationException.class).isThrownBy(() -> this.filter.filter(exchange, chain).block()).satisfies((ex) -> assertThat(ex.getError()).extracting("errorCode").isEqualTo("authorization_error"));
}
Also used : OAuth2AuthorizationException(org.springframework.security.oauth2.core.OAuth2AuthorizationException) ClientRegistration(org.springframework.security.oauth2.client.registration.ClientRegistration) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) OAuth2Error(org.springframework.security.oauth2.core.OAuth2Error) MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) DefaultWebFilterChain(org.springframework.web.server.handler.DefaultWebFilterChain) OAuth2AuthorizationRequest(org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest) Test(org.junit.jupiter.api.Test)

Example 22 with MockServerHttpRequest

use of org.springframework.mock.http.server.reactive.MockServerHttpRequest in project spring-security by spring-projects.

the class OAuth2AuthorizationCodeGrantWebFilterTests method filterWhenMatchThenAuthorizedClientSaved.

@Test
public void filterWhenMatchThenAuthorizedClientSaved() {
    ClientRegistration clientRegistration = TestClientRegistrations.clientRegistration().build();
    given(this.clientRegistrationRepository.findByRegistrationId(any())).willReturn(Mono.just(clientRegistration));
    MockServerHttpRequest authorizationRequest = createAuthorizationRequest("/authorization/callback");
    OAuth2AuthorizationRequest oauth2AuthorizationRequest = createOAuth2AuthorizationRequest(authorizationRequest, clientRegistration);
    given(this.authorizationRequestRepository.loadAuthorizationRequest(any())).willReturn(Mono.just(oauth2AuthorizationRequest));
    given(this.authorizationRequestRepository.removeAuthorizationRequest(any())).willReturn(Mono.just(oauth2AuthorizationRequest));
    given(this.authorizedClientRepository.saveAuthorizedClient(any(), any(), any())).willReturn(Mono.empty());
    given(this.authenticationManager.authenticate(any())).willReturn(Mono.just(TestOAuth2AuthorizationCodeAuthenticationTokens.authenticated()));
    MockServerHttpRequest authorizationResponse = createAuthorizationResponse(authorizationRequest);
    MockServerWebExchange exchange = MockServerWebExchange.from(authorizationResponse);
    DefaultWebFilterChain chain = new DefaultWebFilterChain((e) -> e.getResponse().setComplete(), Collections.emptyList());
    this.filter.filter(exchange, chain).block();
    verify(this.authorizedClientRepository).saveAuthorizedClient(any(), any(AnonymousAuthenticationToken.class), any());
}
Also used : ClientRegistration(org.springframework.security.oauth2.client.registration.ClientRegistration) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) DefaultWebFilterChain(org.springframework.web.server.handler.DefaultWebFilterChain) AnonymousAuthenticationToken(org.springframework.security.authentication.AnonymousAuthenticationToken) OAuth2AuthorizationRequest(org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest) Test(org.junit.jupiter.api.Test)

Example 23 with MockServerHttpRequest

use of org.springframework.mock.http.server.reactive.MockServerHttpRequest in project spring-security by spring-projects.

the class ServerWebExchangeDelegatingReactiveAuthenticationManagerResolverTests method resolveWhenDoesNotMatchThenReturnsDefaultReactiveAuthenticationManager.

@Test
public void resolveWhenDoesNotMatchThenReturnsDefaultReactiveAuthenticationManager() {
    ServerWebExchangeDelegatingReactiveAuthenticationManagerResolver resolver = ServerWebExchangeDelegatingReactiveAuthenticationManagerResolver.builder().add(new PathPatternParserServerWebExchangeMatcher("/one/**"), this.one).add(new PathPatternParserServerWebExchangeMatcher("/two/**"), this.two).build();
    MockServerHttpRequest request = MockServerHttpRequest.get("/wrong/location").build();
    ReactiveAuthenticationManager authenticationManager = resolver.resolve(MockServerWebExchange.from(request)).block();
    Authentication authentication = new TestingAuthenticationToken("principal", "creds");
    assertThatExceptionOfType(AuthenticationServiceException.class).isThrownBy(() -> authenticationManager.authenticate(authentication).block());
}
Also used : PathPatternParserServerWebExchangeMatcher(org.springframework.security.web.server.util.matcher.PathPatternParserServerWebExchangeMatcher) ReactiveAuthenticationManager(org.springframework.security.authentication.ReactiveAuthenticationManager) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) Authentication(org.springframework.security.core.Authentication) TestingAuthenticationToken(org.springframework.security.authentication.TestingAuthenticationToken) AuthenticationServiceException(org.springframework.security.authentication.AuthenticationServiceException) Test(org.junit.jupiter.api.Test)

Example 24 with MockServerHttpRequest

use of org.springframework.mock.http.server.reactive.MockServerHttpRequest in project spring-security by spring-projects.

the class AuthenticationConverterServerWebExchangeMatcherTests method setup.

@BeforeEach
public void setup() {
    MockServerHttpRequest request = MockServerHttpRequest.get("/path").build();
    this.exchange = MockServerWebExchange.from(request);
    this.matcher = new AuthenticationConverterServerWebExchangeMatcher(this.converter);
}
Also used : MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 25 with MockServerHttpRequest

use of org.springframework.mock.http.server.reactive.MockServerHttpRequest in project spring-security by spring-projects.

the class PathMatcherServerWebExchangeMatcherTests method setup.

@BeforeEach
public void setup() {
    MockServerHttpRequest request = MockServerHttpRequest.post("/path").build();
    MockServerHttpResponse response = new MockServerHttpResponse();
    DefaultWebSessionManager sessionManager = new DefaultWebSessionManager();
    this.exchange = MockServerWebExchange.from(request);
    this.path = "/path";
    this.matcher = new PathPatternParserServerWebExchangeMatcher(this.pattern);
}
Also used : MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) DefaultWebSessionManager(org.springframework.web.server.session.DefaultWebSessionManager) MockServerHttpResponse(org.springframework.mock.http.server.reactive.MockServerHttpResponse) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

MockServerHttpRequest (org.springframework.mock.http.server.reactive.MockServerHttpRequest)75 Test (org.junit.jupiter.api.Test)40 MockServerWebExchange (org.springframework.mock.web.server.MockServerWebExchange)35 Test (org.junit.Test)26 ServerWebExchange (org.springframework.web.server.ServerWebExchange)26 URI (java.net.URI)16 OAuth2AuthorizationRequest (org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest)11 HttpHeaders (org.springframework.http.HttpHeaders)9 BeforeEach (org.junit.jupiter.api.BeforeEach)7 ResponseStatusException (org.springframework.web.server.ResponseStatusException)7 AcceptHeaderLocaleContextResolver (org.springframework.web.server.i18n.AcceptHeaderLocaleContextResolver)6 Mono (reactor.core.publisher.Mono)6 InetSocketAddress (java.net.InetSocketAddress)5 Assertions.assertThatIllegalStateException (org.assertj.core.api.Assertions.assertThatIllegalStateException)5 Mockito.mock (org.mockito.Mockito.mock)5 ObjectError (org.springframework.validation.ObjectError)5 HashMap (java.util.HashMap)4 Map (java.util.Map)4 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)4 BDDMockito.given (org.mockito.BDDMockito.given)4