use of org.neo4j.server.security.auth.FileUserRepository in project neo4j by neo4j.
the class SetInitialPasswordCommand method execute.
@Override
public void execute() throws IOException {
Config config = loadNeo4jConfig();
FileSystemAbstraction fileSystem = ctx.fs();
if (realUsersExist(config)) {
Path authFile = CommunitySecurityModule.getUserRepositoryFile(config);
throw new CommandFailedException(realUsersExistErrorMsg(fileSystem, authFile));
} else {
Path file = CommunitySecurityModule.getInitialUserRepositoryFile(config);
if (fileSystem.fileExists(file)) {
fileSystem.deleteFile(file);
}
FileUserRepository userRepository = new FileUserRepository(fileSystem, file, NullLogProvider.getInstance());
try {
userRepository.start();
userRepository.create(new User.Builder(INITIAL_USER_NAME, createCredentialForPassword(UTF8.encode(password))).withRequiredPasswordChange(changeRequired).build());
userRepository.shutdown();
} catch (Exception e) {
throw new RuntimeException(e);
}
ctx.out().println("Changed password for user '" + INITIAL_USER_NAME + "'.");
}
}
use of org.neo4j.server.security.auth.FileUserRepository in project neo4j by neo4j.
the class SetInitialPasswordCommand method realUsersExist.
private boolean realUsersExist(Config config) {
boolean result = false;
Path authFile = CommunitySecurityModule.getUserRepositoryFile(config);
if (ctx.fs().fileExists(authFile)) {
result = true;
// Check if it only contains the default neo4j user
FileUserRepository userRepository = new FileUserRepository(ctx.fs(), authFile, NullLogProvider.getInstance());
try (Lifespan life = new Lifespan(userRepository)) {
ListSnapshot<User> users = userRepository.getSnapshot();
if (users.values().size() == 1) {
User user = users.values().get(0);
if (INITIAL_USER_NAME.equals(user.name()) && user.credentials().matchesPassword(UTF8.encode(INITIAL_PASSWORD))) {
// We allow overwriting an unmodified default neo4j user
result = false;
}
}
} catch (IOException e) {
// Do not allow overwriting if we had a problem reading the file
}
}
return result;
}
Aggregations