Search in sources :

Example 1 with UsernamePasswordMetadata

use of org.springframework.security.rsocket.metadata.UsernamePasswordMetadata in project spring-security by spring-projects.

the class RSocketMessageHandlerConnectionITests method routeWhenNotAuthorized.

@Test
public void routeWhenNotAuthorized() {
    UsernamePasswordMetadata credentials = new UsernamePasswordMetadata("user", "password");
    // @formatter:off
    this.requester = requester().setupMetadata(credentials, UsernamePasswordMetadata.BASIC_AUTHENTICATION_MIME_TYPE).connectTcp(this.server.address().getHostName(), this.server.address().getPort()).block();
    assertThatExceptionOfType(ApplicationErrorException.class).isThrownBy(() -> this.requester.route("secure.admin.retrieve-mono").data("data").retrieveMono(String.class).block());
// @formatter:on
}
Also used : UsernamePasswordMetadata(org.springframework.security.rsocket.metadata.UsernamePasswordMetadata) ApplicationErrorException(io.rsocket.exceptions.ApplicationErrorException) Test(org.junit.jupiter.api.Test)

Example 2 with UsernamePasswordMetadata

use of org.springframework.security.rsocket.metadata.UsernamePasswordMetadata in project spring-security by spring-projects.

the class RSocketMessageHandlerConnectionITests method connectWhenNotAuthorized.

@Test
public void connectWhenNotAuthorized() {
    UsernamePasswordMetadata credentials = new UsernamePasswordMetadata("evil", "password");
    // @formatter:off
    this.requester = requester().setupMetadata(credentials, UsernamePasswordMetadata.BASIC_AUTHENTICATION_MIME_TYPE).connectTcp(this.server.address().getHostName(), this.server.address().getPort()).block();
    assertThatExceptionOfType(Exception.class).isThrownBy(() -> this.requester.route("retrieve-mono").data("data").retrieveMono(String.class).block()).matches((ex) -> ex instanceof RejectedSetupException || ex.getClass().toString().contains("ReactiveException"));
// @formatter:on
// FIXME: https://github.com/rsocket/rsocket-java/issues/686
}
Also used : RejectedSetupException(io.rsocket.exceptions.RejectedSetupException) UsernamePasswordMetadata(org.springframework.security.rsocket.metadata.UsernamePasswordMetadata) Test(org.junit.jupiter.api.Test)

Example 3 with UsernamePasswordMetadata

use of org.springframework.security.rsocket.metadata.UsernamePasswordMetadata in project spring-security by spring-projects.

the class RSocketMessageHandlerConnectionITests method connectWithAnyAuthority.

@Test
public void connectWithAnyAuthority() {
    UsernamePasswordMetadata credentials = new UsernamePasswordMetadata("admin", "password");
    // @formatter:off
    this.requester = requester().setupMetadata(credentials, UsernamePasswordMetadata.BASIC_AUTHENTICATION_MIME_TYPE).connectTcp(this.server.address().getHostName(), this.server.address().getPort()).block();
    String hiEbert = this.requester.route("management.users").data("admin").retrieveMono(String.class).block();
    // @formatter:on
    assertThat(hiEbert).isEqualTo("Hi admin");
}
Also used : UsernamePasswordMetadata(org.springframework.security.rsocket.metadata.UsernamePasswordMetadata) Test(org.junit.jupiter.api.Test)

Example 4 with UsernamePasswordMetadata

use of org.springframework.security.rsocket.metadata.UsernamePasswordMetadata in project spring-security by spring-projects.

the class SimpleAuthenticationITests method retrieveMonoWhenAuthorizedThenGranted.

