Module supertokens_python.recipe.thirdparty.interfaces

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 abc import ABC, abstractmethod
from typing import Union, TYPE_CHECKING, List
try:
    from typing import Literal
except ImportError:
    from typing_extensions import Literal

from .provider import Provider

if TYPE_CHECKING:
    from supertokens_python.framework import BaseRequest, BaseResponse
    from .utils import ThirdPartyConfig
    from .types import User, UsersResponse
    from supertokens_python.supertokens import AppInfo


class SignInUpResult(ABC):
    def __init__(self, status: Literal['OK', 'FIELD_ERROR'], user: Union[User, None] = None,
                 created_new_user: Union[bool, None] = None, error: Union[str, None] = None):
        self.status = status
        self.is_ok = False
        self.is_field_error = False
        self.user = user
        self.created_new_user = created_new_user
        self.error = error


class SignInUpOkResult(SignInUpResult):
    def __init__(self, user: User, created_new_user: bool):
        super().__init__('OK', user, created_new_user)
        self.is_ok = True


class SignInUpFieldErrorResult(SignInUpResult):
    def __init__(self, error: str):
        super().__init__('FIELD_ERROR', error=error)
        self.is_field_error = True


class RecipeInterface(ABC):
    def __init__(self):
        pass

    @abstractmethod
    async def get_user_by_id(self, user_id: str) -> Union[User, None]:
        pass

    @abstractmethod
    async def get_users_by_email(self, email: str) -> List[User]:
        pass

    @abstractmethod
    async def get_user_by_thirdparty_info(self, third_party_id: str, third_party_user_id: str) -> Union[User, None]:
        pass

    @abstractmethod
    async def sign_in_up(self, third_party_id: str, third_party_user_id: str, email: str,
                         email_verified: bool) -> SignInUpResult:
        pass

    @abstractmethod
    async def get_users_oldest_first(self, limit: int = None, next_pagination: str = None) -> UsersResponse:
        pass

    @abstractmethod
    async def get_users_newest_first(self, limit: int = None, next_pagination: str = None) -> UsersResponse:
        pass

    @abstractmethod
    async def get_user_count(self) -> int:
        pass


class APIOptions:
    def __init__(self, request: BaseRequest, response: Union[BaseResponse, None], recipe_id: str,
                 config: ThirdPartyConfig, recipe_implementation: RecipeInterface, providers: List[Provider],
                 app_info: AppInfo):
        self.request = request
        self.response = response
        self.recipe_id = recipe_id
        self.config = config
        self.providers = providers
        self.recipe_implementation = recipe_implementation
        self.app_info = app_info


class SignInUpPostResponse(ABC):
    def __init__(self, status: Literal['OK', 'NO_EMAIL_GIVEN_BY_PROVIDER', 'FIELD_ERROR'], user: Union[User, None] = None,
                 created_new_user: Union[bool, None] = None, auth_code_response: any = None,
                 error: Union[str, None] = None):
        self.type = 'thirdparty'
        self.status = status
        self.is_ok = False
        self.is_no_email_given_by_provider = False
        self.is_field_error = False
        self.user = user
        self.created_new_user = created_new_user
        self.error = error
        self.auth_code_response = auth_code_response

    @abstractmethod
    def to_json(self):
        pass


class GeneratePasswordResetTokenResponse(ABC):
    def __init__(self, status: Literal['OK']):
        self.status = status

    @abstractmethod
    def to_json(self):
        pass


class EmailExistsResponse(ABC):
    def __init__(self, status: Literal['OK'], exists: bool):
        self.status = status
        self.exists = exists

    @abstractmethod
    def to_json(self):
        pass


class PasswordResetResponse(ABC):
    def __init__(self, status: Literal['OK',
                 'RESET_PASSWORD_INVALID_TOKEN_ERROR']):
        self.status = status

    @abstractmethod
    def to_json(self):
        pass


class SignInUpPostOkResponse(SignInUpPostResponse):
    def __init__(self, user: User, created_new_user: bool,
                 auth_code_response: any):
        super().__init__('OK', user, created_new_user, auth_code_response)
        self.is_ok = True

    def to_json(self):
        return {
            'status': self.status,
            'user': {
                'id': self.user.user_id,
                'email': self.user.email,
                'timeJoined': self.user.time_joined,
                'thirdParty': {
                    'id': self.user.third_party_info.id,
                    'userId': self.user.third_party_info.user_id
                }
            },
            'createdNewUser': self.created_new_user
        }


