Skip to content

Commit c2a792f

Browse files
committed
feat: Add support for credential_type override as well
1 parent ad651cc commit c2a792f

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":
@@ -135,6 +138,9 @@ def _load_token_domain(self, parsed_url: ParseResult) -> tuple[str, CredentialTy
135138
if self._extras.override_auth_domain:
136139
token_domain = self._extras.override_auth_domain
137140

141+
if self._extras.override_credential_type:
142+
credential_type = self._extras.override_credential_type
143+
138144
return token_domain, credential_type
139145

140146
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
@@ -380,3 +381,48 @@ def test_load_token_domain_user_provided_auth_domain_override(conda_search_path)
380381

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

0 commit comments

Comments
 (0)