Skip to content

Commit f3a494f

Browse files
authored
feat: add possibility to authenticate using access_token & anonymously
2 parents d7b7e85 + 210e3aa commit f3a494f

4 files changed

Lines changed: 39 additions & 7 deletions

File tree

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ This library enables you to manage Artifactory resources such as users, groups,
1717
- [Install](#install)
1818
- [Usage](#usage)
1919
* [Authentication](#authentication)
20+
+ [Basic authentication](#basic-authentication)
21+
+ [Authentication with access token](#authentication-with-access-token)
2022
* [SSL Cert Verification Options](#ssl-cert-verification-options)
2123
* [Timeout option](#timeout-option)
2224
* [Admin objects](#admin-objects)
@@ -69,11 +71,22 @@ pip install pyartifactory
6971

7072
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.
7173

74+
#### Basic authentication
7275
```python
7376
from pyartifactory import Artifactory
7477
art = Artifactory(url="ARTIFACTORY_URL", auth=('USERNAME','PASSWORD_OR_API_KEY'), api_version=1)
7578
```
7679

80+
#### Authentication with access token
81+
```python
82+
from pyartifactory import Artifactory
83+
art = Artifactory(url="ARTIFACTORY_URL", access_token="your-access-token")
84+
```
85+
86+
Note:
87+
* If you set both `access_token` and `auth`, the access_token authentication will be chosen
88+
* If you do not set any authentication method, API calls will be done without authentication (anonymous)
89+
7790
### SSL Cert Verification Options
7891

7992
Specify a local cert to use as client side certificate

pyartifactory/models/auth.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ class AuthModel(BaseModel):
1212
"""Models an auth response."""
1313

1414
url: str
15-
auth: Tuple[str, SecretStr]
15+
auth: Optional[Tuple[str, SecretStr]] = None
16+
access_token: Optional[str] = None
1617
verify: Union[bool, str] = True
1718
cert: Optional[str] = None
1819
api_version: int = 1

pyartifactory/objects/artifactory.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ class Artifactory:
2020
def __init__(
2121
self,
2222
url: str,
23-
auth: Tuple[str, SecretStr],
23+
auth: Optional[Tuple[str, SecretStr]] = None,
24+
access_token: Optional[str] = None,
2425
verify: Union[bool, str] = True,
2526
cert: Optional[str] = None,
2627
api_version: int = 1,
@@ -29,6 +30,7 @@ def __init__(
2930
self.artifactory = AuthModel(
3031
url=url,
3132
auth=auth,
33+
access_token=access_token,
3234
verify=verify,
3335
cert=cert,
3436
api_version=api_version,

pyartifactory/objects/object.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
"""
44
from __future__ import annotations
55

6+
from typing import Optional, Tuple
7+
68
import requests
79
from requests import Response
810

@@ -14,10 +16,15 @@ class ArtifactoryObject:
1416

1517
def __init__(self, artifactory: AuthModel) -> None:
1618
self._artifactory = artifactory
17-
self._auth = (
18-
self._artifactory.auth[0],
19-
self._artifactory.auth[1].get_secret_value(),
20-
)
19+
self._auth: Optional[Tuple[str, str]] = None
20+
21+
if self._artifactory.auth is not None:
22+
self._auth = (
23+
self._artifactory.auth[0],
24+
self._artifactory.auth[1].get_secret_value(),
25+
)
26+
27+
self._access_token = self._artifactory.access_token
2128
self._api_version = self._artifactory.api_version
2229
self._verify = self._artifactory.verify
2330
self._cert = self._artifactory.cert
@@ -78,10 +85,19 @@ def _generic_http_method_request(
7885
:return: An HTTP response
7986
"""
8087

88+
if self._access_token is not None:
89+
headers = kwargs.get("headers", {})
90+
headers["Authorization"] = f"Bearer {self._access_token}"
91+
kwargs["headers"] = headers
92+
93+
auth = None
94+
else:
95+
auth = self._auth
96+
8197
http_method = getattr(self.session, method)
8298
response: Response = http_method(
8399
f"{self._artifactory.url}/{route}",
84-
auth=self._auth,
100+
auth=auth,
85101
**kwargs,
86102
verify=self._verify,
87103
cert=self._cert,

0 commit comments

Comments
 (0)