diff --git a/README.md b/README.md index e38f161..f552af7 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,8 @@ This library enables you to manage Artifactory resources such as users, groups, - [Install](#install) - [Usage](#usage) * [Authentication](#authentication) + + [Basic authentication](#basic-authentication) + + [Authentication with access token](#authentication-with-access-token) * [SSL Cert Verification Options](#ssl-cert-verification-options) * [Timeout option](#timeout-option) * [Admin objects](#admin-objects) @@ -69,11 +71,22 @@ pip install pyartifactory Since Artifactory 6.6.0 there is version 2 of the REST API for permission management, in case you have that version or higher, you need to pass api_version=2 to the constructor when you instantiate the class. +#### Basic authentication ```python from pyartifactory import Artifactory art = Artifactory(url="ARTIFACTORY_URL", auth=('USERNAME','PASSWORD_OR_API_KEY'), api_version=1) ``` +#### Authentication with access token +```python +from pyartifactory import Artifactory +art = Artifactory(url="ARTIFACTORY_URL", access_token="your-access-token") +``` + +Note: +* If you set both `access_token` and `auth`, the access_token authentication will be chosen +* If you do not set any authentication method, API calls will be done without authentication (anonymous) + ### SSL Cert Verification Options Specify a local cert to use as client side certificate diff --git a/pyartifactory/models/auth.py b/pyartifactory/models/auth.py index b9e52e5..aafa93a 100644 --- a/pyartifactory/models/auth.py +++ b/pyartifactory/models/auth.py @@ -12,7 +12,8 @@ class AuthModel(BaseModel): """Models an auth response.""" url: str - auth: Tuple[str, SecretStr] + auth: Optional[Tuple[str, SecretStr]] = None + access_token: Optional[str] = None verify: Union[bool, str] = True cert: Optional[str] = None api_version: int = 1 diff --git a/pyartifactory/objects/artifactory.py b/pyartifactory/objects/artifactory.py index 1411199..dda2788 100644 --- a/pyartifactory/objects/artifactory.py +++ b/pyartifactory/objects/artifactory.py @@ -20,7 +20,8 @@ class Artifactory: def __init__( self, url: str, - auth: Tuple[str, SecretStr], + auth: Optional[Tuple[str, SecretStr]] = None, + access_token: Optional[str] = None, verify: Union[bool, str] = True, cert: Optional[str] = None, api_version: int = 1, @@ -29,6 +30,7 @@ def __init__( self.artifactory = AuthModel( url=url, auth=auth, + access_token=access_token, verify=verify, cert=cert, api_version=api_version, diff --git a/pyartifactory/objects/object.py b/pyartifactory/objects/object.py index 54b8daa..cb5c9e0 100644 --- a/pyartifactory/objects/object.py +++ b/pyartifactory/objects/object.py @@ -3,6 +3,8 @@ """ from __future__ import annotations +from typing import Optional, Tuple + import requests from requests import Response @@ -14,10 +16,15 @@ class ArtifactoryObject: def __init__(self, artifactory: AuthModel) -> None: self._artifactory = artifactory - self._auth = ( - self._artifactory.auth[0], - self._artifactory.auth[1].get_secret_value(), - ) + self._auth: Optional[Tuple[str, str]] = None + + if self._artifactory.auth is not None: + self._auth = ( + self._artifactory.auth[0], + self._artifactory.auth[1].get_secret_value(), + ) + + self._access_token = self._artifactory.access_token self._api_version = self._artifactory.api_version self._verify = self._artifactory.verify self._cert = self._artifactory.cert @@ -78,10 +85,19 @@ def _generic_http_method_request( :return: An HTTP response """ + if self._access_token is not None: + headers = kwargs.get("headers", {}) + headers["Authorization"] = f"Bearer {self._access_token}" + kwargs["headers"] = headers + + auth = None + else: + auth = self._auth + http_method = getattr(self.session, method) response: Response = http_method( f"{self._artifactory.url}/{route}", - auth=self._auth, + auth=auth, **kwargs, verify=self._verify, cert=self._cert,