Search in sources :

Example 61 with Mono

use of reactor.core.publisher.Mono in project spring-security by spring-projects.

the class WebClientReactiveJwtBearerTokenResponseClientTests method getTokenResponseWhenBodyExtractorSetThenCalled.

@Test
public void getTokenResponseWhenBodyExtractorSetThenCalled() {
    BodyExtractor<Mono<OAuth2AccessTokenResponse>, ReactiveHttpInputMessage> bodyExtractor = mock(BodyExtractor.class);
    OAuth2AccessTokenResponse response = TestOAuth2AccessTokenResponses.accessTokenResponse().build();
    given(bodyExtractor.extract(any(), any())).willReturn(Mono.just(response));
    ClientRegistration clientRegistration = this.clientRegistration.build();
    JwtBearerGrantRequest request = new JwtBearerGrantRequest(clientRegistration, this.jwtAssertion);
    this.client.setBodyExtractor(bodyExtractor);
    enqueueJson(DEFAULT_ACCESS_TOKEN_RESPONSE);
    this.client.getTokenResponse(request).block();
    verify(bodyExtractor).extract(any(), any());
}
Also used : OAuth2AccessTokenResponse(org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse) ClientRegistration(org.springframework.security.oauth2.client.registration.ClientRegistration) Mono(reactor.core.publisher.Mono) ReactiveHttpInputMessage(org.springframework.http.ReactiveHttpInputMessage) Test(org.junit.jupiter.api.Test)

Example 62 with Mono

use of reactor.core.publisher.Mono in project spring-security by spring-projects.

the class WebClientReactiveAuthorizationCodeTokenResponseClientTests method getTokenResponseWhenSuccessCustomResponseThenReturnAccessTokenResponse.

// gh-10260
@Test
public void getTokenResponseWhenSuccessCustomResponseThenReturnAccessTokenResponse() {
    String accessTokenSuccessResponse = "{}";
    WebClientReactiveAuthorizationCodeTokenResponseClient customClient = new WebClientReactiveAuthorizationCodeTokenResponseClient();
    BodyExtractor<Mono<OAuth2AccessTokenResponse>, ReactiveHttpInputMessage> extractor = mock(BodyExtractor.class);
    OAuth2AccessTokenResponse response = TestOAuth2AccessTokenResponses.accessTokenResponse().build();
    given(extractor.extract(any(), any())).willReturn(Mono.just(response));
    customClient.setBodyExtractor(extractor);
    this.server.enqueue(jsonResponse(accessTokenSuccessResponse));
    OAuth2AccessTokenResponse accessTokenResponse = customClient.getTokenResponse(authorizationCodeGrantRequest()).block();
    assertThat(accessTokenResponse.getAccessToken()).isNotNull();
}
Also used : OAuth2AccessTokenResponse(org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse) Mono(reactor.core.publisher.Mono) ReactiveHttpInputMessage(org.springframework.http.ReactiveHttpInputMessage) Test(org.junit.jupiter.api.Test)

Example 63 with Mono

use of reactor.core.publisher.Mono in project spring-security by spring-projects.

the class AuthorizationWebFilterTests method filterWhenNoAuthenticationThenThrowsAccessDenied.

@Test
public void filterWhenNoAuthenticationThenThrowsAccessDenied() {
    given(this.chain.filter(this.exchange)).willReturn(this.chainResult.mono());
    AuthorizationWebFilter filter = new AuthorizationWebFilter((a, e) -> a.flatMap((auth) -> Mono.error(new AccessDeniedException("Denied"))));
    Mono<Void> result = filter.filter(this.exchange, this.chain).subscriberContext(ReactiveSecurityContextHolder.withSecurityContext(Mono.just(new SecurityContextImpl())));
    StepVerifier.create(result).expectError(AccessDeniedException.class).verify();
    this.chainResult.assertWasNotSubscribed();
}
Also used : MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) StepVerifier(reactor.test.StepVerifier) TestingAuthenticationToken(org.springframework.security.authentication.TestingAuthenticationToken) Mock(org.mockito.Mock) SecurityContextImpl(org.springframework.security.core.context.SecurityContextImpl) Mono(reactor.core.publisher.Mono) AccessDeniedException(org.springframework.security.access.AccessDeniedException) PublisherProbe(reactor.test.publisher.PublisherProbe) AuthorizationDecision(org.springframework.security.authorization.AuthorizationDecision) ServerWebExchange(org.springframework.web.server.ServerWebExchange) Test(org.junit.jupiter.api.Test) ReactiveSecurityContextHolder(org.springframework.security.core.context.ReactiveSecurityContextHolder) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) SecurityContext(org.springframework.security.core.context.SecurityContext) BDDMockito.given(org.mockito.BDDMockito.given) WebFilterChain(org.springframework.web.server.WebFilterChain) AccessDeniedException(org.springframework.security.access.AccessDeniedException) SecurityContextImpl(org.springframework.security.core.context.SecurityContextImpl) Test(org.junit.jupiter.api.Test)

