Search in sources :

Example 1 with PrincipalMapper

use of io.scalecube.services.auth.PrincipalMapper in project scalecube by scalecube.

the class ServiceMethodInvokerTest method testAuthMethodWhenThereIsContextAndNoAuthenticator.

@Test
@DisplayName("invocation of auth method should return empty response " + "if auth.context exists and no authenticator")
void testAuthMethodWhenThereIsContextAndNoAuthenticator() throws Exception {
    final String methodName = "helloAuthContext";
    final Class<? extends StubService> serviceClass = stubService.getClass();
    final Method method = serviceClass.getMethod(methodName);
    final MethodInfo methodInfo = new MethodInfo(serviceClass.getName(), methodName, method.getReturnType(), IS_RETURN_TYPE_SERVICE_MESSAGE, CommunicationMode.REQUEST_RESPONSE, method.getParameterCount(), Void.TYPE, IS_REQUEST_TYPE_SERVICE_MESSAGE, AUTH);
    serviceMethodInvoker = new ServiceMethodInvoker(method, stubService, methodInfo, DefaultErrorMapper.INSTANCE, dataDecoder, nullAuthenticator, principalMapper);
    ServiceMessage message = ServiceMessage.builder().qualifier(qualifierPrefix + methodName).build();
    StepVerifier.create(Mono.deferContextual(context -> serviceMethodInvoker.invokeOne(message)).contextWrite(context -> context.put(AUTH_CONTEXT_KEY, AUTH_DATA))).verifyComplete();
}
Also used : ArgumentMatchers(org.mockito.ArgumentMatchers) StepVerifier(reactor.test.StepVerifier) Mono(reactor.core.publisher.Mono) AUTH_CONTEXT_KEY(io.scalecube.services.auth.Authenticator.AUTH_CONTEXT_KEY) CommunicationMode(io.scalecube.services.CommunicationMode) Authenticator(io.scalecube.services.auth.Authenticator) DefaultErrorMapper(io.scalecube.services.exceptions.DefaultErrorMapper) DisplayName(org.junit.jupiter.api.DisplayName) PrincipalMapper(io.scalecube.services.auth.PrincipalMapper) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) Flux(reactor.core.publisher.Flux) ServiceMessageDataDecoder(io.scalecube.services.transport.api.ServiceMessageDataDecoder) Map(java.util.Map) Assertions(org.junit.jupiter.api.Assertions) ServiceMessage(io.scalecube.services.api.ServiceMessage) Method(java.lang.reflect.Method) Collections(java.util.Collections) ServiceMessage(io.scalecube.services.api.ServiceMessage) Method(java.lang.reflect.Method) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 2 with PrincipalMapper

use of io.scalecube.services.auth.PrincipalMapper in project scalecube by scalecube.

the class ServiceAuthRemoteTest method beforeAll.

