Module supertokens_python.recipe.totp.types

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 typing import Any, Dict, List, Optional

from typing_extensions import Literal

from supertokens_python.types.config import (
    BaseConfig,
    BaseNormalisedConfig,
    BaseNormalisedOverrideConfig,
    BaseOverrideableConfig,
    BaseOverrideConfig,
)
from supertokens_python.types.response import APIResponse

from .interfaces import APIInterface, RecipeInterface


class OkResult(APIResponse):
    def __init__(self):
        self.status: Literal["OK"] = "OK"


class UserIdentifierInfoOkResult(OkResult):
    def __init__(self, info: str):
        super().__init__()
        self.info: str = info

    def to_json(self) -> Dict[str, Any]:
        raise NotImplementedError()


class UnknownUserIdError(APIResponse):
    def __init__(self):
        self.status: Literal["UNKNOWN_USER_ID_ERROR"] = "UNKNOWN_USER_ID_ERROR"

    def to_json(self) -> Dict[str, Any]:
        return {"status": self.status}


class UserIdentifierInfoDoesNotExistError:
    def __init__(self):
        self.status: Literal["USER_IDENTIFIER_INFO_DOES_NOT_EXIST_ERROR"] = (
            "USER_IDENTIFIER_INFO_DOES_NOT_EXIST_ERROR"
        )


class CreateDeviceOkResult(OkResult):
    def __init__(self, device_name: str, secret: str, qr_code_string: str):
        super().__init__()
        self.device_name: str = device_name
        self.secret: str = secret
        self.qr_code_string: str = qr_code_string

    def to_json(self) -> Dict[str, Any]:
        return {
            "status": self.status,
            "deviceName": self.device_name,
            "secret": self.secret,
            "qrCodeString": self.qr_code_string,
        }


class DeviceAlreadyExistsError(APIResponse):
    def __init__(self):
        self.status: Literal["DEVICE_ALREADY_EXISTS_ERROR"] = (
            "DEVICE_ALREADY_EXISTS_ERROR"
        )

    def to_json(self) -> Dict[str, Any]:
        return {"status": self.status}


class UpdateDeviceOkResult(OkResult):
    def to_json(self) -> Dict[str, Any]:
        raise NotImplementedError()


class UnknownDeviceError(APIResponse):
    def __init__(self):
        self.status: Literal["UNKNOWN_DEVICE_ERROR"] = "UNKNOWN_DEVICE_ERROR"

    def to_json(self) -> Dict[str, Any]:
        return {"status": self.status}


class Device(APIResponse):
    def __init__(self, name: str, period: int, skew: int, verified: bool):
        self.name: str = name
        self.period: int = period
        self.skew: int = skew
        self.verified: bool = verified

    def to_json(self) -> Dict[str, Any]:
        return {
            "name": self.name,
            "period": self.period,
            "skew": self.skew,
            "verified": self.verified,
        }


class ListDevicesOkResult(OkResult):
    def __init__(self, devices: List[Device]):
        super().__init__()
        self.devices: List[Device] = devices

    def to_json(self) -> Dict[str, Any]:
        return {
            "status": self.status,
            "devices": [device.to_json() for device in self.devices],
        }


class RemoveDeviceOkResult(OkResult):
    def __init__(self, did_device_exist: bool):
        super().__init__()
        self.did_device_exist: bool = did_device_exist

    def to_json(self) -> Dict[str, Any]:
        return {
            "status": self.status,
            "didDeviceExist": self.did_device_exist,
        }


class VerifyDeviceOkResult(OkResult):
    def __init__(
        self,
        was_already_verified: bool,
    ):
        super().__init__()
        self.was_already_verified: bool = was_already_verified

    def to_json(self) -> Dict[str, Any]:
        return {
            "status": self.status,
            "wasAlreadyVerified": self.was_already_verified,
        }


class InvalidTOTPError(APIResponse):
    def __init__(
        self, current_number_of_failed_attempts: int, max_number_of_failed_attempts: int
    ):
        self.status: Literal["INVALID_TOTP_ERROR"] = "INVALID_TOTP_ERROR"
        self.current_number_of_failed_attempts: int = current_number_of_failed_attempts
        self.max_number_of_failed_attempts: int = max_number_of_failed_attempts

    def to_json(self) -> Dict[str, Any]:
        return {
            "status": self.status,
            "currentNumberOfFailedAttempts": self.current_number_of_failed_attempts,
            "maxNumberOfFailedAttempts": self.max_number_of_failed_attempts,
        }


