Module supertokens_python.recipe.totp.api

Expand source code
# Copyright (c) 2024, VRAI Labs and/or its affiliates. All rights reserved.
#
# This software is licensed under the Apache License, Version 2.0 (the
# "License") as published by the Apache Software Foundation.
#
# You may not use this file except in compliance with the License. You may
# obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from .create_device import handle_create_device_api
from .list_devices import handle_list_devices_api
from .remove_device import handle_remove_device_api
from .verify_device import handle_verify_device_api
from .verify_totp import handle_verify_totp_api

__all__ = [
    "handle_create_device_api",
    "handle_list_devices_api",
    "handle_remove_device_api",
    "handle_verify_device_api",
    "handle_verify_totp_api",
]

Sub-modules

supertokens_python.recipe.totp.api.create_device
supertokens_python.recipe.totp.api.implementation
supertokens_python.recipe.totp.api.list_devices
supertokens_python.recipe.totp.api.remove_device
supertokens_python.recipe.totp.api.verify_device
supertokens_python.recipe.totp.api.verify_totp

Functions

async def handle_create_device_api(_: str, api_implementation: APIInterface, api_options: APIOptions, user_context: Dict[str, Any]) ‑> Union[BaseResponse, None]
Expand source code
async def handle_create_device_api(
    _: str,
    api_implementation: APIInterface,
    api_options: APIOptions,
    user_context: Dict[str, Any],
) -> Union[BaseResponse, None]:
    if api_implementation.disable_create_device_post:
        return None

    session = await get_session(
        api_options.request,
        override_global_claim_validators=lambda _, __, ___: [],
        user_context=user_context,
    )

    assert session is not None

    body = await api_options.request.json()
    if body is None:
        raise_bad_input_exception("Please provide a JSON body")

    device_name = body.get("deviceName")

    if device_name is not None and not isinstance(device_name, str):
        raise_bad_input_exception("deviceName must be a string")

    response = await api_implementation.create_device_post(
        device_name, api_options, session, user_context
    )

    return send_200_response(response.to_json(), api_options.response)
async def handle_list_devices_api(_: str, api_implementation: APIInterface, api_options: APIOptions, user_context: Dict[str, Any]) ‑> Union[BaseResponse, None]
Expand source code
async def handle_list_devices_api(
    _: str,
    api_implementation: APIInterface,
    api_options: APIOptions,
    user_context: Dict[str, Any],
) -> Union[BaseResponse, None]:
    if api_implementation.disable_list_devices_get:
        return None

    session = await get_session(
        api_options.request,
        override_global_claim_validators=lambda _, __, ___: [],
        session_required=True,
        user_context=user_context,
    )

    assert session is not None

    response = await api_implementation.list_devices_get(
        api_options, session, user_context
    )

    return send_200_response(response.to_json(), api_options.response)
async def handle_remove_device_api(_: str, api_implementation: APIInterface, api_options: APIOptions, user_context: Dict[str, Any]) ‑> Union[BaseResponse, None]
Expand source code
async def handle_remove_device_api(
    _: str,
    api_implementation: APIInterface,
    api_options: APIOptions,
    user_context: Dict[str, Any],
) -> Union[BaseResponse, None]:
    if api_implementation.disable_remove_device_post:
        return None

    session = await get_session(
        api_options.request,
        override_global_claim_validators=lambda _, __, ___: [],
        session_required=True,
        user_context=user_context,
    )

    assert session is not None

    body = await api_options.request.json()
    if body is None:
        raise_bad_input_exception("Please provide a JSON body")
    device_name = body.get("deviceName")

    if device_name is None or not isinstance(device_name, str) or len(device_name) == 0:
        raise Exception("deviceName is required and must be a non-empty string")

    response = await api_implementation.remove_device_post(
        device_name=device_name,
        options=api_options,
        session=session,
        user_context=user_context,
    )

    return send_200_response(response.to_json(), api_options.response)
async def handle_verify_device_api(_: str, api_implementation: APIInterface, api_options: APIOptions, user_context: Dict[str, Any]) ‑> Union[BaseResponse, None]
Expand source code
async def handle_verify_device_api(
    _: str,
    api_implementation: APIInterface,
    api_options: APIOptions,
    user_context: Dict[str, Any],
) -> Union[BaseResponse, None]:
    if api_implementation.disable_verify_device_post:
        return None

    session = await get_session(
        api_options.request,
        override_global_claim_validators=lambda _, __, ___: [],
        session_required=True,
        user_context=user_context,
    )

    assert session is not None

    body = await api_options.request.json()
    if body is None:
        raise_bad_input_exception("Please provide a JSON body")

    device_name = body.get("deviceName")
    totp = body.get("totp")

    if device_name is None or not isinstance(device_name, str):
        raise Exception("deviceName is required and must be a string")

    if totp is None or not isinstance(totp, str):
        raise Exception("totp is required and must be a string")

    response = await api_implementation.verify_device_post(
        device_name=device_name,
        totp=totp,
        options=api_options,
        session=session,
        user_context=user_context,
    )

    return send_200_response(response.to_json(), api_options.response)
async def handle_verify_totp_api(_: str, api_implementation: APIInterface, api_options: APIOptions, user_context: Dict[str, Any]) ‑> Union[BaseResponse, None]
Expand source code
async def handle_verify_totp_api(
    _: str,
    api_implementation: APIInterface,
    api_options: APIOptions,
    user_context: Dict[str, Any],
) -> Union[BaseResponse, None]:
    if api_implementation.disable_verify_totp_post:
        return None

    session = await get_session(
        api_options.request,
        override_global_claim_validators=lambda _, __, ___: [],
        session_required=True,
        user_context=user_context,
    )

    assert session is not None

    body = await api_options.request.json()
    if body is None:
        raise_bad_input_exception("Please provide a JSON body")

    totp = body.get("totp")

    if totp is None or not isinstance(totp, str):
        raise Exception("totp is required and must be a string")

    response = await api_implementation.verify_totp_post(
        totp=totp,
        options=api_options,
        session=session,
        user_context=user_context,
    )

    return send_200_response(response.to_json(), api_options.response)