def api_verify_email(): """ @TODO: add counter/log to track failed attempts :rtype: Response :return the success or failed in json format """ if "POST" == request.method: token = utils.clean_str(request.form.get("tok")) else: token = utils.clean_str(request.args.get("tok")) if not token: return utils.jsonify_error({"message": "No token specified."}) try: email = utils.get_email_from_token(token, app.config["SECRET_KEY"], app.config["SECRET_KEY"]) except Exception as exc: # @TODO: add dedicated log type app.logger.error("api_verify_email: {}".format(exc.message)) return utils.jsonify_error({"message": exc.message}) app.logger.debug("Decoded email from token: {}".format(email)) user = UserEntity.query.filter_by(email=email).first() if user is None: app.logger.error("Attempt to verify email with incorrect token: {}".format(token)) return utils.jsonify_error({"message": "Sorry."}) user = UserEntity.update(user, email_confirmed_at=datetime.today()) app.logger.debug("Verified token {} for user {}".format(token, user.email)) # @TODO: add dedicated log type LogEntity.account_modified(session["uuid"], "Verified token {} for user {}".format(token, user.email)) return utils.jsonify_success({"message": "Email was verified for {}.".format(email)})
def api_send_verification_email(): """ Send Verification Email to the `user_id` specified in the request :rtype: Response :return the success or failed in json format """ passed, errors = check_email_config() if not passed: app.logger.warn(" ".join(errors)) return utils.jsonify_error( {"message": "Unable to send email due to configuration errors."}) user_id = utils.get_safe_int(request.form.get('user_id')) user = UserEntity.get_by_id(user_id) try: emails.send_verification_email(user) return utils.jsonify_success( {"message": "Verification email was sent."}) except Exception as exc: details = "Connection config: {}/{}:{}".format( app.config['MAIL_USERNAME'], app.config['MAIL_SERVER'], app.config['MAIL_PORT']) app.logger.debug(details) return utils.jsonify_error( {"message": "Unable to send email due: {} {}".format(exc, details)})
def api_verify_email(): """ @TODO: add counter/log to track failed attempts :rtype: Response :return the success or failed in json format """ if 'POST' == request.method: token = utils.clean_str(request.form.get('tok')) else: token = utils.clean_str(request.args.get('tok')) if not token: return utils.jsonify_error({'message': 'No token specified.'}) try: email = utils.get_email_from_token(token, app.config["SECRET_KEY"], app.config["SECRET_KEY"]) except Exception as exc: # @TODO: add dedicated log type app.logger.error("api_verify_email: {}".format(exc.message)) return utils.jsonify_error({'message': exc.message}) app.logger.debug("Decoded email from token: {}".format(email)) user = UserEntity.query.filter_by(email=email).first() if user is None: app.logger.error("Attempt to verify email with incorrect token: {}" .format(token)) return utils.jsonify_error({'message': 'Sorry.'}) user = UserEntity.update(user, email_confirmed_at=datetime.today()) app.logger.debug("Verified token {} for user {}".format(token, user.email)) # @TODO: add dedicated log type LogEntity.account_modified(session['uuid'], "Verified token {} for user {}".format( token, user.email)) return utils.jsonify_success( {"message": "Email was verified for {}.".format(email)})
def api_save_user(): """ Save a new user to the database """ email = request.form["email"] first = request.form["first"] last = request.form["last"] minitial = request.form["minitial"] roles = request.form.getlist("roles[]") email_exists = False try: existing_user = UserEntity.query.filter_by(email=email).one() email_exists = existing_user is not None except: pass if email_exists: return utils.jsonify_error({"message": "Sorry. This email is already taken."}) # @TODO: fix hardcoded values # salt, hashed_pass = generate_auth(app.config['SECRET_KEY'], password) added_date = datetime.today() access_end_date = utils.get_expiration_date(180) user = UserEntity.create( email=email, first=first, last=last, minitial=minitial, added_at=added_date, modified_at=added_date, access_expires_at=access_end_date, password_hash="", ) user_roles = [] try: for role_name in roles: role_entity = RoleEntity.query.filter_by(name=role_name).one() user_roles.append(role_entity) except Exception as exc: app.logger.debug("Problem saving user: {}".format(exc)) [user.roles.append(rol) for rol in user_roles] user = UserEntity.save(user) app.logger.debug("saved user: {}".format(user)) LogEntity.account_created(session["uuid"], user) return utils.jsonify_success({"user": user.serialize()})
def api_save_user(): """ Save a new user to the database """ email = request.form['email'] first = request.form['first'] last = request.form['last'] minitial = request.form['minitial'] roles = request.form.getlist('roles[]') email_exists = False try: existing_user = UserEntity.query.filter_by(email=email).one() email_exists = existing_user is not None except: pass if email_exists: return utils.jsonify_error( {'message': 'Sorry. This email is already taken.'}) # @TODO: fix hardcoded values # salt, hashed_pass = generate_auth(app.config['SECRET_KEY'], password) added_date = datetime.today() access_end_date = utils.get_expiration_date(180) user = UserEntity.create(email=email, first=first, last=last, minitial=minitial, added_at=added_date, modified_at=added_date, access_expires_at=access_end_date, password_hash="") user_roles = [] try: for role_name in roles: role_entity = RoleEntity.query.filter_by(name=role_name).one() user_roles.append(role_entity) except Exception as exc: app.logger.debug("Problem saving user: {}".format(exc)) [user.roles.append(rol) for rol in user_roles] user = UserEntity.save(user) app.logger.debug("saved user: {}".format(user)) LogEntity.account_created(session['uuid'], user) return utils.jsonify_success({'user': user.serialize()})
def api_send_verification_email(): """ @TODO: Send Verification Email to user_id :rtype: Response :return the success or failed in json format """ user_id = utils.get_safe_int(request.form.get("user_id")) user = UserEntity.get_by_id(user_id) try: emails.send_verification_email(user) return utils.jsonify_success({"message": "Verification email was sent."}) except Exception as exc: details = "Connection config: {}/{}:{}".format( app.config["MAIL_USERNAME"], app.config["MAIL_SERVER"], app.config["MAIL_PORT"] ) app.logger.debug(details) return utils.jsonify_error({"message": "Unable to send email due: {} {}".format(exc, details)})