@Test
public void retrieveMonoWhenAuthorizedThenGranted() {
    MimeType authenticationMimeType = MimeTypeUtils.parseMimeType(WellKnownMimeType.MESSAGE_RSOCKET_AUTHENTICATION.getString());
    UsernamePasswordMetadata credentials = new UsernamePasswordMetadata("rob", "password");
    // @formatter:off
    this.requester = RSocketRequester.builder().setupMetadata(credentials, authenticationMimeType).rsocketStrategies(this.handler.getRSocketStrategies()).connectTcp("localhost", this.server.address().getPort()).block();
    // @formatter:on
    String data = "rob";
    // @formatter:off
    String hiRob = this.requester.route("secure.retrieve-mono").metadata(credentials, authenticationMimeType).data(data).retrieveMono(String.class).block();
    // @formatter:on
    assertThat(hiRob).isEqualTo("Hi rob");
    assertThat(this.controller.payloads).containsOnly(data);
}
Also used : UsernamePasswordMetadata(org.springframework.security.rsocket.metadata.UsernamePasswordMetadata) MimeType(org.springframework.util.MimeType) WellKnownMimeType(io.rsocket.metadata.WellKnownMimeType) Test(org.junit.jupiter.api.Test)

Example 5 with UsernamePasswordMetadata

use of org.springframework.security.rsocket.metadata.UsernamePasswordMetadata in project spring-security by spring-projects.

the class AuthenticationPayloadInterceptorTests method createRequestPayload.

private Payload createRequestPayload() {
    UsernamePasswordMetadata credentials = new UsernamePasswordMetadata("user", "password");
    BasicAuthenticationEncoder encoder = new BasicAuthenticationEncoder();
    DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
    ResolvableType elementType = ResolvableType.forClass(UsernamePasswordMetadata.class);
    MimeType mimeType = UsernamePasswordMetadata.BASIC_AUTHENTICATION_MIME_TYPE;
    Map<String, Object> hints = null;
    DataBuffer dataBuffer = encoder.encodeValue(credentials, factory, elementType, mimeType, hints);
    ByteBufAllocator allocator = ByteBufAllocator.DEFAULT;
    CompositeByteBuf metadata = allocator.compositeBuffer();
    CompositeMetadataCodec.encodeAndAddMetadata(metadata, allocator, mimeType.toString(), NettyDataBufferFactory.toByteBuf(dataBuffer));
    return DefaultPayload.create(allocator.buffer(), metadata);
}
Also used : CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBufAllocator(io.netty.buffer.ByteBufAllocator) UsernamePasswordMetadata(org.springframework.security.rsocket.metadata.UsernamePasswordMetadata) BasicAuthenticationEncoder(org.springframework.security.rsocket.metadata.BasicAuthenticationEncoder) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) ResolvableType(org.springframework.core.ResolvableType) MimeType(org.springframework.util.MimeType) WellKnownMimeType(io.rsocket.metadata.WellKnownMimeType) DataBuffer(org.springframework.core.io.buffer.DataBuffer)

Aggregations

UsernamePasswordMetadata (org.springframework.security.rsocket.metadata.UsernamePasswordMetadata)14 Test (org.junit.jupiter.api.Test)13 WellKnownMimeType (io.rsocket.metadata.WellKnownMimeType)3 ApplicationErrorException (io.rsocket.exceptions.ApplicationErrorException)2 MimeType (org.springframework.util.MimeType)2 ByteBufAllocator (io.netty.buffer.ByteBufAllocator)1 CompositeByteBuf (io.netty.buffer.CompositeByteBuf)1 RejectedSetupException (io.rsocket.exceptions.RejectedSetupException)1 Assertions (org.assertj.core.api.Assertions)1 Autowired (org.springframework.beans.factory.annotation.Autowired)1 LocalRSocketServerPort (org.springframework.boot.rsocket.context.LocalRSocketServerPort)1 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)1 ResolvableType (org.springframework.core.ResolvableType)1 DataBuffer (org.springframework.core.io.buffer.DataBuffer)1 DefaultDataBufferFactory (org.springframework.core.io.buffer.DefaultDataBufferFactory)1 RSocketRequester (org.springframework.messaging.rsocket.RSocketRequester)1 BasicAuthenticationEncoder (org.springframework.security.rsocket.metadata.BasicAuthenticationEncoder)1 SimpleAuthenticationEncoder (org.springframework.security.rsocket.metadata.SimpleAuthenticationEncoder)1 MimeTypeUtils (org.springframework.util.MimeTypeUtils)1 Mono (reactor.core.publisher.Mono)1