Skip to content

rs_server_common/authentication/external_authentication_config.md

<< Back to index

ExternalAuthenticationConfig implementation.

ExternalAuthenticationConfig dataclass

A configuration class for storing external authentication details, such as those used for API requiring token-based authentication.

Values are read either from the rs-server.yaml (in local mode) or RSPY__TOKEN__ env vars (in cluster mode).

Attributes:

Name Type Description
station_id str

The unique identifier for the station requesting the token.

domain str

The domain for the external service.

service_name str

The name of the external service.

service_url str

The URL of the external service.

auth_type str

The type of authentication used (e.g., 'token', 'basic').

Source code in docs/rs-server/services/common/rs_server_common/authentication/external_authentication_config.py
@dataclass
class ExternalAuthenticationConfig:  # pylint: disable=too-many-instance-attributes
    """
    A configuration class for storing external authentication details, such as those used for
    API requiring token-based authentication.

    Values are read either from the rs-server.yaml (in local mode) or RSPY__TOKEN__ env vars (in cluster mode).

    Attributes:
        station_id (str): The unique identifier for the station requesting the token.
        domain (str): The domain for the external service.
        service_name (str): The name of the external service.
        service_url (str): The URL of the external service.
        auth_type (str): The type of authentication used (e.g., 'token', 'basic').
    """

    station_id: str
    domain: str
    service_name: str
    service_url: str
    auth_type: str

S3ExternalAuthenticationConfig dataclass

Bases: ExternalAuthenticationConfig

Configuration class for storing external authentication details for S3 buckets.

Attributes:

Name Type Description
station_id str

The unique identifier for the station requesting the token.

domain str

The domain for the external service.

service_name str

The name of the external service.

service_url str

The URL of the external service.

auth_type str

The type of authentication used (e.g., 'token', 'basic').

access_key str

Access key to the S3 storage

secret_key str

Secret key to the S3 storage

trusted_domains Optional[str]

The list of allowed hosts for http redirection

Source code in docs/rs-server/services/common/rs_server_common/authentication/external_authentication_config.py
@dataclass
class S3ExternalAuthenticationConfig(ExternalAuthenticationConfig):
    """
    Configuration class for storing external authentication details for S3 buckets.

    Attributes:
        station_id (str): The unique identifier for the station requesting the token.
        domain (str): The domain for the external service.
        service_name (str): The name of the external service.
        service_url (str): The URL of the external service.
        auth_type (str): The type of authentication used (e.g., 'token', 'basic').
        access_key (str): Access key to the S3 storage
        secret_key (str): Secret key to the S3 storage
        trusted_domains (Optional[str]): The list of allowed hosts for http redirection
    """

    access_key: str
    secret_key: str
    max_requests_per_minute: int | None = None
    trusted_domains: list[str] | None = None

StationExternalAuthenticationConfig dataclass

Bases: ExternalAuthenticationConfig

Configuration class for storing external authentication details for stations.

Attributes:

Name Type Description
station_id str

The unique identifier for the station requesting the token.

domain str

The domain for the external service.

service_name str

The name of the external service.

service_url str

The URL of the external service.

auth_type str

The type of authentication used (e.g., 'token', 'basic').

token_url str

The URL to request the authentication token.

grant_type str

The grant type used for obtaining the token. Currently, only 'password' is available.

username str

The username used for authentication.

password str

The password used for authentication.

client_id str

The client ID used for authentication.

client_secret str

The client secret used for authentication.

scope Optional[str]

The scope of access requested in the authentication token (if applicable).

authorization Optional[str]

Additional authorization header (if required).

trusted_domains Optional[str]

The list of allowed hosts for http redirection

