Search in sources :

Example 6 with HashedPassword

use of games.strategy.engine.lobby.server.db.HashedPassword in project triplea by triplea-game.

the class ModeratorControllerIntegrationTest method setUp.

@BeforeEach
public void setUp() throws UnknownHostException {
    moderatorController = new ModeratorController(serverMessenger, null);
    final String adminName = Util.createUniqueTimeStamp();
    final DBUser dbUser = new DBUser(new DBUser.UserName(adminName), new DBUser.UserEmail("n@n.n"), DBUser.Role.ADMIN);
    final UserController userController = new UserController();
    userController.createUser(dbUser, new HashedPassword(BCrypt.hashpw(adminName, BCrypt.gensalt())));
    userController.makeAdmin(dbUser);
    adminNode = new Node(adminName, InetAddress.getLocalHost(), 0);
    when(serverMessenger.getPlayerMac(adminName)).thenReturn(newHashedMacAddress());
}
Also used : INode(games.strategy.net.INode) Node(games.strategy.net.Node) DBUser(games.strategy.engine.lobby.server.userDB.DBUser) UserController(games.strategy.engine.lobby.server.db.UserController) HashedPassword(games.strategy.engine.lobby.server.db.HashedPassword) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 7 with HashedPassword

use of games.strategy.engine.lobby.server.db.HashedPassword in project triplea by triplea-game.

the class LobbyLoginValidator method createUser.

@Nullable
private String createUser(final Map<String, String> response, final User user) {
    final DBUser dbUser = new DBUser(new DBUser.UserName(user.getUsername()), new DBUser.UserEmail(response.get(EMAIL_KEY)));
    if (!dbUser.isValid()) {
        return dbUser.getValidationErrorMessage();
    }
    if (userDao.doesUserExist(dbUser.getName())) {
        return "That user name has already been taken";
    }
    final HashedPassword password = new HashedPassword(response.get(HASHED_PASSWORD_KEY));
    if (RsaAuthenticator.canProcessResponse(response)) {
        return rsaAuthenticator.decryptPasswordForAction(response, pass -> {
            final HashedPassword newPass = new HashedPassword(BCrypt.hashpw(pass, bcryptSaltGenerator.newSalt()));
            if (password.isHashedWithSalt()) {
                userDao.createUser(dbUser, password);
                userDao.updateUser(dbUser, newPass);
            } else {
                userDao.createUser(dbUser, newPass);
            }
            return null;
        });
    }
    if (!password.isHashedWithSalt()) {
        return "Password is not hashed correctly";
    }
    try {
        userDao.createUser(dbUser, password);
        return null;
    } catch (final Exception e) {
        return e.getMessage();
    }
}
Also used : DBUser(games.strategy.engine.lobby.server.userDB.DBUser) HashedPassword(games.strategy.engine.lobby.server.db.HashedPassword) Nullable(javax.annotation.Nullable)

Example 8 with HashedPassword

use of games.strategy.engine.lobby.server.db.HashedPassword in project triplea by triplea-game.

the class LobbyLoginValidator method authenticateRegisteredUser.

@Nullable
private String authenticateRegisteredUser(final Map<String, String> response, final User user) {
    final String username = user.getUsername();
    final String errorMessage = ErrorMessages.AUTHENTICATION_FAILED;
    final HashedPassword hashedPassword = userDao.getPassword(username);
    if (hashedPassword == null) {
        return errorMessage;
    }
    if (RsaAuthenticator.canProcessResponse(response)) {
        return rsaAuthenticator.decryptPasswordForAction(response, pass -> {
            final String legacyHashedPassword = response.get(HASHED_PASSWORD_KEY);
            if (hashedPassword.isBcrypted()) {
                if (userDao.login(username, new HashedPassword(pass))) {
                    if (legacyHashedPassword != null && userDao.getLegacyPassword(username).value.isEmpty()) {
                        userDao.updateUser(userDao.getUserByName(username), new HashedPassword(legacyHashedPassword));
                        userDao.updateUser(userDao.getUserByName(username), new HashedPassword(BCrypt.hashpw(pass, bcryptSaltGenerator.newSalt())));
                    }
                    return null;
                }
                return errorMessage;
            } else if (userDao.login(username, new HashedPassword(legacyHashedPassword))) {
                userDao.updateUser(userDao.getUserByName(username), new HashedPassword(BCrypt.hashpw(pass, bcryptSaltGenerator.newSalt())));
                return null;
            } else {
                return errorMessage;
            }
        });
    }
    if (!userDao.login(username, new HashedPassword(response.get(HASHED_PASSWORD_KEY)))) {
        return errorMessage;
    }
    return null;
}
Also used : HashedPassword(games.strategy.engine.lobby.server.db.HashedPassword) Nullable(javax.annotation.Nullable)

Aggregations

HashedPassword (games.strategy.engine.lobby.server.db.HashedPassword)8 DBUser (games.strategy.engine.lobby.server.userDB.DBUser)7 UserController (games.strategy.engine.lobby.server.db.UserController)6 Nullable (javax.annotation.Nullable)6 LobbyServer (games.strategy.engine.lobby.server.LobbyServer)4 BadWordController (games.strategy.engine.lobby.server.db.BadWordController)4 RsaAuthenticator.hashPasswordWithSalt (games.strategy.engine.lobby.server.login.RsaAuthenticator.hashPasswordWithSalt)4 ILoginValidator (games.strategy.net.ILoginValidator)4 MacFinder (games.strategy.net.MacFinder)4 Integration (games.strategy.test.Integration)4 Md5Crypt (games.strategy.util.Md5Crypt)4 Util (games.strategy.util.Util)4 InetSocketAddress (java.net.InetSocketAddress)4 SocketAddress (java.net.SocketAddress)4 Arrays (java.util.Arrays)4 Collections (java.util.Collections)4 HashMap (java.util.HashMap)4 Map (java.util.Map)4 Function (java.util.function.Function)4 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)4