paseto 2.0.0__tar.gz → 3.0.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.
- paseto-3.0.1/.gitignore +19 -0
- paseto-3.0.1/CHANGELOG.md +89 -0
- {paseto-2.0.0 → paseto-3.0.1}/LICENSE +0 -22
- paseto-3.0.1/PKG-INFO +213 -0
- paseto-3.0.1/README.md +183 -0
- paseto-3.0.1/paseto/__init__.py +30 -0
- paseto-3.0.1/paseto/_crypto.py +82 -0
- paseto-3.0.1/paseto/exceptions.py +37 -0
- paseto-3.0.1/paseto/experimental/__init__.py +40 -0
- paseto-3.0.1/paseto/experimental/_mldsa.py +78 -0
- paseto-3.0.1/paseto/experimental/_pq.py +217 -0
- paseto-3.0.1/paseto/experimental/v5.py +27 -0
- paseto-3.0.1/paseto/experimental/v6.py +30 -0
- paseto-3.0.1/paseto/helpers.py +169 -0
- paseto-3.0.1/paseto/keys/asymmetric_key.py +260 -0
- paseto-3.0.1/paseto/keys/symmetric_key.py +98 -0
- {paseto-2.0.0 → paseto-3.0.1}/paseto/paseto.py +92 -60
- paseto-3.0.1/paseto/protocols/protocol.py +69 -0
- paseto-3.0.1/paseto/protocols/v2.py +235 -0
- {paseto-2.0.0 → paseto-3.0.1}/paseto/protocols/v3.py +59 -63
- {paseto-2.0.0 → paseto-3.0.1}/paseto/protocols/v4.py +101 -71
- paseto-3.0.1/paseto/py.typed +0 -0
- paseto-3.0.1/pyproject.toml +87 -0
- paseto-3.0.1/tests/__init__.py +0 -0
- paseto-3.0.1/tests/test_claims.py +76 -0
- paseto-3.0.1/tests/test_experimental.py +385 -0
- paseto-3.0.1/tests/test_helpers.py +43 -0
- paseto-3.0.1/tests/test_keys.py +77 -0
- paseto-3.0.1/tests/test_lucidity.py +36 -0
- paseto-3.0.1/tests/test_v2.py +82 -0
- paseto-3.0.1/tests/test_v2_vectors.py +13 -0
- paseto-3.0.1/tests/test_v3.py +123 -0
- paseto-3.0.1/tests/test_v3_vectors.py +12 -0
- paseto-3.0.1/tests/test_v4.py +120 -0
- paseto-3.0.1/tests/test_v4_vectors.py +12 -0
- paseto-3.0.1/tests/v2_raw_vectors.json +167 -0
- paseto-3.0.1/tests/v3_raw_vectors.json +183 -0
- paseto-3.0.1/tests/v4_raw_vectors.json +187 -0
- paseto-3.0.1/tests/vector.py +105 -0
- paseto-2.0.0/PKG-INFO +0 -108
- paseto-2.0.0/README.md +0 -83
- paseto-2.0.0/paseto/__init__.py +0 -7
- paseto-2.0.0/paseto/exceptions.py +0 -22
- paseto-2.0.0/paseto/helpers.py +0 -106
- paseto-2.0.0/paseto/keys/asymmetric_key.py +0 -153
- paseto-2.0.0/paseto/keys/symmetric_key.py +0 -78
- paseto-2.0.0/paseto/protocols/protocol.py +0 -6
- paseto-2.0.0/paseto/protocols/v2.py +0 -188
- paseto-2.0.0/pyproject.toml +0 -27
- {paseto-2.0.0 → paseto-3.0.1}/paseto/keys/__init__.py +0 -0
- {paseto-2.0.0 → paseto-3.0.1}/paseto/protocols/__init__.py +0 -0
paseto-3.0.1/.gitignore
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project are documented here. The format is based
|
|
4
|
+
on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project
|
|
5
|
+
follows [Semantic Versioning](https://semver.org/).
|
|
6
|
+
|
|
7
|
+
## [Unreleased]
|
|
8
|
+
|
|
9
|
+
## [3.0.1] - 2026-12-05
|
|
10
|
+
|
|
11
|
+
The first 3.x release on PyPI. **3.0.0 was tagged on GitHub but never
|
|
12
|
+
published to PyPI**: its release workflow failed before publishing. 3.0.1
|
|
13
|
+
contains everything in the
|
|
14
|
+
[3.0.0 notes](https://github.com/rlittlefield/pypaseto/blob/main/CHANGELOG.md#300---2026-12-05-not-published-to-pypi);
|
|
15
|
+
if you're upgrading from 2.x, read them, including the breaking changes.
|
|
16
|
+
|
|
17
|
+
### Fixed
|
|
18
|
+
- Release workflow: the tag/version check compared against color-coded
|
|
19
|
+
`uv` output, which failed the 3.0.0 release. (No library code changed.)
|
|
20
|
+
|
|
21
|
+
## [3.0.0] - 2026-12-05 (not published to PyPI)
|
|
22
|
+
|
|
23
|
+
### Security
|
|
24
|
+
- Reject non-canonical base64 in tokens (padding, or non-zero trailing bits).
|
|
25
|
+
Previously one valid token could be rewritten into other distinct token
|
|
26
|
+
strings that still authenticated and parsed to the same claims. This
|
|
27
|
+
matters if you deduplicate, cache or revoke tokens by their string value.
|
|
28
|
+
**This is a breaking change** (see below), so it is not backported to 2.x.
|
|
29
|
+
|
|
30
|
+
### Breaking
|
|
31
|
+
- Tokens are decoded strictly, as the PASETO spec requires: base64 padding
|
|
32
|
+
(`=`), non-canonical trailing bits, and characters outside the base64url
|
|
33
|
+
alphabet are rejected with `PasetoException`. Tokens from non-compliant
|
|
34
|
+
libraries that pad their base64 will no longer parse.
|
|
35
|
+
- Python 3.10 or newer is required (3.8 and 3.9 are end-of-life).
|
|
36
|
+
- PASETO v2 emits a `DeprecationWarning`; it will be removed in a future
|
|
37
|
+
major version. Use v4.
|
|
38
|
+
- `pysodium` is replaced by `PyNaCl` and `pendulum` is no longer a
|
|
39
|
+
dependency.
|
|
40
|
+
- Protocol operations now reject keys of the wrong purpose (for example a
|
|
41
|
+
local key passed to `sign`) with `InvalidPurposeException`, and a key for
|
|
42
|
+
the wrong version raises `InvalidVersionException` for every operation.
|
|
43
|
+
- `exp` claims written by `create(exp_seconds=...)` are now always in UTC
|
|
44
|
+
(`+00:00`) instead of the local timezone offset.
|
|
45
|
+
|
|
46
|
+
### Fixed
|
|
47
|
+
- `parse()` on v2 tokens raised `NameError`.
|
|
48
|
+
- `AsymmetricSecretKey` built from a 32-byte Ed25519 seed stored the public
|
|
49
|
+
key instead of the secret key.
|
|
50
|
+
- v3 48-byte secret keys were decoded as little-endian; the spec is
|
|
51
|
+
big-endian.
|
|
52
|
+
- `encode()` / `from_encoded_string()` on all key classes now work.
|
|
53
|
+
- `create()` no longer mutates the claims dict passed to it.
|
|
54
|
+
- Empty payloads no longer fail with "Encryption failed".
|
|
55
|
+
- Truncated tokens raise `PasetoException`.
|
|
56
|
+
- Key `repr()` no longer includes key material.
|
|
57
|
+
- v3: an invalid public key (not a point on P-384) raises `PasetoException`
|
|
58
|
+
instead of a raw `ValueError`. Public keys are now imported as SEC1 points
|
|
59
|
+
by pycryptodome rather than through a hand-built DER prefix.
|
|
60
|
+
- v3: keys on other curves (e.g. a P-256 or Ed25519 PEM) are rejected when
|
|
61
|
+
the key is constructed, with a clear `PasetoException`. Previously they
|
|
62
|
+
were accepted and failed later in `sign`/`verify` with misleading errors
|
|
63
|
+
(or a raw `ValueError`).
|
|
64
|
+
- v3: public keys given as an uncompressed (97-byte) SEC1 point are accepted
|
|
65
|
+
and normalized to the compressed form.
|
|
66
|
+
|
|
67
|
+
### Added
|
|
68
|
+
- Python 3.13 and 3.14 support; macOS is tested again.
|
|
69
|
+
- No system libsodium needed: it is bundled in PyNaCl's wheels.
|
|
70
|
+
- v2/v4 keys can be loaded from PEM/DER; v3 public keys from PEM/DER.
|
|
71
|
+
- Type hints and a `py.typed` marker; `paseto.__version__`.
|
|
72
|
+
- Test vectors synced with paseto-standard/test-vectors, including
|
|
73
|
+
signing from the vector secret keys.
|
|
74
|
+
- **Experimental** support for PASETO v5 and v6 (post-quantum ML-DSA
|
|
75
|
+
`public` tokens) under `paseto.experimental`. Importing it emits a
|
|
76
|
+
`PasetoExperimentalWarning`; it is exempt from semantic versioning and may
|
|
77
|
+
change in any release. `public` tokens need `pip install "paseto[experimental]"`
|
|
78
|
+
(`cryptography>=48`).
|
|
79
|
+
- `paseto.PasetoExperimentalWarning`.
|
|
80
|
+
|
|
81
|
+
## [2.0.0] - 2024-06-04
|
|
82
|
+
- Dropping support for python 3.7
|
|
83
|
+
- Adding support for python 3.11 and 3.12
|
|
84
|
+
- Dependency updates for pendulum, pysodium, pycryptodomex
|
|
85
|
+
|
|
86
|
+
[Unreleased]: https://github.com/rlittlefield/pypaseto/compare/v3.0.1...HEAD
|
|
87
|
+
[3.0.1]: https://github.com/rlittlefield/pypaseto/compare/v3.0.0...v3.0.1
|
|
88
|
+
[3.0.0]: https://github.com/rlittlefield/pypaseto/compare/c2c231d...v3.0.0
|
|
89
|
+
[2.0.0]: https://pypi.org/project/paseto/2.0.0/
|
|
@@ -1,28 +1,6 @@
|
|
|
1
1
|
MIT License
|
|
2
2
|
|
|
3
3
|
Copyright (c) 2018 J. Ryan Littlefield
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
MIT License
|
|
25
|
-
|
|
26
4
|
Copyright (c) 2018 Jim Dabell
|
|
27
5
|
|
|
28
6
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
paseto-3.0.1/PKG-INFO
ADDED
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: paseto
|
|
3
|
+
Version: 3.0.1
|
|
4
|
+
Summary: Platform-Agnostic Security Tokens for Python (PASETO)
|
|
5
|
+
Project-URL: Repository, https://github.com/rlittlefield/pypaseto
|
|
6
|
+
Project-URL: Changelog, https://github.com/rlittlefield/pypaseto/blob/main/CHANGELOG.md
|
|
7
|
+
Project-URL: Issues, https://github.com/rlittlefield/pypaseto/issues
|
|
8
|
+
Author-email: Ryan Littlefield <ryan@ryanlittlefield.com>
|
|
9
|
+
License-Expression: MIT
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Keywords: jwt,paseto,pypaseto,token
|
|
12
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: Operating System :: OS Independent
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
22
|
+
Classifier: Topic :: Security :: Cryptography
|
|
23
|
+
Classifier: Typing :: Typed
|
|
24
|
+
Requires-Python: >=3.10
|
|
25
|
+
Requires-Dist: pycryptodomex>=3.20.0
|
|
26
|
+
Requires-Dist: pynacl>=1.5.0
|
|
27
|
+
Provides-Extra: experimental
|
|
28
|
+
Requires-Dist: cryptography>=48; extra == 'experimental'
|
|
29
|
+
Description-Content-Type: text/markdown
|
|
30
|
+
|
|
31
|
+
PASETO Tokens for Python
|
|
32
|
+
========================
|
|
33
|
+
[](https://pypi.org/project/paseto/)
|
|
34
|
+
[](https://pypi.org/project/paseto/)
|
|
35
|
+
[](https://pypi.org/project/paseto/)
|
|
36
|
+
[](https://github.com/rlittlefield/pypaseto/actions/workflows/main.yml)
|
|
37
|
+
[](https://scorecard.dev/viewer/?uri=github.com/rlittlefield/pypaseto)
|
|
38
|
+
|
|
39
|
+
This is an unofficial implementation of
|
|
40
|
+
[PASETO: Platform-Agnostic Security Tokens](https://paseto.io/) for Python.
|
|
41
|
+
|
|
42
|
+
PASETO versions supported: v2 (deprecated), v3, and v4.
|
|
43
|
+
|
|
44
|
+
**Use v4 for new projects** (or v3 if you need NIST-approved algorithms).
|
|
45
|
+
PASETO v2 is deprecated by the specification; pypaseto still verifies and
|
|
46
|
+
creates v2 tokens but emits a `DeprecationWarning`, and v2 support will be
|
|
47
|
+
removed in a future major release.
|
|
48
|
+
|
|
49
|
+
Installation
|
|
50
|
+
------------
|
|
51
|
+
|
|
52
|
+
pip install paseto
|
|
53
|
+
|
|
54
|
+
Python 3.10 or newer is required. All cryptographic dependencies ship as
|
|
55
|
+
wheels (libsodium is bundled in PyNaCl), so no system libraries are needed.
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
Usage
|
|
59
|
+
-----
|
|
60
|
+
|
|
61
|
+
To create/parse paseto tokens, use the `create`/`parse` functions. These will
|
|
62
|
+
automatically handle encoding/decoding the JSON payload for you, and validate
|
|
63
|
+
claims (currently just the `exp` expiration registered claim).
|
|
64
|
+
|
|
65
|
+
### Local (symmetric, encrypted) tokens
|
|
66
|
+
|
|
67
|
+
```python
|
|
68
|
+
import paseto
|
|
69
|
+
from paseto.keys.symmetric_key import SymmetricKey
|
|
70
|
+
from paseto.protocols.v4 import ProtocolVersion4
|
|
71
|
+
|
|
72
|
+
my_key = SymmetricKey.generate(protocol=ProtocolVersion4)
|
|
73
|
+
|
|
74
|
+
# create a paseto token that expires in 5 minutes (300 seconds)
|
|
75
|
+
token = paseto.create(
|
|
76
|
+
key=my_key,
|
|
77
|
+
purpose="local",
|
|
78
|
+
claims={"my claims": [1, 2, 3]},
|
|
79
|
+
exp_seconds=300,
|
|
80
|
+
)
|
|
81
|
+
|
|
82
|
+
parsed = paseto.parse(
|
|
83
|
+
key=my_key,
|
|
84
|
+
purpose="local",
|
|
85
|
+
token=token,
|
|
86
|
+
)
|
|
87
|
+
print(parsed)
|
|
88
|
+
# {'message': {'exp': '2026-12-05T16:45:00+00:00', 'my claims': [1, 2, 3]}, 'footer': None}
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
### Public (asymmetric, signed) tokens
|
|
92
|
+
|
|
93
|
+
You can also make signed tokens. These are readable by anyone, but can only
|
|
94
|
+
be created by the holder of the secret key, and verified with the public key:
|
|
95
|
+
|
|
96
|
+
```python
|
|
97
|
+
import paseto
|
|
98
|
+
from paseto.keys.asymmetric_key import AsymmetricSecretKey
|
|
99
|
+
from paseto.protocols.v4 import ProtocolVersion4
|
|
100
|
+
|
|
101
|
+
secret_key = AsymmetricSecretKey.generate(protocol=ProtocolVersion4)
|
|
102
|
+
public_key = secret_key.get_public_key()
|
|
103
|
+
|
|
104
|
+
token = paseto.create(
|
|
105
|
+
key=secret_key,
|
|
106
|
+
purpose="public",
|
|
107
|
+
claims={"my claims": [1, 2, 3]},
|
|
108
|
+
exp_seconds=300,
|
|
109
|
+
)
|
|
110
|
+
|
|
111
|
+
parsed = paseto.parse(
|
|
112
|
+
key=public_key,
|
|
113
|
+
purpose="public",
|
|
114
|
+
token=token,
|
|
115
|
+
)
|
|
116
|
+
print(parsed)
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
### Storing and loading keys
|
|
120
|
+
|
|
121
|
+
Keys can be serialized as unpadded base64url and loaded again:
|
|
122
|
+
|
|
123
|
+
```python
|
|
124
|
+
from paseto.keys.asymmetric_key import AsymmetricPublicKey, AsymmetricSecretKey
|
|
125
|
+
from paseto.keys.symmetric_key import SymmetricKey
|
|
126
|
+
from paseto.protocols.v4 import ProtocolVersion4
|
|
127
|
+
|
|
128
|
+
encoded = secret_key.encode()
|
|
129
|
+
secret_key = AsymmetricSecretKey.from_encoded_string(encoded, ProtocolVersion4)
|
|
130
|
+
public_key = AsymmetricPublicKey.from_encoded_string(
|
|
131
|
+
secret_key.get_public_key().encode(), ProtocolVersion4
|
|
132
|
+
)
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
Asymmetric keys can also be constructed from PEM/DER (`AsymmetricSecretKey(pem_bytes,
|
|
136
|
+
ProtocolVersion4)`); v4 secret keys also accept a 32-byte Ed25519 seed.
|
|
137
|
+
|
|
138
|
+
### Footers and claim validation
|
|
139
|
+
|
|
140
|
+
```python
|
|
141
|
+
token = paseto.create(key=my_key, claims={"sub": "alice"}, footer={"kid": "key-1"})
|
|
142
|
+
|
|
143
|
+
parsed = paseto.parse(
|
|
144
|
+
key=my_key,
|
|
145
|
+
token=token,
|
|
146
|
+
required_claims=["sub"], # raise PasetoValidationError if missing
|
|
147
|
+
)
|
|
148
|
+
parsed["footer"] # {'kid': 'key-1'}
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
Expired tokens raise `paseto.PasetoTokenExpired`. Pass `validate=False` to
|
|
152
|
+
skip claim validation (decryption and signature checks always happen).
|
|
153
|
+
All errors derive from `paseto.PasetoException`.
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
Experimental: PASETO v5 and v6 (post-quantum)
|
|
157
|
+
---------------------------------------------
|
|
158
|
+
|
|
159
|
+
pypaseto has **experimental** support for the draft PASETO v5 and v6
|
|
160
|
+
versions, which sign `public` tokens with the post-quantum ML-DSA algorithm
|
|
161
|
+
(FIPS 204):
|
|
162
|
+
|
|
163
|
+
| Version | `local` | `public` |
|
|
164
|
+
| ------- | ------------------------------ | ---------------------------- |
|
|
165
|
+
| v5 | AES-256-CTR + HMAC-SHA384 (as v3) | ML-DSA-87 |
|
|
166
|
+
| v6 | XChaCha20 + BLAKE2b-MAC (as v4) | ML-DSA-44 |
|
|
167
|
+
|
|
168
|
+
> [!WARNING]
|
|
169
|
+
> The v5/v6 specification is not final, and there are no official test
|
|
170
|
+
> vectors yet. Everything under `paseto.experimental` is **exempt from
|
|
171
|
+
> semantic versioning**: its API and token format may change incompatibly in
|
|
172
|
+
> any release, including patch releases. Don't use it for long-lived tokens.
|
|
173
|
+
|
|
174
|
+
Opt in by importing from `paseto.experimental`, which emits a
|
|
175
|
+
`paseto.PasetoExperimentalWarning`. `public` tokens also need the optional
|
|
176
|
+
ML-DSA dependency:
|
|
177
|
+
|
|
178
|
+
pip install "paseto[experimental]"
|
|
179
|
+
|
|
180
|
+
```python
|
|
181
|
+
import warnings
|
|
182
|
+
|
|
183
|
+
import paseto
|
|
184
|
+
|
|
185
|
+
# acknowledge the experimental status (optional; silences the warning)
|
|
186
|
+
warnings.filterwarnings("ignore", category=paseto.PasetoExperimentalWarning)
|
|
187
|
+
|
|
188
|
+
from paseto.experimental.v6 import ProtocolVersion6
|
|
189
|
+
from paseto.keys.asymmetric_key import AsymmetricSecretKey
|
|
190
|
+
|
|
191
|
+
secret_key = AsymmetricSecretKey.generate(protocol=ProtocolVersion6)
|
|
192
|
+
token = paseto.create(key=secret_key, claims={"sub": "alice"}, exp_seconds=300)
|
|
193
|
+
parsed = paseto.parse(key=secret_key.get_public_key(), token=token)
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
v5/v6 secret keys are 32-byte ML-DSA seeds; public keys are the raw ML-DSA
|
|
197
|
+
public keys (2592 bytes for v5, 1312 for v6). v5/v6 tokens are parsed more
|
|
198
|
+
strictly than earlier versions, as their specification requires (for
|
|
199
|
+
example, an empty footer component is rejected).
|
|
200
|
+
|
|
201
|
+
The spec's optional "opaque" token format is not supported yet.
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
Development
|
|
205
|
+
-----------
|
|
206
|
+
|
|
207
|
+
See [CONTRIBUTING.md](CONTRIBUTING.md). Report security issues privately as
|
|
208
|
+
described in [SECURITY.md](SECURITY.md).
|
|
209
|
+
|
|
210
|
+
Changelog
|
|
211
|
+
---------
|
|
212
|
+
|
|
213
|
+
See [CHANGELOG.md](CHANGELOG.md).
|
paseto-3.0.1/README.md
ADDED
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
PASETO Tokens for Python
|
|
2
|
+
========================
|
|
3
|
+
[](https://pypi.org/project/paseto/)
|
|
4
|
+
[](https://pypi.org/project/paseto/)
|
|
5
|
+
[](https://pypi.org/project/paseto/)
|
|
6
|
+
[](https://github.com/rlittlefield/pypaseto/actions/workflows/main.yml)
|
|
7
|
+
[](https://scorecard.dev/viewer/?uri=github.com/rlittlefield/pypaseto)
|
|
8
|
+
|
|
9
|
+
This is an unofficial implementation of
|
|
10
|
+
[PASETO: Platform-Agnostic Security Tokens](https://paseto.io/) for Python.
|
|
11
|
+
|
|
12
|
+
PASETO versions supported: v2 (deprecated), v3, and v4.
|
|
13
|
+
|
|
14
|
+
**Use v4 for new projects** (or v3 if you need NIST-approved algorithms).
|
|
15
|
+
PASETO v2 is deprecated by the specification; pypaseto still verifies and
|
|
16
|
+
creates v2 tokens but emits a `DeprecationWarning`, and v2 support will be
|
|
17
|
+
removed in a future major release.
|
|
18
|
+
|
|
19
|
+
Installation
|
|
20
|
+
------------
|
|
21
|
+
|
|
22
|
+
pip install paseto
|
|
23
|
+
|
|
24
|
+
Python 3.10 or newer is required. All cryptographic dependencies ship as
|
|
25
|
+
wheels (libsodium is bundled in PyNaCl), so no system libraries are needed.
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
Usage
|
|
29
|
+
-----
|
|
30
|
+
|
|
31
|
+
To create/parse paseto tokens, use the `create`/`parse` functions. These will
|
|
32
|
+
automatically handle encoding/decoding the JSON payload for you, and validate
|
|
33
|
+
claims (currently just the `exp` expiration registered claim).
|
|
34
|
+
|
|
35
|
+
### Local (symmetric, encrypted) tokens
|
|
36
|
+
|
|
37
|
+
```python
|
|
38
|
+
import paseto
|
|
39
|
+
from paseto.keys.symmetric_key import SymmetricKey
|
|
40
|
+
from paseto.protocols.v4 import ProtocolVersion4
|
|
41
|
+
|
|
42
|
+
my_key = SymmetricKey.generate(protocol=ProtocolVersion4)
|
|
43
|
+
|
|
44
|
+
# create a paseto token that expires in 5 minutes (300 seconds)
|
|
45
|
+
token = paseto.create(
|
|
46
|
+
key=my_key,
|
|
47
|
+
purpose="local",
|
|
48
|
+
claims={"my claims": [1, 2, 3]},
|
|
49
|
+
exp_seconds=300,
|
|
50
|
+
)
|
|
51
|
+
|
|
52
|
+
parsed = paseto.parse(
|
|
53
|
+
key=my_key,
|
|
54
|
+
purpose="local",
|
|
55
|
+
token=token,
|
|
56
|
+
)
|
|
57
|
+
print(parsed)
|
|
58
|
+
# {'message': {'exp': '2026-12-05T16:45:00+00:00', 'my claims': [1, 2, 3]}, 'footer': None}
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### Public (asymmetric, signed) tokens
|
|
62
|
+
|
|
63
|
+
You can also make signed tokens. These are readable by anyone, but can only
|
|
64
|
+
be created by the holder of the secret key, and verified with the public key:
|
|
65
|
+
|
|
66
|
+
```python
|
|
67
|
+
import paseto
|
|
68
|
+
from paseto.keys.asymmetric_key import AsymmetricSecretKey
|
|
69
|
+
from paseto.protocols.v4 import ProtocolVersion4
|
|
70
|
+
|
|
71
|
+
secret_key = AsymmetricSecretKey.generate(protocol=ProtocolVersion4)
|
|
72
|
+
public_key = secret_key.get_public_key()
|
|
73
|
+
|
|
74
|
+
token = paseto.create(
|
|
75
|
+
key=secret_key,
|
|
76
|
+
purpose="public",
|
|
77
|
+
claims={"my claims": [1, 2, 3]},
|
|
78
|
+
exp_seconds=300,
|
|
79
|
+
)
|
|
80
|
+
|
|
81
|
+
parsed = paseto.parse(
|
|
82
|
+
key=public_key,
|
|
83
|
+
purpose="public",
|
|
84
|
+
token=token,
|
|
85
|
+
)
|
|
86
|
+
print(parsed)
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
### Storing and loading keys
|
|
90
|
+
|
|
91
|
+
Keys can be serialized as unpadded base64url and loaded again:
|
|
92
|
+
|
|
93
|
+
```python
|
|
94
|
+
from paseto.keys.asymmetric_key import AsymmetricPublicKey, AsymmetricSecretKey
|
|
95
|
+
from paseto.keys.symmetric_key import SymmetricKey
|
|
96
|
+
from paseto.protocols.v4 import ProtocolVersion4
|
|
97
|
+
|
|
98
|
+
encoded = secret_key.encode()
|
|
99
|
+
secret_key = AsymmetricSecretKey.from_encoded_string(encoded, ProtocolVersion4)
|
|
100
|
+
public_key = AsymmetricPublicKey.from_encoded_string(
|
|
101
|
+
secret_key.get_public_key().encode(), ProtocolVersion4
|
|
102
|
+
)
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
Asymmetric keys can also be constructed from PEM/DER (`AsymmetricSecretKey(pem_bytes,
|
|
106
|
+
ProtocolVersion4)`); v4 secret keys also accept a 32-byte Ed25519 seed.
|
|
107
|
+
|
|
108
|
+
### Footers and claim validation
|
|
109
|
+
|
|
110
|
+
```python
|
|
111
|
+
token = paseto.create(key=my_key, claims={"sub": "alice"}, footer={"kid": "key-1"})
|
|
112
|
+
|
|
113
|
+
parsed = paseto.parse(
|
|
114
|
+
key=my_key,
|
|
115
|
+
token=token,
|
|
116
|
+
required_claims=["sub"], # raise PasetoValidationError if missing
|
|
117
|
+
)
|
|
118
|
+
parsed["footer"] # {'kid': 'key-1'}
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
Expired tokens raise `paseto.PasetoTokenExpired`. Pass `validate=False` to
|
|
122
|
+
skip claim validation (decryption and signature checks always happen).
|
|
123
|
+
All errors derive from `paseto.PasetoException`.
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
Experimental: PASETO v5 and v6 (post-quantum)
|
|
127
|
+
---------------------------------------------
|
|
128
|
+
|
|
129
|
+
pypaseto has **experimental** support for the draft PASETO v5 and v6
|
|
130
|
+
versions, which sign `public` tokens with the post-quantum ML-DSA algorithm
|
|
131
|
+
(FIPS 204):
|
|
132
|
+
|
|
133
|
+
| Version | `local` | `public` |
|
|
134
|
+
| ------- | ------------------------------ | ---------------------------- |
|
|
135
|
+
| v5 | AES-256-CTR + HMAC-SHA384 (as v3) | ML-DSA-87 |
|
|
136
|
+
| v6 | XChaCha20 + BLAKE2b-MAC (as v4) | ML-DSA-44 |
|
|
137
|
+
|
|
138
|
+
> [!WARNING]
|
|
139
|
+
> The v5/v6 specification is not final, and there are no official test
|
|
140
|
+
> vectors yet. Everything under `paseto.experimental` is **exempt from
|
|
141
|
+
> semantic versioning**: its API and token format may change incompatibly in
|
|
142
|
+
> any release, including patch releases. Don't use it for long-lived tokens.
|
|
143
|
+
|
|
144
|
+
Opt in by importing from `paseto.experimental`, which emits a
|
|
145
|
+
`paseto.PasetoExperimentalWarning`. `public` tokens also need the optional
|
|
146
|
+
ML-DSA dependency:
|
|
147
|
+
|
|
148
|
+
pip install "paseto[experimental]"
|
|
149
|
+
|
|
150
|
+
```python
|
|
151
|
+
import warnings
|
|
152
|
+
|
|
153
|
+
import paseto
|
|
154
|
+
|
|
155
|
+
# acknowledge the experimental status (optional; silences the warning)
|
|
156
|
+
warnings.filterwarnings("ignore", category=paseto.PasetoExperimentalWarning)
|
|
157
|
+
|
|
158
|
+
from paseto.experimental.v6 import ProtocolVersion6
|
|
159
|
+
from paseto.keys.asymmetric_key import AsymmetricSecretKey
|
|
160
|
+
|
|
161
|
+
secret_key = AsymmetricSecretKey.generate(protocol=ProtocolVersion6)
|
|
162
|
+
token = paseto.create(key=secret_key, claims={"sub": "alice"}, exp_seconds=300)
|
|
163
|
+
parsed = paseto.parse(key=secret_key.get_public_key(), token=token)
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
v5/v6 secret keys are 32-byte ML-DSA seeds; public keys are the raw ML-DSA
|
|
167
|
+
public keys (2592 bytes for v5, 1312 for v6). v5/v6 tokens are parsed more
|
|
168
|
+
strictly than earlier versions, as their specification requires (for
|
|
169
|
+
example, an empty footer component is rejected).
|
|
170
|
+
|
|
171
|
+
The spec's optional "opaque" token format is not supported yet.
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
Development
|
|
175
|
+
-----------
|
|
176
|
+
|
|
177
|
+
See [CONTRIBUTING.md](CONTRIBUTING.md). Report security issues privately as
|
|
178
|
+
described in [SECURITY.md](SECURITY.md).
|
|
179
|
+
|
|
180
|
+
Changelog
|
|
181
|
+
---------
|
|
182
|
+
|
|
183
|
+
See [CHANGELOG.md](CHANGELOG.md).
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
from importlib.metadata import PackageNotFoundError, version
|
|
2
|
+
|
|
3
|
+
from paseto.exceptions import (
|
|
4
|
+
InvalidPurposeException,
|
|
5
|
+
InvalidTokenException,
|
|
6
|
+
InvalidVersionException,
|
|
7
|
+
PasetoException,
|
|
8
|
+
PasetoExperimentalWarning,
|
|
9
|
+
PasetoTokenExpired,
|
|
10
|
+
PasetoValidationError,
|
|
11
|
+
)
|
|
12
|
+
from paseto.paseto import create, parse
|
|
13
|
+
|
|
14
|
+
try:
|
|
15
|
+
__version__ = version("paseto")
|
|
16
|
+
except PackageNotFoundError: # pragma: no cover - running from a source tree
|
|
17
|
+
__version__ = "0.0.0"
|
|
18
|
+
|
|
19
|
+
__all__ = [
|
|
20
|
+
"InvalidPurposeException",
|
|
21
|
+
"InvalidTokenException",
|
|
22
|
+
"InvalidVersionException",
|
|
23
|
+
"PasetoException",
|
|
24
|
+
"PasetoExperimentalWarning",
|
|
25
|
+
"PasetoTokenExpired",
|
|
26
|
+
"PasetoValidationError",
|
|
27
|
+
"__version__",
|
|
28
|
+
"create",
|
|
29
|
+
"parse",
|
|
30
|
+
]
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
"""Thin wrappers around the libsodium primitives PASETO v2/v4 need.
|
|
2
|
+
|
|
3
|
+
libsodium is provided by PyNaCl, which ships it inside its wheels, so no
|
|
4
|
+
system library is needed. Raw XChaCha20 (without Poly1305), used by v4.local,
|
|
5
|
+
is not exposed by PyNaCl and comes from pycryptodomex instead.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import nacl.bindings as _nacl
|
|
11
|
+
from Cryptodome.Cipher import ChaCha20
|
|
12
|
+
from nacl.exceptions import CryptoError
|
|
13
|
+
|
|
14
|
+
SIGN_BYTES: int = _nacl.crypto_sign_BYTES # 64
|
|
15
|
+
SIGN_PUBLICKEYBYTES: int = _nacl.crypto_sign_PUBLICKEYBYTES # 32
|
|
16
|
+
SIGN_SECRETKEYBYTES: int = _nacl.crypto_sign_SECRETKEYBYTES # 64
|
|
17
|
+
SIGN_SEEDBYTES: int = _nacl.crypto_sign_SEEDBYTES # 32
|
|
18
|
+
XCHACHA20POLY1305_NPUBBYTES: int = _nacl.crypto_aead_xchacha20poly1305_ietf_NPUBBYTES
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class SignatureError(ValueError):
|
|
22
|
+
"""Raised when a detached signature does not verify."""
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def generichash(data: bytes, key: bytes = b"", outlen: int = 32) -> bytes:
|
|
26
|
+
"""Keyed or unkeyed BLAKE2b."""
|
|
27
|
+
return _nacl.crypto_generichash_blake2b_salt_personal(
|
|
28
|
+
data, digest_size=outlen, key=key
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def xchacha20_xor(data: bytes, nonce: bytes, key: bytes) -> bytes:
|
|
33
|
+
"""Raw XChaCha20 keystream XOR (libsodium's crypto_stream_xchacha20_xor)."""
|
|
34
|
+
if len(nonce) != 24:
|
|
35
|
+
raise ValueError("XChaCha20 nonce must be 24 bytes")
|
|
36
|
+
return ChaCha20.new(key=key, nonce=nonce).encrypt(data)
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def aead_xchacha20poly1305_encrypt(
|
|
40
|
+
message: bytes, ad: bytes, nonce: bytes, key: bytes
|
|
41
|
+
) -> bytes:
|
|
42
|
+
return _nacl.crypto_aead_xchacha20poly1305_ietf_encrypt(message, ad, nonce, key)
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def aead_xchacha20poly1305_decrypt(
|
|
46
|
+
ciphertext: bytes, ad: bytes, nonce: bytes, key: bytes
|
|
47
|
+
) -> bytes:
|
|
48
|
+
try:
|
|
49
|
+
return _nacl.crypto_aead_xchacha20poly1305_ietf_decrypt(
|
|
50
|
+
ciphertext, ad, nonce, key
|
|
51
|
+
)
|
|
52
|
+
except CryptoError as e:
|
|
53
|
+
raise ValueError("decryption failed") from e
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def sign_keypair() -> tuple[bytes, bytes]:
|
|
57
|
+
"""Return a fresh Ed25519 (public_key, secret_key) pair."""
|
|
58
|
+
return _nacl.crypto_sign_keypair()
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def sign_seed_keypair(seed: bytes) -> tuple[bytes, bytes]:
|
|
62
|
+
"""Return the Ed25519 (public_key, secret_key) pair for a 32-byte seed."""
|
|
63
|
+
return _nacl.crypto_sign_seed_keypair(seed)
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def sign_sk_to_pk(secret_key: bytes) -> bytes:
|
|
67
|
+
return _nacl.crypto_sign_ed25519_sk_to_pk(secret_key)
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def sign_detached(message: bytes, secret_key: bytes) -> bytes:
|
|
71
|
+
# crypto_sign returns signature || message; Ed25519 signatures are a fixed
|
|
72
|
+
# 64 bytes, so the detached signature is the prefix.
|
|
73
|
+
return _nacl.crypto_sign(message, secret_key)[:SIGN_BYTES]
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def verify_detached(signature: bytes, message: bytes, public_key: bytes) -> None:
|
|
77
|
+
if len(signature) != SIGN_BYTES or len(public_key) != SIGN_PUBLICKEYBYTES:
|
|
78
|
+
raise SignatureError("invalid signature")
|
|
79
|
+
try:
|
|
80
|
+
_nacl.crypto_sign_open(signature + message, public_key)
|
|
81
|
+
except CryptoError as e:
|
|
82
|
+
raise SignatureError("invalid signature") from e
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
class PasetoException(Exception):
|
|
2
|
+
pass
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class InvalidVersionException(PasetoException):
|
|
6
|
+
pass
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class InvalidPurposeException(PasetoException):
|
|
10
|
+
pass
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class InvalidTokenException(PasetoException):
|
|
14
|
+
pass
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class PasetoValidationError(PasetoException):
|
|
18
|
+
pass
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class PasetoTokenExpired(PasetoValidationError):
|
|
22
|
+
pass
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class PasetoExperimentalWarning(FutureWarning):
|
|
26
|
+
"""
|
|
27
|
+
Emitted when an experimental feature (``paseto.experimental``) is used.
|
|
28
|
+
|
|
29
|
+
Experimental features are exempt from semantic versioning: they may
|
|
30
|
+
change incompatibly, or be removed, in any release. To acknowledge this
|
|
31
|
+
and silence the warning::
|
|
32
|
+
|
|
33
|
+
import warnings
|
|
34
|
+
import paseto
|
|
35
|
+
|
|
36
|
+
warnings.filterwarnings("ignore", category=paseto.PasetoExperimentalWarning)
|
|
37
|
+
"""
|