Skip to content

Commit 932ad34

Browse files
committed
Added default authenticator; cleanup in test services
1 parent 63f5591 commit 932ad34

File tree

5 files changed

+26
-8
lines changed

5 files changed

+26
-8
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package io.scalecube.services.auth;
2+
3+
import io.scalecube.services.exceptions.UnauthorizedException;
4+
import java.util.Map;
5+
import reactor.core.publisher.Mono;
6+
7+
public final class DelegatingAuthenticator implements Authenticator<Object> {
8+
9+
@Override
10+
public Mono<Object> authenticate(Map<String, String> credentials) {
11+
return Mono.deferWithContext(
12+
context -> {
13+
if (!context.hasKey(Authenticator.AUTH_CONTEXT_KEY)) {
14+
throw new UnauthorizedException("Authentication failed (auth context not found)");
15+
}
16+
return context.get(Authenticator.AUTH_CONTEXT_KEY);
17+
});
18+
}
19+
}

services-api/src/main/java/io/scalecube/services/methods/ServiceMethodInvoker.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,6 @@ private Mono<Object> authenticate(ServiceMessage message, Context context) {
157157
if (context.hasKey(Authenticator.AUTH_CONTEXT_KEY)) {
158158
return Mono.just(context.get(Authenticator.AUTH_CONTEXT_KEY));
159159
}
160-
if (authenticator == null) {
161-
throw new UnauthorizedException("Authenticator not found");
162-
}
163160
return authenticator.authenticate(message.headers()).onErrorMap(this::toUnauthorizedException);
164161
}
165162

services/src/main/java/io/scalecube/services/Microservices.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import io.scalecube.net.Address;
44
import io.scalecube.services.api.ServiceMessage;
55
import io.scalecube.services.auth.Authenticator;
6+
import io.scalecube.services.auth.DelegatingAuthenticator;
67
import io.scalecube.services.auth.PrincipalMapper;
78
import io.scalecube.services.discovery.api.ServiceDiscovery;
89
import io.scalecube.services.discovery.api.ServiceDiscoveryEvent;
@@ -314,7 +315,7 @@ public static final class Builder {
314315
private List<ServiceProvider> serviceProviders = new ArrayList<>();
315316
private ServiceRegistry serviceRegistry = new ServiceRegistryImpl();
316317
private ServiceMethodRegistry methodRegistry = new ServiceMethodRegistryImpl();
317-
private Authenticator<Object> authenticator = null;
318+
private Authenticator<Object> authenticator = new DelegatingAuthenticator();
318319
private ServiceDiscoveryBootstrap discoveryBootstrap = new ServiceDiscoveryBootstrap();
319320
private ServiceTransportBootstrap transportBootstrap = new ServiceTransportBootstrap();
320321
private GatewayBootstrap gatewayBootstrap = new GatewayBootstrap();

services/src/test/java/io/scalecube/services/ServiceAuthRemoteTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ final class ServiceAuthRemoteTest extends BaseTest {
4747
return Mono.just(authData);
4848
}
4949

50-
return Mono.error(new UnauthorizedException("Authentication failed"));
50+
return Mono.error(
51+
new UnauthorizedException("Authentication failed (username or password incorrect)"));
5152
};
5253

5354
private static Microservices caller;

services/src/test/java/io/scalecube/services/sut/GreetingServiceImpl.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import io.scalecube.services.Microservices;
44
import io.scalecube.services.annotations.Inject;
55
import io.scalecube.services.api.ServiceMessage;
6-
import io.scalecube.services.exceptions.UnauthorizedException;
6+
import io.scalecube.services.exceptions.ForbiddenException;
77
import java.util.stream.LongStream;
88
import org.reactivestreams.Publisher;
99
import reactor.core.publisher.Flux;
@@ -41,7 +41,7 @@ public Mono<GreetingResponse> greetingPojo(GreetingRequest name) {
4141

4242
@Override
4343
public Mono<GreetingResponse> greetingNotAuthorized(GreetingRequest name) {
44-
return Mono.error(new UnauthorizedException(500, "Not authorized"));
44+
return Mono.error(new ForbiddenException("Not authorized"));
4545
}
4646

4747
@Override
@@ -80,7 +80,7 @@ public Flux<GreetingResponse> bidiGreeting(Publisher<GreetingRequest> request) {
8080

8181
@Override
8282
public Flux<GreetingResponse> bidiGreetingNotAuthorized(Flux<GreetingRequest> request) {
83-
return Flux.error(new UnauthorizedException(500, "Not authorized"));
83+
return Flux.error(new ForbiddenException("Not authorized"));
8484
}
8585

8686
@Override

0 commit comments

Comments
 (0)