Source code in docs/rs-server/services/common/rs_server_common/authentication/external_authentication_config.py
@dataclass
class StationExternalAuthenticationConfig(ExternalAuthenticationConfig):
    """
    Configuration class for storing external authentication details for stations.

    Attributes:
        station_id (str): The unique identifier for the station requesting the token.
        domain (str): The domain for the external service.
        service_name (str): The name of the external service.
        service_url (str): The URL of the external service.
        auth_type (str): The type of authentication used (e.g., 'token', 'basic').
        token_url (str): The URL to request the authentication token.
        grant_type (str): The grant type used for obtaining the token. Currently, only 'password' is available.
        username (str): The username used for authentication.
        password (str): The password used for authentication.
        client_id (str): The client ID used for authentication.
        client_secret (str): The client secret used for authentication.
        scope (Optional[str]): The scope of access requested in the authentication token (if applicable).
        authorization (Optional[str]): Additional authorization header (if required).
        trusted_domains (Optional[str]): The list of allowed hosts for http redirection
    """

    token_url: str
    grant_type: str
    username: str
    password: str
    client_id: str
    client_secret: str
    max_requests_per_minute: int | None = None
    scope: str | None = None
    authorization: str | None = None
    trusted_domains: list[str] | None = None

create_external_auth_config(station_id, station_dict, service_dict)

Create an ExternalAuthenticationConfig object based on the provided station and service dictionaries.

Parameters:

Name Type Description Default
station_id str

The unique identifier for the station.

required
station_dict Dict[str, Any]

Dictionary containing station-specific configuration details.

required
service_dict Dict[str, Any]

Dictionary containing service-specific configuration details.

required

Returns:

Name Type Description
ExternalAuthenticationConfig StationExternalAuthenticationConfig | S3ExternalAuthenticationConfig | None

An object representing the external authentication configuration.

Raises:

Type Description
KeyError

If any required keys are missing in the configuration dictionaries.

Source code in docs/rs-server/services/common/rs_server_common/authentication/external_authentication_config.py
def create_external_auth_config(
    station_id: str,
    station_dict: dict[str, Any],
    service_dict: dict[str, Any],
) -> StationExternalAuthenticationConfig | S3ExternalAuthenticationConfig | None:
    """
    Create an ExternalAuthenticationConfig object based on the provided station and service dictionaries.

    Args:
        station_id (str): The unique identifier for the station.
        station_dict (Dict[str, Any]): Dictionary containing station-specific configuration details.
        service_dict (Dict[str, Any]): Dictionary containing service-specific configuration details.

    Returns:
        ExternalAuthenticationConfig: An object representing the external authentication configuration.

    Raises:
        KeyError: If any required keys are missing in the configuration dictionaries.
    """
    try:
        max_requests_per_minute = _parse_optional_positive_int(station_dict.get("max_requests_per_minute"))
        if service_dict["name"] == "s3":
            return S3ExternalAuthenticationConfig(
                station_id=station_id,
                domain=station_dict["domain"],
                service_name=service_dict["name"],
                service_url=service_dict["url"],
                auth_type=station_dict.get("authentication", {}).get("auth_type"),
                max_requests_per_minute=max_requests_per_minute,
                trusted_domains=station_dict.get("trusteddomains", None),
                access_key=station_dict.get("authentication", {}).get("access_key"),
                secret_key=station_dict.get("authentication", {}).get("secret_key"),
            )

        return StationExternalAuthenticationConfig(
            station_id=station_id,
            domain=station_dict["domain"],
            service_name=service_dict["name"],
            service_url=service_dict["url"],
            auth_type=station_dict.get("authentication", {}).get("auth_type"),
            max_requests_per_minute=max_requests_per_minute,
            token_url=station_dict.get("authentication", {}).get("token_url"),
            grant_type=station_dict.get("authentication", {}).get("grant_type"),
            username=station_dict.get("authentication", {}).get("username"),
            password=station_dict.get("authentication", {}).get("password"),
            client_id=station_dict.get("authentication", {}).get("client_id"),
            client_secret=station_dict.get("authentication", {}).get("client_secret"),
            scope=station_dict.get("authentication", {}).get("scope"),
            authorization=station_dict.get("authentication", {}).get("authorization"),
            trusted_domains=station_dict.get("trusteddomains", None),
        )
    except KeyError as e:
        logger.error(f"Error loading configuration, couldn't find a key: {e}")
    except (TypeError, ValueError) as e:
        logger.error(f"Error loading configuration, invalid max_requests_per_minute value: {e}")
    return None