Skip to content

Commit

Permalink
Merge branch 'main' of github.com:frostming/unearth
Browse files Browse the repository at this point in the history
  • Loading branch information
frostming committed Jun 28, 2024
2 parents 717b4de + 2f85d92 commit f61a088
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ repos:
- id: check-added-large-files

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: 'v0.4.7'
rev: 'v0.4.10'
hooks:
- id: ruff
args: [--fix, --exit-non-zero-on-fix, --show-fixes]
Expand Down
14 changes: 7 additions & 7 deletions pdm.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 16 additions & 5 deletions src/unearth/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import os
import shutil
import subprocess
from typing import TYPE_CHECKING, Optional, Tuple, cast
from typing import TYPE_CHECKING, Literal, Optional, Tuple, cast
from urllib.parse import SplitResult, urlparse, urlsplit

from httpx import URL, Auth, BasicAuth
Expand Down Expand Up @@ -83,20 +83,31 @@ def __init__(self, cmd: str) -> None:
self.keyring = cmd

def get_auth_info(self, url: str, username: str | None) -> AuthInfo | None:
logger.debug("Getting credentials from keyring CLI for url: %s", url)
cred = self._get_secret(url, username or "", mode="creds")
if cred is not None:
username, password = cred.splitlines()
return username, password

if username is None:
username = "__token__"
logger.debug("Getting password from keyring CLI for %s@%s", username, url)
password = self._get_password(url, username)
password = self._get_secret(url, username) # type: ignore[assignment]
if password is not None:
return username, password
return None

def save_auth_info(self, url: str, username: str, password: str) -> None:
return self._set_password(url, username, password)

def _get_password(self, service_name: str, username: str) -> str | None:
"""Mirror the implementation of keyring.get_password using cli"""
cmd = [self.keyring, "get", service_name, username]
def _get_secret(
self,
service_name: str,
username: str,
mode: Literal["password", "creds"] = "password",
) -> str | None:
"""Mirror the implementation of keyring.get_[password|credential] using cli"""
cmd = [self.keyring, f"--mode={mode}", "get", service_name, username]
env = dict(os.environ, PYTHONIOENCODING="utf-8")
res = subprocess.run(
cmd, stdin=subprocess.DEVNULL, capture_output=True, env=env
Expand Down

0 comments on commit f61a088

Please sign in to comment.