class LimitReachedError(APIResponse):
    def __init__(self, retry_after_ms: int):
        self.status: Literal["LIMIT_REACHED_ERROR"] = "LIMIT_REACHED_ERROR"
        self.retry_after_ms: int = retry_after_ms

    def to_json(self) -> Dict[str, Any]:
        return {
            "status": self.status,
            "retryAfterMs": self.retry_after_ms,
        }


class VerifyTOTPOkResult(OkResult):
    def to_json(self) -> Dict[str, Any]:
        return {"status": self.status}


TOTPOverrideConfig = BaseOverrideConfig[RecipeInterface, APIInterface]
NormalisedTOTPOverrideConfig = BaseNormalisedOverrideConfig[
    RecipeInterface, APIInterface
]
OverrideConfig = TOTPOverrideConfig
"""Deprecated: Use `TOTPOverrideConfig` instead."""


class TOTPOverrideableConfig(BaseOverrideableConfig):
    """Input config properties overrideable using the plugin config overrides"""

    issuer: Optional[str] = None
    default_skew: Optional[int] = None
    default_period: Optional[int] = None


class TOTPConfig(
    TOTPOverrideableConfig,
    BaseConfig[RecipeInterface, APIInterface, TOTPOverrideableConfig],
):
    def to_overrideable_config(self) -> TOTPOverrideableConfig:
        """Create a `TOTPOverrideableConfig` from the current config."""
        return TOTPOverrideableConfig(**self.model_dump())

    def from_overrideable_config(
        self,
        overrideable_config: TOTPOverrideableConfig,
    ) -> "TOTPConfig":
        """
        Create a `TOTPConfig` from a `TOTPOverrideableConfig`.
        Not a classmethod since it needs to be used in a dynamic context within plugins.
        """
        return TOTPConfig(
            **overrideable_config.model_dump(),
            override=self.override,
        )


class NormalisedTOTPConfig(BaseNormalisedConfig[RecipeInterface, APIInterface]):
    issuer: str
    default_skew: int
    default_period: int

Classes

class OverrideConfig (**data: Any)

Base class for input override config with API overrides.

Create a new model by parsing and validating input data from keyword arguments.

Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.

self is explicitly positional-only to allow self as a field name.

Ancestors

Inherited members

class CreateDeviceOkResult (device_name: str, secret: str, qr_code_string: str)

Helper class that provides a standard way to create an ABC using inheritance.

Expand source code
class CreateDeviceOkResult(OkResult):
    def __init__(self, device_name: str, secret: str, qr_code_string: str):
        super().__init__()
        self.device_name: str = device_name
        self.secret: str = secret
        self.qr_code_string: str = qr_code_string

    def to_json(self) -> Dict[str, Any]:
        return {
            "status": self.status,
            "deviceName": self.device_name,
            "secret": self.secret,
            "qrCodeString": self.qr_code_string,
        }

Ancestors

Methods

def to_json(self) ‑> Dict[str, Any]
class Device (name: str, period: int, skew: int, verified: bool)

Helper class that provides a standard way to create an ABC using inheritance.

Expand source code
class Device(APIResponse):
    def __init__(self, name: str, period: int, skew: int, verified: bool):
        self.name: str = name
        self.period: int = period
        self.skew: int = skew
        self.verified: bool = verified

    def to_json(self) -> Dict[str, Any]:
        return {
            "name": self.name,
            "period": self.period,
            "skew": self.skew,
            "verified": self.verified,
        }

Ancestors

Methods

def to_json(self) ‑> Dict[str, Any]
class DeviceAlreadyExistsError

Helper class that provides a standard way to create an ABC using inheritance.

Expand source code
class DeviceAlreadyExistsError(APIResponse):
    def __init__(self):
        self.status: Literal["DEVICE_ALREADY_EXISTS_ERROR"] = (
            "DEVICE_ALREADY_EXISTS_ERROR"
        )

    def to_json(self) -> Dict[str, Any]:
        return {"status": self.status}

