pebbenote-cli 0.6.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.
- pebbenote_cli-0.6.1/PKG-INFO +129 -0
- pebbenote_cli-0.6.1/README.md +104 -0
- pebbenote_cli-0.6.1/pebbenote_cli/__init__.py +0 -0
- pebbenote_cli-0.6.1/pebbenote_cli/__main__.py +3 -0
- pebbenote_cli-0.6.1/pebbenote_cli/api.py +62 -0
- pebbenote_cli-0.6.1/pebbenote_cli/auth.py +120 -0
- pebbenote_cli-0.6.1/pebbenote_cli/cli.py +282 -0
- pebbenote_cli-0.6.1/pebbenote_cli/client/__init__.py +8 -0
- pebbenote_cli-0.6.1/pebbenote_cli/client/api/__init__.py +1 -0
- pebbenote_cli-0.6.1/pebbenote_cli/client/api/actions/__init__.py +1 -0
- pebbenote_cli-0.6.1/pebbenote_cli/client/api/actions/apply_action.py +166 -0
- pebbenote_cli-0.6.1/pebbenote_cli/client/api/instructions/__init__.py +1 -0
- pebbenote_cli-0.6.1/pebbenote_cli/client/api/instructions/list_instructions.py +127 -0
- pebbenote_cli-0.6.1/pebbenote_cli/client/api/instructions/replace_instructions.py +204 -0
- pebbenote_cli-0.6.1/pebbenote_cli/client/api/notes/__init__.py +1 -0
- pebbenote_cli-0.6.1/pebbenote_cli/client/api/notes/create_note.py +173 -0
- pebbenote_cli-0.6.1/pebbenote_cli/client/api/notes/delete_note.py +159 -0
- pebbenote_cli-0.6.1/pebbenote_cli/client/api/notes/edit_note_by_id.py +197 -0
- pebbenote_cli-0.6.1/pebbenote_cli/client/api/notes/get_note_by_id.py +184 -0
- pebbenote_cli-0.6.1/pebbenote_cli/client/api/notes/list_notes.py +132 -0
- pebbenote_cli-0.6.1/pebbenote_cli/client/api/notes/set_default_note.py +161 -0
- pebbenote_cli-0.6.1/pebbenote_cli/client/api/sharing/__init__.py +1 -0
- pebbenote_cli-0.6.1/pebbenote_cli/client/api/sharing/get_note_sharing.py +161 -0
- pebbenote_cli-0.6.1/pebbenote_cli/client/api/sharing/get_shared_note.py +172 -0
- pebbenote_cli-0.6.1/pebbenote_cli/client/api/sharing/join_shared_note.py +176 -0
- pebbenote_cli-0.6.1/pebbenote_cli/client/api/sharing/make_note_private.py +167 -0
- pebbenote_cli-0.6.1/pebbenote_cli/client/api/sharing/remove_note_member.py +173 -0
- pebbenote_cli-0.6.1/pebbenote_cli/client/api/sharing/revoke_share_link.py +181 -0
- pebbenote_cli-0.6.1/pebbenote_cli/client/api/sharing/share_note.py +202 -0
- pebbenote_cli-0.6.1/pebbenote_cli/client/client.py +280 -0
- pebbenote_cli-0.6.1/pebbenote_cli/client/errors.py +16 -0
- pebbenote_cli-0.6.1/pebbenote_cli/client/models/__init__.py +47 -0
- pebbenote_cli-0.6.1/pebbenote_cli/client/models/action_accepted_model.py +68 -0
- pebbenote_cli-0.6.1/pebbenote_cli/client/models/action_edit_model.py +96 -0
- pebbenote_cli-0.6.1/pebbenote_cli/client/models/action_model.py +105 -0
- pebbenote_cli-0.6.1/pebbenote_cli/client/models/action_status.py +10 -0
- pebbenote_cli-0.6.1/pebbenote_cli/client/models/http_validation_error.py +79 -0
- pebbenote_cli-0.6.1/pebbenote_cli/client/models/instructions_conflict_model.py +75 -0
- pebbenote_cli-0.6.1/pebbenote_cli/client/models/instructions_edit_model.py +69 -0
- pebbenote_cli-0.6.1/pebbenote_cli/client/models/instructions_model.py +69 -0
- pebbenote_cli-0.6.1/pebbenote_cli/client/models/note_access.py +10 -0
- pebbenote_cli-0.6.1/pebbenote_cli/client/models/note_conflict_model.py +75 -0
- pebbenote_cli-0.6.1/pebbenote_cli/client/models/note_member_model.py +86 -0
- pebbenote_cli-0.6.1/pebbenote_cli/client/models/note_model.py +113 -0
- pebbenote_cli-0.6.1/pebbenote_cli/client/models/note_sharing_model.py +94 -0
- pebbenote_cli-0.6.1/pebbenote_cli/client/models/note_update_model.py +83 -0
- pebbenote_cli-0.6.1/pebbenote_cli/client/models/note_view_model.py +143 -0
- pebbenote_cli-0.6.1/pebbenote_cli/client/models/share_access.py +9 -0
- pebbenote_cli-0.6.1/pebbenote_cli/client/models/share_create_model.py +69 -0
- pebbenote_cli-0.6.1/pebbenote_cli/client/models/share_link_model.py +120 -0
- pebbenote_cli-0.6.1/pebbenote_cli/client/models/shared_note_model.py +89 -0
- pebbenote_cli-0.6.1/pebbenote_cli/client/models/validation_error.py +125 -0
- pebbenote_cli-0.6.1/pebbenote_cli/client/models/validation_error_context.py +44 -0
- pebbenote_cli-0.6.1/pebbenote_cli/client/types.py +54 -0
- pebbenote_cli-0.6.1/pebbenote_cli/config.py +46 -0
- pebbenote_cli-0.6.1/pebbenote_cli/editor.py +23 -0
- pebbenote_cli-0.6.1/pebbenote_cli/errors.py +10 -0
- pebbenote_cli-0.6.1/pebbenote_cli/http.py +36 -0
- pebbenote_cli-0.6.1/pebbenote_cli/instructions.py +24 -0
- pebbenote_cli-0.6.1/pebbenote_cli/merge.py +79 -0
- pebbenote_cli-0.6.1/pyproject.toml +41 -0
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pebbenote-cli
|
|
3
|
+
Version: 0.6.1
|
|
4
|
+
Summary: Command line client for pebbenote
|
|
5
|
+
License: AGPL-3.0-only
|
|
6
|
+
Keywords: pebble,notes,cli
|
|
7
|
+
Author: Grzegorz Koperwas
|
|
8
|
+
Author-email: admin@grzegorzkoperwas.site
|
|
9
|
+
Requires-Python: >=3.13,<4.0
|
|
10
|
+
Classifier: Environment :: Console
|
|
11
|
+
Classifier: License :: OSI Approved :: GNU Affero General Public License v3
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.15
|
|
16
|
+
Classifier: Topic :: Text Editors
|
|
17
|
+
Classifier: Topic :: Utilities
|
|
18
|
+
Requires-Dist: argcomplete (>=3.7.2,<4.0.0)
|
|
19
|
+
Requires-Dist: attrs (>=26.1.0,<27.0.0)
|
|
20
|
+
Requires-Dist: httpx (>=0.28.1,<0.29.0)
|
|
21
|
+
Project-URL: Homepage, https://github.com/HakierGrzonzo/pebbenote
|
|
22
|
+
Project-URL: Repository, https://github.com/HakierGrzonzo/pebbenote
|
|
23
|
+
Description-Content-Type: text/markdown
|
|
24
|
+
|
|
25
|
+
# Pebbenote CLI
|
|
26
|
+
|
|
27
|
+
Opens a note in `$EDITOR` and syncs it with the backend when the editor quits. Python 3.13+, packaged with Poetry. Talks to the backend through a client generated from its OpenAPI schema.
|
|
28
|
+
|
|
29
|
+
## Install
|
|
30
|
+
|
|
31
|
+
From PyPI, as a shell command available everywhere:
|
|
32
|
+
|
|
33
|
+
```sh
|
|
34
|
+
pipx install pebbenote-cli # `pipx upgrade pebbenote-cli` to update
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
From a checkout (builds with Poetry's backend):
|
|
38
|
+
|
|
39
|
+
```sh
|
|
40
|
+
pipx install ./cli # from the repo root; use `pipx install --force ./cli` to update
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
For development, install into the project virtualenv and prefix commands with `poetry run`:
|
|
44
|
+
|
|
45
|
+
```sh
|
|
46
|
+
cd cli
|
|
47
|
+
poetry install
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## Usage
|
|
51
|
+
|
|
52
|
+
```sh
|
|
53
|
+
pebbenote # edit the default note
|
|
54
|
+
pebbenote <note_id> # edit a specific note
|
|
55
|
+
pebbenote list # list notes (* = default)
|
|
56
|
+
pebbenote delete [id] # delete a note (default note without an id; -y skips the prompt)
|
|
57
|
+
pebbenote share [id] # print a link to view a note (default note without an id)
|
|
58
|
+
pebbenote instructions # edit the standing instructions as a list
|
|
59
|
+
pebbenote action <text> # ask the assistant to change the default note (--note <id> for another)
|
|
60
|
+
pebbenote login # force a new login
|
|
61
|
+
pebbenote completion <shell> # print the completion script for bash, zsh or fish
|
|
62
|
+
pebbenote > note.md # piped output prints the note instead of opening the editor
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
`pebbenote instructions` opens the standing instructions (the ones the assistant follows on every note) as a list, one `- ` item each. Add, reword or delete items; the saved list replaces the stored one, and the backend then applies it in the background to every note. `--no-apply` only saves the list. Piped, it prints the list.
|
|
66
|
+
|
|
67
|
+
`pebbenote delete <id>` shows the note's first line and asks before deleting it. Deleting the default note makes the oldest remaining note the default. Without a terminal to ask on it needs `--yes`.
|
|
68
|
+
|
|
69
|
+
`pebbenote share` prints a link to view the note in a browser, without an account. Anyone with the link sees the note as it is when they open it, until the link expires (after 7 days, unless the backend is set up differently) or the note is deleted. A link can't be cancelled on its own. Only the link goes to stdout, so `pebbenote share | wl-copy` copies it.
|
|
70
|
+
|
|
71
|
+
`pebbenote action add oat milk` sends the action to the assistant and prints the updated note. If the assistant takes longer than the backend waits, the CLI says so and quits; the note is updated in the background.
|
|
72
|
+
|
|
73
|
+
### Shell completion
|
|
74
|
+
|
|
75
|
+
Commands, options and note ids complete on Tab once the script is loaded:
|
|
76
|
+
|
|
77
|
+
```sh
|
|
78
|
+
pebbenote completion fish > ~/.config/fish/completions/pebbenote.fish
|
|
79
|
+
eval "$(pebbenote completion bash)" # in ~/.bashrc
|
|
80
|
+
eval "$(pebbenote completion zsh)" # in ~/.zshrc, after compinit
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
Note ids are fetched from the backend with each note's first line as description (fish and zsh show it). That needs a login (`pebbenote login`): completion never starts one, when logged out or offline it only offers `default`.
|
|
84
|
+
|
|
85
|
+
### Conflicts
|
|
86
|
+
|
|
87
|
+
An edit is sent with the version it was made on. When the note or the instructions were changed elsewhere while the editor was open (on the watch, by the assistant, in another terminal), the backend refuses the edit and the CLI merges both like git: changes in different places are combined and synced without asking, changes to the same lines reopen the editor with both versions between markers:
|
|
88
|
+
|
|
89
|
+
```
|
|
90
|
+
<<<<<<< yours
|
|
91
|
+
- oat milk
|
|
92
|
+
=======
|
|
93
|
+
- soy milk
|
|
94
|
+
>>>>>>> server
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
Edit that into what you want and quit. If markers are still there, nothing is synced and the text is kept in `$XDG_STATE_HOME/pebbenote/unresolved.md`.
|
|
98
|
+
|
|
99
|
+
## Configuration
|
|
100
|
+
|
|
101
|
+
On first run `$XDG_CONFIG_HOME/pebbenote/config.toml` (default `~/.config/pebbenote/config.toml`) is created with the production URLs:
|
|
102
|
+
|
|
103
|
+
```toml
|
|
104
|
+
backend_url = "https://dev.grzegorzkoperwas.site"
|
|
105
|
+
issuer = "https://auth.grzegorzkoperwas.site/realms/pebbenote"
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
Edit it to point at another backend, e.g. `http://localhost:8000` and `http://localhost:8080/realms/pebbenote` for local development. The editor comes from `$EDITOR`, then `$VISUAL`, then `vi`.
|
|
109
|
+
|
|
110
|
+
Login uses the Keycloak device flow. Tokens are stored in `$XDG_STATE_HOME/pebbenote/tokens.json` (default `~/.local/state/pebbenote/tokens.json`).
|
|
111
|
+
|
|
112
|
+
## Regenerating the API client
|
|
113
|
+
|
|
114
|
+
`pebbenote_cli/client/` is generated with [openapi-python-client](https://github.com/openapi-generators/openapi-python-client) from a locally running backend (`ENVIRONMENT=local`, which serves `/openapi.json`). Don't edit it by hand; after changing the backend API run:
|
|
115
|
+
|
|
116
|
+
```sh
|
|
117
|
+
./generate_client.sh # uses http://localhost:8000/openapi.json, override with OPENAPI_URL
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
## Releasing
|
|
121
|
+
|
|
122
|
+
Bump `version` in `pyproject.toml` and merge it, then tag that commit:
|
|
123
|
+
|
|
124
|
+
```sh
|
|
125
|
+
git tag cli-v0.6.1 && git push origin cli-v0.6.1
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
The `Publish CLI` workflow checks the tag matches the version, builds and uploads to PyPI with trusted publishing (no token in the repo).
|
|
129
|
+
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
# Pebbenote CLI
|
|
2
|
+
|
|
3
|
+
Opens a note in `$EDITOR` and syncs it with the backend when the editor quits. Python 3.13+, packaged with Poetry. Talks to the backend through a client generated from its OpenAPI schema.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
From PyPI, as a shell command available everywhere:
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
pipx install pebbenote-cli # `pipx upgrade pebbenote-cli` to update
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
From a checkout (builds with Poetry's backend):
|
|
14
|
+
|
|
15
|
+
```sh
|
|
16
|
+
pipx install ./cli # from the repo root; use `pipx install --force ./cli` to update
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
For development, install into the project virtualenv and prefix commands with `poetry run`:
|
|
20
|
+
|
|
21
|
+
```sh
|
|
22
|
+
cd cli
|
|
23
|
+
poetry install
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Usage
|
|
27
|
+
|
|
28
|
+
```sh
|
|
29
|
+
pebbenote # edit the default note
|
|
30
|
+
pebbenote <note_id> # edit a specific note
|
|
31
|
+
pebbenote list # list notes (* = default)
|
|
32
|
+
pebbenote delete [id] # delete a note (default note without an id; -y skips the prompt)
|
|
33
|
+
pebbenote share [id] # print a link to view a note (default note without an id)
|
|
34
|
+
pebbenote instructions # edit the standing instructions as a list
|
|
35
|
+
pebbenote action <text> # ask the assistant to change the default note (--note <id> for another)
|
|
36
|
+
pebbenote login # force a new login
|
|
37
|
+
pebbenote completion <shell> # print the completion script for bash, zsh or fish
|
|
38
|
+
pebbenote > note.md # piped output prints the note instead of opening the editor
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
`pebbenote instructions` opens the standing instructions (the ones the assistant follows on every note) as a list, one `- ` item each. Add, reword or delete items; the saved list replaces the stored one, and the backend then applies it in the background to every note. `--no-apply` only saves the list. Piped, it prints the list.
|
|
42
|
+
|
|
43
|
+
`pebbenote delete <id>` shows the note's first line and asks before deleting it. Deleting the default note makes the oldest remaining note the default. Without a terminal to ask on it needs `--yes`.
|
|
44
|
+
|
|
45
|
+
`pebbenote share` prints a link to view the note in a browser, without an account. Anyone with the link sees the note as it is when they open it, until the link expires (after 7 days, unless the backend is set up differently) or the note is deleted. A link can't be cancelled on its own. Only the link goes to stdout, so `pebbenote share | wl-copy` copies it.
|
|
46
|
+
|
|
47
|
+
`pebbenote action add oat milk` sends the action to the assistant and prints the updated note. If the assistant takes longer than the backend waits, the CLI says so and quits; the note is updated in the background.
|
|
48
|
+
|
|
49
|
+
### Shell completion
|
|
50
|
+
|
|
51
|
+
Commands, options and note ids complete on Tab once the script is loaded:
|
|
52
|
+
|
|
53
|
+
```sh
|
|
54
|
+
pebbenote completion fish > ~/.config/fish/completions/pebbenote.fish
|
|
55
|
+
eval "$(pebbenote completion bash)" # in ~/.bashrc
|
|
56
|
+
eval "$(pebbenote completion zsh)" # in ~/.zshrc, after compinit
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
Note ids are fetched from the backend with each note's first line as description (fish and zsh show it). That needs a login (`pebbenote login`): completion never starts one, when logged out or offline it only offers `default`.
|
|
60
|
+
|
|
61
|
+
### Conflicts
|
|
62
|
+
|
|
63
|
+
An edit is sent with the version it was made on. When the note or the instructions were changed elsewhere while the editor was open (on the watch, by the assistant, in another terminal), the backend refuses the edit and the CLI merges both like git: changes in different places are combined and synced without asking, changes to the same lines reopen the editor with both versions between markers:
|
|
64
|
+
|
|
65
|
+
```
|
|
66
|
+
<<<<<<< yours
|
|
67
|
+
- oat milk
|
|
68
|
+
=======
|
|
69
|
+
- soy milk
|
|
70
|
+
>>>>>>> server
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
Edit that into what you want and quit. If markers are still there, nothing is synced and the text is kept in `$XDG_STATE_HOME/pebbenote/unresolved.md`.
|
|
74
|
+
|
|
75
|
+
## Configuration
|
|
76
|
+
|
|
77
|
+
On first run `$XDG_CONFIG_HOME/pebbenote/config.toml` (default `~/.config/pebbenote/config.toml`) is created with the production URLs:
|
|
78
|
+
|
|
79
|
+
```toml
|
|
80
|
+
backend_url = "https://dev.grzegorzkoperwas.site"
|
|
81
|
+
issuer = "https://auth.grzegorzkoperwas.site/realms/pebbenote"
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
Edit it to point at another backend, e.g. `http://localhost:8000` and `http://localhost:8080/realms/pebbenote` for local development. The editor comes from `$EDITOR`, then `$VISUAL`, then `vi`.
|
|
85
|
+
|
|
86
|
+
Login uses the Keycloak device flow. Tokens are stored in `$XDG_STATE_HOME/pebbenote/tokens.json` (default `~/.local/state/pebbenote/tokens.json`).
|
|
87
|
+
|
|
88
|
+
## Regenerating the API client
|
|
89
|
+
|
|
90
|
+
`pebbenote_cli/client/` is generated with [openapi-python-client](https://github.com/openapi-generators/openapi-python-client) from a locally running backend (`ENVIRONMENT=local`, which serves `/openapi.json`). Don't edit it by hand; after changing the backend API run:
|
|
91
|
+
|
|
92
|
+
```sh
|
|
93
|
+
./generate_client.sh # uses http://localhost:8000/openapi.json, override with OPENAPI_URL
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
## Releasing
|
|
97
|
+
|
|
98
|
+
Bump `version` in `pyproject.toml` and merge it, then tag that commit:
|
|
99
|
+
|
|
100
|
+
```sh
|
|
101
|
+
git tag cli-v0.6.1 && git push origin cli-v0.6.1
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
The `Publish CLI` workflow checks the tag matches the version, builds and uploads to PyPI with trusted publishing (no token in the repo).
|
|
File without changes
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import json
|
|
2
|
+
|
|
3
|
+
import httpx
|
|
4
|
+
|
|
5
|
+
from pebbenote_cli.auth import access_token
|
|
6
|
+
from pebbenote_cli.client import AuthenticatedClient
|
|
7
|
+
from pebbenote_cli.config import Config
|
|
8
|
+
from pebbenote_cli.errors import CliError, ConflictError
|
|
9
|
+
from pebbenote_cli.http import UNAVAILABLE_STATUSES, USER_AGENT
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def _client(config: Config, token: str) -> AuthenticatedClient:
|
|
13
|
+
return AuthenticatedClient(
|
|
14
|
+
base_url=config.backend_url,
|
|
15
|
+
token=token,
|
|
16
|
+
headers={"User-Agent": USER_AGENT},
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def _detail(content: bytes):
|
|
21
|
+
try:
|
|
22
|
+
payload = json.loads(content or b"null")
|
|
23
|
+
except ValueError:
|
|
24
|
+
return None
|
|
25
|
+
return payload.get("detail") if isinstance(payload, dict) else payload
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def call(config: Config, endpoint, *, interactive=True, **kwargs):
|
|
29
|
+
"""
|
|
30
|
+
Call a generated endpoint module, retrying once with a fresh token on 401.
|
|
31
|
+
|
|
32
|
+
A 409 raises ConflictError with the current state of what was edited.
|
|
33
|
+
`interactive=False` raises CliError instead of starting a login.
|
|
34
|
+
"""
|
|
35
|
+
name = endpoint.__name__.rsplit(".", 1)[-1]
|
|
36
|
+
try:
|
|
37
|
+
response = endpoint.sync_detailed(
|
|
38
|
+
client=_client(config, access_token(config, interactive=interactive)),
|
|
39
|
+
**kwargs,
|
|
40
|
+
)
|
|
41
|
+
if response.status_code == 401:
|
|
42
|
+
response = endpoint.sync_detailed(
|
|
43
|
+
client=_client(
|
|
44
|
+
config,
|
|
45
|
+
access_token(config, force_refresh=True, interactive=interactive),
|
|
46
|
+
),
|
|
47
|
+
**kwargs,
|
|
48
|
+
)
|
|
49
|
+
except httpx.TransportError as e:
|
|
50
|
+
raise CliError(f"Cannot reach {config.backend_url}: {e}") from e
|
|
51
|
+
if response.status_code in UNAVAILABLE_STATUSES:
|
|
52
|
+
raise CliError(
|
|
53
|
+
f"Cannot reach {config.backend_url} (HTTP {response.status_code}): "
|
|
54
|
+
"the backend seems to be down"
|
|
55
|
+
)
|
|
56
|
+
if response.status_code >= 400:
|
|
57
|
+
message = f"{name} failed ({response.status_code}): {_detail(response.content)}"
|
|
58
|
+
current = getattr(response.parsed, "current", None)
|
|
59
|
+
if response.status_code == 409 and current is not None:
|
|
60
|
+
raise ConflictError(message, current)
|
|
61
|
+
raise CliError(message)
|
|
62
|
+
return response.parsed
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import os
|
|
3
|
+
import sys
|
|
4
|
+
import time
|
|
5
|
+
|
|
6
|
+
from pebbenote_cli.config import TOKEN_FILE, Config
|
|
7
|
+
from pebbenote_cli.errors import CliError
|
|
8
|
+
from pebbenote_cli.http import UNAVAILABLE_STATUSES, request
|
|
9
|
+
|
|
10
|
+
CLIENT_ID = "pebbenote-watch"
|
|
11
|
+
EXPIRY_MARGIN = 30
|
|
12
|
+
# Offline tokens outlive the 30 minute Keycloak SSO session idle timeout
|
|
13
|
+
SCOPE = "openid offline_access"
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def load_tokens():
|
|
17
|
+
try:
|
|
18
|
+
return json.loads(TOKEN_FILE.read_text())
|
|
19
|
+
except (OSError, ValueError):
|
|
20
|
+
return None
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def save_tokens(token_response):
|
|
24
|
+
tokens = {
|
|
25
|
+
"access_token": token_response["access_token"],
|
|
26
|
+
"refresh_token": token_response.get("refresh_token"),
|
|
27
|
+
"expires_at": time.time() + token_response.get("expires_in", 0),
|
|
28
|
+
}
|
|
29
|
+
TOKEN_FILE.parent.mkdir(parents=True, exist_ok=True)
|
|
30
|
+
fd = os.open(TOKEN_FILE, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o600)
|
|
31
|
+
with os.fdopen(fd, "w") as f:
|
|
32
|
+
json.dump(tokens, f)
|
|
33
|
+
return tokens
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def check_available(config: Config, status):
|
|
37
|
+
if status in UNAVAILABLE_STATUSES:
|
|
38
|
+
raise CliError(
|
|
39
|
+
f"Cannot reach {config.issuer} (HTTP {status}): "
|
|
40
|
+
"Keycloak and the backend seem to be down"
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def login(config: Config):
|
|
45
|
+
status, device = request(
|
|
46
|
+
"POST",
|
|
47
|
+
f"{config.issuer}/protocol/openid-connect/auth/device",
|
|
48
|
+
form={"client_id": CLIENT_ID, "scope": SCOPE},
|
|
49
|
+
)
|
|
50
|
+
check_available(config, status)
|
|
51
|
+
if status != 200:
|
|
52
|
+
raise CliError(f"Device authorization failed: {device}")
|
|
53
|
+
print(f"Sign in at: {device.get('verification_uri_complete')}", file=sys.stderr)
|
|
54
|
+
print(f"Code: {device['user_code']}", file=sys.stderr)
|
|
55
|
+
|
|
56
|
+
interval = device.get("interval", 5)
|
|
57
|
+
deadline = time.time() + device["expires_in"]
|
|
58
|
+
while time.time() < deadline:
|
|
59
|
+
time.sleep(interval)
|
|
60
|
+
status, token = request(
|
|
61
|
+
"POST",
|
|
62
|
+
f"{config.issuer}/protocol/openid-connect/token",
|
|
63
|
+
form={
|
|
64
|
+
"grant_type": "urn:ietf:params:oauth:grant-type:device_code",
|
|
65
|
+
"client_id": CLIENT_ID,
|
|
66
|
+
"device_code": device["device_code"],
|
|
67
|
+
},
|
|
68
|
+
)
|
|
69
|
+
if status == 200:
|
|
70
|
+
return save_tokens(token)
|
|
71
|
+
check_available(config, status)
|
|
72
|
+
error = (token or {}).get("error")
|
|
73
|
+
if error == "authorization_pending":
|
|
74
|
+
continue
|
|
75
|
+
if error == "slow_down":
|
|
76
|
+
interval += 5
|
|
77
|
+
continue
|
|
78
|
+
raise CliError(f"Login failed: {error}")
|
|
79
|
+
raise CliError("Login timed out")
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def require_login(config: Config, interactive):
|
|
83
|
+
"""Starts a new login, or fails when there is no one to sign in."""
|
|
84
|
+
if not interactive:
|
|
85
|
+
raise CliError("Not logged in; run `pebbenote login`")
|
|
86
|
+
return login(config)
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def refresh(config: Config, tokens, interactive=True):
|
|
90
|
+
status, token = request(
|
|
91
|
+
"POST",
|
|
92
|
+
f"{config.issuer}/protocol/openid-connect/token",
|
|
93
|
+
form={
|
|
94
|
+
"grant_type": "refresh_token",
|
|
95
|
+
"client_id": CLIENT_ID,
|
|
96
|
+
"refresh_token": tokens["refresh_token"],
|
|
97
|
+
},
|
|
98
|
+
)
|
|
99
|
+
if status == 200:
|
|
100
|
+
return save_tokens(token)
|
|
101
|
+
# Don't start a new login when the server is down, only when the refresh
|
|
102
|
+
# token was rejected
|
|
103
|
+
check_available(config, status)
|
|
104
|
+
return require_login(config, interactive)
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def access_token(config: Config, force_refresh=False, interactive=True):
|
|
108
|
+
"""
|
|
109
|
+
Returns a valid access token, refreshing it when needed. Without a usable
|
|
110
|
+
token it logs in, unless `interactive` is false: then it raises CliError.
|
|
111
|
+
"""
|
|
112
|
+
tokens = load_tokens()
|
|
113
|
+
if not tokens:
|
|
114
|
+
tokens = require_login(config, interactive)
|
|
115
|
+
elif force_refresh or tokens["expires_at"] - EXPIRY_MARGIN <= time.time():
|
|
116
|
+
if tokens.get("refresh_token"):
|
|
117
|
+
tokens = refresh(config, tokens, interactive)
|
|
118
|
+
else:
|
|
119
|
+
tokens = require_login(config, interactive)
|
|
120
|
+
return tokens["access_token"]
|