class SignInUpPostNoEmailGivenByProviderResponse(SignInUpPostResponse):
    def __init__(self):
        super().__init__('NO_EMAIL_GIVEN_BY_PROVIDER')
        self.is_no_email_given_by_provider = True

    def to_json(self):
        return {
            'status': self.status
        }


class SignInUpPostFieldErrorResponse(SignInUpPostResponse):
    def __init__(self, error: str):
        super().__init__('FIELD_ERROR', error=error)
        self.is_field_error = True

    def to_json(self):
        return {
            'status': self.status,
            'error': self.error
        }


class AuthorisationUrlGetResponse(ABC):
    def __init__(self, status: Literal['OK'], url: str):
        self.status = status
        self.url = url

    def to_json(self):
        return {
            'status': self.status,
            'url': self.url
        }


class AuthorisationUrlGetOkResponse(AuthorisationUrlGetResponse):
    def __init__(self, url: str):
        super().__init__('OK', url)


class APIInterface:
    def __init__(self):
        self.disable_sign_in_up_post = False
        self.disable_authorisation_url_get = False
        self.disable_apple_redirect_handler_post = False

    async def authorisation_url_get(self, provider: Provider, api_options: APIOptions) -> AuthorisationUrlGetResponse:
        pass

    async def sign_in_up_post(self, provider: Provider, code: str, redirect_uri: str, client_id: Union[str, None],
                              auth_code_response: Union[str, None], api_options: APIOptions) -> SignInUpPostResponse:
        pass

    async def apple_redirect_handler_post(self, code: str, state: str, api_options: APIOptions):
        pass

Classes

class APIInterface
Expand source code
class APIInterface:
    def __init__(self):
        self.disable_sign_in_up_post = False
        self.disable_authorisation_url_get = False
        self.disable_apple_redirect_handler_post = False

    async def authorisation_url_get(self, provider: Provider, api_options: APIOptions) -> AuthorisationUrlGetResponse:
        pass

    async def sign_in_up_post(self, provider: Provider, code: str, redirect_uri: str, client_id: Union[str, None],
                              auth_code_response: Union[str, None], api_options: APIOptions) -> SignInUpPostResponse:
        pass

    async def apple_redirect_handler_post(self, code: str, state: str, api_options: APIOptions):
        pass

Subclasses

Methods

async def apple_redirect_handler_post(self, code: str, state: str, api_options: APIOptions)
Expand source code
async def apple_redirect_handler_post(self, code: str, state: str, api_options: APIOptions):
    pass
async def authorisation_url_get(self, provider: Provider, api_options: APIOptions) ‑> AuthorisationUrlGetResponse
Expand source code
async def authorisation_url_get(self, provider: Provider, api_options: APIOptions) -> AuthorisationUrlGetResponse:
    pass
async def sign_in_up_post(self, provider: Provider, code: str, redirect_uri: str, client_id: Union[str, None], auth_code_response: Union[str, None], api_options: APIOptions) ‑> SignInUpPostResponse
Expand source code
async def sign_in_up_post(self, provider: Provider, code: str, redirect_uri: str, client_id: Union[str, None],
                          auth_code_response: Union[str, None], api_options: APIOptions) -> SignInUpPostResponse:
    pass
class APIOptions (request: BaseRequest, response: Union[BaseResponse, None], recipe_id: str, config: ThirdPartyConfig, recipe_implementation: RecipeInterface, providers: List[Provider], app_info: AppInfo)
Expand source code
class APIOptions:
    def __init__(self, request: BaseRequest, response: Union[BaseResponse, None], recipe_id: str,
                 config: ThirdPartyConfig, recipe_implementation: RecipeInterface, providers: List[Provider],
                 app_info: AppInfo):
        self.request = request
        self.response = response
        self.recipe_id = recipe_id
        self.config = config
        self.providers = providers
        self.recipe_implementation = recipe_implementation
        self.app_info = app_info
class AuthorisationUrlGetOkResponse (url: str)

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

Expand source code
class AuthorisationUrlGetOkResponse(AuthorisationUrlGetResponse):
    def __init__(self, url: str):
        super().__init__('OK', url)

Ancestors

class AuthorisationUrlGetResponse (status: "Literal['OK']", url: str)

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

