Skip to content

Seal AuthToken interface #1389

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion driver/src/main/java/org/neo4j/driver/AuthToken.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*/
package org.neo4j.driver;

import org.neo4j.driver.internal.security.InternalAuthToken;

/**
* Token for holding authentication details, such as <em>user name</em> and <em>password</em>.
* Such a token is required by a {@link Driver} to authenticate with a Neo4j
Expand All @@ -27,4 +29,4 @@
* @see GraphDatabase#driver(String, AuthToken)
* @since 1.0
*/
public interface AuthToken {}
public sealed interface AuthToken permits InternalAuthToken {}
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,12 @@
import java.net.SocketAddress;
import java.time.Clock;
import org.neo4j.driver.AuthToken;
import org.neo4j.driver.AuthTokens;
import org.neo4j.driver.Logging;
import org.neo4j.driver.exceptions.ClientException;
import org.neo4j.driver.internal.BoltServerAddress;
import org.neo4j.driver.internal.ConnectionSettings;
import org.neo4j.driver.internal.DomainNameResolver;
import org.neo4j.driver.internal.async.inbound.ConnectTimeoutHandler;
import org.neo4j.driver.internal.cluster.RoutingContext;
import org.neo4j.driver.internal.security.InternalAuthToken;
import org.neo4j.driver.internal.security.SecurityPlan;

public class ChannelConnectorImpl implements ChannelConnector {
Expand Down Expand Up @@ -80,7 +77,7 @@ public ChannelConnectorImpl(
RoutingContext routingContext,
DomainNameResolver domainNameResolver) {
this.userAgent = connectionSettings.userAgent();
this.authToken = requireValidAuthToken(connectionSettings.authToken());
this.authToken = connectionSettings.authToken();
this.routingContext = routingContext;
this.connectTimeoutMillis = connectionSettings.connectTimeoutMillis();
this.securityPlan = requireNonNull(securityPlan);
Expand Down Expand Up @@ -143,13 +140,4 @@ private void installHandshakeCompletedListeners(
handshakeCompleted.addListener(
new HandshakeCompletedListener(userAgent, authToken, routingContext, connectionInitialized));
}

private static AuthToken requireValidAuthToken(AuthToken token) {
if (token instanceof InternalAuthToken) {
return token;
} else {
throw new ClientException("Unknown authentication token, `" + token + "`. Please use one of the supported "
+ "tokens from `" + AuthTokens.class.getSimpleName() + "`.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
* A simple common token for authentication schemes that easily convert to
* an auth token map
*/
public class InternalAuthToken implements AuthToken {
public final class InternalAuthToken implements AuthToken {
public static final String SCHEME_KEY = "scheme";
public static final String PRINCIPAL_KEY = "principal";
public static final String CREDENTIALS_KEY = "credentials";
Expand Down