Search in sources :

Example 1 with Credentials

use of io.divide.shared.transitory.Credentials in project divide by HiddenStage.

the class ResponseFilter method filter.

@Override
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException {
    SecurityContext context = requestContext.getSecurityContext();
    if (context != null && context instanceof UserContext) {
        UserContext userContext = (UserContext) context;
        Credentials user = userContext.getUser();
        if (user != null && user.getAuthToken() != null) {
            responseContext.getHeaders().add("Authorization", user.getAuthToken());
        }
    }
}
Also used : SecurityContext(javax.ws.rs.core.SecurityContext) Credentials(io.divide.shared.transitory.Credentials)

Example 2 with Credentials

use of io.divide.shared.transitory.Credentials in project divide by HiddenStage.

the class AuthenticationEndpoint method userSignIn.

/**
     * Checks username/password against that stored in DB, if same return
     * token, if token expired create new.
     * @param credentials
     * @return authentication token
     */
@PUT
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response userSignIn(@Context ContainerRequestContext context, Credentials credentials) {
    try {
        Credentials dbCreds = authServerLogic.userSignIn(credentials);
        context.setSecurityContext(new UserContext(context.getUriInfo(), dbCreds));
        logger.info("Login Successful. Returning: " + dbCreds);
        return ok(dbCreds);
    } catch (ServerDAO.DAOException e) {
        logger.severe(ExceptionUtils.getStackTrace(e));
        return fromDAOExpection(e);
    } catch (Exception e) {
        logger.severe(ExceptionUtils.getStackTrace(e));
        return Response.serverError().build();
    }
}
Also used : ServerDAO(io.divide.dao.ServerDAO) UserContext(io.divide.server.auth.UserContext) Credentials(io.divide.shared.transitory.Credentials)

Example 3 with Credentials

use of io.divide.shared.transitory.Credentials in project divide by HiddenStage.

the class AuthenticationEndpoint method recoverFromOneTimeToken.

@GET
@Path("/recover/{token}")
@Produces(MediaType.APPLICATION_JSON)
public Response recoverFromOneTimeToken(@Context ContainerRequestContext context, @PathParam("token") String token) {
    try {
        Credentials user = authServerLogic.getUserFromRecoveryToken(token);
        context.setSecurityContext(new UserContext(context.getUriInfo(), user));
        return Response.ok(user).build();
    } catch (ServerDAO.DAOException e) {
        e.printStackTrace();
        logger.severe(ExceptionUtils.getStackTrace(e));
        return fromDAOExpection(e);
    }
}
Also used : ServerDAO(io.divide.dao.ServerDAO) UserContext(io.divide.server.auth.UserContext) Credentials(io.divide.shared.transitory.Credentials)

Example 4 with Credentials

use of io.divide.shared.transitory.Credentials in project divide by HiddenStage.

the class AuthenticationEndpointTest method testUserSignIn.

@Test
public void testUserSignIn() throws Exception {
    // create user
    Credentials user = signUpUser(this);
    // set password for login attempt
    System.out.println("User1:" + TestUtils.getTestUser());
    user.setPassword(TestUtils.getTestUser().getPassword());
    user.encryptPassword(getPublicKey(this));
    target("/auth").request().put(TestUtils.toEntity(user), String.class);
}
Also used : Credentials(io.divide.shared.transitory.Credentials) ServerTest(io.divide.server.ServerTest) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Example 5 with Credentials

use of io.divide.shared.transitory.Credentials in project divide by HiddenStage.

the class AuthenticationEndpointTest method testRecoverUserFromToken.

@Test
public void testRecoverUserFromToken() throws Exception {
    Credentials user = signUpUser(this);
    String token = user.getRecoveryToken();
    token = URLEncoder.encode(token, "ISO-8859-1");
    int status = target("/auth/recover/").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)

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