Search in sources :

Example 6 with Credentials

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;
}
Also used : PublicKey(java.security.PublicKey) Credentials(io.divide.shared.transitory.Credentials)

Example 7 with Credentials

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);
}
Also used : Credentials(io.divide.shared.transitory.Credentials) ServerTest(io.divide.server.ServerTest) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Example 8 with Credentials

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);
}
Also used : Credentials(io.divide.shared.transitory.Credentials) ServerTest(io.divide.server.ServerTest) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Example 9 with Credentials

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());
}
Also used : PublicKey(java.security.PublicKey) QueryBuilder(io.divide.shared.transitory.query.QueryBuilder) TransientObject(io.divide.shared.transitory.TransientObject) Credentials(io.divide.shared.transitory.Credentials) ServerTest(io.divide.server.ServerTest) Test(org.junit.Test) JerseyTest(org.glassfish.jersey.test.JerseyTest)

Example 10 with Credentials

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;
    }
}
Also used : DAOException(io.divide.shared.server.DAO.DAOException) AuthenticationException(io.divide.shared.util.AuthTokenUtils.AuthenticationException) AuthTokenUtils(io.divide.shared.util.AuthTokenUtils) Credentials(io.divide.shared.transitory.Credentials)

Aggregations

Credentials (io.divide.shared.transitory.Credentials)19 ServerTest (io.divide.server.ServerTest)6 JerseyTest (org.glassfish.jersey.test.JerseyTest)6 Test (org.junit.Test)6 ServerDAO (io.divide.dao.ServerDAO)5 UserContext (io.divide.server.auth.UserContext)4 PublicKey (java.security.PublicKey)3 ServerCredentials (io.divide.server.dao.ServerCredentials)2 DAOException (io.divide.shared.server.DAO.DAOException)2 TransientObject (io.divide.shared.transitory.TransientObject)2 Message (com.google.android.gcm.server.Message)1 MulticastResult (com.google.android.gcm.server.MulticastResult)1 Sender (com.google.android.gcm.server.Sender)1 FilePermissions (io.divide.shared.transitory.FilePermissions)1 QueryBuilder (io.divide.shared.transitory.query.QueryBuilder)1 AuthTokenUtils (io.divide.shared.util.AuthTokenUtils)1 AuthenticationException (io.divide.shared.util.AuthTokenUtils.AuthenticationException)1 IOException (java.io.IOException)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1