Example 64 with Mono

use of reactor.core.publisher.Mono in project spring-security by spring-projects.

the class SecurityContextServerWebExchangeWebFilterTests method filterWhenExistingContextAndPrincipalNotNullThenContextPopulated.

@Test
public void filterWhenExistingContextAndPrincipalNotNullThenContextPopulated() {
    Mono<Void> result = this.filter.filter(this.exchange, new DefaultWebFilterChain((e) -> e.getPrincipal().doOnSuccess((contextPrincipal) -> assertThat(contextPrincipal).isEqualTo(this.principal)).flatMap((contextPrincipal) -> Mono.subscriberContext()).doOnSuccess((context) -> assertThat(context.<String>get("foo")).isEqualTo("bar")).then(), Collections.emptyList())).subscriberContext((context) -> context.put("foo", "bar")).subscriberContext(ReactiveSecurityContextHolder.withAuthentication(this.principal));
    StepVerifier.create(result).verifyComplete();
}
Also used : Test(org.junit.jupiter.api.Test) StepVerifier(reactor.test.StepVerifier) ReactiveSecurityContextHolder(org.springframework.security.core.context.ReactiveSecurityContextHolder) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) TestingAuthenticationToken(org.springframework.security.authentication.TestingAuthenticationToken) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) DefaultWebFilterChain(org.springframework.web.server.handler.DefaultWebFilterChain) Mono(reactor.core.publisher.Mono) MockServerWebExchange(org.springframework.mock.web.server.MockServerWebExchange) Authentication(org.springframework.security.core.Authentication) Collections(java.util.Collections) ServerWebExchange(org.springframework.web.server.ServerWebExchange) DefaultWebFilterChain(org.springframework.web.server.handler.DefaultWebFilterChain) Test(org.junit.jupiter.api.Test)

Example 65 with Mono

use of reactor.core.publisher.Mono in project spring-security by spring-projects.

the class CurrentSecurityContextArgumentResolverTests method metaAnnotationWhenCurrentAuthenticationThenInjectAuthentication.

@Test
public void metaAnnotationWhenCurrentAuthenticationThenInjectAuthentication() {
    MethodParameter parameter = ResolvableMethod.on(getClass()).named("currentAuthentication").build().arg(Mono.class, Authentication.class);
    Authentication auth = buildAuthenticationWithPrincipal("current_authentication");
    Context context = ReactiveSecurityContextHolder.withAuthentication(auth);
    Mono<Object> argument = this.resolver.resolveArgument(parameter, this.bindingContext, this.exchange);
    Authentication authentication = (Authentication) argument.subscriberContext(context).cast(Mono.class).block().block();
    assertThat(authentication).isSameAs(auth);
    ReactiveSecurityContextHolder.clearContext();
}
Also used : BindingContext(org.springframework.web.reactive.BindingContext) CurrentSecurityContext(org.springframework.security.core.annotation.CurrentSecurityContext) Context(reactor.util.context.Context) SecurityContext(org.springframework.security.core.context.SecurityContext) Authentication(org.springframework.security.core.Authentication) Mono(reactor.core.publisher.Mono) MethodParameter(org.springframework.core.MethodParameter) Test(org.junit.jupiter.api.Test)

Aggregations

Mono (reactor.core.publisher.Mono)308 Test (org.junit.jupiter.api.Test)143 StepVerifier (reactor.test.StepVerifier)117 List (java.util.List)116 Flux (reactor.core.publisher.Flux)110 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)102 Collections (java.util.Collections)85 Map (java.util.Map)75 HttpStatus (org.springframework.http.HttpStatus)66 ServerWebExchange (org.springframework.web.server.ServerWebExchange)65 Duration (java.time.Duration)60 ArrayList (java.util.ArrayList)58 Test (org.junit.Test)53 MockServerHttpRequest (org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest)52 DataBuffer (org.springframework.core.io.buffer.DataBuffer)51 Assert (org.springframework.util.Assert)49 Mockito.mock (org.mockito.Mockito.mock)48 MockServerWebExchange (org.springframework.web.testfixture.server.MockServerWebExchange)46 Optional (java.util.Optional)44 StandardCharsets (java.nio.charset.StandardCharsets)43