Search in sources :

Example 1 with AuthToken

use of org.neo4j.driver.AuthToken in project spring-boot by spring-projects.

the class Neo4jAutoConfiguration method neo4jDriver.

@Bean
@ConditionalOnMissingBean
public Driver neo4jDriver(Neo4jProperties properties, Environment environment, ObjectProvider<ConfigBuilderCustomizer> configBuilderCustomizers) {
    AuthToken authToken = mapAuthToken(properties.getAuthentication(), environment);
    Config config = mapDriverConfig(properties, configBuilderCustomizers.orderedStream().collect(Collectors.toList()));
    URI serverUri = determineServerUri(properties, environment);
    return GraphDatabase.driver(serverUri, authToken, config);
}
Also used : Config(org.neo4j.driver.Config) AuthToken(org.neo4j.driver.AuthToken) URI(java.net.URI) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean)

Example 2 with AuthToken

use of org.neo4j.driver.AuthToken in project neo4j by neo4j.

the class BoltStateHandlerTest method fallbackTest.

private void fallbackTest(String initialScheme, String fallbackScheme, Runnable failer) throws CommandException {
    final String[] uriScheme = new String[1];
    RecordingDriverProvider provider = new RecordingDriverProvider() {

        @Override
        public Driver apply(String uri, AuthToken authToken, Config config) {
            uriScheme[0] = uri.substring(0, uri.indexOf(':'));
            if (uriScheme[0].equals(initialScheme)) {
                failer.run();
            }
            super.apply(uri, authToken, config);
            return new FakeDriver();
        }
    };
    BoltStateHandler handler = new BoltStateHandler(provider, false);
    ConnectionConfig config = new ConnectionConfig(initialScheme, "", -1, "", "", Encryption.DEFAULT, ABSENT_DB_NAME);
    handler.connect(config);
    assertEquals(fallbackScheme, uriScheme[0]);
}
Also used : ConnectionConfig(org.neo4j.shell.ConnectionConfig) Config(org.neo4j.driver.Config) SessionConfig(org.neo4j.driver.SessionConfig) AuthToken(org.neo4j.driver.AuthToken) FakeDriver(org.neo4j.shell.test.bolt.FakeDriver) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ConnectionConfig(org.neo4j.shell.ConnectionConfig)

Example 3 with AuthToken

use of org.neo4j.driver.AuthToken in project neo4j by neo4j.

the class BoltStateHandler method changePassword.

public void changePassword(@Nonnull ConnectionConfig connectionConfig) {
    if (!connectionConfig.passwordChangeRequired()) {
        return;
    }
    if (isConnected()) {
        silentDisconnect();
    }
    final AuthToken authToken = AuthTokens.basic(connectionConfig.username(), connectionConfig.password());
    try {
        driver = getDriver(connectionConfig, authToken);
        activeDatabaseNameAsSetByUser = SYSTEM_DB_NAME;
        // Supply empty command, so that we do not run ping.
        reconnect(SYSTEM_DB_NAME, SYSTEM_DB_NAME, () -> {
        });
        try {
            String command = "ALTER CURRENT USER SET PASSWORD FROM $o TO $n";
            Value parameters = Values.parameters("o", connectionConfig.password(), "n", connectionConfig.newPassword());
            Result run = session.run(command, parameters);
            run.consume();
        } catch (Neo4jException e) {
            if (isPasswordChangeRequiredException(e)) {
                // In < 4.0 versions use legacy method.
                String oldCommand = "CALL dbms.security.changePassword($n)";
                Value oldParameters = Values.parameters("n", connectionConfig.newPassword());
                Result run = session.run(oldCommand, oldParameters);
                run.consume();
            } else {
                throw e;
            }
        }
        // If successful, use the new password when reconnecting
        connectionConfig.setPassword(connectionConfig.newPassword());
        connectionConfig.setNewPassword(null);
        silentDisconnect();
    } catch (Throwable t) {
        try {
            silentDisconnect();
        } catch (Exception e) {
            t.addSuppressed(e);
        }
        if (t instanceof RuntimeException) {
            throw (RuntimeException) t;
        }
        // we cannot get that since we supply an empty command.
        throw new RuntimeException(t);
    }
}
Also used : Value(org.neo4j.driver.Value) AuthToken(org.neo4j.driver.AuthToken) Neo4jException(org.neo4j.driver.exceptions.Neo4jException) SessionExpiredException(org.neo4j.driver.exceptions.SessionExpiredException) Versions.isPasswordChangeRequiredException(org.neo4j.shell.util.Versions.isPasswordChangeRequiredException) ServiceUnavailableException(org.neo4j.driver.exceptions.ServiceUnavailableException) ClientException(org.neo4j.driver.exceptions.ClientException) Neo4jException(org.neo4j.driver.exceptions.Neo4jException) CommandException(org.neo4j.shell.exception.CommandException) Result(org.neo4j.driver.Result)

