Skip to content

Référence pour hub_sdk/base/auth.py

Note

Ce fichier est disponible à l'adresse https://github.com/ultralytics/ hub-sdk/blob/main/ hub_sdk/base/auth .py. Si tu repères un problème, aide à le corriger en contribuant à une Pull Request 🛠️. Merci 🙏 !



hub_sdk.base.auth.Auth

Représente un gestionnaire d'authentification.

Attributs :

Nom Type Description
api_key (str, None)

La clé API utilisée pour l'authentification.

id_token (str, None)

Le jeton d'authentification.

Code source dans hub_sdk/base/auth.py
class Auth:
    """
    Represents an authentication manager.

    Attributes:
        api_key (str, None): The API key used for authentication.
        id_token (str, None): The authentication token.
    """

    def __init__(self):
        """Initializes the Auth class with default authentication settings."""
        self.api_key = None
        self.id_token = None

    def authenticate(self) -> bool:
        """
        Attempt to authenticate with the server using either id_token or API key.

        Returns:
            (bool): True if authentication is successful, False otherwise.

        Raises:
            (ConnectionError): If request response is hasn't success in json, raised connection error exception.
        """
        try:
            header = self.get_auth_header()
            if header:
                r = requests.post(f"{HUB_API_ROOT}/v1/auth", headers=header)
                if not r.json().get("success", False):
                    raise ConnectionError("Unable to authenticate.")
                return True
            raise ConnectionError("User has not authenticated locally.")
        except ConnectionError:
            logger.warning(f"{PREFIX} Invalid API key ⚠️")
        except requests.exceptions.RequestException as e:
            status_code = None
            if hasattr(e, "response"):
                status_code = e.response.status_code

            error_msg = ErrorHandler(status_code).handle()
            logger.warning(f"{PREFIX} {error_msg}")

        self.id_token = self.api_key = False  # reset invalid
        return False

    def get_auth_header(self) -> Optional[dict]:
        """
        Get the authentication header for making API requests.

        Returns:
            (Optional[dict]): The authentication header if id_token or API key is set, None otherwise.
        """
        if self.id_token:
            return {"authorization": f"Bearer {self.id_token}"}
        elif self.api_key:
            return {"x-api-key": self.api_key}
        else:
            return None

    def get_state(self) -> bool:
        """
        Get the authentication state.

        Returns:
            (bool): True if either id_token or API key is set, False otherwise.
        """
        return self.id_token or self.api_key

    def set_api_key(self, key: str):
        """
        Set the API key for authentication.

        Args:
            key (str): The API key string.
        """
        self.api_key = key

    def authorize(self, email: str, password: str) -> bool:
        """
        Authorize the user by obtaining an idToken through a POST request with email and password.

        Args:
            email (str): User's email.
            password (str): User's password.

        Returns:
            (bool): True if authorization is successful, False otherwise.
        """
        try:
            headers = {"origin": HUB_WEB_ROOT}
            response = requests.post(FIREBASE_AUTH_URL, json={"email": email, "password": password}, headers=headers)
            if response.status_code == 200:
                self.id_token = response.json().get("idToken")
                return True
            else:
                raise ConnectionError("Authorization failed.")
        except ConnectionError:
            logger.warning(f"{PREFIX} Invalid API key ⚠️")
        except requests.exceptions.RequestException as e:
            status_code = None
            if hasattr(e, "response"):
                status_code = e.response.status_code

            error_msg = ErrorHandler(status_code).handle()
            logger.warning(f"{PREFIX} {error_msg}")

__init__()

Initialise la classe Auth avec les paramètres d'authentification par défaut.

Code source dans hub_sdk/base/auth.py
def __init__(self):
    """Initializes the Auth class with default authentication settings."""
    self.api_key = None
    self.id_token = None

authenticate()

Tente de s'authentifier auprès du serveur à l'aide de l'id_token ou de la clé API.

Retourne :

Type Description
bool

Vrai si l'authentification est réussie, Faux sinon.

