Search in sources :

Example 1 with Authenticator

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

the class CompositeProfileAuthExample method main.

/**
 * Main program.
 *
 * @param args arguments
 */
public static void main(String[] args) {
    Microservices service = Microservices.builder().discovery(serviceEndpoint -> new ScalecubeServiceDiscovery().transport(cfg -> cfg.transportFactory(new WebsocketTransportFactory())).options(opts -> opts.metadata(serviceEndpoint))).transport(() -> new RSocketServiceTransport().authenticator(authenticator())).services(call -> Collections.singletonList(ServiceInfo.fromServiceInstance(new SecuredServiceByCompositeProfileImpl()).authenticator(compositeAuthenticator()).build())).startAwait();
    Microservices caller = Microservices.builder().discovery(endpoint -> discovery(service, endpoint)).transport(() -> new RSocketServiceTransport().credentialsSupplier(credentialsSupplier())).startAwait();
    ServiceMessage response = caller.call().requestOne(ServiceMessage.builder().qualifier("securedServiceByCompositeProfile/hello").header("userProfile.name", "SEGA").header("userProfile.role", "ADMIN").data("hello world").build(), String.class).block(Duration.ofSeconds(3));
    System.err.println("### Received 'caller' response: " + response.data());
}
Also used : ScalecubeServiceDiscovery(io.scalecube.services.discovery.ScalecubeServiceDiscovery) ServiceInfo(io.scalecube.services.ServiceInfo) Mono(reactor.core.publisher.Mono) Authenticator(io.scalecube.services.auth.Authenticator) ServiceEndpoint(io.scalecube.services.ServiceEndpoint) RSocketServiceTransport(io.scalecube.services.transport.rsocket.RSocketServiceTransport) Microservices(io.scalecube.services.Microservices) CredentialsSupplier(io.scalecube.services.auth.CredentialsSupplier) Duration(java.time.Duration) WebsocketTransportFactory(io.scalecube.transport.netty.websocket.WebsocketTransportFactory) ServiceMessage(io.scalecube.services.api.ServiceMessage) Collections(java.util.Collections) UnauthorizedException(io.scalecube.services.exceptions.UnauthorizedException) MonoAuthUtil.deferWithPrincipal(io.scalecube.services.auth.MonoAuthUtil.deferWithPrincipal) ScalecubeServiceDiscovery(io.scalecube.services.discovery.ScalecubeServiceDiscovery) ServiceMessage(io.scalecube.services.api.ServiceMessage) WebsocketTransportFactory(io.scalecube.transport.netty.websocket.WebsocketTransportFactory) RSocketServiceTransport(io.scalecube.services.transport.rsocket.RSocketServiceTransport) Microservices(io.scalecube.services.Microservices)

Example 2 with Authenticator

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

the class ServiceTransportAuthExample method main.

/**
 * Main program.
 *
 * @param args arguments
 */
public static void main(String[] args) {
    Microservices service = Microservices.builder().discovery(serviceEndpoint -> new ScalecubeServiceDiscovery().transport(cfg -> cfg.transportFactory(new WebsocketTransportFactory())).options(opts -> opts.metadata(serviceEndpoint))).transport(() -> new RSocketServiceTransport().authenticator(authenticator())).services(new SecuredServiceByUserProfileImpl()).startAwait();
    Microservices caller = Microservices.builder().discovery(endpoint -> discovery(service, endpoint)).transport(() -> new RSocketServiceTransport().credentialsSupplier(credentialsSupplier())).startAwait();
    String response = caller.call().api(SecuredServiceByUserProfile.class).hello(UUID.randomUUID().toString()).block(Duration.ofSeconds(3));
    System.err.println("### Received 'caller' response: " + response);
}
Also used : ServiceEndpoint(io.scalecube.services.ServiceEndpoint) RSocketServiceTransport(io.scalecube.services.transport.rsocket.RSocketServiceTransport) ScalecubeServiceDiscovery(io.scalecube.services.discovery.ScalecubeServiceDiscovery) Microservices(io.scalecube.services.Microservices) CredentialsSupplier(io.scalecube.services.auth.CredentialsSupplier) Duration(java.time.Duration) WebsocketTransportFactory(io.scalecube.transport.netty.websocket.WebsocketTransportFactory) HashMap(java.util.HashMap) Mono(reactor.core.publisher.Mono) UUID(java.util.UUID) UnauthorizedException(io.scalecube.services.exceptions.UnauthorizedException) Authenticator(io.scalecube.services.auth.Authenticator) ScalecubeServiceDiscovery(io.scalecube.services.discovery.ScalecubeServiceDiscovery) WebsocketTransportFactory(io.scalecube.transport.netty.websocket.WebsocketTransportFactory) RSocketServiceTransport(io.scalecube.services.transport.rsocket.RSocketServiceTransport) Microservices(io.scalecube.services.Microservices)

Example 3 with Authenticator

use of io.scalecube.services.auth.Authenticator 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 4 with Authenticator

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

the class ServiceAuthRemoteTest method failedAuthenticationWhenAuthenticatorNotProvided.

@Test
@DisplayName("Authentication failed if authenticator not provided")
void failedAuthenticationWhenAuthenticatorNotProvided() {
    Microservices caller = newCaller();
    try {
        AnotherSecuredService securedService = caller.call().api(AnotherSecuredService.class);
        Consumer<Throwable> verifyError = th -> {
            assertEquals(UnauthorizedException.class, th.getClass());
            assertEquals("Authentication failed", th.getMessage());
        };
        StepVerifier.create(securedService.helloWithRequest("Bob")).expectErrorSatisfies(verifyError).verify();
        StepVerifier.create(securedService.helloWithPrincipal()).expectErrorSatisfies(verifyError).verify();
        StepVerifier.create(securedService.helloWithRequestAndPrincipal("Bob")).expectErrorSatisfies(verifyError).verify();
    } finally {
        caller.shutdown().block(TIMEOUT);
    }
}
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) UnauthorizedException(io.scalecube.services.exceptions.UnauthorizedException) AnotherSecuredService(io.scalecube.services.sut.security.AnotherSecuredService) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 5 with Authenticator

use of io.scalecube.services.auth.Authenticator 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)7 Mono (reactor.core.publisher.Mono)7 ScalecubeServiceDiscovery (io.scalecube.services.discovery.ScalecubeServiceDiscovery)5 UnauthorizedException (io.scalecube.services.exceptions.UnauthorizedException)5 RSocketServiceTransport (io.scalecube.services.transport.rsocket.RSocketServiceTransport)5 WebsocketTransportFactory (io.scalecube.transport.netty.websocket.WebsocketTransportFactory)5 Duration (java.time.Duration)5 Collections (java.util.Collections)5 Map (java.util.Map)5 PrincipalMapper (io.scalecube.services.auth.PrincipalMapper)4 HashMap (java.util.HashMap)4 DisplayName (org.junit.jupiter.api.DisplayName)4 Test (org.junit.jupiter.api.Test)4 StepVerifier (reactor.test.StepVerifier)4 Microservices (io.scalecube.services.Microservices)3 ServiceEndpoint (io.scalecube.services.ServiceEndpoint)3 ServiceMessage (io.scalecube.services.api.ServiceMessage)3 CredentialsSupplier (io.scalecube.services.auth.CredentialsSupplier)3 RejectedSetupException (io.rsocket.exceptions.RejectedSetupException)2 CommunicationMode (io.scalecube.services.CommunicationMode)2