async def on_verify(request):
    """
    Verifies an unverified account.
    """
    two_step_session = await verify_account(request)
    return json("You have verified your account and may login!",
                two_step_session.bearer.json())
async def on_authenticate(request, authentication_session):
    """
    Check if current authentication session is valid.
    """
    response = json("Authenticated!", authentication_session.bearer.json())
    authentication_session.encode(response)
    return response
async def on_logout(request, authentication_session):
    """
    Logout of currently logged in account.
    """
    await logout(authentication_session)
    response = json("Logout successful!", authentication_session.bearer.json())
    return response
async def on_login(request):
    """
    Login to an account with an email and password.
    """
    authentication_session = await login(
        request, two_factor=request.form.get("two_factor") == "true")
    if request.form.get("two_factor") == "true":
        two_step_session = await request_two_step_verification(
            request, authentication_session.bearer)
        response = json("Login successful! Second factor required.",
                        two_step_session.code)
        two_step_session.encode(response)
    else:
        response = json("Login successful!",
                        authentication_session.bearer.json())
    authentication_session.encode(response)
    return response
async def on_request_verification(request):
    """
    Two-step verification is requested with code in the response.
    """
    two_step_session = await request_two_step_verification(request)
    response = json("Verification request successful!", two_step_session.code)
    two_step_session.encode(response)
    return response
async def on_captcha_request(request):
    """
    Request captcha with solution in the response.
    """
    captcha_session = await request_captcha(request)
    response = json("Captcha request successful!", captcha_session.code)
    captcha_session.encode(response)
    return response
async def on_register(request):
    """
    Register an account with email and password.
    """
    account = await register(
        request,
        verified=request.form.get("verified") == "true",
        disabled=request.form.get("disabled") == "true",
    )
    if not account.verified:
        two_step_session = await request_two_step_verification(
            request, account)
        response = json("Registration successful! Verification required.",
                        two_step_session.code)
        two_step_session.encode(response)
    else:
        response = json("Registration successful!", account.json())
    return response
async def on_login_second_factor(request, two_step_verification):
    """
    Removes the second factor requirement from the client authentication session when the two-step verification attempt
    is successful.
    """
    authentication_session = await on_second_factor(request)
    response = json("Second factor attempt successful!",
                    authentication_session.bearer.json())
    return response
async def on_account_creation(request):
    """
    Creates a usable account.
    """
    try:
        username = "******"
        if request.form.get("username"):
            username = request.form.get("username")
        account = await Account.create(
            username=username,
            email=request.form.get("email"),
            password=password_hasher.hash("testtest"),
            verified=True,
            disabled=False,
        )
        response = json("Account creation successful!", account.json())
    except IntegrityError:
        response = json(
            "Account creation has failed due to an expected integrity error!",
            None)
    return response
Example #10
0
async def on_refresh(request):
    """
    Refresh client authentication session with a new session via the client session's refresh token. However, the new authentication
    session is never encoded. Due to the fact that the new session isn't encoded, attempting to refresh again will
    result in an error as a refresh token should only be used once.
    """
    refreshed_authentication_session = await refresh_authentication(request)
    response = json(
        "Authentication session refreshed!",
        refreshed_authentication_session.bearer.json(),
    )
    return response
Example #11
0
async def on_verification_attempt(request, two_step_session):
    """
    Attempt two-step verification.
    """
    return json("Two step verification attempt successful!",
                two_step_session.json())
Example #12
0
async def on_captcha_attempt(request, captcha_session):
    """
    Captcha challenge.
    """
    return json("Captcha attempt successful!", captcha_session.json())
 def __init__(self, message: str, code: int):
     self.json_response = json(message, self.__class__.__name__, code)
     super().__init__(message, code)