tool4seller-cli 1.0.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.
- tool4seller_cli-1.0.0/PKG-INFO +56 -0
- tool4seller_cli-1.0.0/README.md +42 -0
- tool4seller_cli-1.0.0/pyproject.toml +35 -0
- tool4seller_cli-1.0.0/setup.cfg +4 -0
- tool4seller_cli-1.0.0/src/tool4seller_cli/__init__.py +1 -0
- tool4seller_cli-1.0.0/src/tool4seller_cli/auth/__init__.py +1 -0
- tool4seller_cli-1.0.0/src/tool4seller_cli/auth/login.py +194 -0
- tool4seller_cli-1.0.0/src/tool4seller_cli/auth/oauth.py +126 -0
- tool4seller_cli-1.0.0/src/tool4seller_cli/auth/pkce.py +10 -0
- tool4seller_cli-1.0.0/src/tool4seller_cli/auth/token_store.py +65 -0
- tool4seller_cli-1.0.0/src/tool4seller_cli/cli.py +383 -0
- tool4seller_cli-1.0.0/src/tool4seller_cli/config.py +49 -0
- tool4seller_cli-1.0.0/src/tool4seller_cli/data/openapi-v2.bundled.json +1 -0
- tool4seller_cli-1.0.0/src/tool4seller_cli/dynamic.py +71 -0
- tool4seller_cli-1.0.0/src/tool4seller_cli/http_client.py +123 -0
- tool4seller_cli-1.0.0/src/tool4seller_cli/output.py +6 -0
- tool4seller_cli-1.0.0/src/tool4seller_cli/schema/__init__.py +1 -0
- tool4seller_cli-1.0.0/src/tool4seller_cli/schema/normalize.py +29 -0
- tool4seller_cli-1.0.0/src/tool4seller_cli/schema/registry.py +101 -0
- tool4seller_cli-1.0.0/src/tool4seller_cli/skills/__init__.py +1 -0
- tool4seller_cli-1.0.0/src/tool4seller_cli/skills/tool4seller-advertising/SKILL.md +61 -0
- tool4seller_cli-1.0.0/src/tool4seller_cli/skills/tool4seller-inventory/SKILL.md +49 -0
- tool4seller_cli-1.0.0/src/tool4seller_cli/skills/tool4seller-profit/SKILL.md +51 -0
- tool4seller_cli-1.0.0/src/tool4seller_cli/skills/tool4seller-sales/SKILL.md +46 -0
- tool4seller_cli-1.0.0/src/tool4seller_cli/skills/tool4seller-shared/SKILL.md +127 -0
- tool4seller_cli-1.0.0/src/tool4seller_cli/skills/tool4seller-stores/SKILL.md +38 -0
- tool4seller_cli-1.0.0/src/tool4seller_cli/skills_installer.py +107 -0
- tool4seller_cli-1.0.0/src/tool4seller_cli.egg-info/PKG-INFO +56 -0
- tool4seller_cli-1.0.0/src/tool4seller_cli.egg-info/SOURCES.txt +47 -0
- tool4seller_cli-1.0.0/src/tool4seller_cli.egg-info/dependency_links.txt +1 -0
- tool4seller_cli-1.0.0/src/tool4seller_cli.egg-info/entry_points.txt +2 -0
- tool4seller_cli-1.0.0/src/tool4seller_cli.egg-info/requires.txt +8 -0
- tool4seller_cli-1.0.0/src/tool4seller_cli.egg-info/top_level.txt +1 -0
- tool4seller_cli-1.0.0/tests/test_auth_login.py +212 -0
- tool4seller_cli-1.0.0/tests/test_auth_oauth.py +166 -0
- tool4seller_cli-1.0.0/tests/test_cli_api.py +178 -0
- tool4seller_cli-1.0.0/tests/test_cli_auth.py +51 -0
- tool4seller_cli-1.0.0/tests/test_cli_core.py +35 -0
- tool4seller_cli-1.0.0/tests/test_cli_dynamic.py +133 -0
- tool4seller_cli-1.0.0/tests/test_cli_schema.py +42 -0
- tool4seller_cli-1.0.0/tests/test_config.py +50 -0
- tool4seller_cli-1.0.0/tests/test_dynamic.py +103 -0
- tool4seller_cli-1.0.0/tests/test_http_client.py +138 -0
- tool4seller_cli-1.0.0/tests/test_output.py +10 -0
- tool4seller_cli-1.0.0/tests/test_package_metadata.py +15 -0
- tool4seller_cli-1.0.0/tests/test_schema_normalize.py +28 -0
- tool4seller_cli-1.0.0/tests/test_schema_registry.py +136 -0
- tool4seller_cli-1.0.0/tests/test_skills_installer.py +344 -0
- tool4seller_cli-1.0.0/tests/test_token_store.py +44 -0
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: tool4seller-cli
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Tool4Seller CLI for OAuth login and V2 API access
|
|
5
|
+
Requires-Python: >=3.10
|
|
6
|
+
Description-Content-Type: text/markdown
|
|
7
|
+
Requires-Dist: typer>=0.12
|
|
8
|
+
Requires-Dist: rich>=13.0
|
|
9
|
+
Requires-Dist: requests>=2.28
|
|
10
|
+
Provides-Extra: dev
|
|
11
|
+
Requires-Dist: pytest>=8.0; extra == "dev"
|
|
12
|
+
Requires-Dist: build>=1.2; extra == "dev"
|
|
13
|
+
Requires-Dist: twine>=5.0; extra == "dev"
|
|
14
|
+
|
|
15
|
+
# Tool4Seller CLI
|
|
16
|
+
|
|
17
|
+
Python CLI for Tool4Seller OAuth login and V2 API access.
|
|
18
|
+
|
|
19
|
+
## Install
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
pip install tool4seller-cli
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Usage
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
tool4seller --version
|
|
29
|
+
tool4seller auth login
|
|
30
|
+
tool4seller auth status
|
|
31
|
+
tool4seller schema list
|
|
32
|
+
tool4seller schema show sales.list-sales
|
|
33
|
+
tool4seller sales list-sales --start-date 2026-06-01 --end-date 2026-06-07
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## Agent Skills
|
|
37
|
+
|
|
38
|
+
Install the packaged Tool4Seller skills for existing Codex, Claude, and WorkBuddy directories:
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
tool4seller skills install
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
This replaces older packaged Tool4Seller skills under existing `~/.codex`, `~/.claude`, and `~/.workbuddy` directories. Missing agent directories are not created by the default install.
|
|
45
|
+
The install command prints a JSON result with installed destinations and skipped agent directories.
|
|
46
|
+
To install into a custom directory, use:
|
|
47
|
+
|
|
48
|
+
```bash
|
|
49
|
+
tool4seller skills install --destination ~/.codex/skills
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## Upgrade
|
|
53
|
+
|
|
54
|
+
```bash
|
|
55
|
+
pip install -U tool4seller-cli
|
|
56
|
+
```
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# Tool4Seller CLI
|
|
2
|
+
|
|
3
|
+
Python CLI for Tool4Seller OAuth login and V2 API access.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pip install tool4seller-cli
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
tool4seller --version
|
|
15
|
+
tool4seller auth login
|
|
16
|
+
tool4seller auth status
|
|
17
|
+
tool4seller schema list
|
|
18
|
+
tool4seller schema show sales.list-sales
|
|
19
|
+
tool4seller sales list-sales --start-date 2026-06-01 --end-date 2026-06-07
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Agent Skills
|
|
23
|
+
|
|
24
|
+
Install the packaged Tool4Seller skills for existing Codex, Claude, and WorkBuddy directories:
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
tool4seller skills install
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
This replaces older packaged Tool4Seller skills under existing `~/.codex`, `~/.claude`, and `~/.workbuddy` directories. Missing agent directories are not created by the default install.
|
|
31
|
+
The install command prints a JSON result with installed destinations and skipped agent directories.
|
|
32
|
+
To install into a custom directory, use:
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
tool4seller skills install --destination ~/.codex/skills
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Upgrade
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
pip install -U tool4seller-cli
|
|
42
|
+
```
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=64", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "tool4seller-cli"
|
|
7
|
+
version = "1.0.0"
|
|
8
|
+
description = "Tool4Seller CLI for OAuth login and V2 API access"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.10"
|
|
11
|
+
dependencies = [
|
|
12
|
+
"typer>=0.12",
|
|
13
|
+
"rich>=13.0",
|
|
14
|
+
"requests>=2.28",
|
|
15
|
+
]
|
|
16
|
+
|
|
17
|
+
[project.optional-dependencies]
|
|
18
|
+
dev = [
|
|
19
|
+
"pytest>=8.0",
|
|
20
|
+
"build>=1.2",
|
|
21
|
+
"twine>=5.0",
|
|
22
|
+
]
|
|
23
|
+
|
|
24
|
+
[project.scripts]
|
|
25
|
+
tool4seller = "tool4seller_cli.cli:app"
|
|
26
|
+
|
|
27
|
+
[tool.setuptools.packages.find]
|
|
28
|
+
where = ["src"]
|
|
29
|
+
include = ["tool4seller_cli*"]
|
|
30
|
+
|
|
31
|
+
[tool.setuptools.package-data]
|
|
32
|
+
tool4seller_cli = [
|
|
33
|
+
"data/*.json",
|
|
34
|
+
"skills/*/SKILL.md",
|
|
35
|
+
]
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "1.0.0"
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""Authentication helpers for the Tool4Seller CLI."""
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
import secrets
|
|
2
|
+
import time
|
|
3
|
+
import webbrowser
|
|
4
|
+
from collections.abc import Callable
|
|
5
|
+
from http.server import BaseHTTPRequestHandler, HTTPServer
|
|
6
|
+
from urllib.parse import parse_qs, urlparse
|
|
7
|
+
|
|
8
|
+
from tool4seller_cli.auth.oauth import CLIENT_ID, build_authorize_url, exchange_authorization_code
|
|
9
|
+
from tool4seller_cli.auth.pkce import create_pkce_pair
|
|
10
|
+
from tool4seller_cli.auth.token_store import TokenSet
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
REDIRECT_HOST = "127.0.0.1"
|
|
14
|
+
REDIRECT_PORT = 18080
|
|
15
|
+
REDIRECT_PATH = "/callback"
|
|
16
|
+
|
|
17
|
+
LOGIN_COMPLETE_HTML = """<!doctype html>
|
|
18
|
+
<html lang="en">
|
|
19
|
+
<head>
|
|
20
|
+
<meta charset="utf-8">
|
|
21
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
22
|
+
<title>Tool4Seller CLI Login Complete</title>
|
|
23
|
+
<style>
|
|
24
|
+
:root {
|
|
25
|
+
color-scheme: light;
|
|
26
|
+
--ink: #172033;
|
|
27
|
+
--muted: #647084;
|
|
28
|
+
--line: #dbe3ef;
|
|
29
|
+
--panel: #ffffff;
|
|
30
|
+
--bg: #f4f7fb;
|
|
31
|
+
--accent: #1677ff;
|
|
32
|
+
--accent-dark: #0f4fb8;
|
|
33
|
+
--success: #18a058;
|
|
34
|
+
}
|
|
35
|
+
* {
|
|
36
|
+
box-sizing: border-box;
|
|
37
|
+
}
|
|
38
|
+
body {
|
|
39
|
+
margin: 0;
|
|
40
|
+
min-height: 100vh;
|
|
41
|
+
display: grid;
|
|
42
|
+
place-items: center;
|
|
43
|
+
background:
|
|
44
|
+
linear-gradient(135deg, rgba(22, 119, 255, 0.08), rgba(24, 160, 88, 0.08)),
|
|
45
|
+
var(--bg);
|
|
46
|
+
color: var(--ink);
|
|
47
|
+
font-family: ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
|
|
48
|
+
}
|
|
49
|
+
main {
|
|
50
|
+
width: min(92vw, 560px);
|
|
51
|
+
padding: 42px;
|
|
52
|
+
border: 1px solid var(--line);
|
|
53
|
+
border-radius: 8px;
|
|
54
|
+
background: var(--panel);
|
|
55
|
+
box-shadow: 0 24px 70px rgba(23, 32, 51, 0.12);
|
|
56
|
+
}
|
|
57
|
+
.brand {
|
|
58
|
+
margin: 0 0 28px;
|
|
59
|
+
color: var(--accent-dark);
|
|
60
|
+
font-size: 14px;
|
|
61
|
+
font-weight: 700;
|
|
62
|
+
letter-spacing: 0;
|
|
63
|
+
text-transform: uppercase;
|
|
64
|
+
}
|
|
65
|
+
.status {
|
|
66
|
+
display: inline-flex;
|
|
67
|
+
align-items: center;
|
|
68
|
+
gap: 10px;
|
|
69
|
+
margin-bottom: 18px;
|
|
70
|
+
color: var(--success);
|
|
71
|
+
font-size: 15px;
|
|
72
|
+
font-weight: 700;
|
|
73
|
+
}
|
|
74
|
+
.mark {
|
|
75
|
+
display: grid;
|
|
76
|
+
width: 28px;
|
|
77
|
+
height: 28px;
|
|
78
|
+
place-items: center;
|
|
79
|
+
border-radius: 50%;
|
|
80
|
+
background: rgba(24, 160, 88, 0.12);
|
|
81
|
+
}
|
|
82
|
+
h1 {
|
|
83
|
+
margin: 0 0 12px;
|
|
84
|
+
font-size: clamp(30px, 5vw, 44px);
|
|
85
|
+
line-height: 1.05;
|
|
86
|
+
letter-spacing: 0;
|
|
87
|
+
}
|
|
88
|
+
p {
|
|
89
|
+
margin: 0;
|
|
90
|
+
color: var(--muted);
|
|
91
|
+
font-size: 17px;
|
|
92
|
+
line-height: 1.65;
|
|
93
|
+
}
|
|
94
|
+
.footnote {
|
|
95
|
+
margin-top: 28px;
|
|
96
|
+
padding-top: 20px;
|
|
97
|
+
border-top: 1px solid var(--line);
|
|
98
|
+
font-size: 14px;
|
|
99
|
+
}
|
|
100
|
+
</style>
|
|
101
|
+
</head>
|
|
102
|
+
<body>
|
|
103
|
+
<main>
|
|
104
|
+
<p class="brand">Tool4Seller CLI</p>
|
|
105
|
+
<div class="status">
|
|
106
|
+
<span class="mark" aria-hidden="true">✓</span>
|
|
107
|
+
<span>Login complete</span>
|
|
108
|
+
</div>
|
|
109
|
+
<h1>Authorization finished.</h1>
|
|
110
|
+
<p>Your Tool4Seller account is connected to the CLI. You can close this window.</p>
|
|
111
|
+
<p class="footnote">Return to your terminal to continue using <strong>tool4seller</strong>.</p>
|
|
112
|
+
</main>
|
|
113
|
+
</body>
|
|
114
|
+
</html>"""
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
class LoginError(RuntimeError):
|
|
118
|
+
pass
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def login_with_browser(base_url: str) -> TokenSet:
|
|
122
|
+
redirect_uri = f"http://{REDIRECT_HOST}:{REDIRECT_PORT}{REDIRECT_PATH}"
|
|
123
|
+
state = secrets.token_urlsafe(32)
|
|
124
|
+
code_verifier, code_challenge, _ = create_pkce_pair()
|
|
125
|
+
authorize_url = build_authorize_url(base_url, CLIENT_ID, redirect_uri, state, code_challenge)
|
|
126
|
+
|
|
127
|
+
code = wait_for_callback(state, on_ready=lambda: webbrowser.open(authorize_url))
|
|
128
|
+
return exchange_authorization_code(
|
|
129
|
+
base_url,
|
|
130
|
+
code=code,
|
|
131
|
+
client_id=CLIENT_ID,
|
|
132
|
+
redirect_uri=redirect_uri,
|
|
133
|
+
code_verifier=code_verifier,
|
|
134
|
+
)
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
def wait_for_callback(
|
|
138
|
+
expected_state: str,
|
|
139
|
+
on_ready: Callable[[], object] | None = None,
|
|
140
|
+
timeout_seconds: float = 300,
|
|
141
|
+
) -> str:
|
|
142
|
+
result: dict[str, str] = {}
|
|
143
|
+
handler = _make_callback_handler(expected_state, result)
|
|
144
|
+
|
|
145
|
+
with HTTPServer((REDIRECT_HOST, REDIRECT_PORT), handler) as server:
|
|
146
|
+
deadline = time.monotonic() + timeout_seconds
|
|
147
|
+
if on_ready:
|
|
148
|
+
on_ready()
|
|
149
|
+
while not result.get("code") and not result.get("error"):
|
|
150
|
+
remaining = deadline - time.monotonic()
|
|
151
|
+
if remaining <= 0:
|
|
152
|
+
break
|
|
153
|
+
server.timeout = remaining
|
|
154
|
+
server.handle_request()
|
|
155
|
+
|
|
156
|
+
if result.get("error"):
|
|
157
|
+
raise LoginError(result["error"])
|
|
158
|
+
if not result.get("code"):
|
|
159
|
+
raise LoginError("timed out waiting for authorization callback")
|
|
160
|
+
return result["code"]
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
def _make_callback_handler(expected_state: str, result: dict[str, str]) -> type[BaseHTTPRequestHandler]:
|
|
164
|
+
class CallbackHandler(BaseHTTPRequestHandler):
|
|
165
|
+
def do_GET(self) -> None:
|
|
166
|
+
parsed = urlparse(self.path)
|
|
167
|
+
query = parse_qs(parsed.query)
|
|
168
|
+
if parsed.path != REDIRECT_PATH:
|
|
169
|
+
self.send_error(404)
|
|
170
|
+
return
|
|
171
|
+
if query.get("state", [""])[0] != expected_state:
|
|
172
|
+
result["error"] = "invalid state"
|
|
173
|
+
self.send_error(400, result["error"])
|
|
174
|
+
return
|
|
175
|
+
if query.get("error"):
|
|
176
|
+
result["error"] = _format_callback_error(query)
|
|
177
|
+
self.send_error(400, result["error"])
|
|
178
|
+
return
|
|
179
|
+
result["code"] = query.get("code", [""])[0]
|
|
180
|
+
self.send_response(200)
|
|
181
|
+
self.send_header("Content-Type", "text/html; charset=utf-8")
|
|
182
|
+
self.end_headers()
|
|
183
|
+
self.wfile.write(LOGIN_COMPLETE_HTML.encode("utf-8"))
|
|
184
|
+
|
|
185
|
+
def log_message(self, format: str, *args: object) -> None:
|
|
186
|
+
return
|
|
187
|
+
|
|
188
|
+
return CallbackHandler
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
def _format_callback_error(query: dict[str, list[str]]) -> str:
|
|
192
|
+
error = query.get("error", ["authorization error"])[0]
|
|
193
|
+
description = query.get("error_description", [""])[0]
|
|
194
|
+
return error if not description else f"{error}: {description}"
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import time
|
|
2
|
+
from typing import Any
|
|
3
|
+
from urllib.parse import urlencode, urlsplit, urlunsplit
|
|
4
|
+
|
|
5
|
+
import requests
|
|
6
|
+
|
|
7
|
+
from tool4seller_cli.auth.token_store import TokenSet
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
CLIENT_ID = "tool4seller-cli"
|
|
11
|
+
DEFAULT_TIMEOUT = 30
|
|
12
|
+
_MAX_ERROR_TEXT = 500
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class OAuthError(RuntimeError):
|
|
16
|
+
pass
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def build_authorize_url(
|
|
20
|
+
base_url: str,
|
|
21
|
+
client_id: str,
|
|
22
|
+
redirect_uri: str,
|
|
23
|
+
state: str,
|
|
24
|
+
code_challenge: str,
|
|
25
|
+
) -> str:
|
|
26
|
+
query = urlencode(
|
|
27
|
+
{
|
|
28
|
+
"response_type": "code",
|
|
29
|
+
"client_id": client_id,
|
|
30
|
+
"redirect_uri": redirect_uri,
|
|
31
|
+
"state": state,
|
|
32
|
+
"code_challenge": code_challenge,
|
|
33
|
+
"code_challenge_method": "S256",
|
|
34
|
+
}
|
|
35
|
+
)
|
|
36
|
+
return f"{_root_url(base_url)}/oauth/authorize?{query}"
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def exchange_authorization_code(
|
|
40
|
+
base_url: str,
|
|
41
|
+
*,
|
|
42
|
+
code: str,
|
|
43
|
+
client_id: str,
|
|
44
|
+
redirect_uri: str,
|
|
45
|
+
code_verifier: str,
|
|
46
|
+
session: Any | None = None,
|
|
47
|
+
now: int | None = None,
|
|
48
|
+
) -> TokenSet:
|
|
49
|
+
session = session or requests.Session()
|
|
50
|
+
response = session.post(
|
|
51
|
+
f"{_root_url(base_url)}/oauth/token",
|
|
52
|
+
data={
|
|
53
|
+
"grant_type": "authorization_code",
|
|
54
|
+
"code": code,
|
|
55
|
+
"client_id": client_id,
|
|
56
|
+
"redirect_uri": redirect_uri,
|
|
57
|
+
"code_verifier": code_verifier,
|
|
58
|
+
},
|
|
59
|
+
timeout=DEFAULT_TIMEOUT,
|
|
60
|
+
)
|
|
61
|
+
return parse_token_response(response, now=now)
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def refresh_access_token(
|
|
65
|
+
base_url: str,
|
|
66
|
+
*,
|
|
67
|
+
refresh_token: str,
|
|
68
|
+
client_id: str,
|
|
69
|
+
session: Any | None = None,
|
|
70
|
+
now: int | None = None,
|
|
71
|
+
) -> TokenSet:
|
|
72
|
+
session = session or requests.Session()
|
|
73
|
+
response = session.post(
|
|
74
|
+
f"{_root_url(base_url)}/oauth/token",
|
|
75
|
+
data={
|
|
76
|
+
"grant_type": "refresh_token",
|
|
77
|
+
"refresh_token": refresh_token,
|
|
78
|
+
"client_id": client_id,
|
|
79
|
+
},
|
|
80
|
+
timeout=DEFAULT_TIMEOUT,
|
|
81
|
+
)
|
|
82
|
+
return parse_token_response(response, now=now)
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def parse_token_response(response: Any, *, now: int | None = None) -> TokenSet:
|
|
86
|
+
status_code = getattr(response, "status_code", 200)
|
|
87
|
+
try:
|
|
88
|
+
data = response.json()
|
|
89
|
+
except ValueError as exc:
|
|
90
|
+
if status_code < 200 or status_code >= 300:
|
|
91
|
+
raise OAuthError(f"HTTP {status_code}: {_bounded_response_text(response)}") from exc
|
|
92
|
+
raise OAuthError("invalid token response") from exc
|
|
93
|
+
|
|
94
|
+
if isinstance(data, dict) and data.get("error"):
|
|
95
|
+
description = data.get("error_description")
|
|
96
|
+
message = data["error"] if not description else f"{data['error']}: {description}"
|
|
97
|
+
raise OAuthError(message)
|
|
98
|
+
if status_code < 200 or status_code >= 300:
|
|
99
|
+
detail = _bounded_response_text(response) or _bounded_json(data)
|
|
100
|
+
raise OAuthError(f"HTTP {status_code}: {detail}".rstrip(": "))
|
|
101
|
+
if not isinstance(data, dict) or not data.get("access_token"):
|
|
102
|
+
raise OAuthError("missing access_token")
|
|
103
|
+
|
|
104
|
+
expires_at = 0
|
|
105
|
+
if data.get("expires_in") is not None:
|
|
106
|
+
current_time = int(time.time() if now is None else now)
|
|
107
|
+
expires_at = current_time + int(data["expires_in"])
|
|
108
|
+
|
|
109
|
+
return TokenSet(
|
|
110
|
+
access_token=data["access_token"],
|
|
111
|
+
refresh_token=data.get("refresh_token", ""),
|
|
112
|
+
expires_at=expires_at,
|
|
113
|
+
)
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def _root_url(base_url: str) -> str:
|
|
117
|
+
parts = urlsplit(base_url.strip())
|
|
118
|
+
return urlunsplit((parts.scheme, parts.netloc, "", "", "")).rstrip("/")
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def _bounded_response_text(response: Any) -> str:
|
|
122
|
+
return str(getattr(response, "text", "") or "")[:_MAX_ERROR_TEXT]
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
def _bounded_json(data: Any) -> str:
|
|
126
|
+
return str(data)[:_MAX_ERROR_TEXT]
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import base64
|
|
2
|
+
import hashlib
|
|
3
|
+
import secrets
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def create_pkce_pair() -> tuple[str, str, str]:
|
|
7
|
+
code_verifier = secrets.token_urlsafe(64)[:128]
|
|
8
|
+
digest = hashlib.sha256(code_verifier.encode("ascii")).digest()
|
|
9
|
+
code_challenge = base64.urlsafe_b64encode(digest).decode("ascii").rstrip("=")
|
|
10
|
+
return code_verifier, code_challenge, "S256"
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import json
|
|
2
|
+
from dataclasses import dataclass
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
@dataclass(frozen=True)
|
|
7
|
+
class TokenSet:
|
|
8
|
+
access_token: str = ""
|
|
9
|
+
refresh_token: str = ""
|
|
10
|
+
expires_at: int = 0
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class FileTokenStore:
|
|
14
|
+
def __init__(self, directory: str | Path):
|
|
15
|
+
self.directory = Path(directory)
|
|
16
|
+
|
|
17
|
+
def load(self, env_name: str) -> TokenSet:
|
|
18
|
+
path = self._path(env_name)
|
|
19
|
+
if not path.exists():
|
|
20
|
+
return TokenSet()
|
|
21
|
+
data = json.loads(path.read_text(encoding="utf-8"))
|
|
22
|
+
return TokenSet(
|
|
23
|
+
access_token=data.get("accessToken", ""),
|
|
24
|
+
refresh_token=data.get("refreshToken", ""),
|
|
25
|
+
expires_at=int(data.get("expiresAt", 0) or 0),
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
def save(self, env_name: str, token: TokenSet) -> None:
|
|
29
|
+
self.directory.mkdir(parents=True, exist_ok=True)
|
|
30
|
+
path = self._path(env_name)
|
|
31
|
+
path.write_text(
|
|
32
|
+
json.dumps(
|
|
33
|
+
{
|
|
34
|
+
"accessToken": token.access_token,
|
|
35
|
+
"refreshToken": token.refresh_token,
|
|
36
|
+
"expiresAt": token.expires_at,
|
|
37
|
+
},
|
|
38
|
+
indent=2,
|
|
39
|
+
sort_keys=True,
|
|
40
|
+
),
|
|
41
|
+
encoding="utf-8",
|
|
42
|
+
)
|
|
43
|
+
path.chmod(0o600)
|
|
44
|
+
|
|
45
|
+
def clear(self, env_name: str) -> None:
|
|
46
|
+
path = self._path(env_name)
|
|
47
|
+
if path.exists():
|
|
48
|
+
path.unlink()
|
|
49
|
+
|
|
50
|
+
def _path(self, env_name: str) -> Path:
|
|
51
|
+
env_path = Path(env_name)
|
|
52
|
+
if (
|
|
53
|
+
not env_name
|
|
54
|
+
or env_path.is_absolute()
|
|
55
|
+
or ".." in env_path.parts
|
|
56
|
+
or "/" in env_name
|
|
57
|
+
or "\\" in env_name
|
|
58
|
+
):
|
|
59
|
+
raise ValueError("invalid environment name")
|
|
60
|
+
|
|
61
|
+
directory = self.directory.resolve(strict=False)
|
|
62
|
+
path = (directory / f"{env_name}.token.json").resolve(strict=False)
|
|
63
|
+
if not path.is_relative_to(directory):
|
|
64
|
+
raise ValueError("invalid environment name")
|
|
65
|
+
return path
|