Skip to content

deep.api.auth

Services for customizing auth connection.

AuthProvider

Bases: ABC

This is the abstract class to define an AuthProvider.

The 'provide' function will be called when the system needs to get an auth token.

Source code in deep/api/auth/__init__.py
class AuthProvider(abc.ABC):
    """
    This is the abstract class to define an AuthProvider.

    The 'provide' function will be called when the system needs to get an auth token.
    """

    def __init__(self, config: ConfigService) -> None:
        """
        Create a new auth provider.

        :param config: the deep config service
        """
        self._config = config

    @staticmethod
    def get_provider(config: ConfigService) -> Optional['AuthProvider']:
        """
        Get the provider to use.

        Static function to load the correct auth provider based on the current config.

        :param config: The agent config
        :return: the loaded provider
        :raises: UnknownAuthProvider if we cannot load the provider configured
        """
        provider = config.SERVICE_AUTH_PROVIDER
        if provider is None or provider == "":
            return None

        module, cls = provider.rsplit(".", 1)
        provider_class = getattr(import_module(module), cls)
        if provider_class is None:
            raise UnknownAuthProvider("Cannot find auth provider with name: %s" % provider)

        return provider_class(config)

    @abc.abstractmethod
    def provide(self):
        """
        Provide the auth metadata.

        This is called when we need to get the auth for the request.

        :return: a list of tuples to be attached to the outbound request
        """
        raise NotImplementedError()

__init__(config)

Create a new auth provider.

:param config: the deep config service

Source code in deep/api/auth/__init__.py
def __init__(self, config: ConfigService) -> None:
    """
    Create a new auth provider.

    :param config: the deep config service
    """
    self._config = config

get_provider(config) staticmethod

Get the provider to use.

Static function to load the correct auth provider based on the current config.

:param config: The agent config :return: the loaded provider :raises: UnknownAuthProvider if we cannot load the provider configured

Source code in deep/api/auth/__init__.py
@staticmethod
def get_provider(config: ConfigService) -> Optional['AuthProvider']:
    """
    Get the provider to use.

    Static function to load the correct auth provider based on the current config.

    :param config: The agent config
    :return: the loaded provider
    :raises: UnknownAuthProvider if we cannot load the provider configured
    """
    provider = config.SERVICE_AUTH_PROVIDER
    if provider is None or provider == "":
        return None

    module, cls = provider.rsplit(".", 1)
    provider_class = getattr(import_module(module), cls)
    if provider_class is None:
        raise UnknownAuthProvider("Cannot find auth provider with name: %s" % provider)

    return provider_class(config)

provide() abstractmethod

Provide the auth metadata.

This is called when we need to get the auth for the request.

:return: a list of tuples to be attached to the outbound request

Source code in deep/api/auth/__init__.py
@abc.abstractmethod
def provide(self):
    """
    Provide the auth metadata.

    This is called when we need to get the auth for the request.

    :return: a list of tuples to be attached to the outbound request
    """
    raise NotImplementedError()

BasicAuthProvider

Bases: AuthProvider

This is a provider for http basic auth. This expects the config to provide a username and password.

Source code in deep/api/auth/__init__.py
class BasicAuthProvider(AuthProvider):
    """This is a provider for http basic auth. This expects the config to provide a username and password."""

    def provide(self):
        """
        Provide the auth metadata.

        This is called when we need to get the auth for the request.

        :return: a list of tuples to be attached to the outbound request
        """
        username = self._config.SERVICE_USERNAME
        password = self._config.SERVICE_PASSWORD
        if username is not None and password is not None:
            encode = base64.b64encode(
                (username + ':' + password).encode("utf-8"))
            return [('authorization', 'Basic%20' + encode.decode('utf-8'))]
        return []

provide()

Provide the auth metadata.

This is called when we need to get the auth for the request.

:return: a list of tuples to be attached to the outbound request

Source code in deep/api/auth/__init__.py
def provide(self):
    """
    Provide the auth metadata.

    This is called when we need to get the auth for the request.

    :return: a list of tuples to be attached to the outbound request
    """
    username = self._config.SERVICE_USERNAME
    password = self._config.SERVICE_PASSWORD
    if username is not None and password is not None:
        encode = base64.b64encode(
            (username + ':' + password).encode("utf-8"))
        return [('authorization', 'Basic%20' + encode.decode('utf-8'))]
    return []

UnknownAuthProvider

Bases: Exception

This exception is thrown when the configured auth provider cannot be loaded.

Source code in deep/api/auth/__init__.py
class UnknownAuthProvider(Exception):
    """This exception is thrown when the configured auth provider cannot be loaded."""

    pass