use of io.divide.shared.transitory.Credentials in project divide by HiddenStage.
the class AuthenticationEndpointTest method signUpUser.
public static synchronized Credentials signUpUser(JerseyTest test) throws Exception {
PublicKey publicKey = getPublicKey(test);
Credentials signInUser = TestUtils.getTestUser();
signInUser.encryptPassword(publicKey);
String user = test.target("/auth").request().post(TestUtils.toEntity(signInUser), String.class);
Credentials returnedUser = TestUtils.getGson().fromJson(user, Credentials.class);
assertEquals(signInUser.getUsername(), returnedUser.getUsername());
return returnedUser;
}
use of io.divide.shared.transitory.Credentials in project divide by HiddenStage.
the class AuthenticationEndpointTest method testGetUserFromToken.
// @Test
// public void testValidateAccount() throws Exception {
//
// }
//
@Test
public void testGetUserFromToken() throws Exception {
Credentials user = signUpUser(this);
String token = user.getAuthToken();
token = URLEncoder.encode(token, "UTF-8");
int status = target("/auth/from/").path(token).request().buildGet().invoke().getStatus();
assertEquals(200, status);
}
use of io.divide.shared.transitory.Credentials in project divide by HiddenStage.
the class AuthenticationEndpointTest method testGetUserData.
@Test
public void testGetUserData() throws Exception {
Credentials user = signUpUser(this);
int statusCode = target("/auth/user/data/" + user.getOwnerId()).request().header(ContainerRequest.AUTHORIZATION, "CUSTOM " + user.getAuthToken()).put(TestUtils.toEntity(1)).getStatus();
assertEquals(200, statusCode);
}
use of io.divide.shared.transitory.Credentials in project divide by HiddenStage.
the class PushEndpointTest method testRegister.
@Test
public void testRegister() throws Exception {
Credentials user = AuthenticationEndpointTest.signUpUser(this);
PublicKey key = AuthenticationEndpointTest.getPublicKey(this);
registerToken(user, key, this);
Collection<TransientObject> list = container.serverDao.query(new QueryBuilder().select().from(Credentials.class).build());
TransientObject o = ObjectUtils.get1stOrNull(list);
user = TestUtils.convert(o, Credentials.class);
assertNotNull(user);
// check the token was actually saved
assertEquals("whatwhat", user.getPushMessagingKey());
}
use of io.divide.shared.transitory.Credentials in project divide by HiddenStage.
the class AuthServerLogic method userSignIn.
/**
* Checks username/password against that stored in DB, if same return
* token, if token expired create new.
* @param credentials
* @return authentication token
*/
public Credentials userSignIn(Credentials credentials) throws DAOException {
Credentials dbCreds = getUserByEmail(dao, credentials.getEmailAddress());
if (dbCreds == null) {
throw new DAOException(HttpStatus.SC_UNAUTHORIZED, "User Doesnt exist");
} else {
//check if we are resetting the password
if (dbCreds.getValidation() != null && dbCreds.getValidation().equals(credentials.getValidation())) {
//decrypt the password
credentials.decryptPassword(keyManager.getPrivateKey());
//set the new password
dbCreds.setPassword(BCrypt.hashpw(credentials.getPassword(), BCrypt.gensalt(10)));
} else //else check password
{
String en = credentials.getPassword();
//decrypt the password
credentials.decryptPassword(keyManager.getPrivateKey());
String de = credentials.getPassword();
String ha = BCrypt.hashpw(de, BCrypt.gensalt(10));
System.out.println("Comparing passwords.\n" + "Encrypted: " + en + "\n" + "Decrypted: " + de + "\n" + "Hashed: " + ha + "\n" + "Stored: " + dbCreds.getPassword());
if (!BCrypt.checkpw(de, dbCreds.getPassword())) {
throw new DAOException(HttpStatus.SC_UNAUTHORIZED, "User Already Exists");
}
}
// check if token is expired, if so return/set new
AuthTokenUtils.AuthToken token;
try {
token = new AuthTokenUtils.AuthToken(keyManager.getSymmetricKey(), dbCreds.getAuthToken());
} catch (AuthenticationException e) {
throw new DAOException(HttpStatus.SC_INTERNAL_SERVER_ERROR, "internal error");
}
if (c.getTime().getTime() > token.expirationDate) {
dbCreds.setAuthToken(AuthTokenUtils.getNewToken(keyManager.getSymmetricKey(), dbCreds));
dao.save(dbCreds);
}
return dbCreds;
}
}
Aggregations