Example 4 with AuthToken

use of org.neo4j.driver.AuthToken in project neo4j by neo4j.

the class BoltStateHandler method connect.

@Override
public ConnectionConfig connect(@Nonnull ConnectionConfig connectionConfig, ThrowingAction<CommandException> command) throws CommandException {
    if (isConnected()) {
        throw new CommandException("Already connected");
    }
    final AuthToken authToken = AuthTokens.basic(connectionConfig.username(), connectionConfig.password());
    try {
        String previousDatabaseName = activeDatabaseNameAsSetByUser;
        try {
            activeDatabaseNameAsSetByUser = connectionConfig.database();
            driver = getDriver(connectionConfig, authToken);
            reconnect(activeDatabaseNameAsSetByUser, previousDatabaseName, command);
        } catch (ServiceUnavailableException | SessionExpiredException e) {
            String scheme = connectionConfig.scheme();
            String fallbackScheme;
            switch(scheme) {
                case Scheme.NEO4J_URI_SCHEME:
                    fallbackScheme = Scheme.BOLT_URI_SCHEME;
                    break;
                case Scheme.NEO4J_LOW_TRUST_URI_SCHEME:
                    fallbackScheme = Scheme.BOLT_LOW_TRUST_URI_SCHEME;
                    break;
                case Scheme.NEO4J_HIGH_TRUST_URI_SCHEME:
                    fallbackScheme = Scheme.BOLT_HIGH_TRUST_URI_SCHEME;
                    break;
                default:
                    throw e;
            }
            connectionConfig = new ConnectionConfig(fallbackScheme, connectionConfig.host(), connectionConfig.port(), connectionConfig.username(), connectionConfig.password(), connectionConfig.encryption(), connectionConfig.database());
            try {
                driver = getDriver(connectionConfig, authToken);
                reconnect(activeDatabaseNameAsSetByUser, previousDatabaseName, command);
            } catch (Throwable fallbackThrowable) {
                // Throw the original exception to not cause confusion.
                throw e;
            }
        }
    } catch (Throwable t) {
        try {
            silentDisconnect();
        } catch (Exception e) {
            t.addSuppressed(e);
        }
        throw t;
    }
    return connectionConfig;
}
Also used : AuthToken(org.neo4j.driver.AuthToken) SessionExpiredException(org.neo4j.driver.exceptions.SessionExpiredException) CommandException(org.neo4j.shell.exception.CommandException) ServiceUnavailableException(org.neo4j.driver.exceptions.ServiceUnavailableException) ConnectionConfig(org.neo4j.shell.ConnectionConfig) SessionExpiredException(org.neo4j.driver.exceptions.SessionExpiredException) Versions.isPasswordChangeRequiredException(org.neo4j.shell.util.Versions.isPasswordChangeRequiredException) ServiceUnavailableException(org.neo4j.driver.exceptions.ServiceUnavailableException) ClientException(org.neo4j.driver.exceptions.ClientException) Neo4jException(org.neo4j.driver.exceptions.Neo4jException) CommandException(org.neo4j.shell.exception.CommandException)

Aggregations

AuthToken (org.neo4j.driver.AuthToken)4 Config (org.neo4j.driver.Config)2 ClientException (org.neo4j.driver.exceptions.ClientException)2 Neo4jException (org.neo4j.driver.exceptions.Neo4jException)2 ServiceUnavailableException (org.neo4j.driver.exceptions.ServiceUnavailableException)2 SessionExpiredException (org.neo4j.driver.exceptions.SessionExpiredException)2 ConnectionConfig (org.neo4j.shell.ConnectionConfig)2 CommandException (org.neo4j.shell.exception.CommandException)2 Versions.isPasswordChangeRequiredException (org.neo4j.shell.util.Versions.isPasswordChangeRequiredException)2 URI (java.net.URI)1 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)1 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)1 Result (org.neo4j.driver.Result)1 SessionConfig (org.neo4j.driver.SessionConfig)1 Value (org.neo4j.driver.Value)1 FakeDriver (org.neo4j.shell.test.bolt.FakeDriver)1 ConditionalOnMissingBean (org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean)1 Bean (org.springframework.context.annotation.Bean)1