|
| 1 | +package io.scalecube.services.security; |
| 2 | + |
| 3 | +import io.scalecube.services.auth.Authenticator; |
| 4 | +import java.util.Map; |
| 5 | +import org.jctools.maps.NonBlockingHashMapLong; |
| 6 | +import reactor.core.publisher.Mono; |
| 7 | + |
| 8 | +/** |
| 9 | + * Implementation of {@link Authenticator} which works on top of existing {@code authenticator}. |
| 10 | + * Internally maintains a map of service claims where key is some id (of type {@code long}) and |
| 11 | + * value is {@link ServiceClaims} object. |
| 12 | + * |
| 13 | + * @see #saveAuthData(long, ServiceClaims) |
| 14 | + * @see #getAuthData(long) |
| 15 | + * @see #removeAuthData(long) |
| 16 | + */ |
| 17 | +public final class CompositeAuthenticator implements Authenticator<ServiceClaims> { |
| 18 | + |
| 19 | + private final Authenticator<ServiceClaims> authenticator; |
| 20 | + |
| 21 | + private final Map<Long, ServiceClaims> registry = new NonBlockingHashMapLong<>(); |
| 22 | + |
| 23 | + public CompositeAuthenticator(Authenticator<ServiceClaims> authenticator) { |
| 24 | + this.authenticator = authenticator; |
| 25 | + } |
| 26 | + |
| 27 | + @Override |
| 28 | + public Mono<ServiceClaims> apply(Map<String, String> credentials) { |
| 29 | + return authenticator.apply(credentials); |
| 30 | + } |
| 31 | + |
| 32 | + public void saveAuthData(long id, ServiceClaims serviceClaims) { |
| 33 | + registry.put(id, serviceClaims); |
| 34 | + } |
| 35 | + |
| 36 | + public ServiceClaims getAuthData(long id) { |
| 37 | + return registry.get(id); |
| 38 | + } |
| 39 | + |
| 40 | + public void removeAuthData(long id) { |
| 41 | + registry.remove(id); |
| 42 | + } |
| 43 | + |
| 44 | + public boolean containsAuthData(long id) { |
| 45 | + return registry.containsKey(id); |
| 46 | + } |
| 47 | +} |
0 commit comments