Exemplo n.º 1
0
def login():
    try:
        data = get_request_data(request, ["email", "password"])
    except (ValueError, TypeError) as e:
        return handle_error(message="Invalid parameters: %s" % str(e),
                            logger=logger,
                            status_code=422)

    db = DatabaseService()
    userType = db.authenticate_user(
        email=data["email"],
        password=data["password"],
    )
    if not userType:
        return make_response("Error: invalid credentials", 401)

    token = jwt.encode(
        {
            "subject": data["email"],
            "userType": userType
        },
        app.config["SECRET_KEY"],
        algorithm="HS256",
    )
    return jsonify({"token": token})
Exemplo n.º 2
0
def forgot_password():

    try:
        data = get_request_data(
            request,
            required_params=["email", "resetURL"],
        )
    except (ValueError, TypeError) as e:
        return handle_error(
            message="%s: %s" % (request.url, str(e)),
            logger=logger,
            status_code=422,
        )

    token = uuid.uuid4().hex
    mailgun_response = send_reset_link(
        email=data["email"],
        token=token,
        url=data["resetURL"],
    )
    if mailgun_response.status_code != 200:
        return make_response(
            '''There was an error sending your reset link,
            please email [email protected] with this error: %s''' %
            mailgun_response.text, mailgun_response.status_code)

    db = DatabaseService()
    db.save_reset_token(
        email=data["email"],
        token=token,
    )
    return make_response("OK", 200)
Exemplo n.º 3
0
def create_validation_token():
    try:
        data = get_request_data(
            request,
            required_params=["email"],
        )
    except (ValueError, TypeError) as e:
        return handle_error(
            message="%s: %s" % (request.url, str(e)),
            logger=logger,
            status_code=422,
        )

    token = DatabaseService().create_validation_token(email=data["email"])
    if token is None:
        return make_response("Could not generate unique token", 500)

    return jsonify({"token": token})