async def recover_password( email: str, db: AsyncSession = Depends(deps.get_db)) -> Any: """ Password Recovery """ user = await crud.user.get_by_key(db, key="email", value=email) if not user: raise HTTPException( status_code=404, detail="The user with this username does not exist in the system.", ) else: user = user[0] password_reset_token = generate_token(email=email) send_email( type="password_recovery", email_to=user.email, name=user.name, surname=user.surname, token=password_reset_token, email_language=user.language_preference, ) return {"msg": "Password recovery email sent"}
def test_invalidation(): ''' A token that has been invalidated should raise an error ''' for user_id in range(100): new_token = generate_token(user_id) invalidate_token(new_token) with pytest.raises(AccessError): assert check_token(new_token) assert len(get_users()) == 0
async def create_demo_user( *, db: AsyncSession = Depends(deps.get_db), user_in: schemas.UserCreateDemo = Body( ..., example=request_examples["create_demo_user"]), ) -> Any: """ Create new user. """ user = await crud.user.get_by_key(db, key="email", value=user_in.email) if user and len(user) > 0: raise HTTPException( status_code=400, detail="The user with this email already exists in the system.", ) organization_demo = await crud.organization.get_by_key(db, key="name", value="demo") study_area_demo = await crud.study_area.get_by_key( db, key="id", value=settings.DEMO_USER_STUDY_AREA_ID) if len(organization_demo) == 0 or len(study_area_demo) == 0: raise HTTPException( status_code=400, detail= "Can't create a demo user at this time. Please contact the administrator.", ) organization_demo = organization_demo[0] study_area_demo = study_area_demo[0] user_in = user_in.dict() user_in.update({ "organization_id": organization_demo.id, "roles": ["user"], "active_study_area_id": study_area_demo.id, "active_data_upload_ids": [], "storage": 0, "limit_scenarios": settings.DEMO_USER_SCENARIO_LIMIT, "is_active": False, }) user_obj = schemas.UserCreate(**user_in) user = await crud.user.create(db, obj_in=user_obj) activate_token = generate_token(email=user.email) if settings.EMAILS_ENABLED and user.email: send_email( type="activate_new_account", email_to=user.email, name=user.name, surname=user.surname, token=activate_token, email_language=user.language_preference, ) return user
def auth_login(email, password): '''Given a valid email and password, logs a user in and returns a valid corresponding token :param email: user's email :type email: str :param password: user's password :type password: str :return: users token and user id :rtype: dict ''' u_id = check_login_inputs(email, password) token = generate_token(u_id) return { 'u_id': u_id, 'token': token, }
def auth_register(email, password, name_first, name_last): '''Registers a user, saves their information, and logs them in. Note: The first user is set as a slackr owner by default :param email: user's email :type email: str :param password: user's password :type password: str :param name_first: user's first name :type name_first: str :param name_last: user's last name :type name_last: str :return: user's token and user id :rtype: dict ''' # Checking inputs check_registration_inputs(email, password, name_first, name_last) glob_users = get_users() u_id = len(glob_users) password_hash = hash_string(password) handle_str = create_handle(name_first, name_last) is_owner = False if u_id == 0: is_owner = True glob_users[u_id] = { 'disabled': False, 'email': email, 'name_first': name_first, 'name_last': name_last, 'handle_str': handle_str, 'password_hash': password_hash, 'is_owner': is_owner, 'profile_img_url': '/imgurl/default.png' } token = generate_token(u_id) return { 'u_id': u_id, 'token': token, }
def create_token_for_uid(uid): token = generate_token() token_redis_client.set_user_token(uid=str(uid), token=token) return token