Search in sources :

Example 6 with Authenticator

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

the class PrincipalMapperAuthExample 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(ServiceInfo.fromServiceInstance(new SecuredServiceByApiKeyImpl()).principalMapper(PrincipalMapperAuthExample::apiKeyPrincipalMapper).build()).services(ServiceInfo.fromServiceInstance(new SecuredServiceByUserProfileImpl()).principalMapper(PrincipalMapperAuthExample::userProfilePrincipalMapper).build()).startAwait();
    Microservices userProfileCaller = Microservices.builder().discovery(endpoint -> discovery(service, endpoint)).transport(() -> new RSocketServiceTransport().credentialsSupplier(credentialsSupplier())).startAwait();
    String responseByUserProfile = userProfileCaller.call().api(SecuredServiceByUserProfile.class).hello(UUID.randomUUID().toString()).block(Duration.ofSeconds(3));
    System.err.println("### Received 'userProfileCaller' response: " + responseByUserProfile);
    Microservices apiKeyCaller = Microservices.builder().discovery(endpoint -> discovery(service, endpoint)).transport(() -> new RSocketServiceTransport().credentialsSupplier(credentialsSupplier())).startAwait();
    String responseByApiKey = apiKeyCaller.call().api(SecuredServiceByApiKey.class).hello(UUID.randomUUID().toString()).block(Duration.ofSeconds(3));
    System.err.println("### Received 'apiKeyCaller' response: " + responseByApiKey);
}
Also used : ScalecubeServiceDiscovery(io.scalecube.services.discovery.ScalecubeServiceDiscovery) ServiceInfo(io.scalecube.services.ServiceInfo) HashMap(java.util.HashMap) Mono(reactor.core.publisher.Mono) UUID(java.util.UUID) 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) Map(java.util.Map) WebsocketTransportFactory(io.scalecube.transport.netty.websocket.WebsocketTransportFactory) UnauthorizedException(io.scalecube.services.exceptions.UnauthorizedException) 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 7 with Authenticator

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

the class ServiceMethodInvokerTest method testAuthMethodWhenNoContextAndNoAuthenticator.

@Test
@DisplayName("invocation of auth method should return error " + "if there're no auth.context and no authenticator")
void testAuthMethodWhenNoContextAndNoAuthenticator() 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, nullPrincipalMapper);
    ServiceMessage message = ServiceMessage.builder().qualifier(qualifierPrefix + methodName).build();
    // invokeOne
    final Mono<ServiceMessage> invokeOne = serviceMethodInvoker.invokeOne(message);
    StepVerifier.create(invokeOne).assertNext(serviceMessage -> Assertions.assertTrue(serviceMessage.isError())).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)

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