Skip to content

Commit fa6e4f8

Browse files
authored
Seal AuthToken interface (#1389)
Driver effectively does not expect external implementations of `AuthToken` interface. This update makes it more explicit.
1 parent c0737be commit fa6e4f8

File tree

3 files changed

+5
-15
lines changed

3 files changed

+5
-15
lines changed

driver/src/main/java/org/neo4j/driver/AuthToken.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
*/
1919
package org.neo4j.driver;
2020

21+
import org.neo4j.driver.internal.security.InternalAuthToken;
22+
2123
/**
2224
* Token for holding authentication details, such as <em>user name</em> and <em>password</em>.
2325
* Such a token is required by a {@link Driver} to authenticate with a Neo4j
@@ -27,4 +29,4 @@
2729
* @see GraphDatabase#driver(String, AuthToken)
2830
* @since 1.0
2931
*/
30-
public interface AuthToken {}
32+
public sealed interface AuthToken permits InternalAuthToken {}

driver/src/main/java/org/neo4j/driver/internal/async/connection/ChannelConnectorImpl.java

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,12 @@
3131
import java.net.SocketAddress;
3232
import java.time.Clock;
3333
import org.neo4j.driver.AuthToken;
34-
import org.neo4j.driver.AuthTokens;
3534
import org.neo4j.driver.Logging;
36-
import org.neo4j.driver.exceptions.ClientException;
3735
import org.neo4j.driver.internal.BoltServerAddress;
3836
import org.neo4j.driver.internal.ConnectionSettings;
3937
import org.neo4j.driver.internal.DomainNameResolver;
4038
import org.neo4j.driver.internal.async.inbound.ConnectTimeoutHandler;
4139
import org.neo4j.driver.internal.cluster.RoutingContext;
42-
import org.neo4j.driver.internal.security.InternalAuthToken;
4340
import org.neo4j.driver.internal.security.SecurityPlan;
4441

4542
public class ChannelConnectorImpl implements ChannelConnector {
@@ -80,7 +77,7 @@ public ChannelConnectorImpl(
8077
RoutingContext routingContext,
8178
DomainNameResolver domainNameResolver) {
8279
this.userAgent = connectionSettings.userAgent();
83-
this.authToken = requireValidAuthToken(connectionSettings.authToken());
80+
this.authToken = connectionSettings.authToken();
8481
this.routingContext = routingContext;
8582
this.connectTimeoutMillis = connectionSettings.connectTimeoutMillis();
8683
this.securityPlan = requireNonNull(securityPlan);
@@ -143,13 +140,4 @@ private void installHandshakeCompletedListeners(
143140
handshakeCompleted.addListener(
144141
new HandshakeCompletedListener(userAgent, authToken, routingContext, connectionInitialized));
145142
}
146-
147-
private static AuthToken requireValidAuthToken(AuthToken token) {
148-
if (token instanceof InternalAuthToken) {
149-
return token;
150-
} else {
151-
throw new ClientException("Unknown authentication token, `" + token + "`. Please use one of the supported "
152-
+ "tokens from `" + AuthTokens.class.getSimpleName() + "`.");
153-
}
154-
}
155143
}

driver/src/main/java/org/neo4j/driver/internal/security/InternalAuthToken.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
* A simple common token for authentication schemes that easily convert to
2727
* an auth token map
2828
*/
29-
public class InternalAuthToken implements AuthToken {
29+
public final class InternalAuthToken implements AuthToken {
3030
public static final String SCHEME_KEY = "scheme";
3131
public static final String PRINCIPAL_KEY = "principal";
3232
public static final String CREDENTIALS_KEY = "credentials";

0 commit comments

Comments
 (0)