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);
}
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]);
}
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);
}
}
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;
}
Aggregations