Skip to content

Commit

Permalink
feat: allow anonymous authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
Ananias Carvalho committed Sep 20, 2024
1 parent d13143c commit 210e3aa
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 23 deletions.
9 changes: 3 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ This library enables you to manage Artifactory resources such as users, groups,
* [Authentication](#authentication)
+ [Basic authentication](#basic-authentication)
+ [Authentication with access token](#authentication-with-access-token)
+ [Anonymous authentication](#anonymous-authentication)
* [SSL Cert Verification Options](#ssl-cert-verification-options)
* [Timeout option](#timeout-option)
* [Admin objects](#admin-objects)
Expand Down Expand Up @@ -84,11 +83,9 @@ from pyartifactory import Artifactory
art = Artifactory(url="ARTIFACTORY_URL", access_token="your-access-token")
```

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

Expand Down
3 changes: 1 addition & 2 deletions pyartifactory/models/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +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
anonymous_auth: bool = False
verify: Union[bool, str] = True
cert: Optional[str] = None
api_version: int = 1
Expand Down
4 changes: 3 additions & 1 deletion pyartifactory/objects/artifactory.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
30 changes: 16 additions & 14 deletions pyartifactory/objects/object.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
"""
from __future__ import annotations

from typing import Optional, Tuple

import requests
from requests import Response

Expand All @@ -14,12 +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._anonymous_auth = self._artifactory.anonymous_auth
self._api_version = self._artifactory.api_version
self._verify = self._artifactory.verify
self._cert = self._artifactory.cert
Expand Down Expand Up @@ -80,17 +85,14 @@ def _generic_http_method_request(
:return: An HTTP response
"""

if self._anonymous_auth:
if self._access_token is not None:
headers = kwargs.get("headers", {})
headers["Authorization"] = f"Bearer {self._access_token}"
kwargs["headers"] = headers

auth = None
else:
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
auth = self._auth

http_method = getattr(self.session, method)
response: Response = http_method(
Expand Down

0 comments on commit 210e3aa

Please sign in to comment.