Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions osctiny/osc.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
from .extensions.staging import Staging
from .extensions.users import Group, Person
from .models import ParamsType
from .utils.auth import HttpSignatureAuth
from .utils.auth import HttpSignatureAuth, HttpTokenAuth
from .utils.backports import cached_property
from .utils.conf import BOOLEAN_PARAMS, get_credentials
from .utils.cookies import CookieManager
Expand Down Expand Up @@ -147,17 +147,20 @@ class Osc:

def __init__(self, url: typing.Optional[str] = None, username: typing.Optional[str] = None,
password: typing.Optional[str] = None, verify: typing.Optional[str] = None,
ssh_key_file: typing.Optional[typing.Union[Path, str]] = None):
ssh_key_file: typing.Optional[typing.Union[Path, str]] = None,
token: typing.Optional[str] = None):
# Basic URL and authentication settings
self.url = url or self.url
self.username = username or self.username
self.password = password or self.password
self.verify = verify
self.ssh_key = ssh_key_file
self.token = token

if self.ssh_key is not None and not isinstance(self.ssh_key, Path):
self.ssh_key = Path(self.ssh_key)

if not self.username and not self.password and not self.ssh_key:
if not self.username and not self.password and not self.ssh_key and not self.token:
try:
self.username, self.password, self.ssh_key = get_credentials(self.url)
except (ValueError, RuntimeError, FileNotFoundError) as error:
Expand Down Expand Up @@ -193,6 +196,8 @@ def session(self) -> Session:
if self.ssh_key is not None:
auth = HttpSignatureAuth(username=self.username, password=self.password,
ssh_key_file=self.ssh_key)
elif self.token is not None:
auth = HttpTokenAuth(self.username, self.token)
else:
auth = HTTPBasicAuth(self.username, self.password)
session = init_session(auth=auth, policy=self.retry_policy, verify=self.verify)
Expand Down
19 changes: 18 additions & 1 deletion osctiny/utils/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import sys
from time import time

from requests.auth import HTTPDigestAuth
from requests.auth import AuthBase, HTTPDigestAuth
from requests.cookies import extract_cookies_to_jar
from requests.utils import parse_dict_header
from requests import Response
Expand Down Expand Up @@ -275,3 +275,20 @@ def handle_401(self, r: Response, **kwargs) -> Response:

self._thread_local.num_401_calls = 1
return r


class HttpTokenAuth(AuthBase):
"""Attaches HTTP Token Authentication to the given Request object."""

def __init__(self, token):
self.token = token

def __eq__(self, other):
return self.token == getattr(other, "token", None)

def __ne__(self, other):
return not self == other

def __call__(self, r):
r.headers["Authorization"] = f"Token {self.token}"
return r
Loading