Expand source code
class AuthorisationUrlGetResponse(ABC):
    def __init__(self, status: Literal['OK'], url: str):
        self.status = status
        self.url = url

    def to_json(self):
        return {
            'status': self.status,
            'url': self.url
        }

Ancestors

  • abc.ABC

Subclasses

Methods

def to_json(self)
Expand source code
def to_json(self):
    return {
        'status': self.status,
        'url': self.url
    }
class EmailExistsResponse (status: "Literal['OK']", exists: bool)

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

Expand source code
class EmailExistsResponse(ABC):
    def __init__(self, status: Literal['OK'], exists: bool):
        self.status = status
        self.exists = exists

    @abstractmethod
    def to_json(self):
        pass

Ancestors

  • abc.ABC

Methods

def to_json(self)
Expand source code
@abstractmethod
def to_json(self):
    pass
class GeneratePasswordResetTokenResponse (status: "Literal['OK']")

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

Expand source code
class GeneratePasswordResetTokenResponse(ABC):
    def __init__(self, status: Literal['OK']):
        self.status = status

    @abstractmethod
    def to_json(self):
        pass

Ancestors

  • abc.ABC

Methods

def to_json(self)
Expand source code
@abstractmethod
def to_json(self):
    pass
class PasswordResetResponse (status: "Literal['OK', 'RESET_PASSWORD_INVALID_TOKEN_ERROR']")

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

Expand source code
class PasswordResetResponse(ABC):
    def __init__(self, status: Literal['OK',
                 'RESET_PASSWORD_INVALID_TOKEN_ERROR']):
        self.status = status

    @abstractmethod
    def to_json(self):
        pass

Ancestors

  • abc.ABC

Methods

def to_json(self)
Expand source code
@abstractmethod
def to_json(self):
    pass
class RecipeInterface

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

Expand source code
class RecipeInterface(ABC):
    def __init__(self):
        pass

    @abstractmethod
    async def get_user_by_id(self, user_id: str) -> Union[User, None]:
        pass

    @abstractmethod
    async def get_users_by_email(self, email: str) -> List[User]:
        pass

    @abstractmethod
    async def get_user_by_thirdparty_info(self, third_party_id: str, third_party_user_id: str) -> Union[User, None]:
        pass

    @abstractmethod
    async def sign_in_up(self, third_party_id: str, third_party_user_id: str, email: str,
                         email_verified: bool) -> SignInUpResult:
        pass

    @abstractmethod
    async def get_users_oldest_first(self, limit: int = None, next_pagination: str = None) -> UsersResponse:
        pass

    @abstractmethod
    async def get_users_newest_first(self, limit: int = None, next_pagination: str = None) -> UsersResponse:
        pass

    @abstractmethod
    async def get_user_count(self) -> int:
        pass

Ancestors

  • abc.ABC

Subclasses

Methods

async def get_user_by_id(self, user_id: str) ‑> Union[User, None]
Expand source code
@abstractmethod
async def get_user_by_id(self, user_id: str) -> Union[User, None]:
    pass
async def get_user_by_thirdparty_info(self, third_party_id: str, third_party_user_id: str) ‑> Union[User, None]
Expand source code
@abstractmethod
async def get_user_by_thirdparty_info(self, third_party_id: str, third_party_user_id: str) -> Union[User, None]:
    pass
async def get_user_count(self) ‑> int
Expand source code
@abstractmethod
async def get_user_count(self) -> int:
    pass
async def get_users_by_email(self, email: str) ‑> List[User]
Expand source code
@abstractmethod
async def get_users_by_email(self, email: str) -> List[User]:
    pass
async def get_users_newest_first(self, limit: int = None, next_pagination: str = None) ‑> UsersResponse
Expand source code
@abstractmethod
async def get_users_newest_first(self, limit: int = None, next_pagination: str = None) -> UsersResponse:
    pass
async def get_users_oldest_first(self, limit: int = None, next_pagination: str = None) ‑> UsersResponse
Expand source code
@abstractmethod
async def get_users_oldest_first(self, limit: int = None, next_pagination: str = None) -> UsersResponse:
    pass
async def sign_in_up(self, third_party_id: str, third_party_user_id: str, email: str, email_verified: bool) ‑> SignInUpResult
Expand source code
@abstractmethod
async def sign_in_up(self, third_party_id: str, third_party_user_id: str, email: str,
                     email_verified: bool) -> SignInUpResult:
    pass
