apheris-foundry-cli 2.7.1__tar.gz → 2.7.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (74) hide show
  1. {apheris_foundry_cli-2.7.1/src/apheris_foundry_cli.egg-info → apheris_foundry_cli-2.7.2}/PKG-INFO +2 -1
  2. {apheris_foundry_cli-2.7.1 → apheris_foundry_cli-2.7.2}/README.md +1 -0
  3. {apheris_foundry_cli-2.7.1 → apheris_foundry_cli-2.7.2}/pyproject.toml +1 -1
  4. {apheris_foundry_cli-2.7.1 → apheris_foundry_cli-2.7.2/src/apheris_foundry_cli.egg-info}/PKG-INFO +2 -1
  5. {apheris_foundry_cli-2.7.1 → apheris_foundry_cli-2.7.2}/src/apherisfold_cli/commands/login.py +3 -1
  6. {apheris_foundry_cli-2.7.1 → apheris_foundry_cli-2.7.2}/src/apherisfold_cli/core/auth.py +29 -5
  7. apheris_foundry_cli-2.7.2/src/apherisfold_cli/version.py +1 -0
  8. {apheris_foundry_cli-2.7.1 → apheris_foundry_cli-2.7.2}/tests/test_auth.py +9 -3
  9. {apheris_foundry_cli-2.7.1 → apheris_foundry_cli-2.7.2}/tests/test_auth_lifecycle.py +64 -8
  10. {apheris_foundry_cli-2.7.1 → apheris_foundry_cli-2.7.2}/tests/test_login_callback.py +16 -6
  11. apheris_foundry_cli-2.7.1/src/apherisfold_cli/version.py +0 -1
  12. {apheris_foundry_cli-2.7.1 → apheris_foundry_cli-2.7.2}/LICENSE +0 -0
  13. {apheris_foundry_cli-2.7.1 → apheris_foundry_cli-2.7.2}/setup.cfg +0 -0
  14. {apheris_foundry_cli-2.7.1 → apheris_foundry_cli-2.7.2}/src/apheris_foundry_cli.egg-info/SOURCES.txt +0 -0
  15. {apheris_foundry_cli-2.7.1 → apheris_foundry_cli-2.7.2}/src/apheris_foundry_cli.egg-info/dependency_links.txt +0 -0
  16. {apheris_foundry_cli-2.7.1 → apheris_foundry_cli-2.7.2}/src/apheris_foundry_cli.egg-info/entry_points.txt +0 -0
  17. {apheris_foundry_cli-2.7.1 → apheris_foundry_cli-2.7.2}/src/apheris_foundry_cli.egg-info/requires.txt +0 -0
  18. {apheris_foundry_cli-2.7.1 → apheris_foundry_cli-2.7.2}/src/apheris_foundry_cli.egg-info/top_level.txt +0 -0
  19. {apheris_foundry_cli-2.7.1 → apheris_foundry_cli-2.7.2}/src/apherisfold_cli/__init__.py +0 -0
  20. {apheris_foundry_cli-2.7.1 → apheris_foundry_cli-2.7.2}/src/apherisfold_cli/commands/__init__.py +0 -0
  21. {apheris_foundry_cli-2.7.1 → apheris_foundry_cli-2.7.2}/src/apherisfold_cli/commands/compounds.py +0 -0
  22. {apheris_foundry_cli-2.7.1 → apheris_foundry_cli-2.7.2}/src/apherisfold_cli/commands/docs.py +0 -0
  23. {apheris_foundry_cli-2.7.1 → apheris_foundry_cli-2.7.2}/src/apherisfold_cli/commands/foundry.py +0 -0
  24. {apheris_foundry_cli-2.7.1 → apheris_foundry_cli-2.7.2}/src/apherisfold_cli/commands/settings.py +0 -0
  25. {apheris_foundry_cli-2.7.1 → apheris_foundry_cli-2.7.2}/src/apherisfold_cli/commands/skills.py +0 -0
  26. {apheris_foundry_cli-2.7.1 → apheris_foundry_cli-2.7.2}/src/apherisfold_cli/commands/weights.py +0 -0
  27. {apheris_foundry_cli-2.7.1 → apheris_foundry_cli-2.7.2}/src/apherisfold_cli/core/__init__.py +0 -0
  28. {apheris_foundry_cli-2.7.1 → apheris_foundry_cli-2.7.2}/src/apherisfold_cli/core/archive_paths.py +0 -0
  29. {apheris_foundry_cli-2.7.1 → apheris_foundry_cli-2.7.2}/src/apherisfold_cli/core/browser.py +0 -0
  30. {apheris_foundry_cli-2.7.1 → apheris_foundry_cli-2.7.2}/src/apherisfold_cli/core/config.py +0 -0
  31. {apheris_foundry_cli-2.7.1 → apheris_foundry_cli-2.7.2}/src/apherisfold_cli/core/context.py +0 -0
  32. {apheris_foundry_cli-2.7.1 → apheris_foundry_cli-2.7.2}/src/apherisfold_cli/core/documents.py +0 -0
  33. {apheris_foundry_cli-2.7.1 → apheris_foundry_cli-2.7.2}/src/apherisfold_cli/core/errors.py +0 -0
  34. {apheris_foundry_cli-2.7.1 → apheris_foundry_cli-2.7.2}/src/apherisfold_cli/core/hub_requests.py +0 -0
  35. {apheris_foundry_cli-2.7.1 → apheris_foundry_cli-2.7.2}/src/apherisfold_cli/core/input_archive.py +0 -0
  36. {apheris_foundry_cli-2.7.1 → apheris_foundry_cli-2.7.2}/src/apherisfold_cli/core/io.py +0 -0
  37. {apheris_foundry_cli-2.7.1 → apheris_foundry_cli-2.7.2}/src/apherisfold_cli/core/limits.py +0 -0
  38. {apheris_foundry_cli-2.7.1 → apheris_foundry_cli-2.7.2}/src/apherisfold_cli/core/login_profiles.py +0 -0
  39. {apheris_foundry_cli-2.7.1 → apheris_foundry_cli-2.7.2}/src/apherisfold_cli/core/msa.py +0 -0
  40. {apheris_foundry_cli-2.7.1 → apheris_foundry_cli-2.7.2}/src/apherisfold_cli/core/pubchem.py +0 -0
  41. {apheris_foundry_cli-2.7.1 → apheris_foundry_cli-2.7.2}/src/apherisfold_cli/core/render.py +0 -0
  42. {apheris_foundry_cli-2.7.1 → apheris_foundry_cli-2.7.2}/src/apherisfold_cli/core/result_artifacts.py +0 -0
  43. {apheris_foundry_cli-2.7.1 → apheris_foundry_cli-2.7.2}/src/apherisfold_cli/core/transient.py +0 -0
  44. {apheris_foundry_cli-2.7.1 → apheris_foundry_cli-2.7.2}/src/apherisfold_cli/core/weight_archive.py +0 -0
  45. {apheris_foundry_cli-2.7.1 → apheris_foundry_cli-2.7.2}/src/apherisfold_cli/core/weight_import.py +0 -0
  46. {apheris_foundry_cli-2.7.1 → apheris_foundry_cli-2.7.2}/src/apherisfold_cli/handlers.py +0 -0
  47. {apheris_foundry_cli-2.7.1 → apheris_foundry_cli-2.7.2}/src/apherisfold_cli/main.py +0 -0
  48. {apheris_foundry_cli-2.7.1 → apheris_foundry_cli-2.7.2}/src/apherisfold_cli/skills/apheris-foundry-hub/SKILL.md +0 -0
  49. {apheris_foundry_cli-2.7.1 → apheris_foundry_cli-2.7.2}/tests/test_benchmark_examples.py +0 -0
  50. {apheris_foundry_cli-2.7.1 → apheris_foundry_cli-2.7.2}/tests/test_browser.py +0 -0
  51. {apheris_foundry_cli-2.7.1 → apheris_foundry_cli-2.7.2}/tests/test_cli.py +0 -0
  52. {apheris_foundry_cli-2.7.1 → apheris_foundry_cli-2.7.2}/tests/test_config.py +0 -0
  53. {apheris_foundry_cli-2.7.1 → apheris_foundry_cli-2.7.2}/tests/test_docs.py +0 -0
  54. {apheris_foundry_cli-2.7.1 → apheris_foundry_cli-2.7.2}/tests/test_documents.py +0 -0
  55. {apheris_foundry_cli-2.7.1 → apheris_foundry_cli-2.7.2}/tests/test_dry_run_guard.py +0 -0
  56. {apheris_foundry_cli-2.7.1 → apheris_foundry_cli-2.7.2}/tests/test_foundry_execution.py +0 -0
  57. {apheris_foundry_cli-2.7.1 → apheris_foundry_cli-2.7.2}/tests/test_foundry_helpers.py +0 -0
  58. {apheris_foundry_cli-2.7.1 → apheris_foundry_cli-2.7.2}/tests/test_foundry_warnings.py +0 -0
  59. {apheris_foundry_cli-2.7.1 → apheris_foundry_cli-2.7.2}/tests/test_handlers.py +0 -0
  60. {apheris_foundry_cli-2.7.1 → apheris_foundry_cli-2.7.2}/tests/test_hub_requests.py +0 -0
  61. {apheris_foundry_cli-2.7.1 → apheris_foundry_cli-2.7.2}/tests/test_input_archive.py +0 -0
  62. {apheris_foundry_cli-2.7.1 → apheris_foundry_cli-2.7.2}/tests/test_io.py +0 -0
  63. {apheris_foundry_cli-2.7.1 → apheris_foundry_cli-2.7.2}/tests/test_jobs_metadata.py +0 -0
  64. {apheris_foundry_cli-2.7.1 → apheris_foundry_cli-2.7.2}/tests/test_jobs_update.py +0 -0
  65. {apheris_foundry_cli-2.7.1 → apheris_foundry_cli-2.7.2}/tests/test_main.py +0 -0
  66. {apheris_foundry_cli-2.7.1 → apheris_foundry_cli-2.7.2}/tests/test_model_discovery.py +0 -0
  67. {apheris_foundry_cli-2.7.1 → apheris_foundry_cli-2.7.2}/tests/test_msa.py +0 -0
  68. {apheris_foundry_cli-2.7.1 → apheris_foundry_cli-2.7.2}/tests/test_pubchem.py +0 -0
  69. {apheris_foundry_cli-2.7.1 → apheris_foundry_cli-2.7.2}/tests/test_render.py +0 -0
  70. {apheris_foundry_cli-2.7.1 → apheris_foundry_cli-2.7.2}/tests/test_result_artifacts.py +0 -0
  71. {apheris_foundry_cli-2.7.1 → apheris_foundry_cli-2.7.2}/tests/test_skills.py +0 -0
  72. {apheris_foundry_cli-2.7.1 → apheris_foundry_cli-2.7.2}/tests/test_weight_archive.py +0 -0
  73. {apheris_foundry_cli-2.7.1 → apheris_foundry_cli-2.7.2}/tests/test_weight_import.py +0 -0
  74. {apheris_foundry_cli-2.7.1 → apheris_foundry_cli-2.7.2}/tests/test_weights.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: apheris-foundry-cli