@BeforeAll
static void beforeAll() {
    StepVerifier.setDefaultTimeout(TIMEOUT);
    principalMapper = authData -> new UserProfile(authData.get("name"), authData.get("role"));
    service = Microservices.builder().discovery(serviceEndpoint -> new ScalecubeServiceDiscovery().transport(cfg -> cfg.transportFactory(new WebsocketTransportFactory())).options(opts -> opts.metadata(serviceEndpoint))).transport(() -> new RSocketServiceTransport().authenticator(authenticator)).services(ServiceInfo.fromServiceInstance(new SecuredServiceImpl()).principalMapper(principalMapper).build()).startAwait();
    serviceWithoutAuthenticator = Microservices.builder().discovery(serviceEndpoint -> new ScalecubeServiceDiscovery().transport(cfg -> cfg.transportFactory(new WebsocketTransportFactory())).options(opts -> opts.metadata(serviceEndpoint))).transport(RSocketServiceTransport::new).services(ServiceInfo.fromServiceInstance(new AnotherSecuredServiceImpl()).principalMapper(principalMapper).build()).startAwait();
    partiallySecuredService = Microservices.builder().discovery(serviceEndpoint -> new ScalecubeServiceDiscovery().transport(cfg -> cfg.transportFactory(new WebsocketTransportFactory())).options(opts -> opts.metadata(serviceEndpoint))).transport(() -> new RSocketServiceTransport().authenticator(authenticator)).services(ServiceInfo.fromServiceInstance(new PartiallySecuredServiceImpl()).principalMapper(principalMapper).build()).startAwait();
}
Also used : StepVerifier(reactor.test.StepVerifier) HashMap(java.util.HashMap) RejectedSetupException(io.rsocket.exceptions.RejectedSetupException) PartiallySecuredServiceImpl(io.scalecube.services.sut.security.PartiallySecuredServiceImpl) SecuredServiceImpl(io.scalecube.services.sut.security.SecuredServiceImpl) Authenticator(io.scalecube.services.auth.Authenticator) AfterAll(org.junit.jupiter.api.AfterAll) SecuredService(io.scalecube.services.sut.security.SecuredService) UserProfile(io.scalecube.services.sut.security.UserProfile) BeforeAll(org.junit.jupiter.api.BeforeAll) Duration(java.time.Duration) Map(java.util.Map) PartiallySecuredService(io.scalecube.services.sut.security.PartiallySecuredService) WebsocketTransportFactory(io.scalecube.transport.netty.websocket.WebsocketTransportFactory) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) UnauthorizedException(io.scalecube.services.exceptions.UnauthorizedException) ScalecubeServiceDiscovery(io.scalecube.services.discovery.ScalecubeServiceDiscovery) AnotherSecuredService(io.scalecube.services.sut.security.AnotherSecuredService) Mono(reactor.core.publisher.Mono) AnotherSecuredServiceImpl(io.scalecube.services.sut.security.AnotherSecuredServiceImpl) ServiceDiscovery(io.scalecube.services.discovery.api.ServiceDiscovery) DisplayName(org.junit.jupiter.api.DisplayName) PrincipalMapper(io.scalecube.services.auth.PrincipalMapper) RSocketServiceTransport(io.scalecube.services.transport.rsocket.RSocketServiceTransport) Consumer(java.util.function.Consumer) Test(org.junit.jupiter.api.Test) Collections(java.util.Collections) ScalecubeServiceDiscovery(io.scalecube.services.discovery.ScalecubeServiceDiscovery) UserProfile(io.scalecube.services.sut.security.UserProfile) WebsocketTransportFactory(io.scalecube.transport.netty.websocket.WebsocketTransportFactory) PartiallySecuredServiceImpl(io.scalecube.services.sut.security.PartiallySecuredServiceImpl) SecuredServiceImpl(io.scalecube.services.sut.security.SecuredServiceImpl) AnotherSecuredServiceImpl(io.scalecube.services.sut.security.AnotherSecuredServiceImpl) PartiallySecuredServiceImpl(io.scalecube.services.sut.security.PartiallySecuredServiceImpl) RSocketServiceTransport(io.scalecube.services.transport.rsocket.RSocketServiceTransport) AnotherSecuredServiceImpl(io.scalecube.services.sut.security.AnotherSecuredServiceImpl) BeforeAll(org.junit.jupiter.api.BeforeAll)

Aggregations

Authenticator (io.scalecube.services.auth.Authenticator)2 PrincipalMapper (io.scalecube.services.auth.PrincipalMapper)2 Collections (java.util.Collections)2 Map (java.util.Map)2 DisplayName (org.junit.jupiter.api.DisplayName)2 Test (org.junit.jupiter.api.Test)2 Mono (reactor.core.publisher.Mono)2 StepVerifier (reactor.test.StepVerifier)2 RejectedSetupException (io.rsocket.exceptions.RejectedSetupException)1 CommunicationMode (io.scalecube.services.CommunicationMode)1 ServiceMessage (io.scalecube.services.api.ServiceMessage)1 AUTH_CONTEXT_KEY (io.scalecube.services.auth.Authenticator.AUTH_CONTEXT_KEY)1 ScalecubeServiceDiscovery (io.scalecube.services.discovery.ScalecubeServiceDiscovery)1 ServiceDiscovery (io.scalecube.services.discovery.api.ServiceDiscovery)1 DefaultErrorMapper (io.scalecube.services.exceptions.DefaultErrorMapper)1 UnauthorizedException (io.scalecube.services.exceptions.UnauthorizedException)1 AnotherSecuredService (io.scalecube.services.sut.security.AnotherSecuredService)1 AnotherSecuredServiceImpl (io.scalecube.services.sut.security.AnotherSecuredServiceImpl)1 PartiallySecuredService (io.scalecube.services.sut.security.PartiallySecuredService)1 PartiallySecuredServiceImpl (io.scalecube.services.sut.security.PartiallySecuredServiceImpl)1