def password_reset(mailman, username, email=None): my_user = User.query_by_username(username) old_password = my_user.password right_email = my_user.email if right_email != email: return new_random_password = RandomGenerator.random_password() random_id, my_time = PasswordResetAction.insert( username=username, email=email, old_password=old_password, new_password=new_random_password) UserAction.insert(username=username, action_type=PASSWORD_RESET_ACTION, action_id=random_id, my_time=my_time) token_password_reset = TokenManager.token_provider( { 'username': username, 'new_password': new_random_password, 'action_id': random_id }, OtherConfig.CONFIRMATION_TIME_OUT) my_thread = MyThread( ACTION_SEND_MAIL, app, mailman, PASSWORD_RESET_TITLE, 'username: {}\npassword: {}\n'.format( username, new_random_password) + PASSWORD_RESET_MESSAGE + OtherConfig.HOST + '/confirm/password_reset/' + str(token_password_reset), email) my_thread.start() return token_password_reset
def register(mailman, user, by_admin): if not UserValidation.register_format_valid(user): return REGISTER_VALIDATION_FAILED user['password'] = PasswordManager.hashed_password(user['password']) my_user = User.query_by_username_or_email(user['username'], user['email']) if my_user: return REGISTER_DUPLICATED_USERNAME_OR_EMAIL random_id, my_time = RegisterAction.insert(user['username'], user['password'], user['email']) UserAction.insert(user['username'], REGISTER_ACTION, random_id, my_time) token_register = TokenManager.token_provider( { 'username': user['username'], 'password': user['password'], 'email': user['email'], 'action_id': random_id }, OtherConfig.CONFIRMATION_TIME_OUT) my_thread = MyThread( ACTION_SEND_MAIL, app, mailman, REGISTER_TITLE, REGISTER_MESSAGE + OtherConfig.HOST + '/confirm/register/' + str(token_register), user['email']) my_thread.start() return REGISTER_SUCCESS
def token_is_alive(token): token_payload = decode_token(token) if TokenManager.token_expired(token_payload): return False exp = MyTimer.get_time() lim = LoginAction.get_logout_time(token_payload['username']) return exp <= lim
def get_basic_information_by_username(username): record = db.session.query(User).filter( User.username == username).first() if record: metadata = {'email': record.email, 'username': username} token = TokenManager.token_provider(metadata, OtherConfig.LOGIN_TIME_OUT) return {'metadata': metadata, 'token': token} return None
def insert(domain, username): my_time = int(time.time()) secret_key = TokenManager.token_provider({'domain': domain}, 0) new_record = SSORegister(username=username, domain=domain, secret_key=secret_key, register_time=my_time) db.session.add(new_record) db.session.commit() return secret_key
def confirm_register(token): payload = decode_token(token) if TokenManager.token_expired(payload): return render_template( 'register_confirmation.html', message="Your session expired, Please register again!") success = ConfirmationController.confirm_register(payload['identity']) if success: return render_template('register_confirmation.html', message="You registered successfully!") return render_template('register_confirmation.html', message="Your email or username is duplicated!")
def confirm_password_reset(token): payload = decode_token(token) if TokenManager.token_expired(payload): return render_template( 'register_confirmation.html', message= "Your session expired, Please reset your password again!") success = ConfirmationController.confirm_password_reset( payload['identity']) if success: return render_template('register_confirmation.html', message="Your password reset successfully!") return render_template('register_confirmation.html', message="You reset password already!")
def login(user): if not UserValidation.login_format_valid(user): return None username = user['username'] password = user['password'] password = PasswordManager.hashed_password(password) my_user = User.query_by_username(username=username) returned_value = {'signal': SIGNAL_NOTHING, 'token': None} if my_user: if my_user.password == password: random_id = UserController.login_action(my_user.username) if not AccountLock.account_being_locked(username): AccountLock.unlock_account(username) return { 'signal': SIGNAL_NOTHING, 'token': TokenManager.token_provider( { 'username': my_user.username, 'privilege': my_user.privilege, 'action_id': random_id }, OtherConfig.LOGIN_TIME_OUT) } else: return {'signal': SIGNAL_ACCOUNT_LOCK, 'token': None} else: returned_value['signal'] = AccountLock.failed_login(username) random_id, my_time = LoginAction.insert(username=username, success=False) UserAction.insert(username=username, action_type=LOGIN_ACTION, action_id=random_id, my_time=my_time) return returned_value