Search in sources :

Example 1 with PayloadInterceptor

use of org.springframework.security.rsocket.api.PayloadInterceptor in project spring-security by spring-projects.

the class PayloadSocketAcceptorTests method acceptWhenDelegateAcceptRequiresReactiveSecurityContext.

@Test
public // gh-8654
void acceptWhenDelegateAcceptRequiresReactiveSecurityContext() {
    given(this.setupPayload.metadataMimeType()).willReturn(MediaType.TEXT_PLAIN_VALUE);
    given(this.setupPayload.dataMimeType()).willReturn(MediaType.APPLICATION_JSON_VALUE);
    SecurityContext expectedSecurityContext = new SecurityContextImpl(new TestingAuthenticationToken("user", "password", "ROLE_USER"));
    CaptureSecurityContextSocketAcceptor captureSecurityContext = new CaptureSecurityContextSocketAcceptor(this.rSocket);
    PayloadInterceptor authenticateInterceptor = (exchange, chain) -> {
        Context withSecurityContext = ReactiveSecurityContextHolder.withSecurityContext(Mono.just(expectedSecurityContext));
        return chain.next(exchange).subscriberContext(withSecurityContext);
    };
    List<PayloadInterceptor> interceptors = Arrays.asList(authenticateInterceptor);
    this.acceptor = new PayloadSocketAcceptor(captureSecurityContext, interceptors);
    this.acceptor.accept(this.setupPayload, this.rSocket).block();
    assertThat(captureSecurityContext.getSecurityContext()).isEqualTo(expectedSecurityContext);
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) TestingAuthenticationToken(org.springframework.security.authentication.TestingAuthenticationToken) PayloadInterceptor(org.springframework.security.rsocket.api.PayloadInterceptor) Mock(org.mockito.Mock) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) WellKnownMimeType(io.rsocket.metadata.WellKnownMimeType) ArgumentCaptor(org.mockito.ArgumentCaptor) ReactiveSecurityContextHolder(org.springframework.security.core.context.ReactiveSecurityContextHolder) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) RSocket(io.rsocket.RSocket) BDDMockito.given(org.mockito.BDDMockito.given) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) SecurityContextImpl(org.springframework.security.core.context.SecurityContextImpl) Context(reactor.util.context.Context) MediaType(org.springframework.http.MediaType) Mono(reactor.core.publisher.Mono) Mockito.times(org.mockito.Mockito.times) SocketAcceptor(io.rsocket.SocketAcceptor) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) PayloadExchange(org.springframework.security.rsocket.api.PayloadExchange) List(java.util.List) Payload(io.rsocket.Payload) SecurityContext(org.springframework.security.core.context.SecurityContext) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) ConnectionSetupPayload(io.rsocket.ConnectionSetupPayload) Collections(java.util.Collections) Context(reactor.util.context.Context) SecurityContext(org.springframework.security.core.context.SecurityContext) SecurityContextImpl(org.springframework.security.core.context.SecurityContextImpl) PayloadInterceptor(org.springframework.security.rsocket.api.PayloadInterceptor) SecurityContext(org.springframework.security.core.context.SecurityContext) TestingAuthenticationToken(org.springframework.security.authentication.TestingAuthenticationToken) Test(org.junit.jupiter.api.Test)

Aggregations

ConnectionSetupPayload (io.rsocket.ConnectionSetupPayload)1 Payload (io.rsocket.Payload)1 RSocket (io.rsocket.RSocket)1 SocketAcceptor (io.rsocket.SocketAcceptor)1 WellKnownMimeType (io.rsocket.metadata.WellKnownMimeType)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 List (java.util.List)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 Assertions.assertThatIllegalArgumentException (org.assertj.core.api.Assertions.assertThatIllegalArgumentException)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1 Test (org.junit.jupiter.api.Test)1 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)1 ArgumentCaptor (org.mockito.ArgumentCaptor)1 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)1 BDDMockito.given (org.mockito.BDDMockito.given)1 Mock (org.mockito.Mock)1 Mockito.times (org.mockito.Mockito.times)1 Mockito.verify (org.mockito.Mockito.verify)1 MockitoExtension (org.mockito.junit.jupiter.MockitoExtension)1