Skip to content

Commit 210e3aa

Browse files
author
Ananias Carvalho
committed
feat: allow anonymous authentication
1 parent d13143c commit 210e3aa

4 files changed

Lines changed: 23 additions & 23 deletions

File tree

README.md

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ This library enables you to manage Artifactory resources such as users, groups,
1919
* [Authentication](#authentication)
2020
+ [Basic authentication](#basic-authentication)
2121
+ [Authentication with access token](#authentication-with-access-token)
22-
+ [Anonymous authentication](#anonymous-authentication)
2322
* [SSL Cert Verification Options](#ssl-cert-verification-options)
2423
* [Timeout option](#timeout-option)
2524
* [Admin objects](#admin-objects)
@@ -84,11 +83,9 @@ from pyartifactory import Artifactory
8483
art = Artifactory(url="ARTIFACTORY_URL", access_token="your-access-token")
8584
```
8685

87-
#### Anonymous authentication
88-
```python
89-
from pyartifactory import Artifactory
90-
art = Artifactory(url="ARTIFACTORY_URL", anonymous_auth=True)
91-
```
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)
9289

9390
### SSL Cert Verification Options
9491

pyartifactory/models/auth.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +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
1616
access_token: Optional[str] = None
17-
anonymous_auth: bool = False
1817
verify: Union[bool, str] = True
1918
cert: Optional[str] = None
2019
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: 16 additions & 14 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,12 +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+
2127
self._access_token = self._artifactory.access_token
22-
self._anonymous_auth = self._artifactory.anonymous_auth
2328
self._api_version = self._artifactory.api_version
2429
self._verify = self._artifactory.verify
2530
self._cert = self._artifactory.cert
@@ -80,17 +85,14 @@ def _generic_http_method_request(
8085
:return: An HTTP response
8186
"""
8287

83-
if self._anonymous_auth:
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+
8493
auth = None
8594
else:
86-
if self._access_token is not None:
87-
headers = kwargs.get("headers", {})
88-
headers["Authorization"] = f"Bearer {self._access_token}"
89-
kwargs["headers"] = headers
90-
91-
auth = None
92-
else:
93-
auth = self._auth
95+
auth = self._auth
9496

9597
http_method = getattr(self.session, method)
9698
response: Response = http_method(

0 commit comments

Comments
 (0)