Ancestors

Methods

def to_json(self) ‑> Dict[str, Any]
class InvalidTOTPError (current_number_of_failed_attempts: int, max_number_of_failed_attempts: int)

Helper class that provides a standard way to create an ABC using inheritance.

Expand source code
class InvalidTOTPError(APIResponse):
    def __init__(
        self, current_number_of_failed_attempts: int, max_number_of_failed_attempts: int
    ):
        self.status: Literal["INVALID_TOTP_ERROR"] = "INVALID_TOTP_ERROR"
        self.current_number_of_failed_attempts: int = current_number_of_failed_attempts
        self.max_number_of_failed_attempts: int = max_number_of_failed_attempts

    def to_json(self) -> Dict[str, Any]:
        return {
            "status": self.status,
            "currentNumberOfFailedAttempts": self.current_number_of_failed_attempts,
            "maxNumberOfFailedAttempts": self.max_number_of_failed_attempts,
        }

Ancestors

Methods

def to_json(self) ‑> Dict[str, Any]
class LimitReachedError (retry_after_ms: int)

Helper class that provides a standard way to create an ABC using inheritance.

Expand source code
class LimitReachedError(APIResponse):
    def __init__(self, retry_after_ms: int):
        self.status: Literal["LIMIT_REACHED_ERROR"] = "LIMIT_REACHED_ERROR"
        self.retry_after_ms: int = retry_after_ms

    def to_json(self) -> Dict[str, Any]:
        return {
            "status": self.status,
            "retryAfterMs": self.retry_after_ms,
        }

Ancestors

Methods

def to_json(self) ‑> Dict[str, Any]
class ListDevicesOkResult (devices: List[Device])

Helper class that provides a standard way to create an ABC using inheritance.

Expand source code
class ListDevicesOkResult(OkResult):
    def __init__(self, devices: List[Device]):
        super().__init__()
        self.devices: List[Device] = devices

    def to_json(self) -> Dict[str, Any]:
        return {
            "status": self.status,
            "devices": [device.to_json() for device in self.devices],
        }

Ancestors

Methods

def to_json(self) ‑> Dict[str, Any]
class NormalisedTOTPConfig (**data: Any)

Base class for normalized config of a Recipe with API overrides.

Create a new model by parsing and validating input data from keyword arguments.

Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.

self is explicitly positional-only to allow self as a field name.

Expand source code
class NormalisedTOTPConfig(BaseNormalisedConfig[RecipeInterface, APIInterface]):
    issuer: str
    default_skew: int
    default_period: int

Ancestors

Class variables

var default_period : int

The type of the None singleton.

var default_skew : int

The type of the None singleton.

var issuer : str

The type of the None singleton.

Inherited members

class OkResult

Helper class that provides a standard way to create an ABC using inheritance.

Expand source code
class OkResult(APIResponse):
    def __init__(self):
        self.status: Literal["OK"] = "OK"

Ancestors

Subclasses

class RemoveDeviceOkResult (did_device_exist: bool)

Helper class that provides a standard way to create an ABC using inheritance.

Expand source code
class RemoveDeviceOkResult(OkResult):
    def __init__(self, did_device_exist: bool):
        super().__init__()
        self.did_device_exist: bool = did_device_exist

    def to_json(self) -> Dict[str, Any]:
        return {
            "status": self.status,
            "didDeviceExist": self.did_device_exist,
        }

Ancestors

Methods

def to_json(self) ‑> Dict[str, Any]
class TOTPConfig (**data: Any)

Input config properties overrideable using the plugin config overrides

Create a new model by parsing and validating input data from keyword arguments.

Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.

self is explicitly positional-only to allow self as a field name.

Expand source code
class TOTPConfig(
    TOTPOverrideableConfig,
    BaseConfig[RecipeInterface, APIInterface, TOTPOverrideableConfig],
):
    def to_overrideable_config(self) -> TOTPOverrideableConfig:
        """Create a `TOTPOverrideableConfig` from the current config."""
        return TOTPOverrideableConfig(**self.model_dump())

    def from_overrideable_config(
        self,
        overrideable_config: TOTPOverrideableConfig,
    ) -> "TOTPConfig":
        """
        Create a `TOTPConfig` from a `TOTPOverrideableConfig`.
        Not a classmethod since it needs to be used in a dynamic context within plugins.
        """
        return TOTPConfig(
            **overrideable_config.model_dump(),
            override=self.override,
        )

