def set_standalone_user(user_id: int): ''' changes user password when logged in''' post_data = request.get_json() if not post_data: raise InvalidPayload() username = post_data.get('username') pw_old = post_data.get('old_password') pw_new = post_data.get('new_password') if not username or not pw_old or not pw_new: raise InvalidPayload() # fetch the user data user = User.get(user_id) if not user.fb_id: raise NotFoundException( message='Must be a facebook user login. Please try again.') # fetch the user data user = User.get(user_id) if not bcrypt.check_password_hash(user.password, pw_old): raise NotFoundException(message='Invalid password. Please try again.') if not User.first(User.username == username): with session_scope(db.session): user.username = username user.password = bcrypt.generate_password_hash( pw_new, current_app.config.get('BCRYPT_LOG_ROUNDS')).decode() return { 'status': 'success', 'message': 'Successfully changed password.', } else: raise BusinessException( message= 'Sorry. That username already exists, choose another username')
def email_verification(user_id): ''' creates a email_token_hash and sends email with token to user (assumes login=email), idempotent (could be use for resend)''' post_data = request.get_json() if not post_data: raise InvalidPayload() email = post_data.get('email') if not email: raise InvalidPayload() # fetch the user data user = User.first_by(email=email) if user: token = user.encode_email_token() with session_scope(db.session): user.email_token_hash = bcrypt.generate_password_hash( token, current_app.config.get('BCRYPT_LOG_ROUNDS')).decode() if not current_app.testing: from project.api.common.utils.mails import send_email_verification_email send_email_verification_email( user, token.decode()) # send recovery email return { 'status': 'success', 'message': 'Successfully sent email with email verification.', } else: raise NotFoundException( message= 'Login/email does not exist, please write a valid login/email')
def password_reset(): ''' reset user password (assumes login=email)''' post_data = request.get_json() if not post_data: raise InvalidPayload() token = post_data.get('token') pw_new = post_data.get('password') if not token or not pw_new: raise InvalidPayload() # fetch the user data user_id = User.decode_password_token(token) user = User.get(user_id) if not user or not user.token_hash or not bcrypt.check_password_hash( user.token_hash, token): raise NotFoundException(message='Invalid reset. Please try again.') with session_scope(db.session): user.password = bcrypt.generate_password_hash( pw_new, current_app.config.get('BCRYPT_LOG_ROUNDS')).decode() user.token_hash = None return { 'status': 'success', 'message': 'Successfully reset password.', }
def register_user(): # get post data post_data = request.get_json() if not post_data: raise InvalidPayload() username = post_data.get('username') email = post_data.get('email') password = post_data.get('password') if not password or not username or not email: raise InvalidPayload() # check for existing user user = User.first(or_(User.username == username, User.email == email)) if not user: # add new user to db new_user = User(username=username, email=email, password=password) with session_scope(db.session) as session: session.add(new_user) # need another scope if not new_user does not exists yet with session_scope(db.session) as session: token = new_user.encode_email_token() new_user.email_token_hash = bcrypt.generate_password_hash( token, current_app.config.get('BCRYPT_LOG_ROUNDS')).decode() if not current_app.testing: from project.api.common.utils.mails import send_registration_email send_registration_email(new_user, token) # save the device if all(x in request.headers for x in [ Constants.HttpHeaders.DEVICE_ID, Constants.HttpHeaders.DEVICE_TYPE ]): device_id = request.headers.get(Constants.HttpHeaders.DEVICE_ID) device_type = request.headers.get( Constants.HttpHeaders.DEVICE_TYPE) with session_scope(db.session): Device.create_or_update(device_id=device_id, device_type=device_type, user=user) # generate auth token auth_token = new_user.encode_auth_token() return { 'status': 'success', 'message': 'Successfully registered.', 'auth_token': auth_token }, 201 else: # user already registered, set False to device.active if Constants.HttpHeaders.DEVICE_ID in request.headers: device_id = request.headers.get(Constants.HttpHeaders.DEVICE_ID) device = Device.first_by(device_id=device_id) if device: with session_scope(db.session): device.active = False raise BusinessException(message='Sorry. That user already exists.')
def __init__(self, first_name: str, last_name: str, password: str, email: str = ''): self.first_name = first_name self.last_name = last_name self.email = email self.password = bcrypt.generate_password_hash( password, current_app.config.get('BCRYPT_LOG_ROUNDS')).decode()
def __init__(self, username: str, password: str, email: str = '', admin: bool = False): self.username = username self.email = email self.admin = admin self.password = bcrypt.generate_password_hash( password, current_app.config.get('BCRYPT_LOG_ROUNDS')).decode()
def password_change(user_id: int): ''' changes user password when logged in''' post_data = request.get_json() if not post_data: raise InvalidPayload() pw_old = post_data.get('old_password') pw_new = post_data.get('new_password') if not pw_old or not pw_new: raise InvalidPayload() # fetch the user data user = User.get(user_id) if not bcrypt.check_password_hash(user.password, pw_old): raise BusinessException(message='Invalid password. Please try again.') with session_scope(db.session): user.password = bcrypt.generate_password_hash( pw_new, current_app.config.get('BCRYPT_LOG_ROUNDS')).decode() return { 'status': 'success', 'message': 'Successfully changed password.', }
def set_password(self, password): """Set password.""" self.password = bcrypt.generate_password_hash(password)
def __init__(self, id, username, password, email): self.id = id self.username = username self.password = bcrypt.generate_password_hash(password) self.email = email
def set_user_email_token_hash(user: User, token: str): user.email_token_hash = bcrypt.generate_password_hash( token, app.config.get('BCRYPT_LOG_ROUNDS')).decode() db.session.commit() return user
def encrypt_password(cls, password): return bcrypt.generate_password_hash(password).decode('UTF-8')