Search in sources :

Example 16 with PilotUser

use of com.sanction.thunder.models.PilotUser in project thunder by RohanNagar.

the class UserResource method getUser.

/**
 * Retrieves a PilotUser from the database.
 *
 * @param key The basic authentication key necessary to access the resource.
 * @param password The password of the user to fetch. Used to verify authentication.
 * @param email The email of the user to retrieve.
 * @return The pilotUser that was found in the database.
 */
@GET
public Response getUser(@Auth Key key, @HeaderParam("password") String password, @QueryParam("email") String email) {
    getRequests.mark();
    if (email == null || email.isEmpty()) {
        LOG.warn("Attempted to get a null user.");
        return Response.status(Response.Status.BAD_REQUEST).entity("Incorrect or missing email query parameter.").build();
    }
    if (password == null || password.isEmpty()) {
        LOG.warn("Attempted to get user {} without a password", email);
        return Response.status(Response.Status.BAD_REQUEST).entity("Incorrect or missing header credentials.").build();
    }
    LOG.info("Attempting to get user {}.", email);
    PilotUser user;
    try {
        user = usersDao.findByEmail(email);
    } catch (DatabaseException e) {
        LOG.error("Error retrieving user {} in database. Caused by: {}", email, e.getErrorKind());
        return e.getErrorKind().buildResponse(email);
    }
    // Check that the password is correct for the user that was requested
    if (!user.getPassword().equals(password)) {
        LOG.error("The password for user {} was incorrect.", email);
        return Response.status(Response.Status.UNAUTHORIZED).entity("Unable to validate user with provided credentials.").build();
    }
    LOG.info("Successfully retrieved user {}.", email);
    return Response.ok(user).build();
}
Also used : PilotUser(com.sanction.thunder.models.PilotUser) DatabaseException(com.sanction.thunder.dao.DatabaseException) GET(javax.ws.rs.GET)

Example 17 with PilotUser

use of com.sanction.thunder.models.PilotUser in project thunder by RohanNagar.

the class VerificationResource method createVerificationEmail.

/**
 * Validates a user account by sending an email with a unique token.
 *
 * @param key The basic authentication key necessary to access the resource.
 * @param email The email to send a unique token to.
 * @return A response status and message.
 */
@POST
public Response createVerificationEmail(@Auth Key key, @QueryParam("email") String email, @HeaderParam("password") String password) {
    verifyUserRequests.mark();
    if (email == null || email.isEmpty()) {
        LOG.warn("Attempted user verification without an email.");
        return Response.status(Response.Status.BAD_REQUEST).entity("Incorrect or missing email query parameter.").build();
    }
    if (password == null || password.isEmpty()) {
        LOG.warn("Attempted to verify user {} without a password.", email);
        return Response.status(Response.Status.BAD_REQUEST).entity("Incorrect or missing header credentials.").build();
    }
    LOG.info("Attempting to send verification email to user {}", email);
    // Get the existing PilotUser
    PilotUser user;
    try {
        user = usersDao.findByEmail(email);
    } catch (DatabaseException e) {
        LOG.error("Error retrieving user {} in database. Caused by: {}", email, e.getErrorKind());
        return e.getErrorKind().buildResponse(email);
    }
    // Generate the unique verification token
    String token = generateVerificationToken();
    // Update the user's verification token
    PilotUser updatedUser = new PilotUser(new Email(user.getEmail().getAddress(), false, token), user.getPassword(), user.getFacebookAccessToken(), user.getTwitterAccessToken(), user.getTwitterAccessSecret());
    PilotUser result;
    try {
        result = usersDao.update(user.getEmail().getAddress(), updatedUser);
    } catch (DatabaseException e) {
        LOG.error("Error posting user {} to the database. Caused by {}", user.getEmail(), e.getErrorKind());
        return e.getErrorKind().buildResponse(user.getEmail().getAddress());
    }
    // Send the token URL to the users email
    boolean emailResult = emailService.sendEmail(result.getEmail(), "Account Verification", new StringJoiner("\n").add("<h1> Welcome to Pilot! </h1>").add("<p> Click the below link to verify your account. </p>").add(String.format("<a href=\"http://thunder.sanctionco.com/verify" + "?email=%s&token=%s&response_type=html\">Click here to verify your account!</a>", result.getEmail().getAddress(), token)).toString(), new StringJoiner("\n").add("Visit the below address to verify your account.").add(String.format("http://thunder.sanctionco.com/verify?email=%s&token=%s&response_type=html", result.getEmail().getAddress(), token)).toString());
    if (!emailResult) {
        LOG.error("Error sending email to address {}", result.getEmail().getAddress());
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity("An error occurred while attempting to send an email.").build();
    }
    LOG.info("Successfully sent verification email to user {}.", email);
    return Response.ok(result).build();
}
Also used : Email(com.sanction.thunder.models.Email) PilotUser(com.sanction.thunder.models.PilotUser) DatabaseException(com.sanction.thunder.dao.DatabaseException) StringJoiner(java.util.StringJoiner) POST(javax.ws.rs.POST)

