Module supertokens_python.recipe.jwt.utils

Expand source code
# Copyright (c) 2021, 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 __future__ import annotations

from typing import Optional

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

from .interfaces import APIInterface, RecipeInterface

JWTOverrideConfig = BaseOverrideConfig[RecipeInterface, APIInterface]
NormalisedJWTOverrideConfig = BaseNormalisedOverrideConfig[
    RecipeInterface, APIInterface
]
OverrideConfig = JWTOverrideConfig
"""Deprecated, use `JWTOverrideConfig` instead."""


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

    jwt_validity_seconds: Optional[int] = None


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

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


class NormalisedJWTConfig(BaseNormalisedConfig[RecipeInterface, APIInterface]):
    jwt_validity_seconds: int


def validate_and_normalise_user_input(config: JWTConfig):
    override_config = NormalisedJWTOverrideConfig.from_input_config(
        override_config=config.override
    )

    jwt_validity_seconds = config.jwt_validity_seconds

    if config.jwt_validity_seconds is None:
        jwt_validity_seconds = 3153600000

    if not isinstance(jwt_validity_seconds, int):  # type: ignore
        raise ValueError("jwt_validity_seconds must be an integer or None")

    return NormalisedJWTConfig(
        jwt_validity_seconds=jwt_validity_seconds, override=override_config
    )

Functions

def validate_and_normalise_user_input(config: JWTConfig)

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 JWTConfig (**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 JWTConfig(
    JWTOverrideableConfig,
    BaseConfig[RecipeInterface, APIInterface, JWTOverrideableConfig],
):
    def to_overrideable_config(self) -> JWTOverrideableConfig:
        """Create a `JWTOverrideableConfig` from the current config."""
        return JWTOverrideableConfig(**self.model_dump())

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

Ancestors

Methods

def from_overrideable_config(self, overrideable_config: JWTOverrideableConfig) ‑> JWTConfig

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

def to_overrideable_config(self) ‑> JWTOverrideableConfig

Create a JWTOverrideableConfig from the current config.

Inherited members

class JWTOverrideableConfig (**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 JWTOverrideableConfig(BaseOverrideableConfig):
    """Input config properties overrideable using the plugin config overrides"""

    jwt_validity_seconds: Optional[int] = None

Ancestors

Subclasses

Class variables

var jwt_validity_seconds : Optional[int]

The type of the None singleton.

Inherited members

class NormalisedJWTConfig (**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 NormalisedJWTConfig(BaseNormalisedConfig[RecipeInterface, APIInterface]):
    jwt_validity_seconds: int

Ancestors

Class variables

var jwt_validity_seconds : int

The type of the None singleton.

Inherited members