8000 feat: add keyring cli get creds mode (#127) · frostming/unearth@4a7d737 · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Commit 4a7d737

Browse files
authored
feat: add keyring cli get creds mode (#127)
1 parent bd388cf commit 4a7d737

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

src/unearth/auth.py

+16-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import os
77
import shutil
88
import subprocess
9-
from typing import TYPE_CHECKING, Optional, Tuple, cast
9+
from typing import TYPE_CHECKING, Literal, Optional, Tuple, cast
1010
from urllib.parse import SplitResult, urlparse, urlsplit
1111

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

8585
def get_auth_info(self, url: str, username: str | None) -> AuthInfo | None:
86+
logger.debug("Getting credentials from keyring CLI for url: %s", url)
87+
cred = self._get_secret(url, username or "", mode="creds")
88+
if cred is not None:
89+
username, password = cred.splitlines()
90+
return username, password
91+
8692
if username is None:
8793
username = "__token__"
8894
logger.debug("Getting password from keyring CLI for %s@%s", username, url)
89-
password = self._get_password(url, username)
95+
password = self._get_secret(url, username) # type: ignore[assignment]
9096
if password is not None:
9197
return username, password
9298
return None
9399

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

97-
def _get_password(self, service_name: str, username: str) -> str | None:
98-
"""Mirror the implementation of keyring.get_password using cli"""
99-
cmd = [self.keyring, "get", service_name, username]
103+
def _get_secret(
104+
self,
105+
service_name: str,
106+
username: str,
107+
mode: Literal["password", "creds"] = "password",
108+
) -> str | None:
109+
"""Mirror the implementation of keyring.get_[password|credential] using cli"""
110+
cmd = [self.keyring, f"--mode={mode}", "get", service_name, username]
100111
env = dict(os.environ, PYTHONIOENCODING="utf-8")
101112
res = subprocess.run(
102113
cmd, stdin=subprocess.DEVNULL, capture_output=True, env=env

0 commit comments

Comments
 (0)
0