Augmente :

Type Description
ConnectionError

Si la réponse à la demande n'est pas réussie dans le fichier json, une exception d'erreur de connexion est soulevée.

Code source dans hub_sdk/base/auth.py
def authenticate(self) -> bool:
    """
    Attempt to authenticate with the server using either id_token or API key.

    Returns:
        (bool): True if authentication is successful, False otherwise.

    Raises:
        (ConnectionError): If request response is hasn't success in json, raised connection error exception.
    """
    try:
        header = self.get_auth_header()
        if header:
            r = requests.post(f"{HUB_API_ROOT}/v1/auth", headers=header)
            if not r.json().get("success", False):
                raise ConnectionError("Unable to authenticate.")
            return True
        raise ConnectionError("User has not authenticated locally.")
    except ConnectionError:
        logger.warning(f"{PREFIX} Invalid API key ⚠️")
    except requests.exceptions.RequestException as e:
        status_code = None
        if hasattr(e, "response"):
            status_code = e.response.status_code

        error_msg = ErrorHandler(status_code).handle()
        logger.warning(f"{PREFIX} {error_msg}")

    self.id_token = self.api_key = False  # reset invalid
    return False

authorize(email, password)

Autorise l'utilisateur en obtenant un idToken par le biais d'une requĂŞte POST avec l'email et le mot de passe.

Paramètres :

Nom Type Description DĂ©faut
email str

Courriel de l'utilisateur.

requis
password str

Mot de passe de l'utilisateur.

requis

Retourne :

Type Description
bool

Vrai si l'autorisation est réussie, Faux sinon.

Code source dans hub_sdk/base/auth.py
def authorize(self, email: str, password: str) -> bool:
    """
    Authorize the user by obtaining an idToken through a POST request with email and password.

    Args:
        email (str): User's email.
        password (str): User's password.

    Returns:
        (bool): True if authorization is successful, False otherwise.
    """
    try:
        headers = {"origin": HUB_WEB_ROOT}
        response = requests.post(FIREBASE_AUTH_URL, json={"email": email, "password": password}, headers=headers)
        if response.status_code == 200:
            self.id_token = response.json().get("idToken")
            return True
        else:
            raise ConnectionError("Authorization failed.")
    except ConnectionError:
        logger.warning(f"{PREFIX} Invalid API key ⚠️")
    except requests.exceptions.RequestException as e:
        status_code = None
        if hasattr(e, "response"):
            status_code = e.response.status_code

        error_msg = ErrorHandler(status_code).handle()
        logger.warning(f"{PREFIX} {error_msg}")

get_auth_header()

Récupère l'en-tête d'authentification pour faire des demandes d'API.

Retourne :

Type Description
Optional[dict]

L'en-tête d'authentification si id_token ou la clé API est définie, Aucun sinon.

Code source dans hub_sdk/base/auth.py
def get_auth_header(self) -> Optional[dict]:
    """
    Get the authentication header for making API requests.

    Returns:
        (Optional[dict]): The authentication header if id_token or API key is set, None otherwise.
    """
    if self.id_token:
        return {"authorization": f"Bearer {self.id_token}"}
    elif self.api_key:
        return {"x-api-key": self.api_key}
    else:
        return None

get_state()

Obtenir l'Ă©tat de l'authentification.

Retourne :

Type Description
bool

True si l'id_token ou la clé API est définie, False sinon.

Code source dans hub_sdk/base/auth.py
def get_state(self) -> bool:
    """
    Get the authentication state.

    Returns:
        (bool): True if either id_token or API key is set, False otherwise.
    """
    return self.id_token or self.api_key

set_api_key(key)

Définis la clé API pour l'authentification.

Paramètres :

Nom Type Description DĂ©faut
key str

La chaîne de la clé de l'API.

requis
Code source dans hub_sdk/base/auth.py
def set_api_key(self, key: str):
    """
    Set the API key for authentication.

    Args:
        key (str): The API key string.
    """
    self.api_key = key