Skip to content

Commit 518efa3

Browse files
committed
Moved PrincipalMapper, CredentialsSupplier, Authenticator to ".auth" package
1 parent 2dfc15b commit 518efa3

File tree

22 files changed

+148
-163
lines changed

22 files changed

+148
-163
lines changed

services-api/src/main/java/io/scalecube/services/ServiceInfo.java

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package io.scalecube.services;
22

3-
import io.scalecube.services.auth.Authenticator;
3+
import io.scalecube.services.auth.PrincipalMapper;
44
import io.scalecube.services.exceptions.ServiceProviderErrorMapper;
55
import io.scalecube.services.transport.api.ServiceMessageDataDecoder;
66
import java.util.Collections;
@@ -17,15 +17,15 @@ public class ServiceInfo {
1717
private final Map<String, String> tags;
1818
private final ServiceProviderErrorMapper errorMapper;
1919
private final ServiceMessageDataDecoder dataDecoder;
20-
private final Authenticator authenticator;
20+
private final PrincipalMapper principalMapper;
2121
private final Logger logger;
2222

2323
private ServiceInfo(Builder builder) {
2424
this.serviceInstance = builder.serviceInstance;
2525
this.tags = Collections.unmodifiableMap(new HashMap<>(builder.tags));
2626
this.errorMapper = builder.errorMapper;
2727
this.dataDecoder = builder.dataDecoder;
28-
this.authenticator = builder.authenticator;
28+
this.principalMapper = builder.principalMapper;
2929
this.logger = builder.logger;
3030
}
3131

@@ -53,8 +53,8 @@ public ServiceMessageDataDecoder dataDecoder() {
5353
return dataDecoder;
5454
}
5555

56-
public Authenticator authenticator() {
57-
return authenticator;
56+
public PrincipalMapper principalMapper() {
57+
return principalMapper;
5858
}
5959

6060
public Logger logger() {
@@ -68,7 +68,7 @@ public String toString() {
6868
.add("tags=" + tags)
6969
.add("errorMapper=" + errorMapper)
7070
.add("dataDecoder=" + dataDecoder)
71-
.add("authenticator=" + authenticator)
71+
.add("principalMapper=" + principalMapper)
7272
.add("logger=" + logger)
7373
.toString();
7474
}
@@ -79,15 +79,15 @@ public static class Builder {
7979
private final Map<String, String> tags = new HashMap<>();
8080
private ServiceProviderErrorMapper errorMapper;
8181
private ServiceMessageDataDecoder dataDecoder;
82-
private Authenticator authenticator;
82+
private PrincipalMapper principalMapper;
8383
private Logger logger;
8484

8585
private Builder(ServiceInfo serviceInfo) {
8686
this.serviceInstance = serviceInfo.serviceInstance;
8787
this.tags.putAll(new HashMap<>(serviceInfo.tags));
8888
this.errorMapper = serviceInfo.errorMapper;
8989
this.dataDecoder = serviceInfo.dataDecoder;
90-
this.authenticator = serviceInfo.authenticator;
90+
this.principalMapper = serviceInfo.principalMapper;
9191
this.logger = serviceInfo.logger;
9292
}
9393

@@ -166,13 +166,13 @@ public Builder dataDecoder(ServiceMessageDataDecoder dataDecoder) {
166166
}
167167

168168
/**
169-
* Setter for {@code authenticator}. Overrides default {@code Microservices.authenticator}.
169+
* Setter for {@code principalMapper}. Overrides default {@code Microservices.principalMapper}.
170170
*
171-
* @param authenticator authenticator (optional)
171+
* @param principalMapper principalMapper (optional)
172172
* @return this
173173
*/
174-
public Builder authenticator(Authenticator authenticator) {
175-
this.authenticator = authenticator;
174+
public Builder principalMapper(PrincipalMapper principalMapper) {
175+
this.principalMapper = principalMapper;
176176
return this;
177177
}
178178

@@ -190,9 +190,9 @@ Builder dataDecoderIfAbsent(ServiceMessageDataDecoder dataDecoder) {
190190
return this;
191191
}
192192

193-
Builder authenticatorIfAbsent(Authenticator authenticator) {
194-
if (this.authenticator == null) {
195-
return authenticator(authenticator);
193+
Builder principalMapperIfAbsent(PrincipalMapper principalMapper) {
194+
if (this.principalMapper == null) {
195+
return principalMapper(principalMapper);
196196
}
197197
return this;
198198
}
Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
package io.scalecube.services.auth;
22

3-
import io.scalecube.services.RequestContext;
43
import reactor.core.publisher.Mono;
54

5+
/**
6+
* Service authentication interface to handle authentication of clients to the service. Result of
7+
* authentication is abstract {@link Principal} with role and permissions.
8+
*/
69
@FunctionalInterface
710
public interface Authenticator {
811

9-
Mono<Principal> authenticate(RequestContext requestContext);
12+
/**
13+
* Authenticates service clients by given credentials.
14+
*
15+
* @param credentials credentials
16+
* @return result
17+
*/
18+
Mono<Principal> authenticate(byte[] credentials);
1019
}
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 reactor.core.publisher.Mono;
4+
5+
/**
6+
* Supplier of credentials for authentication with remote service. Implementations can use {@code
7+
* byte[0]} to denote empty credentials.
8+
*/
9+
@FunctionalInterface
10+
public interface CredentialsSupplier {
11+
12+
/**
13+
* Obtains credentials for the given service role.
14+
*
15+
* @param serviceRole serviceRole
16+
* @return credentials
17+
*/
18+
Mono<byte[]> credentials(String serviceRole);
19+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package io.scalecube.services.auth;
2+
3+
import io.scalecube.services.RequestContext;
4+
import reactor.core.publisher.Mono;
5+
6+
@FunctionalInterface
7+
public interface PrincipalMapper {
8+
9+
Mono<Principal> map(RequestContext requestContext);
10+
}

services-api/src/main/java/io/scalecube/services/auth/ServiceRolesProcessor.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* Handler for processing of service roles which come out of registered services. Used as
88
* post-construction step in bootstraping of services.
99
*/
10+
@FunctionalInterface
1011
public interface ServiceRolesProcessor {
1112

1213
/**

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

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
import io.scalecube.services.CommunicationMode;
44
import io.scalecube.services.RequestContext;
55
import io.scalecube.services.api.ServiceMessage;
6-
import io.scalecube.services.auth.Authenticator;
76
import io.scalecube.services.auth.Principal;
7+
import io.scalecube.services.auth.PrincipalMapper;
88
import io.scalecube.services.exceptions.BadRequestException;
9+
import io.scalecube.services.exceptions.ForbiddenException;
910
import io.scalecube.services.exceptions.ServiceException;
1011
import io.scalecube.services.exceptions.ServiceProviderErrorMapper;
11-
import io.scalecube.services.exceptions.UnauthorizedException;
1212
import io.scalecube.services.transport.api.ServiceMessageDataDecoder;
1313
import java.lang.reflect.InvocationTargetException;
1414
import java.lang.reflect.Method;
@@ -27,7 +27,7 @@ public class ServiceMethodInvoker {
2727
private final MethodInfo methodInfo;
2828
private final ServiceProviderErrorMapper errorMapper;
2929
private final ServiceMessageDataDecoder dataDecoder;
30-
private final Authenticator authenticator;
30+
private final PrincipalMapper principalMapper;
3131
private final Logger logger;
3232

3333
public ServiceMethodInvoker(
@@ -36,14 +36,14 @@ public ServiceMethodInvoker(
3636
MethodInfo methodInfo,
3737
ServiceProviderErrorMapper errorMapper,
3838
ServiceMessageDataDecoder dataDecoder,
39-
Authenticator authenticator,
39+
PrincipalMapper principalMapper,
4040
Logger logger) {
4141
this.method = Objects.requireNonNull(method, "method");
4242
this.service = Objects.requireNonNull(service, "service");
4343
this.methodInfo = Objects.requireNonNull(methodInfo, "methodInfo");
4444
this.errorMapper = Objects.requireNonNull(errorMapper, "errorMapper");
4545
this.dataDecoder = Objects.requireNonNull(dataDecoder, "dataDecoder");
46-
this.authenticator = authenticator;
46+
this.principalMapper = principalMapper;
4747
this.logger = logger;
4848
}
4949

@@ -58,7 +58,7 @@ public Mono<ServiceMessage> invokeOne(ServiceMessage message) {
5858
.flatMap(
5959
context -> {
6060
final var request = toRequest(message);
61-
return authenticate(context)
61+
return mapPrincipal(context)
6262
.flatMap(
6363
principal ->
6464
Mono.defer(() -> Mono.from(invokeRequest(request)))
@@ -99,7 +99,7 @@ public Flux<ServiceMessage> invokeMany(ServiceMessage message) {
9999
.flatMapMany(
100100
context -> {
101101
final var request = toRequest(message);
102-
return authenticate(context)
102+
return mapPrincipal(context)
103103
.flatMapMany(
104104
principal ->
105105
Flux.defer(() -> Flux.from(invokeRequest(request)))
@@ -230,34 +230,34 @@ public MethodInfo methodInfo() {
230230
return methodInfo;
231231
}
232232

233-
public Authenticator authenticator() {
234-
return authenticator;
233+
public PrincipalMapper principalMapper() {
234+
return principalMapper;
235235
}
236236

237-
private Mono<Principal> authenticate(RequestContext context) {
237+
private Mono<Principal> mapPrincipal(RequestContext context) {
238238
if (!methodInfo.isSecured()) {
239239
return Mono.just(context.principal());
240240
}
241241

242-
if (authenticator == null) {
242+
if (principalMapper == null) {
243243
if (context.hasPrincipal()) {
244244
return Mono.just(context.principal());
245245
} else {
246-
throw new UnauthorizedException("Authentication failed");
246+
throw new ForbiddenException("Insufficient permissions");
247247
}
248248
}
249249

250-
return authenticator
251-
.authenticate(context)
250+
return principalMapper
251+
.map(context)
252252
.switchIfEmpty(Mono.just(context.principal()))
253-
.onErrorMap(ServiceMethodInvoker::toUnauthorizedException);
253+
.onErrorMap(ServiceMethodInvoker::toForbiddenException);
254254
}
255255

256-
private static UnauthorizedException toUnauthorizedException(Throwable ex) {
256+
private static ForbiddenException toForbiddenException(Throwable ex) {
257257
if (ex instanceof ServiceException e) {
258-
return new UnauthorizedException(e.errorCode(), e.getMessage());
258+
return new ForbiddenException(e.errorCode(), e.getMessage());
259259
} else {
260-
return new UnauthorizedException(ex);
260+
return new ForbiddenException(ex);
261261
}
262262
}
263263
}

services-api/src/main/java/io/scalecube/services/routing/StaticAddressRouter.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@
55
import io.scalecube.services.ServiceReference;
66
import io.scalecube.services.ServiceRegistration;
77
import io.scalecube.services.api.ServiceMessage;
8+
import io.scalecube.services.auth.CredentialsSupplier;
89
import io.scalecube.services.methods.ServiceMethodDefinition;
910
import io.scalecube.services.registry.api.ServiceRegistry;
10-
import io.scalecube.services.transport.api.ClientTransport;
11-
import io.scalecube.services.transport.api.ClientTransport.CredentialsSupplier;
1211
import java.util.Collections;
1312
import java.util.List;
1413
import java.util.Objects;
@@ -70,10 +69,9 @@ public Builder address(Address address) {
7069
}
7170

7271
/**
73-
* Setter for whether to apply behavior of {@link ClientTransport.CredentialsSupplier}, or not.
74-
* If it is known upfront that destination service is secured, then set this flag to {@code
75-
* true}, in such case {@link CredentialsSupplier#credentials(ServiceReference, String)} will be
76-
* invoked.
72+
* Setter for whether to apply behavior of {@link CredentialsSupplier}, or not. If it is known
73+
* upfront that destination service is secured, then set this flag to {@code true}, in such case
74+
* {@link CredentialsSupplier#credentials(String)} will be invoked.
7775
*
7876
* @param secured secured flag
7977
* @return this
@@ -85,7 +83,7 @@ public Builder secured(boolean secured) {
8583

8684
/**
8785
* Setter for {@code serviceRole} property, will be used in the invocation of {@link
88-
* CredentialsSupplier#credentials(ServiceReference, String)}.
86+
* CredentialsSupplier#credentials(String)}.
8987
*
9088
* @param serviceRole serviceRole
9189
* @return this
Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package io.scalecube.services.transport.api;
22

33
import io.scalecube.services.ServiceReference;
4-
import reactor.core.publisher.Mono;
54

65
public interface ClientTransport extends AutoCloseable {
76

@@ -12,21 +11,4 @@ public interface ClientTransport extends AutoCloseable {
1211
* @return {@link ClientChannel} instance
1312
*/
1413
ClientChannel create(ServiceReference serviceReference);
15-
16-
/**
17-
* Supplier of credentials for authentication on the {@link ServerTransport}. Being used in the
18-
* connection setup phase with remote {@link ServerTransport}.
19-
*/
20-
@FunctionalInterface
21-
interface CredentialsSupplier {
22-
23-
/**
24-
* Obtains credentials for the given {@code serviceReference}.
25-
*
26-
* @param serviceReference target serviceReference
27-
* @param serviceRole target serviceRole
28-
* @return result
29-
*/
30-
Mono<byte[]> credentials(ServiceReference serviceReference, String serviceRole);
31-
}
3214
}
Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package io.scalecube.services.transport.api;
22

33
import io.scalecube.services.Address;
4-
import io.scalecube.services.auth.Principal;
5-
import reactor.core.publisher.Mono;
64

75
public interface ServerTransport {
86

@@ -22,20 +20,4 @@ public interface ServerTransport {
2220

2321
/** Stops this instance and release allocated resources. */
2422
void stop();
25-
26-
/**
27-
* Authentication interface to handle clients that being connected from remote {@link
28-
* ClientTransport} instances.
29-
*/
30-
@FunctionalInterface
31-
interface Authenticator {
32-
33-
/**
34-
* Authenticates service transport connection by given credentials.
35-
*
36-
* @param credentials credentials
37-
* @return result
38-
*/
39-
Mono<Principal> authenticate(byte[] credentials);
40-
}
4123
}

0 commit comments

Comments
 (0)