Skip to content

Commit e9e4de5

Browse files
committed
feat: Add support for credential_type override as well
1 parent 2bf83f7 commit e9e4de5

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

src/anaconda_auth/_conda/auth_handler.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ def _load_channel_settings(channel_name: str) -> dict[str, Any]:
9191

9292
class AnacondaAuthHandlerExtraSettings(BaseModel):
9393
override_auth_domain: Optional[str] = Field(default=None, alias="auth_domain")
94+
override_credential_type: Optional[CredentialType] = Field(
95+
default=None, alias="credential_type"
96+
)
9497

9598
@classmethod
9699
def from_channel_name(cls, channel_name: str) -> "AnacondaAuthHandlerExtraSettings":
@@ -138,6 +141,9 @@ def _load_token_domain(self, parsed_url: ParseResult) -> tuple[str, CredentialTy
138141
if self._extras.override_auth_domain:
139142
token_domain = self._extras.override_auth_domain
140143

144+
if self._extras.override_credential_type:
145+
credential_type = self._extras.override_credential_type
146+
141147
return token_domain, credential_type
142148

143149
def _load_token_from_keyring(self, url: str) -> Optional[AccessCredential]:

src/anaconda_auth/_conda/condarc.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from ruamel.yaml import YAML
77
from ruamel.yaml import YAMLError
88

9+
from anaconda_auth._conda.config import CredentialType
910
from anaconda_cli_base import console
1011

1112
DEFAULT_CONDARC_PATH = Path("~/.condarc").expanduser()
@@ -52,6 +53,7 @@ def update_channel_settings(
5253
username: str | None = None,
5354
*,
5455
auth_domain: str | None = None,
56+
credential_type: CredentialType | None = None,
5557
) -> None:
5658
"""
5759
Update the condarc file's "channel_settings" section
@@ -61,6 +63,7 @@ def update_channel_settings(
6163
"auth": auth_type,
6264
"username": username,
6365
"auth_domain": auth_domain,
66+
"credential_type": credential_type.value if credential_type else None,
6467
}
6568

6669
# Filter out any None values

tests/test_conda_plugins.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from enum import Enum
12
from urllib.parse import urlparse
23

34
import pytest
@@ -383,3 +384,48 @@ def test_load_token_domain_user_provided_auth_domain_override(conda_search_path)
383384

384385
assert token_domain == "auth.some-domain.com"
385386
assert credential_type == CredentialType.API_KEY
387+
388+
389+
@pytest.mark.parametrize(
390+
"override_credential_type", [CredentialType.API_KEY, CredentialType.REPO_TOKEN]
391+
)
392+
def test_load_token_domain_user_provided_credential_type_override(
393+
override_credential_type, conda_search_path
394+
):
395+
channel_url = "https://some-domain.com/repo/some-channel"
396+
condarc = CondaRC()
397+
condarc.update_channel_settings(
398+
channel=channel_url + "/*",
399+
auth_type="anaconda-auth",
400+
credential_type=override_credential_type,
401+
)
402+
condarc.save()
403+
conda_context.__init__()
404+
405+
handler = AnacondaAuthHandler(channel_name=channel_url)
406+
url = channel_url + "/noarch/repodata.json"
407+
token_domain, credential_type = handler._load_token_domain(parsed_url=urlparse(url))
408+
409+
assert token_domain == "some-domain.com"
410+
assert credential_type == override_credential_type
411+
412+
413+
def test_load_token_domain_user_provided_credential_type_override_invalid(
414+
conda_search_path,
415+
):
416+
channel_url = "https://some-domain.com/repo/some-channel"
417+
condarc = CondaRC()
418+
419+
class BadCredentialType(Enum):
420+
INVALID = "invalid"
421+
422+
condarc.update_channel_settings(
423+
channel=channel_url + "/*",
424+
auth_type="anaconda-auth",
425+
credential_type=BadCredentialType.INVALID,
426+
)
427+
condarc.save()
428+
conda_context.__init__()
429+
430+
with pytest.raises(AnacondaAuthError):
431+
AnacondaAuthHandler(channel_name=channel_url)

0 commit comments

Comments
 (0)