sweatstack-cli 0.13.0__tar.gz → 0.15.0__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.
- {sweatstack_cli-0.13.0 → sweatstack_cli-0.15.0}/CHANGELOG.md +50 -0
- {sweatstack_cli-0.13.0 → sweatstack_cli-0.15.0}/DEVELOPMENT.md +1 -1
- {sweatstack_cli-0.13.0 → sweatstack_cli-0.15.0}/Makefile +6 -1
- {sweatstack_cli-0.13.0 → sweatstack_cli-0.15.0}/PKG-INFO +5 -2
- {sweatstack_cli-0.13.0 → sweatstack_cli-0.15.0}/README.md +1 -1
- {sweatstack_cli-0.13.0 → sweatstack_cli-0.15.0}/pyproject.toml +8 -2
- {sweatstack_cli-0.13.0 → sweatstack_cli-0.15.0}/src/sweatstack_cli/api/client.py +11 -1
- {sweatstack_cli-0.13.0 → sweatstack_cli-0.15.0}/src/sweatstack_cli/auth/__init__.py +95 -15
- {sweatstack_cli-0.13.0 → sweatstack_cli-0.15.0}/src/sweatstack_cli/commands/auth.py +37 -23
- {sweatstack_cli-0.13.0 → sweatstack_cli-0.15.0}/src/sweatstack_cli/exceptions.py +21 -0
- {sweatstack_cli-0.13.0 → sweatstack_cli-0.15.0}/src/sweatstack_cli/main.py +1 -1
- {sweatstack_cli-0.13.0 → sweatstack_cli-0.15.0}/src/sweatstack_cli/templates/page/index.html +166 -101
- sweatstack_cli-0.15.0/tests/e2e/test_page_template.py +483 -0
- {sweatstack_cli-0.13.0 → sweatstack_cli-0.15.0}/tests/test_api/test_client.py +60 -1
- sweatstack_cli-0.15.0/tests/test_auth/test_authenticator.py +281 -0
- sweatstack_cli-0.15.0/tests/test_auth/test_authorize_url.py +21 -0
- sweatstack_cli-0.15.0/tests/test_commands/test_auth.py +115 -0
- {sweatstack_cli-0.13.0 → sweatstack_cli-0.15.0}/tests/test_commands/test_page_init.py +13 -0
- sweatstack_cli-0.15.0/tests/test_services/__init__.py +0 -0
- {sweatstack_cli-0.13.0 → sweatstack_cli-0.15.0}/uv.lock +196 -2
- sweatstack_cli-0.13.0/tests/test_commands/test_auth.py +0 -49
- {sweatstack_cli-0.13.0 → sweatstack_cli-0.15.0}/.claude/settings.local.json +0 -0
- {sweatstack_cli-0.13.0 → sweatstack_cli-0.15.0}/.claude/skills/sweatstack/SKILL.md +0 -0
- {sweatstack_cli-0.13.0 → sweatstack_cli-0.15.0}/.claude/skills/sweatstack/api.md +0 -0
- {sweatstack_cli-0.13.0 → sweatstack_cli-0.15.0}/.claude/skills/sweatstack/auth.md +0 -0
- {sweatstack_cli-0.13.0 → sweatstack_cli-0.15.0}/.claude/skills/sweatstack/cli.md +0 -0
- {sweatstack_cli-0.13.0 → sweatstack_cli-0.15.0}/.claude/skills/sweatstack/pyscript.md +0 -0
- {sweatstack_cli-0.13.0 → sweatstack_cli-0.15.0}/.claude/skills/sweatstack/webapp.md +0 -0
- {sweatstack_cli-0.13.0 → sweatstack_cli-0.15.0}/.gitignore +0 -0
- {sweatstack_cli-0.13.0 → sweatstack_cli-0.15.0}/.python-version +0 -0
- {sweatstack_cli-0.13.0 → sweatstack_cli-0.15.0}/CREATE_PRIVATE_APP.md +0 -0
- {sweatstack_cli-0.13.0 → sweatstack_cli-0.15.0}/PLAN.md +0 -0
- {sweatstack_cli-0.13.0 → sweatstack_cli-0.15.0}/plans/001-page-init-command.md +0 -0
- {sweatstack_cli-0.13.0 → sweatstack_cli-0.15.0}/src/sweatstack_cli/__init__.py +0 -0
- {sweatstack_cli-0.13.0 → sweatstack_cli-0.15.0}/src/sweatstack_cli/api/__init__.py +0 -0
- {sweatstack_cli-0.13.0 → sweatstack_cli-0.15.0}/src/sweatstack_cli/auth/callback_server.py +0 -0
- {sweatstack_cli-0.13.0 → sweatstack_cli-0.15.0}/src/sweatstack_cli/auth/jwt.py +0 -0
- {sweatstack_cli-0.13.0 → sweatstack_cli-0.15.0}/src/sweatstack_cli/auth/pkce.py +0 -0
- {sweatstack_cli-0.13.0 → sweatstack_cli-0.15.0}/src/sweatstack_cli/auth/tokens.py +0 -0
- {sweatstack_cli-0.13.0 → sweatstack_cli-0.15.0}/src/sweatstack_cli/commands/__init__.py +0 -0
- {sweatstack_cli-0.13.0 → sweatstack_cli-0.15.0}/src/sweatstack_cli/commands/app.py +0 -0
- {sweatstack_cli-0.13.0 → sweatstack_cli-0.15.0}/src/sweatstack_cli/commands/page.py +0 -0
- {sweatstack_cli-0.13.0 → sweatstack_cli-0.15.0}/src/sweatstack_cli/config.py +0 -0
- {sweatstack_cli-0.13.0 → sweatstack_cli-0.15.0}/src/sweatstack_cli/console.py +0 -0
- {sweatstack_cli-0.13.0 → sweatstack_cli-0.15.0}/src/sweatstack_cli/go_live.py +0 -0
- {sweatstack_cli-0.13.0 → sweatstack_cli-0.15.0}/src/sweatstack_cli/project.py +0 -0
- {sweatstack_cli-0.13.0 → sweatstack_cli-0.15.0}/src/sweatstack_cli/py.typed +0 -0
- {sweatstack_cli-0.13.0 → sweatstack_cli-0.15.0}/src/sweatstack_cli/services/__init__.py +0 -0
- {sweatstack_cli-0.13.0 → sweatstack_cli-0.15.0}/src/sweatstack_cli/services/applications.py +0 -0
- {sweatstack_cli-0.13.0 → sweatstack_cli-0.15.0}/src/sweatstack_cli/version_check.py +0 -0
- {sweatstack_cli-0.13.0 → sweatstack_cli-0.15.0}/sweatstack.toml +0 -0
- {sweatstack_cli-0.13.0 → sweatstack_cli-0.15.0}/tests/__init__.py +0 -0
- {sweatstack_cli-0.13.0 → sweatstack_cli-0.15.0}/tests/conftest.py +0 -0
- {sweatstack_cli-0.13.0/tests/test_services → sweatstack_cli-0.15.0/tests/e2e}/__init__.py +0 -0
- {sweatstack_cli-0.13.0 → sweatstack_cli-0.15.0}/tests/test_api/__init__.py +0 -0
- {sweatstack_cli-0.13.0 → sweatstack_cli-0.15.0}/tests/test_auth/__init__.py +0 -0
- {sweatstack_cli-0.13.0 → sweatstack_cli-0.15.0}/tests/test_auth/test_callback_server.py +0 -0
- {sweatstack_cli-0.13.0 → sweatstack_cli-0.15.0}/tests/test_auth/test_jwt.py +0 -0
- {sweatstack_cli-0.13.0 → sweatstack_cli-0.15.0}/tests/test_auth/test_pkce.py +0 -0
- {sweatstack_cli-0.13.0 → sweatstack_cli-0.15.0}/tests/test_auth/test_tokens.py +0 -0
- {sweatstack_cli-0.13.0 → sweatstack_cli-0.15.0}/tests/test_commands/__init__.py +0 -0
- {sweatstack_cli-0.13.0 → sweatstack_cli-0.15.0}/tests/test_commands/test_app.py +0 -0
- {sweatstack_cli-0.13.0 → sweatstack_cli-0.15.0}/tests/test_commands/test_cli.py +0 -0
- {sweatstack_cli-0.13.0 → sweatstack_cli-0.15.0}/tests/test_commands/test_page_deploy.py +0 -0
- {sweatstack_cli-0.13.0 → sweatstack_cli-0.15.0}/tests/test_project.py +0 -0
- {sweatstack_cli-0.13.0 → sweatstack_cli-0.15.0}/tests/test_services/test_applications.py +0 -0
- {sweatstack_cli-0.13.0 → sweatstack_cli-0.15.0}/tests/test_version.py +0 -0
- {sweatstack_cli-0.13.0 → sweatstack_cli-0.15.0}/tests/test_version_check.py +0 -0
|
@@ -4,6 +4,56 @@ All notable changes to this project are documented here. The format is based on
|
|
|
4
4
|
[Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project
|
|
5
5
|
adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
6
6
|
|
|
7
|
+
## [0.15.0] - 2026-11-19
|
|
8
|
+
|
|
9
|
+
### Fixed
|
|
10
|
+
|
|
11
|
+
- The page `page init` scaffolds can sign in to a new application. It requested
|
|
12
|
+
`data:read`, which SweatStack refuses with `invalid_scope` for an application
|
|
13
|
+
registered since 13 October 2026. It now requests
|
|
14
|
+
`openid profile activities:read offline_access`.
|
|
15
|
+
- With `SWEATSTACK_API_KEY` and `SWEATSTACK_REFRESH_TOKEN` set, a refresh no longer
|
|
16
|
+
writes the environment's tokens over the tokens file, which holds the sign-in of
|
|
17
|
+
`sweatstack login` and is shared with the Python SDK. A command now refreshes once
|
|
18
|
+
and keeps the result; every request used to refresh again with the variable's token.
|
|
19
|
+
|
|
20
|
+
### Changed
|
|
21
|
+
|
|
22
|
+
- From SweatStack 0.162.0, every refresh returns a new refresh token. With the tokens
|
|
23
|
+
from `SWEATSTACK_REFRESH_TOKEN`, the CLI uses the new one for the rest of the command
|
|
24
|
+
and warns on stderr that the variable still holds the old one, which SweatStack will
|
|
25
|
+
stop accepting. Tokens from `sweatstack login` are saved after every refresh, as before.
|
|
26
|
+
- `sweatstack logout` ends the sign-in on SweatStack, so its refresh token stops working
|
|
27
|
+
everywhere, and then removes the stored credentials. When SweatStack can't be reached,
|
|
28
|
+
the credentials are still removed and `logout` says that the sign-in was not ended.
|
|
29
|
+
- The scaffolded page's Sign out ends the sign-in on SweatStack too.
|
|
30
|
+
|
|
31
|
+
- The scaffolded page stays signed in. Access tokens last 15 minutes, and the page
|
|
32
|
+
now refreshes them instead of asking the user to connect again. It keeps the new
|
|
33
|
+
refresh token from every refresh, and only one tab refreshes at a time.
|
|
34
|
+
- A returning user goes straight back through sign-in, and a cancelled sign-in
|
|
35
|
+
shows why instead of starting again. The button reads "Sign in with SweatStack".
|
|
36
|
+
|
|
37
|
+
## [0.14.0] - 2026-10-29
|
|
38
|
+
|
|
39
|
+
### Added
|
|
40
|
+
|
|
41
|
+
- Passkeys (SweatStack plan 084): `sweatstack login` asks the browser to confirm
|
|
42
|
+
your passkey when your account has one (`acr_values=mfa`), and the CLI's tokens
|
|
43
|
+
carry that confirmation, so creating an application and deploying a page work
|
|
44
|
+
without the CLI ever handling the passkey. `sweatstack status` shows whether the
|
|
45
|
+
login confirmed it.
|
|
46
|
+
|
|
47
|
+
### Changed
|
|
48
|
+
|
|
49
|
+
- `sweatstack login` always opens the browser, also when you are logged in:
|
|
50
|
+
signing in again is how a confirmation reaches the CLI. `--force` is still
|
|
51
|
+
accepted and does nothing.
|
|
52
|
+
- A command refused for want of a recent passkey confirmation says to run
|
|
53
|
+
`sweatstack login` and the command again, and one refused because the account
|
|
54
|
+
has no passkey says where to add one; both exit 2. Such a refusal used to read
|
|
55
|
+
"Session expired".
|
|
56
|
+
|
|
7
57
|
## [0.13.0] - 2026-10-15
|
|
8
58
|
|
|
9
59
|
### Changed
|
|
@@ -299,7 +299,7 @@ pytest
|
|
|
299
299
|
| `SWEATSTACK_API_KEY` | Access token (CI/CD) | — |
|
|
300
300
|
| `SWEATSTACK_REFRESH_TOKEN` | Refresh token (CI/CD) | — |
|
|
301
301
|
|
|
302
|
-
For CI/CD pipelines, set `SWEATSTACK_API_KEY` and `SWEATSTACK_REFRESH_TOKEN` to bypass interactive login.
|
|
302
|
+
For CI/CD pipelines, set `SWEATSTACK_API_KEY` and `SWEATSTACK_REFRESH_TOKEN` to bypass interactive login. Both are needed: an API key alone cannot act for the CLI, so it is ignored. SweatStack rotates refresh tokens, so the CLI keeps a refreshed pair for the rest of the command only, never writes it to the tokens file, and warns that the variable still holds the old token, which SweatStack will stop accepting (plan 095 in the server repo).
|
|
303
303
|
|
|
304
304
|
## Error Handling
|
|
305
305
|
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
.PHONY: help install dev test lint format typecheck check clean build publish
|
|
1
|
+
.PHONY: help install dev test test-e2e lint format typecheck check clean build publish
|
|
2
2
|
|
|
3
3
|
help:
|
|
4
4
|
@echo "SweatStack CLI Development Commands"
|
|
@@ -6,6 +6,7 @@ help:
|
|
|
6
6
|
@echo " make install Install the package"
|
|
7
7
|
@echo " make dev Install with development dependencies"
|
|
8
8
|
@echo " make test Run tests"
|
|
9
|
+
@echo " make test-e2e Run the page template in a browser (downloads Chromium once)"
|
|
9
10
|
@echo " make lint Run linter"
|
|
10
11
|
@echo " make format Format code"
|
|
11
12
|
@echo " make typecheck Run type checker"
|
|
@@ -23,6 +24,10 @@ dev:
|
|
|
23
24
|
test:
|
|
24
25
|
uv run pytest
|
|
25
26
|
|
|
27
|
+
test-e2e:
|
|
28
|
+
uv run --extra dev --extra e2e playwright install chromium
|
|
29
|
+
uv run --extra dev --extra e2e pytest tests/e2e -m e2e -v
|
|
30
|
+
|
|
26
31
|
lint:
|
|
27
32
|
uv run ruff check .
|
|
28
33
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: sweatstack-cli
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.15.0
|
|
4
4
|
Summary: Command-line interface for SweatStack — the sports data platform for developers
|
|
5
5
|
Project-URL: Homepage, https://sweatstack.no
|
|
6
6
|
Project-URL: Documentation, https://docs.sweatstack.no
|
|
@@ -26,6 +26,9 @@ Requires-Dist: mypy>=1.14; extra == 'dev'
|
|
|
26
26
|
Requires-Dist: pytest-httpx>=0.35; extra == 'dev'
|
|
27
27
|
Requires-Dist: pytest>=8.0; extra == 'dev'
|
|
28
28
|
Requires-Dist: ruff>=0.9; extra == 'dev'
|
|
29
|
+
Provides-Extra: e2e
|
|
30
|
+
Requires-Dist: playwright>=1.50; extra == 'e2e'
|
|
31
|
+
Requires-Dist: pyarrow>=18.0; extra == 'e2e'
|
|
29
32
|
Description-Content-Type: text/markdown
|
|
30
33
|
|
|
31
34
|
# SweatStack CLI
|
|
@@ -83,7 +86,7 @@ sweatstack page deploy
|
|
|
83
86
|
|---------|-------------|
|
|
84
87
|
| `sweatstack login` | Authenticate via browser (OAuth2 PKCE) |
|
|
85
88
|
| `sweatstack login --force` | Force re-authentication |
|
|
86
|
-
| `sweatstack logout` |
|
|
89
|
+
| `sweatstack logout` | End the sign-in on SweatStack and remove stored credentials |
|
|
87
90
|
| `sweatstack status` | Show auth status, user info, and version |
|
|
88
91
|
| `sweatstack --version` | Show version and check for updates |
|
|
89
92
|
|
|
@@ -53,7 +53,7 @@ sweatstack page deploy
|
|
|
53
53
|
|---------|-------------|
|
|
54
54
|
| `sweatstack login` | Authenticate via browser (OAuth2 PKCE) |
|
|
55
55
|
| `sweatstack login --force` | Force re-authentication |
|
|
56
|
-
| `sweatstack logout` |
|
|
56
|
+
| `sweatstack logout` | End the sign-in on SweatStack and remove stored credentials |
|
|
57
57
|
| `sweatstack status` | Show auth status, user info, and version |
|
|
58
58
|
| `sweatstack --version` | Show version and check for updates |
|
|
59
59
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "sweatstack-cli"
|
|
3
|
-
version = "0.
|
|
3
|
+
version = "0.15.0"
|
|
4
4
|
description = "Command-line interface for SweatStack — the sports data platform for developers"
|
|
5
5
|
readme = "README.md"
|
|
6
6
|
authors = [{ name = "Aart Goossens", email = "aart@sweatstack.no" }]
|
|
@@ -39,6 +39,11 @@ dev = [
|
|
|
39
39
|
"mypy>=1.14",
|
|
40
40
|
"coverage>=7.0",
|
|
41
41
|
]
|
|
42
|
+
# The browser test of the `page init` template (`make test-e2e`).
|
|
43
|
+
e2e = [
|
|
44
|
+
"playwright>=1.50",
|
|
45
|
+
"pyarrow>=18.0",
|
|
46
|
+
]
|
|
42
47
|
|
|
43
48
|
[build-system]
|
|
44
49
|
requires = ["hatchling"]
|
|
@@ -80,7 +85,8 @@ warn_unreachable = true
|
|
|
80
85
|
|
|
81
86
|
[tool.pytest.ini_options]
|
|
82
87
|
testpaths = ["tests"]
|
|
83
|
-
addopts = "-ra -q"
|
|
88
|
+
addopts = "-ra -q -m 'not e2e'"
|
|
89
|
+
markers = ["e2e: drives the page template in a real browser (`make test-e2e`)"]
|
|
84
90
|
|
|
85
91
|
[tool.coverage.run]
|
|
86
92
|
source = ["src/sweatstack_cli"]
|
|
@@ -10,7 +10,12 @@ import httpx
|
|
|
10
10
|
|
|
11
11
|
from sweatstack_cli.auth import Authenticator
|
|
12
12
|
from sweatstack_cli.config import get_settings
|
|
13
|
-
from sweatstack_cli.exceptions import
|
|
13
|
+
from sweatstack_cli.exceptions import (
|
|
14
|
+
APIError,
|
|
15
|
+
AuthenticationError,
|
|
16
|
+
SecondFactorEnrollmentRequired,
|
|
17
|
+
SecondFactorRequired,
|
|
18
|
+
)
|
|
14
19
|
|
|
15
20
|
|
|
16
21
|
class APIClient:
|
|
@@ -152,11 +157,16 @@ class APIClient:
|
|
|
152
157
|
AuthenticationError: For 401 responses.
|
|
153
158
|
APIError: For other error responses.
|
|
154
159
|
"""
|
|
160
|
+
# A step-up (RFC 9470) is a 401 too, but not an expired session: read the challenge first.
|
|
155
161
|
if response.status_code == 401:
|
|
162
|
+
if "insufficient_user_authentication" in response.headers.get("WWW-Authenticate", ""):
|
|
163
|
+
raise SecondFactorRequired()
|
|
156
164
|
raise AuthenticationError("Session expired. Run 'sweatstack login' again.")
|
|
157
165
|
|
|
158
166
|
if response.status_code >= 400:
|
|
159
167
|
detail, code = self._extract_error(response)
|
|
168
|
+
if code == "second_factor_enrollment_required":
|
|
169
|
+
raise SecondFactorEnrollmentRequired(detail)
|
|
160
170
|
raise APIError(
|
|
161
171
|
f"API error ({response.status_code}): {detail}",
|
|
162
172
|
status_code=response.status_code,
|
|
@@ -13,6 +13,7 @@ from sweatstack_cli.auth.callback_server import CallbackServer
|
|
|
13
13
|
from sweatstack_cli.auth.pkce import PKCEChallenge, generate_pkce
|
|
14
14
|
from sweatstack_cli.auth.tokens import FileTokenStorage, TokenPair
|
|
15
15
|
from sweatstack_cli.config import get_settings
|
|
16
|
+
from sweatstack_cli.console import stderr_console
|
|
16
17
|
from sweatstack_cli.exceptions import AuthenticationError
|
|
17
18
|
|
|
18
19
|
if TYPE_CHECKING:
|
|
@@ -24,6 +25,15 @@ CLIENT_ID = "5382f68b0d254378"
|
|
|
24
25
|
# Default OAuth2 scopes (offline_access required for refresh tokens)
|
|
25
26
|
SCOPES = "offline_access data:read data:write profile"
|
|
26
27
|
|
|
28
|
+
# Said once per command when the refresh token from the environment was replaced by a new one,
|
|
29
|
+
# which the CLI can keep only for that command.
|
|
30
|
+
ROTATION_LOST_WARNING = (
|
|
31
|
+
"[yellow]Warning:[/yellow] SweatStack replaced the refresh token from "
|
|
32
|
+
"SWEATSTACK_REFRESH_TOKEN. This command used the new one, but the variable still holds the "
|
|
33
|
+
"old one, which SweatStack will stop accepting. To stay signed in on this machine, unset "
|
|
34
|
+
"SWEATSTACK_API_KEY and SWEATSTACK_REFRESH_TOKEN and run 'sweatstack login'."
|
|
35
|
+
)
|
|
36
|
+
|
|
27
37
|
|
|
28
38
|
class Authenticator:
|
|
29
39
|
"""
|
|
@@ -57,6 +67,10 @@ class Authenticator:
|
|
|
57
67
|
self._client_id = client_id
|
|
58
68
|
self._scopes = scopes
|
|
59
69
|
self._settings = get_settings()
|
|
70
|
+
# The environment's tokens once this command refreshed them. Never written to the
|
|
71
|
+
# tokens file: that holds the sign-in of `sweatstack login`, shared with the Python SDK.
|
|
72
|
+
self._renewed_environment_tokens: TokenPair | None = None
|
|
73
|
+
self._warned_rotation_lost = False
|
|
60
74
|
|
|
61
75
|
def login(self, force: bool = False) -> TokenPair:
|
|
62
76
|
"""
|
|
@@ -110,39 +124,77 @@ class Authenticator:
|
|
|
110
124
|
Checks token expiry with a 30-second margin to avoid
|
|
111
125
|
edge cases where token expires during a request.
|
|
112
126
|
|
|
127
|
+
Every refresh returns a new refresh token, and the old one stops working. Tokens from
|
|
128
|
+
the tokens file are saved back to it. Tokens from the environment are kept for the
|
|
129
|
+
rest of this command only, with a warning, because the variables cannot be updated.
|
|
130
|
+
|
|
113
131
|
Returns:
|
|
114
132
|
Valid TokenPair, or None if not authenticated or refresh fails.
|
|
115
133
|
"""
|
|
116
|
-
tokens = self._load_tokens()
|
|
134
|
+
tokens, from_environment = self._load_tokens()
|
|
117
135
|
if tokens is None:
|
|
118
136
|
return None
|
|
119
137
|
|
|
120
138
|
if tokens.is_expired(margin_seconds=30):
|
|
121
139
|
try:
|
|
122
|
-
|
|
123
|
-
self._storage.save(tokens)
|
|
140
|
+
renewed = self._refresh_tokens(tokens.refresh_token)
|
|
124
141
|
except AuthenticationError:
|
|
125
142
|
return None
|
|
143
|
+
if from_environment:
|
|
144
|
+
self._renewed_environment_tokens = renewed
|
|
145
|
+
if renewed.refresh_token != tokens.refresh_token:
|
|
146
|
+
self._warn_rotation_lost()
|
|
147
|
+
else:
|
|
148
|
+
self._storage.save(renewed)
|
|
149
|
+
tokens = renewed
|
|
126
150
|
|
|
127
151
|
return tokens
|
|
128
152
|
|
|
129
|
-
def logout(self) -> None:
|
|
130
|
-
"""
|
|
131
|
-
|
|
153
|
+
def logout(self) -> str | None:
|
|
154
|
+
"""
|
|
155
|
+
End the sign-in of ``sweatstack login`` and remove the tokens file.
|
|
156
|
+
|
|
157
|
+
The refresh token is revoked on SweatStack first, so it stops working everywhere, also
|
|
158
|
+
for a copy of the file. The file is removed whether or not that succeeds.
|
|
159
|
+
|
|
160
|
+
Returns:
|
|
161
|
+
Why SweatStack did not end the sign-in, or None when it did or nothing was stored.
|
|
162
|
+
"""
|
|
163
|
+
tokens = self._storage.load()
|
|
164
|
+
try:
|
|
165
|
+
return None if tokens is None else self._revoke(tokens.refresh_token)
|
|
166
|
+
finally:
|
|
167
|
+
self._storage.delete()
|
|
132
168
|
|
|
133
|
-
def _load_tokens(self) -> TokenPair | None:
|
|
169
|
+
def _load_tokens(self) -> tuple[TokenPair | None, bool]:
|
|
134
170
|
"""
|
|
135
|
-
|
|
171
|
+
The tokens to use, and whether they come from the environment.
|
|
136
172
|
|
|
137
|
-
|
|
173
|
+
``SWEATSTACK_API_KEY`` and ``SWEATSTACK_REFRESH_TOKEN`` together take precedence over
|
|
174
|
+
the tokens file, for CI. Either one alone is ignored: an API key cannot act for the CLI.
|
|
138
175
|
"""
|
|
176
|
+
if self._renewed_environment_tokens is not None:
|
|
177
|
+
return self._renewed_environment_tokens, True
|
|
178
|
+
|
|
139
179
|
access = os.environ.get("SWEATSTACK_API_KEY")
|
|
140
180
|
refresh = os.environ.get("SWEATSTACK_REFRESH_TOKEN")
|
|
141
181
|
|
|
142
182
|
if access and refresh:
|
|
143
|
-
return TokenPair(access_token=access, refresh_token=refresh)
|
|
183
|
+
return TokenPair(access_token=access, refresh_token=refresh), True
|
|
184
|
+
|
|
185
|
+
return self._storage.load(), False
|
|
186
|
+
|
|
187
|
+
def _oauth_client(self, timeout: float) -> httpx.Client:
|
|
188
|
+
"""A client for the OAuth endpoints, named as the API client names itself."""
|
|
189
|
+
return httpx.Client(
|
|
190
|
+
timeout=timeout,
|
|
191
|
+
headers={"User-Agent": f"sweatstack-cli/{self._settings.version}"},
|
|
192
|
+
)
|
|
144
193
|
|
|
145
|
-
|
|
194
|
+
def _warn_rotation_lost(self) -> None:
|
|
195
|
+
if not self._warned_rotation_lost:
|
|
196
|
+
self._warned_rotation_lost = True
|
|
197
|
+
stderr_console.print(ROTATION_LOST_WARNING)
|
|
146
198
|
|
|
147
199
|
def _build_auth_url(self, redirect_uri: str, pkce: PKCEChallenge) -> str:
|
|
148
200
|
"""Construct OAuth2 authorization URL with PKCE challenge."""
|
|
@@ -154,6 +206,9 @@ class Authenticator:
|
|
|
154
206
|
"code_challenge": pkce.challenge,
|
|
155
207
|
"code_challenge_method": "S256",
|
|
156
208
|
"state": pkce.state,
|
|
209
|
+
# SweatStack plan 084 D11: a user with a passkey confirms it in the browser here, and the
|
|
210
|
+
# CLI's tokens carry that confirmation. For anyone else it changes nothing.
|
|
211
|
+
"acr_values": "mfa",
|
|
157
212
|
}
|
|
158
213
|
return f"{self._settings.api_url}/oauth/authorize?{urlencode(params)}"
|
|
159
214
|
|
|
@@ -164,7 +219,7 @@ class Authenticator:
|
|
|
164
219
|
code_verifier: str,
|
|
165
220
|
) -> TokenPair:
|
|
166
221
|
"""Exchange authorization code for tokens."""
|
|
167
|
-
with
|
|
222
|
+
with self._oauth_client(timeout=30.0) as client:
|
|
168
223
|
response = client.post(
|
|
169
224
|
f"{self._settings.api_url}/api/v1/oauth/token",
|
|
170
225
|
data={
|
|
@@ -191,7 +246,7 @@ class Authenticator:
|
|
|
191
246
|
|
|
192
247
|
def _refresh_tokens(self, refresh_token: str) -> TokenPair:
|
|
193
248
|
"""Refresh expired access token using refresh token."""
|
|
194
|
-
with
|
|
249
|
+
with self._oauth_client(timeout=30.0) as client:
|
|
195
250
|
response = client.post(
|
|
196
251
|
f"{self._settings.api_url}/api/v1/oauth/token",
|
|
197
252
|
data={
|
|
@@ -207,10 +262,35 @@ class Authenticator:
|
|
|
207
262
|
data = response.json()
|
|
208
263
|
return TokenPair(
|
|
209
264
|
access_token=data["access_token"],
|
|
210
|
-
#
|
|
211
|
-
|
|
265
|
+
# SweatStack returns a new refresh token with every refresh (it rotates them); an
|
|
266
|
+
# older server returned none, and the presented one stays good then.
|
|
267
|
+
refresh_token=data.get("refresh_token") or refresh_token,
|
|
212
268
|
)
|
|
213
269
|
|
|
270
|
+
def _revoke(self, refresh_token: str) -> str | None:
|
|
271
|
+
"""Revoke a refresh token (RFC 7009). Returns why it failed, or None."""
|
|
272
|
+
try:
|
|
273
|
+
with self._oauth_client(timeout=10.0) as client:
|
|
274
|
+
response = client.post(
|
|
275
|
+
f"{self._settings.api_url}/api/v1/oauth/revoke",
|
|
276
|
+
data={
|
|
277
|
+
"token": refresh_token,
|
|
278
|
+
"token_type_hint": "refresh_token",
|
|
279
|
+
"client_id": self._client_id,
|
|
280
|
+
},
|
|
281
|
+
)
|
|
282
|
+
except httpx.HTTPError:
|
|
283
|
+
return "SweatStack could not be reached"
|
|
284
|
+
|
|
285
|
+
if response.status_code == 200:
|
|
286
|
+
return None
|
|
287
|
+
try:
|
|
288
|
+
body = response.json()
|
|
289
|
+
reason = body.get("error_description") or body.get("error")
|
|
290
|
+
except Exception:
|
|
291
|
+
reason = None
|
|
292
|
+
return f"SweatStack answered {response.status_code}" + (f": {reason}" if reason else "")
|
|
293
|
+
|
|
214
294
|
|
|
215
295
|
# Re-export commonly used types
|
|
216
296
|
__all__ = ["AuthenticationError", "Authenticator", "FileTokenStorage", "TokenPair"]
|
|
@@ -2,10 +2,13 @@
|
|
|
2
2
|
|
|
3
3
|
from __future__ import annotations
|
|
4
4
|
|
|
5
|
+
from datetime import datetime
|
|
6
|
+
|
|
5
7
|
import typer
|
|
6
8
|
|
|
7
9
|
from sweatstack_cli.api import APIClient
|
|
8
10
|
from sweatstack_cli.auth import Authenticator
|
|
11
|
+
from sweatstack_cli.auth.jwt import decode_jwt_payload
|
|
9
12
|
from sweatstack_cli.console import console
|
|
10
13
|
from sweatstack_cli.exceptions import APIError, AuthenticationError
|
|
11
14
|
from sweatstack_cli.version_check import check_for_update
|
|
@@ -22,30 +25,22 @@ def _display_name(user: dict[str, object]) -> str:
|
|
|
22
25
|
|
|
23
26
|
|
|
24
27
|
def login(
|
|
28
|
+
# Kept so scripts that pass it keep working.
|
|
25
29
|
force: bool = typer.Option(
|
|
26
30
|
False,
|
|
27
31
|
"--force",
|
|
28
32
|
"-f",
|
|
29
|
-
|
|
33
|
+
hidden=True,
|
|
34
|
+
help="No longer needed: login always opens the browser.",
|
|
30
35
|
),
|
|
31
36
|
) -> None:
|
|
32
|
-
"""Authenticate with SweatStack via browser.
|
|
33
|
-
auth = Authenticator()
|
|
37
|
+
"""Authenticate with SweatStack via browser.
|
|
34
38
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
client = APIClient(authenticator=auth)
|
|
41
|
-
user = client.get("/api/v1/oauth/userinfo")
|
|
42
|
-
console.print(
|
|
43
|
-
f"Already logged in as [bold]{_display_name(user)}[/bold]. "
|
|
44
|
-
f"Use [cyan]--force[/cyan] to re-authenticate."
|
|
45
|
-
)
|
|
46
|
-
return
|
|
47
|
-
except AuthenticationError:
|
|
48
|
-
pass # Token invalid, proceed with login
|
|
39
|
+
Always opens the browser, as ``gh auth login`` does: signing in again is how a
|
|
40
|
+
passkey confirmation reaches the CLI's tokens (SweatStack plan 084). Other
|
|
41
|
+
commands never open a browser on their own.
|
|
42
|
+
"""
|
|
43
|
+
auth = Authenticator()
|
|
49
44
|
|
|
50
45
|
with console.status("[bold]Opening browser for authentication...[/bold]"):
|
|
51
46
|
try:
|
|
@@ -64,11 +59,29 @@ def login(
|
|
|
64
59
|
raise typer.Exit(2)
|
|
65
60
|
|
|
66
61
|
|
|
62
|
+
def _confirmation(access_token: str) -> str:
|
|
63
|
+
"""Whether the login that issued these tokens confirmed a passkey (SweatStack plan 084):
|
|
64
|
+
the ``amr`` and ``auth_time`` claims the browser's confirmation put on them. Which actions
|
|
65
|
+
need a confirmation, and how recent, is the server's to decide: a refusal says what to do."""
|
|
66
|
+
try:
|
|
67
|
+
claims = decode_jwt_payload(access_token)
|
|
68
|
+
except ValueError:
|
|
69
|
+
return "unknown"
|
|
70
|
+
if "mfa" in (claims.get("amr") or []) and isinstance(claims.get("auth_time"), int):
|
|
71
|
+
confirmed_at = datetime.fromtimestamp(claims["auth_time"]).astimezone()
|
|
72
|
+
return f"confirmed at login {confirmed_at:%Y-%m-%d %H:%M}"
|
|
73
|
+
return "not confirmed at login"
|
|
74
|
+
|
|
75
|
+
|
|
67
76
|
def logout() -> None:
|
|
68
|
-
"""
|
|
69
|
-
|
|
70
|
-
auth.logout()
|
|
77
|
+
"""End the sign-in on SweatStack and remove the stored credentials."""
|
|
78
|
+
failure = Authenticator().logout()
|
|
71
79
|
console.print("[green]✓[/green] Logged out")
|
|
80
|
+
if failure:
|
|
81
|
+
console.print(
|
|
82
|
+
f"[yellow]![/yellow] The sign-in could not be ended on SweatStack ({failure}). "
|
|
83
|
+
"The removed refresh token keeps working until it goes a year unused."
|
|
84
|
+
)
|
|
72
85
|
|
|
73
86
|
|
|
74
87
|
def status() -> None:
|
|
@@ -86,11 +99,12 @@ def status() -> None:
|
|
|
86
99
|
try:
|
|
87
100
|
client = APIClient(authenticator=auth)
|
|
88
101
|
user = client.get("/api/v1/oauth/userinfo")
|
|
89
|
-
console.print(f" Name:
|
|
90
|
-
console.print(f" Email:
|
|
91
|
-
console.print(f" ID:
|
|
102
|
+
console.print(f" Name: {_display_name(user)}")
|
|
103
|
+
console.print(f" Email: {user.get('email', 'unknown')}")
|
|
104
|
+
console.print(f" ID: {user.get('sub', 'unknown')}")
|
|
92
105
|
except (AuthenticationError, APIError):
|
|
93
106
|
pass # Silently skip user info on error
|
|
107
|
+
console.print(f" Passkey: {_confirmation(tokens.access_token)}")
|
|
94
108
|
|
|
95
109
|
# Check version
|
|
96
110
|
update_available, latest = check_for_update()
|
|
@@ -18,6 +18,27 @@ class AuthenticationError(CLIError):
|
|
|
18
18
|
exit_code = 2
|
|
19
19
|
|
|
20
20
|
|
|
21
|
+
class SecondFactorRequired(AuthenticationError):
|
|
22
|
+
"""The action needs a recent passkey confirmation (SweatStack plan 084).
|
|
23
|
+
|
|
24
|
+
The server answers RFC 9470's step-up challenge: a ``401`` whose ``WWW-Authenticate`` says
|
|
25
|
+
``insufficient_user_authentication``. The browser confirms it at ``sweatstack login``; the CLI
|
|
26
|
+
never handles a passkey itself. Not an expired session, so it never says so."""
|
|
27
|
+
|
|
28
|
+
def __init__(self) -> None:
|
|
29
|
+
super().__init__(
|
|
30
|
+
"This needs your passkey. Run 'sweatstack login' to confirm in your browser, "
|
|
31
|
+
"then run this command again."
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
class SecondFactorEnrollmentRequired(AuthenticationError):
|
|
36
|
+
"""The account has no passkey, and the action needs one (SweatStack plan 084)."""
|
|
37
|
+
|
|
38
|
+
def __init__(self, detail: str) -> None:
|
|
39
|
+
super().__init__(f"{detail} Then run 'sweatstack login' and this command again.")
|
|
40
|
+
|
|
41
|
+
|
|
21
42
|
class APIError(CLIError):
|
|
22
43
|
"""API returned an error response.
|
|
23
44
|
|
|
@@ -27,7 +27,7 @@ app.add_typer(app_commands.app, name="app")
|
|
|
27
27
|
# Register top-level auth commands for convenience
|
|
28
28
|
# These are the most common operations, so we promote them to top level
|
|
29
29
|
app.command(name="login", help="Authenticate with SweatStack via browser.")(auth.login)
|
|
30
|
-
app.command(name="logout", help="
|
|
30
|
+
app.command(name="logout", help="End the sign-in and remove stored credentials.")(auth.logout)
|
|
31
31
|
app.command(name="status", help="Show authentication and version status.")(auth.status)
|
|
32
32
|
|
|
33
33
|
|