Ejemplo n.º 1
0
def login_access_token(form_data: OAuth2PasswordRequestForm = Depends()):
    """
    Generate access token for user
    """
    user = auth_handler.authenticate_user(username=form_data.username,
                                          password=form_data.password)
    if user is None:
        raise HTTPException(detail="Incorrect username and/or password",
                            status_code=400)

    return APIResponse(msg=TokenResponse(
        access_token=auth_handler.encode_token(user.id), token_type="bearer"))
Ejemplo n.º 2
0
def user_registration_confirmation(new_user: CreateUser):
    """
    Update user's password after confirmation by email
    """
    user: User = User.get_user(username=new_user.username)

    if not user.confirmed:
        raise HTTPException(detail="User's email is not confirmed",
                            status_code=400)

    hashed_password = auth_handler.get_password_hash(new_user.password)
    user.hashed_password = hashed_password
    user.confirmed = True
    user.update()
    return APIResponse(msg="Successfully registered")
Ejemplo n.º 3
0
def email_confirmation(new_user: UserTokenPayload):
    """
    User's personality confirmation via email
    """
    user: User = User.get_user(username=new_user.username)

    if user is None:
        raise HTTPException(detail="User is not exist", status_code=400)
    if user.confirmed:
        raise HTTPException(detail="User already confirmed", status_code=400)

    verified_token_status = auth_handler.verify_token(
        token=new_user.token, user_id=f"{new_user.email}:{new_user.username}")
    if not verified_token_status:
        raise HTTPException(detail="Confirmation token is not correct",
                            status_code=400)

    user.confirmed = True
    user.update()

    return APIResponse(msg="Success")
Ejemplo n.º 4
0
def user_registration(new_user: BaseUser):
    """
    Create user without password and send email to confirmation
    personality
    """
    token = auth_handler.encode_token(f"{new_user.email}:{new_user.username}")
    user, _status = User.get_or_create(username=new_user.username,
                                       email=new_user.email)
    if not _status:
        raise HTTPException(detail="User already exists", status_code=400)

    TaskManager.register_task(
        task="send_mail",
        args={
            "username": new_user.username,
            "email": new_user.email,
            "code": token,
        },
    )

    return APIResponse(msg="Send email")