Ancestors

Methods

def from_overrideable_config(self, overrideable_config: TOTPOverrideableConfig) ‑> TOTPConfig

Create a TOTPConfig from a TOTPOverrideableConfig. Not a classmethod since it needs to be used in a dynamic context within plugins.

def to_overrideable_config(self) ‑> TOTPOverrideableConfig

Create a TOTPOverrideableConfig from the current config.

Inherited members

class TOTPOverrideableConfig (**data: Any)

Input config properties overrideable using the plugin config overrides

Create a new model by parsing and validating input data from keyword arguments.

Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.

self is explicitly positional-only to allow self as a field name.

Expand source code
class TOTPOverrideableConfig(BaseOverrideableConfig):
    """Input config properties overrideable using the plugin config overrides"""

    issuer: Optional[str] = None
    default_skew: Optional[int] = None
    default_period: Optional[int] = None

Ancestors

Subclasses

Class variables

var default_period : Optional[int]

The type of the None singleton.

var default_skew : Optional[int]

The type of the None singleton.

var issuer : Optional[str]

The type of the None singleton.

Inherited members

class UnknownDeviceError

Helper class that provides a standard way to create an ABC using inheritance.

Expand source code
class UnknownDeviceError(APIResponse):
    def __init__(self):
        self.status: Literal["UNKNOWN_DEVICE_ERROR"] = "UNKNOWN_DEVICE_ERROR"

    def to_json(self) -> Dict[str, Any]:
        return {"status": self.status}

Ancestors

Methods

def to_json(self) ‑> Dict[str, Any]
class UnknownUserIdError

Helper class that provides a standard way to create an ABC using inheritance.

Expand source code
class UnknownUserIdError(APIResponse):
    def __init__(self):
        self.status: Literal["UNKNOWN_USER_ID_ERROR"] = "UNKNOWN_USER_ID_ERROR"

    def to_json(self) -> Dict[str, Any]:
        return {"status": self.status}

Ancestors

Methods

def to_json(self) ‑> Dict[str, Any]
class UpdateDeviceOkResult

Helper class that provides a standard way to create an ABC using inheritance.

Expand source code
class UpdateDeviceOkResult(OkResult):
    def to_json(self) -> Dict[str, Any]:
        raise NotImplementedError()

Ancestors

Methods

def to_json(self) ‑> Dict[str, Any]
class UserIdentifierInfoDoesNotExistError
Expand source code
class UserIdentifierInfoDoesNotExistError:
    def __init__(self):
        self.status: Literal["USER_IDENTIFIER_INFO_DOES_NOT_EXIST_ERROR"] = (
            "USER_IDENTIFIER_INFO_DOES_NOT_EXIST_ERROR"
        )
class UserIdentifierInfoOkResult (info: str)

Helper class that provides a standard way to create an ABC using inheritance.

Expand source code
class UserIdentifierInfoOkResult(OkResult):
    def __init__(self, info: str):
        super().__init__()
        self.info: str = info

    def to_json(self) -> Dict[str, Any]:
        raise NotImplementedError()

Ancestors

Methods

def to_json(self) ‑> Dict[str, Any]
class VerifyDeviceOkResult (was_already_verified: bool)

Helper class that provides a standard way to create an ABC using inheritance.

Expand source code
class VerifyDeviceOkResult(OkResult):
    def __init__(
        self,
        was_already_verified: bool,
    ):
        super().__init__()
        self.was_already_verified: bool = was_already_verified

    def to_json(self) -> Dict[str, Any]:
        return {
            "status": self.status,
            "wasAlreadyVerified": self.was_already_verified,
        }

Ancestors

Methods

def to_json(self) ‑> Dict[str, Any]
class VerifyTOTPOkResult

Helper class that provides a standard way to create an ABC using inheritance.

Expand source code
class VerifyTOTPOkResult(OkResult):
    def to_json(self) -> Dict[str, Any]:
        return {"status": self.status}

Ancestors

Methods

def to_json(self) ‑> Dict[str, Any]