3
- Version: 2.7.1
3
+ Version: 2.7.2
4
4
  Summary: Command-line interface for Apheris Foundry workflows.
5
5
  Author: Apheris
6
6
  License-Expression: LicenseRef-Apheris-Foundry-Software-License
@@ -100,6 +100,7 @@ apheris-foundry login --url https://hub.example.com
100
100
 
101
101
  Follow the sign-in instructions. The CLI saves your session for later commands.
102
102
  Use `apheris-foundry logout` to sign out, or log in again to switch Hubs.
103
+ See [callback URL settings](docs/usage.md#login-and-saved-sessions) to pin a registered URL.
103
104
 
104
105
  If login reports that its local ports are already in use, follow the commands
105
106
  in the error message to identify the conflicting application. Close it if
@@ -74,6 +74,7 @@ apheris-foundry login --url https://hub.example.com
74
74
 
75
75
  Follow the sign-in instructions. The CLI saves your session for later commands.
76
76
  Use `apheris-foundry logout` to sign out, or log in again to switch Hubs.
77
+ See [callback URL settings](docs/usage.md#login-and-saved-sessions) to pin a registered URL.
77
78
 
78
79
  If login reports that its local ports are already in use, follow the commands
79
80
  in the error message to identify the conflicting application. Close it if
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "apheris-foundry-cli"
7
- version = "2.7.1"
7
+ version = "2.7.2"
8
8
  description = "Command-line interface for Apheris Foundry workflows."
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.10"
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: apheris-foundry-cli
3
- Version: 2.7.1
3
+ Version: 2.7.2
4
4
  Summary: Command-line interface for Apheris Foundry workflows.
5
5
  Author: Apheris
6
6
  License-Expression: LicenseRef-Apheris-Foundry-Software-License
@@ -100,6 +100,7 @@ apheris-foundry login --url https://hub.example.com
100
100
 
101
101
  Follow the sign-in instructions. The CLI saves your session for later commands.
102
102
  Use `apheris-foundry logout` to sign out, or log in again to switch Hubs.
103
+ See [callback URL settings](docs/usage.md#login-and-saved-sessions) to pin a registered URL.
103
104
 
104
105
  If login reports that its local ports are already in use, follow the commands
105
106
  in the error message to identify the conflicting application. Close it if
@@ -12,6 +12,7 @@ from rich.table import Table
12
12
  from rich.text import Text
13
13
 
14
14
  from apherisfold_cli.core.auth import (
15
+ CALLBACK_URL_ENV,
15
16
  DEFAULT_CALLBACK_BIND_HOST,
16
17
  DEFAULT_CALLBACK_PORTS,
17
18
  LoginResult,
@@ -35,7 +36,8 @@ console = Console()
35
36
  _MARKDOWN_LINK_RE = re.compile(r"^\[[^\]]+\]\((?P<url>[^)]+)\)$")
36
37
  _CALLBACK_PORT_HELP = (
37
38
  f"Login uses port {DEFAULT_CALLBACK_PORTS[0]}, falling back to {DEFAULT_CALLBACK_PORTS[1]} "
38
- "only if the first port is already in use."
39
+ f"only if the first port is already in use. Set {CALLBACK_URL_ENV} to "
40
+ "http://HOST:PORT/callback to use only that port. HOST must match --callback-host (default: localhost)."
39
41
  )
40
42
 
41
43
 
@@ -27,6 +27,7 @@ from .errors import (
27
27
  EXIT_INTERNAL,
28
28
  EXIT_PRECONDITION,
29
29
  EXIT_TRANSIENT,
30
+ EXIT_USAGE,
30
31
  CLIError,
31
32
  )
32
33
  from .transient import RETRYABLE_STATUS_CODES, UNAVAILABLE_HINT, call_with_transient_retry
@@ -37,9 +38,10 @@ except ImportError: # pragma: no cover - dependency is present in packaged inst
37
38
  truststore = None
38
39
 
39
40
 
40
- DEFAULT_CALLBACK_PORTS = (53147, 8765)
41
+ DEFAULT_CALLBACK_PORTS = (8765, 53147)
41
42
  DEFAULT_CALLBACK_PATH = "/callback"
42
43
  DEFAULT_CALLBACK_BIND_HOST = "localhost"
44
+ CALLBACK_URL_ENV = "APHERIS_FOUNDRY_CALLBACK_URL"
43
45
  DEFAULT_LOGIN_TIMEOUT_SECONDS = 120
44
46
  AUTH_REFRESH_SKEW_SECONDS = 60
45
47
 
@@ -1236,8 +1238,7 @@ class _CallbackServer:
1236
1238
  category="precondition",
1237
1239
  message=(
1238
1240
  f"Cannot start the login callback listener:\n{failures}\n\n{guidance}\n\n"
1239
- "Free one of these ports by closing the identified application or stopping its container "
1240
- "if appropriate, then retry your login command."
1241
+ "Close the identified application or stop its container if appropriate, then retry your login command."
1241
1242
  ),
1242
1243
  task="login",
1243
1244
  details={"ports": list(self._ports), "bind_errors": bind_errors},
@@ -1298,6 +1299,7 @@ def _exchange_code_for_session(
1298
1299
  def _run_local_callback_login(
1299
1300
  config: RuntimeAuthConfig,
1300
1301
  *,
1302
+ callback_ports: tuple[int, ...],
1301
1303
  auto_open_browser: bool | None = None,
1302
1304
  callback_host: str = DEFAULT_CALLBACK_BIND_HOST,
1303
1305
  ) -> LocalCallbackLoginResult:
@@ -1312,7 +1314,7 @@ def _run_local_callback_login(
1312
1314
  )
1313
1315
 
1314
1316
  callback_server = _CallbackServer(
1315
- ports=DEFAULT_CALLBACK_PORTS,
1317
+ ports=callback_ports,
1316
1318
  callback_path=DEFAULT_CALLBACK_PATH,
1317
1319
  callback_host=callback_host,
1318
1320
  )
@@ -1447,18 +1449,40 @@ def perform_login(
1447
1449
  )
1448
1450
 
1449
1451
 
1452
+ def _callback_ports(callback_host: str) -> tuple[int, ...]:
1453
+ configured_url = os.getenv(CALLBACK_URL_ENV)
1454
+ if configured_url is None:
1455
+ return DEFAULT_CALLBACK_PORTS
1456
+
1457
+ allowed_urls = {f"http://{callback_host}:{port}{DEFAULT_CALLBACK_PATH}": port for port in DEFAULT_CALLBACK_PORTS}
1458
+ if configured_url not in allowed_urls:
1459
+ raise CLIError(
1460
+ code=EXIT_USAGE,
1461
+ category="usage",
1462
+ message=f"{CALLBACK_URL_ENV} must be one of: {', '.join(allowed_urls)}.",
1463
+ task="login",
1464
+ )
1465
+ return (allowed_urls[configured_url],)
1466
+
1467
+
1450
1468
  def _login_with_config(
1451
1469
  config: RuntimeAuthConfig,
1452
1470
  *,
1453
1471
  auto_open_browser: bool | None,
1454
1472
  callback_host: str,
1455
1473
  ) -> LocalCallbackLoginResult:
1474
+ callback_ports = _callback_ports(callback_host)
1456
1475
  previous = load_runtime_config()
1457
1476
  # An explicit login/account switch must not resurrect the previous account on failure.
1458
1477
  _clear_hub_sessions(config.hub_url)
1459
1478
  save_runtime_config(config)
1460
1479
  try:
1461
- return _run_local_callback_login(config, auto_open_browser=auto_open_browser, callback_host=callback_host)
1480
+ return _run_local_callback_login(
1481
+ config,
1482
+ auto_open_browser=auto_open_browser,
1483
+ callback_host=callback_host,
1484
+ callback_ports=callback_ports,
1485
+ )
1462
1486
  except BaseException:
1463
1487
  # Any failure, including OSError while writing session files, must not leave a partial login.
1464
1488
  _clear_hub_sessions(config.hub_url)
@@ -0,0 +1 @@
1
+ __version__ = "2.7.2"
@@ -244,11 +244,12 @@ def test_perform_login_uses_local_callback_for_oidc(monkeypatch, tmp_path: Path)
244
244
  captured: dict[str, object] = {}
245
245
  monkeypatch.setattr(
246
246
  "apherisfold_cli.core.auth._run_local_callback_login",
247
- lambda _config, auto_open_browser=None, callback_host="localhost": (
247
+ lambda _config, *, auto_open_browser, callback_host, callback_ports: (
248
248
  captured.update(
249
249
  {
250
250
  "auto_open_browser": auto_open_browser,
251
251
  "callback_host": callback_host,
252
+ "callback_ports": callback_ports,
252
253
  }
253
254
  )
254
255
  or LocalCallbackLoginResult(
@@ -269,6 +270,7 @@ def test_perform_login_uses_local_callback_for_oidc(monkeypatch, tmp_path: Path)
269
270
  assert captured == {
270
271
  "auto_open_browser": None,
271
272
  "callback_host": "localhost",
273
+ "callback_ports": (8765, 53147),
272
274
  }
273
275
 
274
276
 
@@ -300,11 +302,12 @@ def test_perform_login_accepts_callback_host_override(monkeypatch, tmp_path: Pat
300
302
  captured: dict[str, object] = {}
301
303
  monkeypatch.setattr(
302
304
  "apherisfold_cli.core.auth._run_local_callback_login",
303
- lambda _config, auto_open_browser=None, callback_host="localhost": (
305
+ lambda _config, *, auto_open_browser, callback_host, callback_ports: (
304
306
  captured.update(
305
307
  {
306
308
  "auto_open_browser": auto_open_browser,
307
309
  "callback_host": callback_host,
310
+ "callback_ports": callback_ports,
308
311
  }
309
312
  )
310
313
  or LocalCallbackLoginResult(
@@ -325,6 +328,7 @@ def test_perform_login_accepts_callback_host_override(monkeypatch, tmp_path: Pat
325
328
  assert captured == {
326
329
  "auto_open_browser": None,
327
330
  "callback_host": "localhost",
331
+ "callback_ports": (8765, 53147),
328
332
  }
329
333
 
330
334
 
@@ -994,10 +998,11 @@ def test_perform_login_direct_builds_config_from_issuer(monkeypatch, tmp_path: P
994
998
  captured: dict[str, object] = {}
995
999
  monkeypatch.setattr(
996
1000
  "apherisfold_cli.core.auth._run_local_callback_login",
997
- lambda cfg, auto_open_browser=None, callback_host="localhost": (
1001
+ lambda cfg, *, auto_open_browser, callback_host, callback_ports: (
998
1002
  captured.update(
999
1003
  {
1000
1004
  "callback_host": callback_host,
1005
+ "callback_ports": callback_ports,
1001
1006
  "issuer": cfg.issuer,
1002
1007
  "client_id": cfg.client_id,
1003
1008
  "token_endpoint": cfg.token_endpoint,
@@ -1025,6 +1030,7 @@ def test_perform_login_direct_builds_config_from_issuer(monkeypatch, tmp_path: P
1025
1030
  assert result.hub_url == "https://hub.iap-protected.example.com"
1026
1031
  assert result.client_id == "apheris-hub"
1027
1032
  assert captured["callback_host"] == "localhost"
1033
+ assert captured["callback_ports"] == (8765, 53147)
1028
1034
  assert captured["issuer"] == "https://dex.iap-protected.example.com"
1029
1035
  assert captured["client_id"] == "apheris-hub"
1030
1036
  assert captured["token_endpoint"] == "https://dex.iap-protected.example.com/token"
@@ -1,5 +1,6 @@
1
1
  from __future__ import annotations
2
2
 
3
+ import errno
3
4
  import json
4
5
  import socket
5
6
  import ssl
@@ -15,7 +16,7 @@ import pytest
15
16
  from typer.testing import CliRunner
16
17
 
17
18
  from apherisfold_cli.core import auth, hub_requests
18
- from apherisfold_cli.core.errors import EXIT_PRECONDITION, EXIT_TRANSIENT, CLIError
19
+ from apherisfold_cli.core.errors import EXIT_PRECONDITION, EXIT_TRANSIENT, EXIT_USAGE, CLIError
19
20
  from apherisfold_cli.main import app
20
21
 
21
22
 
@@ -76,16 +77,16 @@ def mock_discovery(config, monkeypatch):
76
77
  return urls
77
78
 
78
79
 
79
- def mock_callback(monkeypatch, outcome):
80
+ def mock_callback(monkeypatch, outcome, expected_ports=(8765, 53147)):
80
81
  observed = {}
81
82
  monkeypatch.setattr(auth.secrets, "token_urlsafe", lambda _size: "random-state")
82
83
 
83
84
  class Callback:
84
85
  def __init__(self, *, ports, callback_path, callback_host):
85
- assert ports == auth.DEFAULT_CALLBACK_PORTS
86
+ assert ports == expected_ports
86
87
  assert callback_path == "/callback"
87
88
  assert callback_host == "localhost"
88
- self.redirect_uri = "http://localhost:8765/callback"
89
+ self.redirect_uri = f"http://{callback_host}:{ports[0]}{callback_path}"
89
90
 
90
91
  def start(self):
91
92
  observed["started"] = True
@@ -155,9 +156,64 @@ def test_failed_callback_leaves_no_partial_login_or_target_cache(config, monkeyp
155
156
 
156
157
 
157
158
  @pytest.mark.parametrize("direct", [False, True])
158
- def test_login_keeps_discovery_pkce_and_published_client_contract(config, monkeypatch, capsys, direct):
159
+ @pytest.mark.parametrize(
160
+ "url",
161
+ [
162
+ "",
163
+ "http://127.0.0.1:8765/callback",
164
+ "http://localhost:8888/callback",
165
+ "https://localhost:8765/callback",
166
+ "http://localhost:8765/other",
167
+ "http://localhost:8765/callback?state=ignored",
168
+ ],
169
+ )
170
+ def test_invalid_callback_url_preserves_session(config, monkeypatch, direct, url):
171
+ saved = seed_session(config)
172
+ mock_discovery(config, monkeypatch)
173
+ monkeypatch.setenv("APHERIS_FOUNDRY_CALLBACK_URL", url)
174
+ monkeypatch.setattr(auth, "_CallbackServer", Mock(side_effect=AssertionError("unexpected listener")))
175
+
176
+ arguments = ["login", "--url", config.hub_url]
177
+ if direct:
178
+ arguments += ["--issuer", config.issuer, "--client-id", config.client_id, "--audience", config.audience]
179
+ result = CliRunner().invoke(app, arguments)
180
+
181
+ assert isinstance(result.exception, CLIError)
182
+ assert result.exception.code == EXIT_USAGE
183
+ assert "APHERIS_FOUNDRY_CALLBACK_URL" in result.exception.message
184
+ assert auth.load_runtime_config() == config
185
+ assert auth.load_session() == saved
186
+ assert auth.session_cache_path(config).exists()
187
+
188
+
189
+ def test_pinned_occupied_port_fails_before_browser(config, monkeypatch):
190
+ mock_discovery(config, monkeypatch)
191
+ monkeypatch.setenv("APHERIS_FOUNDRY_CALLBACK_URL", "http://localhost:8765/callback")
192
+ listener = Mock(side_effect=OSError(errno.EADDRINUSE, "Address already in use"))
193
+ monkeypatch.setattr(auth, "HTTPServer", listener)
194
+ browser = Mock()
195
+ monkeypatch.setattr(auth, "_browser_open", browser)
196
+
197
+ with pytest.raises(CLIError) as caught:
198
+ auth.perform_login(config.hub_url, auto_open_browser=True)
199
+
200
+ assert caught.value.code == EXIT_PRECONDITION
201
+ assert caught.value.details["ports"] == [8765]
202
+ assert "localhost:8765" in caught.value.message
203
+ assert "53147" not in caught.value.message
204
+ assert listener.call_count == 1
205
+ assert listener.call_args.args[0] == ("localhost", 8765)
206
+ browser.assert_not_called()
207
+
208
+
209
+ @pytest.mark.parametrize("direct", [False, True])
210
+ @pytest.mark.parametrize("pinned", [False, True])
211
+ def test_login_keeps_discovery_pkce_and_published_client_contract(config, monkeypatch, capsys, direct, pinned):
212
+ expected_uri = "http://localhost:53147/callback" if pinned else "http://localhost:8765/callback"
213
+ if pinned:
214
+ monkeypatch.setenv("APHERIS_FOUNDRY_CALLBACK_URL", expected_uri)
159
215
  urls = mock_discovery(config, monkeypatch)
160
- mock_callback(monkeypatch, "success")
216
+ mock_callback(monkeypatch, "success", expected_ports=(53147,) if pinned else (8765, 53147))
161
217
  observed = {}
162
218
 
163
219
  def post(url, form):
@@ -171,7 +227,7 @@ def test_login_keeps_discovery_pkce_and_published_client_contract(config, monkey
171
227
  assert parameters == {
172
228
  "response_type": ["code"],
173
229
  "client_id": [config.client_id],
174
- "redirect_uri": ["http://localhost:8765/callback"],
230
+ "redirect_uri": [expected_uri],
175
231
  "scope": ["openid profile email offline_access"],
176
232
  "state": ["random-state"],
177
233
  "code_challenge": [auth._pkce_code_challenge(observed["form"]["code_verifier"])],
@@ -179,7 +235,7 @@ def test_login_keeps_discovery_pkce_and_published_client_contract(config, monkey
179
235
  "audience": [config.audience],
180
236
  }
181
237
  assert observed["form"]["code"] == "authorization-code"
182
- assert observed["form"]["redirect_uri"] == "http://localhost:8765/callback"
238
+ assert observed["form"]["redirect_uri"] == expected_uri
183
239
  assert observed["form"]["client_id"] == config.client_id
184
240
  assert observed["form"]["audience"] == config.audience
185
241
  assert observed["url"] == (config.issuer + "/token" if direct else config.hub_url + "/api/v1/auth/token")
@@ -22,11 +22,21 @@ def occupied_port():
22
22
  yield listener.getsockname()[1]
23
23
 
24
24
 
25
- @pytest.mark.parametrize("primary_occupied", [False, True])
25
+ @pytest.mark.parametrize(
26
+ "pinned_port, primary_occupied, expected_attempts",
27
+ [
28
+ (None, False, [8765]),
29
+ (None, True, [8765, 53147]),
30
+ (8765, False, [8765]),
31
+ (53147, False, [53147]),
32
+ ],
33
+ )
26
34
  def test_login_uses_selected_callback_for_authorization_and_token_exchange(
27
- monkeypatch, tmp_path, occupied_port, primary_occupied
35
+ monkeypatch, tmp_path, occupied_port, pinned_port, primary_occupied, expected_attempts
28
36
  ) -> None:
29
37
  monkeypatch.setenv("APHERIS_FOUNDRY_CONFIG_DIR", str(tmp_path))
38
+ if pinned_port is not None:
39
+ monkeypatch.setenv("APHERIS_FOUNDRY_CALLBACK_URL", f"http://127.0.0.1:{pinned_port}/callback")
30
40
  config = auth.RuntimeAuthConfig(
31
41
  hub_url="https://hub.example.com",
32
42
  config_url="https://hub.example.com/config.json",
@@ -80,8 +90,8 @@ def test_login_uses_selected_callback_for_authorization_and_token_exchange(
80
90
  monkeypatch.setattr(auth, "_post_form", exchange)
81
91
 
82
92
  try:
83
- result = auth._run_local_callback_login(config, auto_open_browser=True, callback_host="127.0.0.1")
84
- assert attempted_ports == ([53147, 8765] if primary_occupied else [53147])
93
+ result = auth._login_with_config(config, auto_open_browser=True, callback_host="127.0.0.1")
94
+ assert attempted_ports == expected_attempts
85
95
  expected_uri = f"http://127.0.0.1:{servers[0].server_port}/callback"
86
96
  assert result.callback_redirect_uri == expected_uri
87
97
  assert authorization["redirect_uri"] == [expected_uri]
@@ -142,7 +152,7 @@ def test_other_bind_errors_stop_without_fallback(monkeypatch, first_occupied, er
142
152
  assert error.__cause__ is failure
143
153
  assert "specific OS failure" in error.message
144
154
  assert "address already in use" not in error.message
145
- assert attempted_ports == ([53147, 8765] if first_occupied else [53147])
155
+ assert attempted_ports == ([8765, 53147] if first_occupied else [8765])
146
156
  assert f"localhost:{attempted_ports[-1]}" in error.message
147
157
  assert error.details["ports"] == attempted_ports
148
158
  assert error.details["bind_errors"][-1]["errno"] == error_number
@@ -164,7 +174,7 @@ def test_windows_address_in_use_tries_both_ports(monkeypatch) -> None:
164
174
  )
165
175
  with pytest.raises(CLIError) as caught:
166
176
  callback.start()
167
- assert attempted_ports == [53147, 8765]
177
+ assert attempted_ports == [8765, 53147]
168
178
  assert caught.value.details["ports"] == attempted_ports
169
179
  assert len(caught.value.details["bind_errors"]) == 2
170
180
  assert "localhost:53147" in caught.value.message
@@ -1 +0,0 @@
1
- __version__ = "2.7.1"