Module supertokens_python.recipe.openid.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 TYPE_CHECKING, Optional

from supertokens_python.normalised_url_domain import NormalisedURLDomain
from supertokens_python.normalised_url_path import NormalisedURLPath
from supertokens_python.types.config import (
    BaseConfig,
    BaseNormalisedConfig,
    BaseNormalisedOverrideConfig,
    BaseOverrideableConfig,
    BaseOverrideConfig,
)

from .interfaces import APIInterface, RecipeInterface

if TYPE_CHECKING:
    from supertokens_python import AppInfo


OpenIdOverrideConfig = BaseOverrideConfig[RecipeInterface, APIInterface]
NormalisedOpenIdOverrideConfig = BaseNormalisedOverrideConfig[
    RecipeInterface, APIInterface
]
InputOverrideConfig = OpenIdOverrideConfig
"""Deprecated, use `OpenIdOverrideConfig` instead."""


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

    issuer: Optional[str] = None


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

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


class NormalisedOpenIdConfig(BaseNormalisedConfig[RecipeInterface, APIInterface]):
    issuer_domain: NormalisedURLDomain
    issuer_path: NormalisedURLPath


def validate_and_normalise_user_input(
    app_info: AppInfo,
    config: OpenIdConfig,
) -> NormalisedOpenIdConfig:
    if config.issuer is None:
        issuer_domain = app_info.api_domain
        issuer_path = app_info.api_base_path
    else:
        issuer_domain = NormalisedURLDomain(config.issuer)
        issuer_path = NormalisedURLPath(config.issuer)

    if not issuer_path.equals(app_info.api_base_path):
        raise Exception(
            "The path of the issuer URL must be equal to the apiBasePath. The default value is /auth"
        )

    override_config = NormalisedOpenIdOverrideConfig.from_input_config(
        override_config=config.override
    )

    return NormalisedOpenIdConfig(
        issuer_domain=issuer_domain,
        issuer_path=issuer_path,
        override=override_config,
    )

Functions

def validate_and_normalise_user_input(app_info: AppInfo, config: OpenIdConfig)

Classes

class InputOverrideConfig (**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 NormalisedOpenIdConfig (**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 NormalisedOpenIdConfig(BaseNormalisedConfig[RecipeInterface, APIInterface]):
    issuer_domain: NormalisedURLDomain
    issuer_path: NormalisedURLPath

Ancestors

Class variables

var issuer_domainNormalisedURLDomain

The type of the None singleton.

var issuer_pathNormalisedURLPath

The type of the None singleton.

Inherited members

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

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

Ancestors

Methods

def from_overrideable_config(self, overrideable_config: OpenIdOverrideableConfig) ‑> OpenIdConfig

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

def to_overrideable_config(self) ‑> OpenIdOverrideableConfig

Create a OpenIdOverrideableConfig from the current config.

Inherited members

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

    issuer: Optional[str] = None

Ancestors

Subclasses

Class variables

var issuer : Optional[str]

The type of the None singleton.

Inherited members