class SignInUpFieldErrorResult (error: str)

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

Expand source code
class SignInUpFieldErrorResult(SignInUpResult):
    def __init__(self, error: str):
        super().__init__('FIELD_ERROR', error=error)
        self.is_field_error = True

Ancestors

class SignInUpOkResult (user: User, created_new_user: bool)

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

Expand source code
class SignInUpOkResult(SignInUpResult):
    def __init__(self, user: User, created_new_user: bool):
        super().__init__('OK', user, created_new_user)
        self.is_ok = True

Ancestors

class SignInUpPostFieldErrorResponse (error: str)

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

Expand source code
class SignInUpPostFieldErrorResponse(SignInUpPostResponse):
    def __init__(self, error: str):
        super().__init__('FIELD_ERROR', error=error)
        self.is_field_error = True

    def to_json(self):
        return {
            'status': self.status,
            'error': self.error
        }

Ancestors

Methods

def to_json(self)
Expand source code
def to_json(self):
    return {
        'status': self.status,
        'error': self.error
    }
class SignInUpPostNoEmailGivenByProviderResponse

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

Expand source code
class SignInUpPostNoEmailGivenByProviderResponse(SignInUpPostResponse):
    def __init__(self):
        super().__init__('NO_EMAIL_GIVEN_BY_PROVIDER')
        self.is_no_email_given_by_provider = True

    def to_json(self):
        return {
            'status': self.status
        }

Ancestors

Methods

def to_json(self)
Expand source code
def to_json(self):
    return {
        'status': self.status
    }
class SignInUpPostOkResponse (user: User, created_new_user: bool, auth_code_response: any)

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

Expand source code
class SignInUpPostOkResponse(SignInUpPostResponse):
    def __init__(self, user: User, created_new_user: bool,
                 auth_code_response: any):
        super().__init__('OK', user, created_new_user, auth_code_response)
        self.is_ok = True

    def to_json(self):
        return {
            'status': self.status,
            'user': {
                'id': self.user.user_id,
                'email': self.user.email,
                'timeJoined': self.user.time_joined,
                'thirdParty': {
                    'id': self.user.third_party_info.id,
                    'userId': self.user.third_party_info.user_id
                }
            },
            'createdNewUser': self.created_new_user
        }

Ancestors

Methods

def to_json(self)
Expand source code
def to_json(self):
    return {
        'status': self.status,
        'user': {
            'id': self.user.user_id,
            'email': self.user.email,
            'timeJoined': self.user.time_joined,
            'thirdParty': {
                'id': self.user.third_party_info.id,
                'userId': self.user.third_party_info.user_id
            }
        },
        'createdNewUser': self.created_new_user
    }
class SignInUpPostResponse (status: "Literal['OK', 'NO_EMAIL_GIVEN_BY_PROVIDER', 'FIELD_ERROR']", user: Union[User, None] = None, created_new_user: Union[bool, None] = None, auth_code_response: any = None, error: Union[str, None] = None)

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

Expand source code
class SignInUpPostResponse(ABC):
    def __init__(self, status: Literal['OK', 'NO_EMAIL_GIVEN_BY_PROVIDER', 'FIELD_ERROR'], user: Union[User, None] = None,
                 created_new_user: Union[bool, None] = None, auth_code_response: any = None,
                 error: Union[str, None] = None):
        self.type = 'thirdparty'
        self.status = status
        self.is_ok = False
        self.is_no_email_given_by_provider = False
        self.is_field_error = False
        self.user = user
        self.created_new_user = created_new_user
        self.error = error
        self.auth_code_response = auth_code_response

    @abstractmethod
    def to_json(self):
        pass

Ancestors

  • abc.ABC

Subclasses

Methods

def to_json(self)
Expand source code
@abstractmethod
def to_json(self):
    pass
class SignInUpResult (status: "Literal['OK', 'FIELD_ERROR']", user: Union[User, None] = None, created_new_user: Union[bool, None] = None, error: Union[str, None] = None)

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

Expand source code
class SignInUpResult(ABC):
    def __init__(self, status: Literal['OK', 'FIELD_ERROR'], user: Union[User, None] = None,
                 created_new_user: Union[bool, None] = None, error: Union[str, None] = None):
        self.status = status
        self.is_ok = False
        self.is_field_error = False
        self.user = user
        self.created_new_user = created_new_user
        self.error = error

Ancestors

  • abc.ABC

Subclasses