Search in sources :

Example 1 with Email

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

the class UserResource method postUser.

/**
 * Posts a new PilotUser to the database.
 *
 * @param key The basic authentication key necessary to access the resource.
 * @param user The user to post to the database.
 * @return The user that was created in the database.
 */
@POST
public Response postUser(@Auth Key key, PilotUser user) {
    postRequests.mark();
    if (user == null) {
        LOG.warn("Attempted to post a null user.");
        return Response.status(Response.Status.BAD_REQUEST).entity("Cannot post a null user.").build();
    }
    if (user.getEmail() == null) {
        LOG.warn("Attempted to post a user with a null Email object.");
        return Response.status(Response.Status.BAD_REQUEST).entity("Cannot post a user without an email address.").build();
    }
    LOG.info("Attempting to create new user {}.", user.getEmail().getAddress());
    if (!isValidEmail(user.getEmail().getAddress())) {
        LOG.error("The new user has an invalid email address: {}", user.getEmail());
        return Response.status(Response.Status.BAD_REQUEST).entity("Invalid email address format. Please try again.").build();
    }
    // Update the user to non-verified status
    PilotUser updatedUser = new PilotUser(new Email(user.getEmail().getAddress(), false, null), user.getPassword(), user.getFacebookAccessToken(), user.getTwitterAccessToken(), user.getTwitterAccessSecret());
    PilotUser result;
    try {
        result = usersDao.insert(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());
    }
    LOG.info("Successfully created new user {}.", user.getEmail());
    return Response.status(Response.Status.CREATED).entity(result).build();
}
Also used : Email(com.sanction.thunder.models.Email) PilotUser(com.sanction.thunder.models.PilotUser) DatabaseException(com.sanction.thunder.dao.DatabaseException) POST(javax.ws.rs.POST)

Example 2 with Email

use of com.sanction.thunder.models.Email 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 3 with Email

use of com.sanction.thunder.models.Email 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)

Aggregations

DatabaseException (com.sanction.thunder.dao.DatabaseException)3 Email (com.sanction.thunder.models.Email)3 PilotUser (com.sanction.thunder.models.PilotUser)3 POST (javax.ws.rs.POST)2 URI (java.net.URI)1 StringJoiner (java.util.StringJoiner)1 GET (javax.ws.rs.GET)1