podojo-cli 1.34.0__tar.gz → 1.35.1__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.
- {podojo_cli-1.34.0 → podojo_cli-1.35.1}/CHANGELOG.md +22 -0
- {podojo_cli-1.34.0 → podojo_cli-1.35.1}/CLAUDE.md +11 -0
- {podojo_cli-1.34.0 → podojo_cli-1.35.1}/PKG-INFO +18 -8
- {podojo_cli-1.34.0 → podojo_cli-1.35.1}/README.md +17 -7
- {podojo_cli-1.34.0 → podojo_cli-1.35.1}/pyproject.toml +1 -1
- {podojo_cli-1.34.0 → podojo_cli-1.35.1}/src/podojo_cli/client.py +5 -1
- podojo_cli-1.35.1/src/podojo_cli/commands/auth.py +89 -0
- {podojo_cli-1.34.0 → podojo_cli-1.35.1}/src/podojo_cli/config.py +16 -2
- podojo_cli-1.35.1/src/podojo_cli/oauth.py +220 -0
- podojo_cli-1.35.1/tests/test_auth.py +279 -0
- {podojo_cli-1.34.0 → podojo_cli-1.35.1}/uv.lock +1 -1
- podojo_cli-1.34.0/src/podojo_cli/commands/auth.py +0 -49
- podojo_cli-1.34.0/tests/test_auth.py +0 -53
- {podojo_cli-1.34.0 → podojo_cli-1.35.1}/.github/workflows/publish.yml +0 -0
- {podojo_cli-1.34.0 → podojo_cli-1.35.1}/.gitignore +0 -0
- {podojo_cli-1.34.0 → podojo_cli-1.35.1}/LICENSE +0 -0
- {podojo_cli-1.34.0 → podojo_cli-1.35.1}/src/podojo_cli/__init__.py +0 -0
- {podojo_cli-1.34.0 → podojo_cli-1.35.1}/src/podojo_cli/commands/__init__.py +0 -0
- {podojo_cli-1.34.0 → podojo_cli-1.35.1}/src/podojo_cli/commands/aiinterviews.py +0 -0
- {podojo_cli-1.34.0 → podojo_cli-1.35.1}/src/podojo_cli/commands/interviews.py +0 -0
- {podojo_cli-1.34.0 → podojo_cli-1.35.1}/src/podojo_cli/commands/projects.py +0 -0
- {podojo_cli-1.34.0 → podojo_cli-1.35.1}/src/podojo_cli/commands/showreel.py +0 -0
- {podojo_cli-1.34.0 → podojo_cli-1.35.1}/src/podojo_cli/commands/synth.py +0 -0
- {podojo_cli-1.34.0 → podojo_cli-1.35.1}/src/podojo_cli/commands/transcripts.py +0 -0
- {podojo_cli-1.34.0 → podojo_cli-1.35.1}/src/podojo_cli/commands/usertests.py +0 -0
- {podojo_cli-1.34.0 → podojo_cli-1.35.1}/src/podojo_cli/commands/videos.py +0 -0
- {podojo_cli-1.34.0 → podojo_cli-1.35.1}/src/podojo_cli/main.py +0 -0
- {podojo_cli-1.34.0 → podojo_cli-1.35.1}/src/podojo_cli/synth/__init__.py +0 -0
- {podojo_cli-1.34.0 → podojo_cli-1.35.1}/src/podojo_cli/synth/driver.py +0 -0
- {podojo_cli-1.34.0 → podojo_cli-1.35.1}/src/podojo_cli/synth/session.py +0 -0
- {podojo_cli-1.34.0 → podojo_cli-1.35.1}/src/podojo_cli/tables.py +0 -0
- {podojo_cli-1.34.0 → podojo_cli-1.35.1}/src/podojo_cli/version_check.py +0 -0
- {podojo_cli-1.34.0 → podojo_cli-1.35.1}/src/podojo_cli/video/__init__.py +0 -0
- {podojo_cli-1.34.0 → podojo_cli-1.35.1}/src/podojo_cli/video/showreel.py +0 -0
- {podojo_cli-1.34.0 → podojo_cli-1.35.1}/tests/conftest.py +0 -0
- {podojo_cli-1.34.0 → podojo_cli-1.35.1}/tests/test_aiinterviews.py +0 -0
- {podojo_cli-1.34.0 → podojo_cli-1.35.1}/tests/test_interviews.py +0 -0
- {podojo_cli-1.34.0 → podojo_cli-1.35.1}/tests/test_projects.py +0 -0
- {podojo_cli-1.34.0 → podojo_cli-1.35.1}/tests/test_showreel.py +0 -0
- {podojo_cli-1.34.0 → podojo_cli-1.35.1}/tests/test_transcripts.py +0 -0
- {podojo_cli-1.34.0 → podojo_cli-1.35.1}/tests/test_usertests.py +0 -0
- {podojo_cli-1.34.0 → podojo_cli-1.35.1}/tests/test_videos.py +0 -0
|
@@ -5,6 +5,28 @@ All notable changes to the Podojo CLI will be documented in this file.
|
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com),
|
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org).
|
|
7
7
|
|
|
8
|
+
## [1.35.1] - 2026-11-26
|
|
9
|
+
|
|
10
|
+
### Changed
|
|
11
|
+
- When `podojo auth login` finds no workspace for the account, it now says to
|
|
12
|
+
confirm the email address first. New accounts get their workspace only after
|
|
13
|
+
the email is confirmed.
|
|
14
|
+
|
|
15
|
+
## [1.35.0] - 2026-11-26
|
|
16
|
+
|
|
17
|
+
### Added
|
|
18
|
+
- `podojo auth login` logs in with your Podojo account through the browser, so
|
|
19
|
+
you no longer need an API key. The login is kept in the system keychain and
|
|
20
|
+
renewed on its own; `podojo auth logout` removes it.
|
|
21
|
+
- Commands say "Not logged in" when there is neither a login nor an API key,
|
|
22
|
+
instead of failing with a 401.
|
|
23
|
+
|
|
24
|
+
### Changed
|
|
25
|
+
- `podojo auth login` without `--key` no longer prompts for an API key. API keys
|
|
26
|
+
keep working with `podojo auth login --key XXX` or `PODOJO_API_KEY`, and win
|
|
27
|
+
over a browser login. Use them where the CLI and the browser are not on the
|
|
28
|
+
same machine (CI, remote shells, sandboxes).
|
|
29
|
+
|
|
8
30
|
## [1.34.0] - 2026-11-14
|
|
9
31
|
|
|
10
32
|
### Added
|
|
@@ -61,6 +61,17 @@ plus the path to a fresh full-page screenshot. The agent reads the screenshot an
|
|
|
61
61
|
decides the next action. State lives in `~/.podojo/synth/`; v1 supports one session
|
|
62
62
|
at a time. No LLM is bundled with the CLI — the calling agent provides the reasoning.
|
|
63
63
|
|
|
64
|
+
## Authentication
|
|
65
|
+
|
|
66
|
+
`podojo auth login` logs in through the browser (OAuth authorization code flow
|
|
67
|
+
with PKCE, `src/podojo_cli/oauth.py`); `--key` or `PODOJO_API_KEY` uses an API
|
|
68
|
+
key instead and wins over a browser login. The CLI asks the API where to log in
|
|
69
|
+
(`GET /api/v1/auth/config`: Auth0 domain, client ID, audience), so it holds no
|
|
70
|
+
Auth0 settings of its own. Auth0 redirects to `http://127.0.0.1:<port>/callback`
|
|
71
|
+
on one of `CALLBACK_PORTS`, which must match the Allowed Callback URLs of the
|
|
72
|
+
"Podojo CLI" application in Auth0. The keyring keeps the refresh token
|
|
73
|
+
(`session`) and the current access token (`access_token`).
|
|
74
|
+
|
|
64
75
|
## Configuration
|
|
65
76
|
|
|
66
77
|
Set via env vars or `~/.podojo.toml`:
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: podojo-cli
|
|
3
|
-
Version: 1.
|
|
3
|
+
Version: 1.35.1
|
|
4
4
|
Summary: CLI for the Podojo user research platform
|
|
5
5
|
Project-URL: Homepage, https://github.com/podojo/cli-podojo
|
|
6
6
|
Project-URL: Source, https://github.com/podojo/cli-podojo
|
|
@@ -58,18 +58,28 @@ Requires Python 3.11+. Some commands need extra tooling:
|
|
|
58
58
|
|
|
59
59
|
## Authentication
|
|
60
60
|
|
|
61
|
-
Log in once with your Podojo
|
|
61
|
+
Log in once with your Podojo account — the CLI opens your browser and keeps the
|
|
62
|
+
login in your system keychain:
|
|
62
63
|
|
|
63
64
|
```bash
|
|
64
|
-
podojo auth login #
|
|
65
|
-
podojo auth
|
|
66
|
-
podojo auth logout # remove the stored key
|
|
65
|
+
podojo auth login # log in through the browser
|
|
66
|
+
podojo auth logout # remove the stored login
|
|
67
67
|
```
|
|
68
68
|
|
|
69
|
-
|
|
69
|
+
The browser sends the login back to the CLI on `127.0.0.1` (port 8914, 8915 or
|
|
70
|
+
8916), so the CLI and the browser have to run on the same machine. Where they
|
|
71
|
+
don't — CI, a remote shell, a sandbox — use an API key instead:
|
|
70
72
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
+
```bash
|
|
74
|
+
podojo auth login --key XXX # store an API key in the keychain
|
|
75
|
+
export PODOJO_API_KEY=XXX # or set it for the session
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
An API key always wins over a browser login. Configuration is read from, in
|
|
79
|
+
order of precedence:
|
|
80
|
+
|
|
81
|
+
1. `~/.podojo.toml`
|
|
82
|
+
2. Environment variables — `PODOJO_API_KEY`, `PODOJO_BASE_URL`
|
|
73
83
|
3. The system keyring (set by `podojo auth login`)
|
|
74
84
|
|
|
75
85
|
## Usage
|
|
@@ -26,18 +26,28 @@ Requires Python 3.11+. Some commands need extra tooling:
|
|
|
26
26
|
|
|
27
27
|
## Authentication
|
|
28
28
|
|
|
29
|
-
Log in once with your Podojo
|
|
29
|
+
Log in once with your Podojo account — the CLI opens your browser and keeps the
|
|
30
|
+
login in your system keychain:
|
|
30
31
|
|
|
31
32
|
```bash
|
|
32
|
-
podojo auth login #
|
|
33
|
-
podojo auth
|
|
34
|
-
podojo auth logout # remove the stored key
|
|
33
|
+
podojo auth login # log in through the browser
|
|
34
|
+
podojo auth logout # remove the stored login
|
|
35
35
|
```
|
|
36
36
|
|
|
37
|
-
|
|
37
|
+
The browser sends the login back to the CLI on `127.0.0.1` (port 8914, 8915 or
|
|
38
|
+
8916), so the CLI and the browser have to run on the same machine. Where they
|
|
39
|
+
don't — CI, a remote shell, a sandbox — use an API key instead:
|
|
38
40
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
+
```bash
|
|
42
|
+
podojo auth login --key XXX # store an API key in the keychain
|
|
43
|
+
export PODOJO_API_KEY=XXX # or set it for the session
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
An API key always wins over a browser login. Configuration is read from, in
|
|
47
|
+
order of precedence:
|
|
48
|
+
|
|
49
|
+
1. `~/.podojo.toml`
|
|
50
|
+
2. Environment variables — `PODOJO_API_KEY`, `PODOJO_BASE_URL`
|
|
41
51
|
3. The system keyring (set by `podojo auth login`)
|
|
42
52
|
|
|
43
53
|
## Usage
|
|
@@ -3,6 +3,7 @@ from pathlib import Path
|
|
|
3
3
|
|
|
4
4
|
import httpx
|
|
5
5
|
|
|
6
|
+
from . import oauth
|
|
6
7
|
from .config import load_config
|
|
7
8
|
|
|
8
9
|
|
|
@@ -13,7 +14,10 @@ class PodojoClient:
|
|
|
13
14
|
self.api_key = config["api_key"]
|
|
14
15
|
|
|
15
16
|
def _headers(self) -> dict:
|
|
16
|
-
|
|
17
|
+
token = self.api_key or oauth.access_token()
|
|
18
|
+
if not token:
|
|
19
|
+
raise oauth.LoginError("Not logged in. Run `podojo auth login`.")
|
|
20
|
+
return {"Authorization": f"Bearer {token}"}
|
|
17
21
|
|
|
18
22
|
def list_projects(self, skip: int = 0, limit: int = 50) -> list:
|
|
19
23
|
r = httpx.get(
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import os
|
|
2
|
+
|
|
3
|
+
import httpx
|
|
4
|
+
import typer
|
|
5
|
+
from rich.console import Console
|
|
6
|
+
|
|
7
|
+
from .. import oauth
|
|
8
|
+
from ..config import clear_api_key, load_config, save_config
|
|
9
|
+
|
|
10
|
+
app = typer.Typer(help="Authenticate with Podojo")
|
|
11
|
+
console = Console()
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def _api_status(token: str) -> int | None:
|
|
15
|
+
"""The API's answer to this API key or access token; None if unreachable."""
|
|
16
|
+
base_url = load_config()["base_url"].rstrip("/") + "/api/v1"
|
|
17
|
+
try:
|
|
18
|
+
r = httpx.get(
|
|
19
|
+
f"{base_url}/projects",
|
|
20
|
+
params={"limit": 1},
|
|
21
|
+
headers={"Authorization": f"Bearer {token}"},
|
|
22
|
+
timeout=10,
|
|
23
|
+
)
|
|
24
|
+
return r.status_code
|
|
25
|
+
except httpx.RequestError:
|
|
26
|
+
return None
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def _login_with_key(api_key: str):
|
|
30
|
+
console.print("Verifying key…", end=" ")
|
|
31
|
+
if _api_status(api_key) != 200:
|
|
32
|
+
console.print("[red]failed[/red]")
|
|
33
|
+
console.print("[red]Error:[/red] Invalid API key or could not reach the server.")
|
|
34
|
+
raise typer.Exit(1)
|
|
35
|
+
save_config(api_key)
|
|
36
|
+
oauth.clear_session()
|
|
37
|
+
console.print("[green]done[/green]")
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def _login_with_browser():
|
|
41
|
+
auth_config = oauth.fetch_auth_config(load_config()["base_url"])
|
|
42
|
+
if auth_config is None:
|
|
43
|
+
console.print(
|
|
44
|
+
"[red]Error:[/red] This server does not offer browser login. "
|
|
45
|
+
"Log in with an API key: `podojo auth login --key XXX`."
|
|
46
|
+
)
|
|
47
|
+
raise typer.Exit(1)
|
|
48
|
+
|
|
49
|
+
def show_url(url: str):
|
|
50
|
+
console.print("Opening your browser to log in. If it does not open, visit:")
|
|
51
|
+
typer.echo(url)
|
|
52
|
+
|
|
53
|
+
tokens = oauth.login(auth_config, show_url=show_url)
|
|
54
|
+
status = _api_status(tokens["access_token"])
|
|
55
|
+
if status == 403:
|
|
56
|
+
console.print(
|
|
57
|
+
"[red]Error:[/red] Your account has no Podojo workspace yet. If you just signed up, "
|
|
58
|
+
"confirm your email address with the link we sent you, then log in again."
|
|
59
|
+
)
|
|
60
|
+
raise typer.Exit(1)
|
|
61
|
+
if status != 200:
|
|
62
|
+
console.print("[red]Error:[/red] Logged in, but the Podojo API did not accept the login.")
|
|
63
|
+
raise typer.Exit(1)
|
|
64
|
+
oauth.save_session(auth_config, tokens)
|
|
65
|
+
clear_api_key()
|
|
66
|
+
if os.getenv("PODOJO_API_KEY"):
|
|
67
|
+
console.print("[yellow]Note:[/yellow] PODOJO_API_KEY is set and takes precedence over this login.")
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
@app.command("login")
|
|
71
|
+
def login(
|
|
72
|
+
api_key: str = typer.Option(
|
|
73
|
+
None, "--key", "-k", help="Log in with a Podojo API key instead of the browser"
|
|
74
|
+
),
|
|
75
|
+
):
|
|
76
|
+
"""Log in through the browser, or with an API key (--key)."""
|
|
77
|
+
if api_key:
|
|
78
|
+
_login_with_key(api_key)
|
|
79
|
+
else:
|
|
80
|
+
_login_with_browser()
|
|
81
|
+
console.print("[green]Logged in successfully.[/green]")
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
@app.command("logout")
|
|
85
|
+
def logout():
|
|
86
|
+
"""Remove the saved login or API key."""
|
|
87
|
+
clear_api_key()
|
|
88
|
+
oauth.clear_session()
|
|
89
|
+
console.print("Logged out.")
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import json
|
|
1
2
|
import os
|
|
2
3
|
from pathlib import Path
|
|
3
4
|
|
|
@@ -32,11 +33,24 @@ def save_config(api_key: str):
|
|
|
32
33
|
keyring.set_password(KEYRING_SERVICE, KEYRING_USERNAME, api_key)
|
|
33
34
|
|
|
34
35
|
|
|
35
|
-
def
|
|
36
|
+
def load_secret(name: str) -> dict | None:
|
|
37
|
+
value = keyring.get_password(KEYRING_SERVICE, name)
|
|
38
|
+
return json.loads(value) if value else None
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def save_secret(name: str, value: dict):
|
|
42
|
+
keyring.set_password(KEYRING_SERVICE, name, json.dumps(value))
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def clear_secret(name: str):
|
|
36
46
|
try:
|
|
37
|
-
keyring.delete_password(KEYRING_SERVICE,
|
|
47
|
+
keyring.delete_password(KEYRING_SERVICE, name)
|
|
38
48
|
except keyring.errors.PasswordDeleteError:
|
|
39
49
|
pass
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def clear_api_key():
|
|
53
|
+
clear_secret(KEYRING_USERNAME)
|
|
40
54
|
if not CONFIG_PATH.exists():
|
|
41
55
|
return
|
|
42
56
|
config = tomllib.loads(CONFIG_PATH.read_text(encoding="utf-8"))
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
"""Browser login: OAuth authorization code flow with PKCE against Podojo's Auth0.
|
|
2
|
+
|
|
3
|
+
The API tells the CLI where to log in (`/auth/config`), the browser hands the
|
|
4
|
+
authorization code to a short-lived server on 127.0.0.1, and the keyring keeps
|
|
5
|
+
the resulting tokens.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
import base64
|
|
9
|
+
import hashlib
|
|
10
|
+
import secrets
|
|
11
|
+
import time
|
|
12
|
+
import urllib.parse
|
|
13
|
+
import webbrowser
|
|
14
|
+
from http.server import BaseHTTPRequestHandler, HTTPServer
|
|
15
|
+
|
|
16
|
+
import httpx
|
|
17
|
+
import keyring
|
|
18
|
+
|
|
19
|
+
from .config import clear_secret, load_secret, save_secret
|
|
20
|
+
|
|
21
|
+
# Auth0 matches callback URLs exactly, port included, so these are registered
|
|
22
|
+
# on the "Podojo CLI" application.
|
|
23
|
+
CALLBACK_PORTS = (8914, 8915, 8916)
|
|
24
|
+
CALLBACK_PATH = "/callback"
|
|
25
|
+
LOGIN_TIMEOUT = 300
|
|
26
|
+
EXPIRY_MARGIN = 60
|
|
27
|
+
AUTH_TIMEOUT = httpx.Timeout(15.0)
|
|
28
|
+
|
|
29
|
+
KEYRING_SESSION = "session"
|
|
30
|
+
KEYRING_ACCESS_TOKEN = "access_token"
|
|
31
|
+
|
|
32
|
+
PAGE = """<!doctype html>
|
|
33
|
+
<html><head><meta charset="utf-8"><title>Podojo CLI</title></head>
|
|
34
|
+
<body style="font-family: system-ui, sans-serif; text-align: center; margin-top: 20vh">
|
|
35
|
+
<h2>{title}</h2><p>{text}</p>
|
|
36
|
+
</body></html>"""
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
class LoginError(SystemExit):
|
|
40
|
+
"""Ends the command with a message, wherever in a command it is raised."""
|
|
41
|
+
|
|
42
|
+
def __init__(self, message: str):
|
|
43
|
+
super().__init__(f"Error: {message}")
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
class LoginRejected(LoginError):
|
|
47
|
+
pass
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def fetch_auth_config(base_url: str) -> dict | None:
|
|
51
|
+
"""Where to log in for this API, or None if it offers no browser login."""
|
|
52
|
+
try:
|
|
53
|
+
r = httpx.get(f"{base_url.rstrip('/')}/api/v1/auth/config", timeout=AUTH_TIMEOUT)
|
|
54
|
+
except httpx.RequestError:
|
|
55
|
+
raise LoginError(f"Could not reach {base_url}.")
|
|
56
|
+
if r.status_code == 404:
|
|
57
|
+
return None
|
|
58
|
+
r.raise_for_status()
|
|
59
|
+
return r.json()
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
class _CallbackHandler(BaseHTTPRequestHandler):
|
|
63
|
+
def do_GET(self):
|
|
64
|
+
url = urllib.parse.urlparse(self.path)
|
|
65
|
+
if url.path != CALLBACK_PATH:
|
|
66
|
+
self.send_error(404)
|
|
67
|
+
return
|
|
68
|
+
params = dict(urllib.parse.parse_qsl(url.query))
|
|
69
|
+
self.server.params = params
|
|
70
|
+
if "code" in params:
|
|
71
|
+
page = PAGE.format(title="You're logged in", text="You can close this tab and return to the terminal.")
|
|
72
|
+
else:
|
|
73
|
+
page = PAGE.format(title="Login failed", text="Return to the terminal for details.")
|
|
74
|
+
body = page.encode()
|
|
75
|
+
self.send_response(200)
|
|
76
|
+
self.send_header("Content-Type", "text/html; charset=utf-8")
|
|
77
|
+
self.send_header("Content-Length", str(len(body)))
|
|
78
|
+
self.end_headers()
|
|
79
|
+
self.wfile.write(body)
|
|
80
|
+
|
|
81
|
+
def log_message(self, *args):
|
|
82
|
+
pass
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def _start_callback_server() -> HTTPServer:
|
|
86
|
+
for port in CALLBACK_PORTS:
|
|
87
|
+
try:
|
|
88
|
+
server = HTTPServer(("127.0.0.1", port), _CallbackHandler)
|
|
89
|
+
except OSError:
|
|
90
|
+
continue
|
|
91
|
+
server.params = None
|
|
92
|
+
server.timeout = 1
|
|
93
|
+
return server
|
|
94
|
+
ports = ", ".join(str(p) for p in CALLBACK_PORTS)
|
|
95
|
+
raise LoginError(f"Ports {ports} are all in use; free one of them to log in.")
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def _wait_for_callback(server: HTTPServer) -> dict:
|
|
99
|
+
deadline = time.monotonic() + LOGIN_TIMEOUT
|
|
100
|
+
while server.params is None:
|
|
101
|
+
if time.monotonic() > deadline:
|
|
102
|
+
raise LoginError("Timed out waiting for the browser login.")
|
|
103
|
+
server.handle_request()
|
|
104
|
+
return server.params
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def _token_request(auth_config: dict, data: dict) -> dict:
|
|
108
|
+
try:
|
|
109
|
+
r = httpx.post(
|
|
110
|
+
f"https://{auth_config['domain']}/oauth/token",
|
|
111
|
+
data={"client_id": auth_config["client_id"], **data},
|
|
112
|
+
timeout=AUTH_TIMEOUT,
|
|
113
|
+
)
|
|
114
|
+
except httpx.RequestError:
|
|
115
|
+
raise LoginError(f"Could not reach {auth_config['domain']}.")
|
|
116
|
+
if r.is_client_error:
|
|
117
|
+
raise LoginRejected(_error_description(r) or "Login was rejected.")
|
|
118
|
+
r.raise_for_status()
|
|
119
|
+
return r.json()
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
def _error_description(response: httpx.Response) -> str | None:
|
|
123
|
+
try:
|
|
124
|
+
return response.json().get("error_description")
|
|
125
|
+
except ValueError:
|
|
126
|
+
return None
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
def _challenge(verifier: str) -> str:
|
|
130
|
+
return base64.urlsafe_b64encode(hashlib.sha256(verifier.encode()).digest()).rstrip(b"=").decode()
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
def login(auth_config: dict, show_url=lambda url: None) -> dict:
|
|
134
|
+
"""Run the browser login and return Auth0's token response."""
|
|
135
|
+
verifier = secrets.token_urlsafe(64)
|
|
136
|
+
state = secrets.token_urlsafe(32)
|
|
137
|
+
|
|
138
|
+
server = _start_callback_server()
|
|
139
|
+
try:
|
|
140
|
+
redirect_uri = f"http://127.0.0.1:{server.server_port}{CALLBACK_PATH}"
|
|
141
|
+
url = f"https://{auth_config['domain']}/authorize?" + urllib.parse.urlencode({
|
|
142
|
+
"response_type": "code",
|
|
143
|
+
"client_id": auth_config["client_id"],
|
|
144
|
+
"redirect_uri": redirect_uri,
|
|
145
|
+
"audience": auth_config["audience"],
|
|
146
|
+
"scope": "openid offline_access",
|
|
147
|
+
"code_challenge": _challenge(verifier),
|
|
148
|
+
"code_challenge_method": "S256",
|
|
149
|
+
"state": state,
|
|
150
|
+
})
|
|
151
|
+
show_url(url)
|
|
152
|
+
webbrowser.open(url)
|
|
153
|
+
params = _wait_for_callback(server)
|
|
154
|
+
finally:
|
|
155
|
+
server.server_close()
|
|
156
|
+
|
|
157
|
+
if params.get("state") != state:
|
|
158
|
+
raise LoginError("The login response did not match this login attempt.")
|
|
159
|
+
if "code" not in params:
|
|
160
|
+
raise LoginError(params.get("error_description") or params.get("error") or "Login failed.")
|
|
161
|
+
|
|
162
|
+
return _token_request(auth_config, {
|
|
163
|
+
"grant_type": "authorization_code",
|
|
164
|
+
"code": params["code"],
|
|
165
|
+
"code_verifier": verifier,
|
|
166
|
+
"redirect_uri": redirect_uri,
|
|
167
|
+
})
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
def save_session(auth_config: dict, tokens: dict, refresh_token: str | None = None):
|
|
171
|
+
"""Keep the tokens of a login, or of a refresh that used `refresh_token`."""
|
|
172
|
+
save_secret(KEYRING_SESSION, {
|
|
173
|
+
"domain": auth_config["domain"],
|
|
174
|
+
"client_id": auth_config["client_id"],
|
|
175
|
+
"refresh_token": tokens.get("refresh_token") or refresh_token,
|
|
176
|
+
})
|
|
177
|
+
try:
|
|
178
|
+
save_secret(KEYRING_ACCESS_TOKEN, {
|
|
179
|
+
"token": tokens["access_token"],
|
|
180
|
+
"expires_at": time.time() + tokens["expires_in"],
|
|
181
|
+
})
|
|
182
|
+
except keyring.errors.PasswordSetError:
|
|
183
|
+
# Windows Credential Manager caps an entry at about 1,280 characters;
|
|
184
|
+
# without the cached token every command refreshes it instead.
|
|
185
|
+
clear_secret(KEYRING_ACCESS_TOKEN)
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
def access_token() -> str | None:
|
|
189
|
+
"""A valid access token for the logged-in user, or None if nobody is."""
|
|
190
|
+
cached = load_secret(KEYRING_ACCESS_TOKEN)
|
|
191
|
+
if cached and cached["expires_at"] - EXPIRY_MARGIN > time.time():
|
|
192
|
+
return cached["token"]
|
|
193
|
+
session = load_secret(KEYRING_SESSION)
|
|
194
|
+
if not session or not session.get("refresh_token"):
|
|
195
|
+
return None
|
|
196
|
+
try:
|
|
197
|
+
tokens = _token_request(session, {
|
|
198
|
+
"grant_type": "refresh_token",
|
|
199
|
+
"refresh_token": session["refresh_token"],
|
|
200
|
+
})
|
|
201
|
+
except LoginRejected:
|
|
202
|
+
raise LoginError("Your login has expired. Run `podojo auth login` again.")
|
|
203
|
+
save_session(session, tokens, refresh_token=session["refresh_token"])
|
|
204
|
+
return tokens["access_token"]
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
def clear_session():
|
|
208
|
+
session = load_secret(KEYRING_SESSION)
|
|
209
|
+
clear_secret(KEYRING_SESSION)
|
|
210
|
+
clear_secret(KEYRING_ACCESS_TOKEN)
|
|
211
|
+
if not session or not session.get("refresh_token"):
|
|
212
|
+
return
|
|
213
|
+
try:
|
|
214
|
+
httpx.post(
|
|
215
|
+
f"https://{session['domain']}/oauth/revoke",
|
|
216
|
+
json={"client_id": session["client_id"], "token": session["refresh_token"]},
|
|
217
|
+
timeout=AUTH_TIMEOUT,
|
|
218
|
+
)
|
|
219
|
+
except httpx.RequestError:
|
|
220
|
+
pass
|
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import threading
|
|
3
|
+
import time
|
|
4
|
+
import urllib.parse
|
|
5
|
+
import urllib.request
|
|
6
|
+
|
|
7
|
+
import pytest
|
|
8
|
+
|
|
9
|
+
from podojo_cli import oauth
|
|
10
|
+
from podojo_cli.config import KEYRING_SERVICE, KEYRING_USERNAME
|
|
11
|
+
from podojo_cli.main import app
|
|
12
|
+
|
|
13
|
+
AUTH_CONFIG = {"domain": "auth.test", "client_id": "cli-client", "audience": "https://api.test"}
|
|
14
|
+
TOKEN_URL = "https://auth.test/oauth/token"
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def stored(keyring_backend, name):
|
|
18
|
+
value = keyring_backend.get_password(KEYRING_SERVICE, name)
|
|
19
|
+
return json.loads(value) if value else None
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def form(request):
|
|
23
|
+
return dict(urllib.parse.parse_qsl(request.content.decode()))
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
@pytest.fixture
|
|
27
|
+
def browser(monkeypatch):
|
|
28
|
+
"""Stands in for the browser: follows the authorize URL straight to the
|
|
29
|
+
CLI's callback, the way Auth0 redirects after a login."""
|
|
30
|
+
monkeypatch.setattr(oauth, "CALLBACK_PORTS", (0,))
|
|
31
|
+
seen = {"answer": {"code": "auth-code"}}
|
|
32
|
+
|
|
33
|
+
def open_url(url):
|
|
34
|
+
query = dict(urllib.parse.parse_qsl(urllib.parse.urlparse(url).query))
|
|
35
|
+
seen["url"] = url
|
|
36
|
+
seen["query"] = query
|
|
37
|
+
answer = {"state": query["state"], **seen["answer"]}
|
|
38
|
+
callback = query["redirect_uri"] + "?" + urllib.parse.urlencode(answer)
|
|
39
|
+
threading.Thread(target=lambda: urllib.request.urlopen(callback).read()).start()
|
|
40
|
+
|
|
41
|
+
monkeypatch.setattr(oauth.webbrowser, "open", open_url)
|
|
42
|
+
return seen
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def test_login_valid_key(runner, httpx_mock, mock_keyring):
|
|
46
|
+
httpx_mock.add_response(
|
|
47
|
+
url="http://test.local/api/v1/projects?limit=1",
|
|
48
|
+
status_code=200,
|
|
49
|
+
json=[],
|
|
50
|
+
)
|
|
51
|
+
|
|
52
|
+
result = runner.invoke(app, ["auth", "login", "--key", "valid-key-abc"])
|
|
53
|
+
|
|
54
|
+
assert result.exit_code == 0
|
|
55
|
+
assert "Logged in successfully" in result.output
|
|
56
|
+
assert mock_keyring.get_password(KEYRING_SERVICE, KEYRING_USERNAME) == "valid-key-abc"
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def test_login_invalid_key(runner, httpx_mock):
|
|
60
|
+
httpx_mock.add_response(
|
|
61
|
+
url="http://test.local/api/v1/projects?limit=1",
|
|
62
|
+
status_code=401,
|
|
63
|
+
)
|
|
64
|
+
|
|
65
|
+
result = runner.invoke(app, ["auth", "login", "--key", "bad-key"])
|
|
66
|
+
|
|
67
|
+
assert result.exit_code != 0
|
|
68
|
+
assert "Invalid API key" in result.output
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def test_login_with_key_replaces_browser_login(runner, httpx_mock, mock_keyring):
|
|
72
|
+
oauth.save_session(AUTH_CONFIG, {"access_token": "at", "expires_in": 3600})
|
|
73
|
+
httpx_mock.add_response(url="http://test.local/api/v1/projects?limit=1", json=[])
|
|
74
|
+
|
|
75
|
+
result = runner.invoke(app, ["auth", "login", "--key", "valid-key-abc"])
|
|
76
|
+
|
|
77
|
+
assert result.exit_code == 0
|
|
78
|
+
assert stored(mock_keyring, oauth.KEYRING_SESSION) is None
|
|
79
|
+
assert stored(mock_keyring, oauth.KEYRING_ACCESS_TOKEN) is None
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def test_browser_login(runner, httpx_mock, mock_keyring, browser):
|
|
83
|
+
mock_keyring.set_password(KEYRING_SERVICE, KEYRING_USERNAME, "old-key")
|
|
84
|
+
httpx_mock.add_response(url="http://test.local/api/v1/auth/config", json=AUTH_CONFIG)
|
|
85
|
+
httpx_mock.add_response(
|
|
86
|
+
url=TOKEN_URL,
|
|
87
|
+
method="POST",
|
|
88
|
+
json={"access_token": "access-1", "refresh_token": "refresh-1", "expires_in": 86400},
|
|
89
|
+
)
|
|
90
|
+
httpx_mock.add_response(
|
|
91
|
+
url="http://test.local/api/v1/projects?limit=1",
|
|
92
|
+
match_headers={"Authorization": "Bearer access-1"},
|
|
93
|
+
json=[],
|
|
94
|
+
)
|
|
95
|
+
|
|
96
|
+
result = runner.invoke(app, ["auth", "login"])
|
|
97
|
+
|
|
98
|
+
assert result.exit_code == 0, result.output
|
|
99
|
+
assert "Logged in successfully" in result.output
|
|
100
|
+
|
|
101
|
+
authorize = browser["query"]
|
|
102
|
+
assert browser["url"].startswith("https://auth.test/authorize?")
|
|
103
|
+
assert authorize["client_id"] == "cli-client"
|
|
104
|
+
assert authorize["audience"] == "https://api.test"
|
|
105
|
+
assert authorize["code_challenge_method"] == "S256"
|
|
106
|
+
assert "offline_access" in authorize["scope"]
|
|
107
|
+
|
|
108
|
+
exchange = form(httpx_mock.get_request(url=TOKEN_URL))
|
|
109
|
+
assert exchange["grant_type"] == "authorization_code"
|
|
110
|
+
assert exchange["code"] == "auth-code"
|
|
111
|
+
assert exchange["redirect_uri"] == authorize["redirect_uri"]
|
|
112
|
+
assert oauth._challenge(exchange["code_verifier"]) == authorize["code_challenge"]
|
|
113
|
+
|
|
114
|
+
assert stored(mock_keyring, oauth.KEYRING_SESSION) == {
|
|
115
|
+
"domain": "auth.test",
|
|
116
|
+
"client_id": "cli-client",
|
|
117
|
+
"refresh_token": "refresh-1",
|
|
118
|
+
}
|
|
119
|
+
assert stored(mock_keyring, oauth.KEYRING_ACCESS_TOKEN)["token"] == "access-1"
|
|
120
|
+
assert mock_keyring.get_password(KEYRING_SERVICE, KEYRING_USERNAME) is None
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
def test_browser_login_denied(runner, httpx_mock, mock_keyring, browser):
|
|
124
|
+
browser["answer"] = {"error": "access_denied", "error_description": "User cancelled the login"}
|
|
125
|
+
httpx_mock.add_response(url="http://test.local/api/v1/auth/config", json=AUTH_CONFIG)
|
|
126
|
+
|
|
127
|
+
result = runner.invoke(app, ["auth", "login"])
|
|
128
|
+
|
|
129
|
+
assert result.exit_code != 0
|
|
130
|
+
assert "User cancelled the login" in result.output
|
|
131
|
+
assert stored(mock_keyring, oauth.KEYRING_SESSION) is None
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
def test_browser_login_rejects_foreign_state(runner, httpx_mock, mock_keyring, browser):
|
|
135
|
+
browser["answer"] = {"code": "auth-code", "state": "someone-elses"}
|
|
136
|
+
httpx_mock.add_response(url="http://test.local/api/v1/auth/config", json=AUTH_CONFIG)
|
|
137
|
+
|
|
138
|
+
result = runner.invoke(app, ["auth", "login"])
|
|
139
|
+
|
|
140
|
+
assert result.exit_code != 0
|
|
141
|
+
assert "did not match" in result.output
|
|
142
|
+
assert stored(mock_keyring, oauth.KEYRING_SESSION) is None
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
def test_browser_login_without_workspace(runner, httpx_mock, mock_keyring, browser):
|
|
146
|
+
httpx_mock.add_response(url="http://test.local/api/v1/auth/config", json=AUTH_CONFIG)
|
|
147
|
+
httpx_mock.add_response(
|
|
148
|
+
url=TOKEN_URL,
|
|
149
|
+
method="POST",
|
|
150
|
+
json={"access_token": "access-1", "refresh_token": "refresh-1", "expires_in": 86400},
|
|
151
|
+
)
|
|
152
|
+
httpx_mock.add_response(url="http://test.local/api/v1/projects?limit=1", status_code=403)
|
|
153
|
+
|
|
154
|
+
result = runner.invoke(app, ["auth", "login"])
|
|
155
|
+
|
|
156
|
+
assert result.exit_code != 0
|
|
157
|
+
assert "no Podojo workspace" in result.output
|
|
158
|
+
assert "confirm your email" in " ".join(result.output.split())
|
|
159
|
+
assert stored(mock_keyring, oauth.KEYRING_SESSION) is None
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
def test_browser_login_not_offered(runner, httpx_mock, mock_keyring):
|
|
163
|
+
httpx_mock.add_response(url="http://test.local/api/v1/auth/config", status_code=404)
|
|
164
|
+
|
|
165
|
+
result = runner.invoke(app, ["auth", "login"])
|
|
166
|
+
|
|
167
|
+
assert result.exit_code != 0
|
|
168
|
+
assert "--key" in result.output
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
def test_logout(runner, mock_keyring):
|
|
172
|
+
mock_keyring.set_password(KEYRING_SERVICE, KEYRING_USERNAME, "some-key")
|
|
173
|
+
|
|
174
|
+
result = runner.invoke(app, ["auth", "logout"])
|
|
175
|
+
|
|
176
|
+
assert result.exit_code == 0
|
|
177
|
+
assert mock_keyring.get_password(KEYRING_SERVICE, KEYRING_USERNAME) is None
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
def test_logout_revokes_browser_login(runner, httpx_mock, mock_keyring):
|
|
181
|
+
oauth.save_session(AUTH_CONFIG, {"access_token": "at", "refresh_token": "rt", "expires_in": 3600})
|
|
182
|
+
httpx_mock.add_response(url="https://auth.test/oauth/revoke", method="POST")
|
|
183
|
+
|
|
184
|
+
result = runner.invoke(app, ["auth", "logout"])
|
|
185
|
+
|
|
186
|
+
assert result.exit_code == 0
|
|
187
|
+
revoked = json.loads(httpx_mock.get_request().content)
|
|
188
|
+
assert revoked == {"client_id": "cli-client", "token": "rt"}
|
|
189
|
+
assert stored(mock_keyring, oauth.KEYRING_SESSION) is None
|
|
190
|
+
assert stored(mock_keyring, oauth.KEYRING_ACCESS_TOKEN) is None
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
@pytest.fixture
|
|
194
|
+
def logged_in_by_browser(monkeypatch, mock_keyring):
|
|
195
|
+
monkeypatch.delenv("PODOJO_API_KEY")
|
|
196
|
+
oauth.save_session(AUTH_CONFIG, {"access_token": "access-1", "refresh_token": "refresh-1", "expires_in": 86400})
|
|
197
|
+
return mock_keyring
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
def test_commands_use_the_browser_login(runner, httpx_mock, logged_in_by_browser):
|
|
201
|
+
httpx_mock.add_response(
|
|
202
|
+
url="http://test.local/api/v1/projects?skip=0&limit=50",
|
|
203
|
+
match_headers={"Authorization": "Bearer access-1"},
|
|
204
|
+
json={"projects": []},
|
|
205
|
+
)
|
|
206
|
+
|
|
207
|
+
result = runner.invoke(app, ["projects", "list"])
|
|
208
|
+
|
|
209
|
+
assert result.exit_code == 0, result.output
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
def test_expired_access_token_is_refreshed(runner, httpx_mock, logged_in_by_browser):
|
|
213
|
+
oauth.save_secret(oauth.KEYRING_ACCESS_TOKEN, {"token": "access-1", "expires_at": time.time() - 1})
|
|
214
|
+
httpx_mock.add_response(url=TOKEN_URL, method="POST", json={"access_token": "access-2", "expires_in": 86400})
|
|
215
|
+
httpx_mock.add_response(
|
|
216
|
+
url="http://test.local/api/v1/projects?skip=0&limit=50",
|
|
217
|
+
match_headers={"Authorization": "Bearer access-2"},
|
|
218
|
+
json={"projects": []},
|
|
219
|
+
)
|
|
220
|
+
|
|
221
|
+
result = runner.invoke(app, ["projects", "list"])
|
|
222
|
+
|
|
223
|
+
assert result.exit_code == 0, result.output
|
|
224
|
+
refresh = form(httpx_mock.get_request(url=TOKEN_URL))
|
|
225
|
+
assert refresh == {"client_id": "cli-client", "grant_type": "refresh_token", "refresh_token": "refresh-1"}
|
|
226
|
+
assert stored(logged_in_by_browser, oauth.KEYRING_ACCESS_TOKEN)["token"] == "access-2"
|
|
227
|
+
assert stored(logged_in_by_browser, oauth.KEYRING_SESSION)["refresh_token"] == "refresh-1"
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
def test_rotated_refresh_token_is_kept(runner, httpx_mock, logged_in_by_browser):
|
|
231
|
+
oauth.clear_secret(oauth.KEYRING_ACCESS_TOKEN)
|
|
232
|
+
httpx_mock.add_response(
|
|
233
|
+
url=TOKEN_URL,
|
|
234
|
+
method="POST",
|
|
235
|
+
json={"access_token": "access-2", "refresh_token": "refresh-2", "expires_in": 86400},
|
|
236
|
+
)
|
|
237
|
+
httpx_mock.add_response(url="http://test.local/api/v1/projects?skip=0&limit=50", json={"projects": []})
|
|
238
|
+
|
|
239
|
+
result = runner.invoke(app, ["projects", "list"])
|
|
240
|
+
|
|
241
|
+
assert result.exit_code == 0, result.output
|
|
242
|
+
assert stored(logged_in_by_browser, oauth.KEYRING_SESSION)["refresh_token"] == "refresh-2"
|
|
243
|
+
|
|
244
|
+
|
|
245
|
+
def test_expired_login_asks_to_log_in_again(runner, httpx_mock, logged_in_by_browser):
|
|
246
|
+
oauth.clear_secret(oauth.KEYRING_ACCESS_TOKEN)
|
|
247
|
+
httpx_mock.add_response(
|
|
248
|
+
url=TOKEN_URL,
|
|
249
|
+
method="POST",
|
|
250
|
+
status_code=403,
|
|
251
|
+
json={"error": "invalid_grant", "error_description": "Unknown or invalid refresh token."},
|
|
252
|
+
)
|
|
253
|
+
|
|
254
|
+
result = runner.invoke(app, ["projects", "list"])
|
|
255
|
+
|
|
256
|
+
assert result.exit_code != 0
|
|
257
|
+
assert "podojo auth login" in result.output
|
|
258
|
+
|
|
259
|
+
|
|
260
|
+
def test_not_logged_in(runner, monkeypatch, mock_keyring):
|
|
261
|
+
monkeypatch.delenv("PODOJO_API_KEY")
|
|
262
|
+
|
|
263
|
+
result = runner.invoke(app, ["projects", "list"])
|
|
264
|
+
|
|
265
|
+
assert result.exit_code != 0
|
|
266
|
+
assert "Not logged in" in result.output
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
def test_api_key_wins_over_browser_login(runner, httpx_mock, mock_keyring):
|
|
270
|
+
oauth.save_session(AUTH_CONFIG, {"access_token": "access-1", "refresh_token": "refresh-1", "expires_in": 86400})
|
|
271
|
+
httpx_mock.add_response(
|
|
272
|
+
url="http://test.local/api/v1/projects?skip=0&limit=50",
|
|
273
|
+
match_headers={"Authorization": "Bearer test-key"},
|
|
274
|
+
json={"projects": []},
|
|
275
|
+
)
|
|
276
|
+
|
|
277
|
+
result = runner.invoke(app, ["projects", "list"])
|
|
278
|
+
|
|
279
|
+
assert result.exit_code == 0, result.output
|
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
import httpx
|
|
2
|
-
import typer
|
|
3
|
-
from rich.console import Console
|
|
4
|
-
|
|
5
|
-
from ..config import DEFAULT_BASE_URL, clear_api_key, load_config, save_config
|
|
6
|
-
|
|
7
|
-
app = typer.Typer(help="Authenticate with Podojo")
|
|
8
|
-
console = Console()
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
def _verify_key(api_key: str) -> bool:
|
|
12
|
-
config = load_config()
|
|
13
|
-
base_url = config["base_url"].rstrip("/") + "/api/v1"
|
|
14
|
-
try:
|
|
15
|
-
r = httpx.get(
|
|
16
|
-
f"{base_url}/projects",
|
|
17
|
-
params={"limit": 1},
|
|
18
|
-
headers={"Authorization": f"Bearer {api_key}"},
|
|
19
|
-
timeout=10,
|
|
20
|
-
)
|
|
21
|
-
return r.status_code == 200
|
|
22
|
-
except httpx.RequestError:
|
|
23
|
-
return False
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
@app.command("login")
|
|
27
|
-
def login(
|
|
28
|
-
api_key: str = typer.Option(
|
|
29
|
-
None, "--key", "-k", help="Your Podojo API key (prompted if omitted)"
|
|
30
|
-
),
|
|
31
|
-
):
|
|
32
|
-
"""Save your API key and verify it works."""
|
|
33
|
-
if not api_key:
|
|
34
|
-
api_key = typer.prompt("API Key", hide_input=True)
|
|
35
|
-
console.print("Verifying key…", end=" ")
|
|
36
|
-
if not _verify_key(api_key):
|
|
37
|
-
console.print("[red]failed[/red]")
|
|
38
|
-
console.print("[red]Error:[/red] Invalid API key or could not reach the server.")
|
|
39
|
-
raise typer.Exit(1)
|
|
40
|
-
save_config(api_key)
|
|
41
|
-
console.print("[green]done[/green]")
|
|
42
|
-
console.print("[green]Logged in successfully.[/green]")
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
@app.command("logout")
|
|
46
|
-
def logout():
|
|
47
|
-
"""Remove the saved API key."""
|
|
48
|
-
clear_api_key()
|
|
49
|
-
console.print("Logged out.")
|
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
from unittest.mock import patch
|
|
2
|
-
|
|
3
|
-
from podojo_cli.config import KEYRING_SERVICE, KEYRING_USERNAME
|
|
4
|
-
from podojo_cli.main import app
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
def test_login_valid_key(runner, httpx_mock, mock_keyring):
|
|
8
|
-
httpx_mock.add_response(
|
|
9
|
-
url="http://test.local/api/v1/projects?limit=1",
|
|
10
|
-
status_code=200,
|
|
11
|
-
json=[],
|
|
12
|
-
)
|
|
13
|
-
|
|
14
|
-
result = runner.invoke(app, ["auth", "login", "--key", "valid-key-abc"])
|
|
15
|
-
|
|
16
|
-
assert result.exit_code == 0
|
|
17
|
-
assert "Logged in successfully" in result.output
|
|
18
|
-
assert mock_keyring.get_password(KEYRING_SERVICE, KEYRING_USERNAME) == "valid-key-abc"
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
def test_login_prompted(runner, httpx_mock, mock_keyring):
|
|
22
|
-
httpx_mock.add_response(
|
|
23
|
-
url="http://test.local/api/v1/projects?limit=1",
|
|
24
|
-
status_code=200,
|
|
25
|
-
json=[],
|
|
26
|
-
)
|
|
27
|
-
|
|
28
|
-
result = runner.invoke(app, ["auth", "login"], input="prompted-key\n")
|
|
29
|
-
|
|
30
|
-
assert result.exit_code == 0
|
|
31
|
-
assert "Logged in successfully" in result.output
|
|
32
|
-
assert mock_keyring.get_password(KEYRING_SERVICE, KEYRING_USERNAME) == "prompted-key"
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
def test_login_invalid_key(runner, httpx_mock):
|
|
36
|
-
httpx_mock.add_response(
|
|
37
|
-
url="http://test.local/api/v1/projects?limit=1",
|
|
38
|
-
status_code=401,
|
|
39
|
-
)
|
|
40
|
-
|
|
41
|
-
result = runner.invoke(app, ["auth", "login", "--key", "bad-key"])
|
|
42
|
-
|
|
43
|
-
assert result.exit_code != 0
|
|
44
|
-
assert "Invalid API key" in result.output
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
def test_logout(runner, mock_keyring):
|
|
48
|
-
mock_keyring.set_password(KEYRING_SERVICE, KEYRING_USERNAME, "some-key")
|
|
49
|
-
|
|
50
|
-
result = runner.invoke(app, ["auth", "logout"])
|
|
51
|
-
|
|
52
|
-
assert result.exit_code == 0
|
|
53
|
-
assert mock_keyring.get_password(KEYRING_SERVICE, KEYRING_USERNAME) is None
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|