Example 18 with PilotUser

use of com.sanction.thunder.models.PilotUser in project thunder by RohanNagar.

the class VerificationResource method verifyEmail.

/**
 * Verifies the provided email, setting it as valid in the database.
 *
 * @param email The email to verify in the database.
 * @param token The verification token associated with the user.
 * @param responseType The type of object to respond with. Either JSON or HTML.
 * @return A response status and message.
 */
@GET
public Response verifyEmail(@QueryParam("email") String email, @QueryParam("token") String token, @QueryParam("response_type") @DefaultValue("json") ResponseType responseType) {
    verifyEmailRequests.mark();
    if (email == null || email.isEmpty()) {
        LOG.warn("Attempted email verification without an email.");
        return Response.status(Response.Status.BAD_REQUEST).entity("Incorrect or missing email query parameter.").build();
    }
    if (token == null || token.isEmpty()) {
        LOG.warn("Attempted email verification without a token");
        return Response.status(Response.Status.BAD_REQUEST).entity("Incorrect or missing verification token query parameter.").build();
    }
    LOG.info("Attempting to verify email {}", email);
    PilotUser user;
    try {
        user = usersDao.findByEmail(email);
    } catch (DatabaseException e) {
        LOG.error("Error retrieving email {} in database. Caused by: {}", email, e.getErrorKind());
        return e.getErrorKind().buildResponse(email);
    }
    String verificationToken = user.getEmail().getVerificationToken();
    if (verificationToken == null || verificationToken.isEmpty()) {
        LOG.warn("Tried to read null or empty verification token");
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity("Bad value found for user verification token.").build();
    }
    if (!token.equals(verificationToken)) {
        LOG.warn("User provided verification token does not match database verification token.");
        return Response.status(Response.Status.BAD_REQUEST).entity("Incorrect verification token.").build();
    }
    // Create the verified pilot user
    PilotUser updatedUser = new PilotUser(new Email(user.getEmail().getAddress(), true, user.getEmail().getVerificationToken()), user.getPassword(), user.getFacebookAccessToken(), user.getTwitterAccessToken(), user.getFacebookAccessToken());
    try {
        usersDao.update(user.getEmail().getAddress(), updatedUser);
    } catch (DatabaseException e) {
        LOG.error("Error verifying email {} in database. Caused by: {}", email, e.getErrorKind());
        return e.getErrorKind().buildResponse(email);
    }
    LOG.info("Successfully verified email {}.", email);
    if (responseType.equals(ResponseType.JSON)) {
        LOG.info("Returning JSON in the response.");
        return Response.ok(updatedUser).build();
    }
    LOG.info("Redirecting to /verify/success in order to return HTML.");
    URI uri = UriBuilder.fromUri("/verify/success").build();
    return Response.seeOther(uri).build();
}
Also used : Email(com.sanction.thunder.models.Email) PilotUser(com.sanction.thunder.models.PilotUser) DatabaseException(com.sanction.thunder.dao.DatabaseException) URI(java.net.URI) GET(javax.ws.rs.GET)

Example 19 with PilotUser

use of com.sanction.thunder.models.PilotUser in project thunder by RohanNagar.

the class UsersDaoTest method testSuccessfulFindByEmail.

@Test
public void testSuccessfulFindByEmail() {
    when(table.getItem(anyString(), anyString())).thenReturn(item);
    PilotUser result = usersDao.findByEmail("email");
    verify(table, times(1)).getItem(anyString(), anyString());
    assertEquals(user, result);
}
Also used : PilotUser(com.sanction.thunder.models.PilotUser) Test(org.junit.Test)

Example 20 with PilotUser

use of com.sanction.thunder.models.PilotUser in project thunder by RohanNagar.

the class UsersDaoTest method testSuccessfulInsert.

@Test
public void testSuccessfulInsert() {
    PilotUser result = usersDao.insert(user);
    verify(table, times(1)).putItem(any(Item.class), any(Expected.class));
    assertEquals(user, result);
}
Also used : Item(com.amazonaws.services.dynamodbv2.document.Item) Expected(com.amazonaws.services.dynamodbv2.document.Expected) PilotUser(com.sanction.thunder.models.PilotUser) Test(org.junit.Test)

Aggregations

PilotUser (com.sanction.thunder.models.PilotUser)26 Test (org.junit.Test)20 Response (javax.ws.rs.core.Response)9 DatabaseException (com.sanction.thunder.dao.DatabaseException)6 Email (com.sanction.thunder.models.Email)4 Expected (com.amazonaws.services.dynamodbv2.document.Expected)3 Item (com.amazonaws.services.dynamodbv2.document.Item)3 DeleteItemSpec (com.amazonaws.services.dynamodbv2.document.spec.DeleteItemSpec)2 GET (javax.ws.rs.GET)2 POST (javax.ws.rs.POST)2 URI (java.net.URI)1 StringJoiner (java.util.StringJoiner)1 DELETE (javax.ws.rs.DELETE)1 PUT (javax.ws.rs.PUT)1