pyjwt-aws-kms 0.2.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.
- pyjwt_aws_kms-0.2.0/.github/workflows/main.yml +174 -0
- pyjwt_aws_kms-0.2.0/.gitignore +3 -0
- pyjwt_aws_kms-0.2.0/.python-version +1 -0
- pyjwt_aws_kms-0.2.0/LICENSE +21 -0
- pyjwt_aws_kms-0.2.0/PKG-INFO +285 -0
- pyjwt_aws_kms-0.2.0/README.md +270 -0
- pyjwt_aws_kms-0.2.0/pyproject.toml +42 -0
- pyjwt_aws_kms-0.2.0/run-local-kms.sh +5 -0
- pyjwt_aws_kms-0.2.0/src/pyjwt_aws_kms/__init__.py +4 -0
- pyjwt_aws_kms-0.2.0/src/pyjwt_aws_kms/algorithms.py +167 -0
- pyjwt_aws_kms-0.2.0/src/pyjwt_aws_kms/key.py +55 -0
- pyjwt_aws_kms-0.2.0/src/pyjwt_aws_kms/py.typed +0 -0
- pyjwt_aws_kms-0.2.0/tests/__init__.py +0 -0
- pyjwt_aws_kms-0.2.0/tests/fake_kms.py +106 -0
- pyjwt_aws_kms-0.2.0/tests/test_kms.py +231 -0
- pyjwt_aws_kms-0.2.0/tests/test_local_kms.py +167 -0
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
# Installs dependencies with uv, then runs tests (unit tests across an OS/Python matrix,
|
|
2
|
+
# functional tests against Local KMS). Tagged releases are published to PyPI.
|
|
3
|
+
|
|
4
|
+
name: Test
|
|
5
|
+
|
|
6
|
+
on:
|
|
7
|
+
push:
|
|
8
|
+
branches: [ "**" ]
|
|
9
|
+
tags: ["v*.*.*"]
|
|
10
|
+
pull_request:
|
|
11
|
+
branches: [ "main" ]
|
|
12
|
+
|
|
13
|
+
# Least privilege by default; jobs that need more ask for it.
|
|
14
|
+
permissions:
|
|
15
|
+
contents: read
|
|
16
|
+
|
|
17
|
+
jobs:
|
|
18
|
+
test:
|
|
19
|
+
strategy:
|
|
20
|
+
matrix:
|
|
21
|
+
os: ["ubuntu-latest", "windows-latest", "macos-latest", "ubuntu-26.04-arm"]
|
|
22
|
+
python-version: ["3.12", "3.13", "3.14", "3.15"]
|
|
23
|
+
runs-on: ${{ matrix.os }}
|
|
24
|
+
|
|
25
|
+
outputs:
|
|
26
|
+
coverage: ${{ steps.get_coverage.outputs.coverage }}
|
|
27
|
+
|
|
28
|
+
steps:
|
|
29
|
+
- uses: actions/checkout@v4
|
|
30
|
+
|
|
31
|
+
- name: Install uv and Python ${{ matrix.python-version }}
|
|
32
|
+
uses: astral-sh/setup-uv@v6
|
|
33
|
+
with:
|
|
34
|
+
python-version: ${{ matrix.python-version }}
|
|
35
|
+
enable-cache: true
|
|
36
|
+
cache-dependency-glob: pyproject.toml
|
|
37
|
+
|
|
38
|
+
- name: Install dependencies
|
|
39
|
+
run: uv sync
|
|
40
|
+
|
|
41
|
+
- name: Lint and type check
|
|
42
|
+
if: ${{ matrix.os == 'ubuntu-latest' && matrix.python-version == '3.15' }}
|
|
43
|
+
run: |
|
|
44
|
+
uv run ruff check .
|
|
45
|
+
uv run mypy src
|
|
46
|
+
|
|
47
|
+
# Functional tests (marked "integration") need Local KMS; see the functional job.
|
|
48
|
+
- name: Test with pytest
|
|
49
|
+
run: |
|
|
50
|
+
uv run pytest -m "not integration" --cov --cov-report=term --cov-report=json -vv
|
|
51
|
+
|
|
52
|
+
- name: Extract coverage
|
|
53
|
+
id: get_coverage
|
|
54
|
+
if: ${{ matrix.os == 'ubuntu-latest' && matrix.python-version == '3.15' }}
|
|
55
|
+
run: |
|
|
56
|
+
COVERAGE=$(jq -r '.totals.percent_covered | floor' coverage.json)
|
|
57
|
+
echo "Coverage is: ${COVERAGE}%"
|
|
58
|
+
echo "coverage=$COVERAGE" >> "$GITHUB_OUTPUT"
|
|
59
|
+
|
|
60
|
+
# Unit tests against the oldest versions pyproject.toml allows, so the lower bounds stay honest.
|
|
61
|
+
lowest-deps:
|
|
62
|
+
runs-on: ubuntu-latest
|
|
63
|
+
env:
|
|
64
|
+
# Set for the whole job: a plain `uv run` would re-sync to the newest versions.
|
|
65
|
+
UV_RESOLUTION: lowest-direct
|
|
66
|
+
|
|
67
|
+
steps:
|
|
68
|
+
- uses: actions/checkout@v4
|
|
69
|
+
|
|
70
|
+
- name: Install uv and Python
|
|
71
|
+
uses: astral-sh/setup-uv@v6
|
|
72
|
+
with:
|
|
73
|
+
python-version: "3.12"
|
|
74
|
+
|
|
75
|
+
- name: Install lowest direct dependencies
|
|
76
|
+
run: uv sync
|
|
77
|
+
|
|
78
|
+
- name: Test with pytest
|
|
79
|
+
run: uv run pytest -m "not integration" -vv
|
|
80
|
+
|
|
81
|
+
# Container services only run on Linux, so the functional tests get their own job.
|
|
82
|
+
functional:
|
|
83
|
+
runs-on: ubuntu-latest
|
|
84
|
+
|
|
85
|
+
services:
|
|
86
|
+
local-kms:
|
|
87
|
+
image: nsmithuk/local-kms:4.0.0-rc.2
|
|
88
|
+
ports:
|
|
89
|
+
- 8080:8080
|
|
90
|
+
env:
|
|
91
|
+
KMS_SEED_PATH: /seed.yaml
|
|
92
|
+
|
|
93
|
+
steps:
|
|
94
|
+
- uses: actions/checkout@v4
|
|
95
|
+
|
|
96
|
+
- name: Install uv and Python
|
|
97
|
+
uses: astral-sh/setup-uv@v6
|
|
98
|
+
with:
|
|
99
|
+
python-version: "3.15"
|
|
100
|
+
enable-cache: true
|
|
101
|
+
cache-dependency-glob: pyproject.toml
|
|
102
|
+
|
|
103
|
+
- name: Install dependencies
|
|
104
|
+
run: uv sync
|
|
105
|
+
|
|
106
|
+
- name: Wait for Local KMS
|
|
107
|
+
run: |
|
|
108
|
+
for i in $(seq 1 30); do
|
|
109
|
+
if curl -s -o /dev/null http://localhost:8080; then exit 0; fi
|
|
110
|
+
sleep 1
|
|
111
|
+
done
|
|
112
|
+
echo "Local KMS did not start" >&2
|
|
113
|
+
exit 1
|
|
114
|
+
|
|
115
|
+
- name: Functional tests against Local KMS
|
|
116
|
+
run: uv run pytest -m integration -vv
|
|
117
|
+
|
|
118
|
+
publish:
|
|
119
|
+
if: startsWith(github.ref, 'refs/tags/v')
|
|
120
|
+
needs: [test, lowest-deps, functional]
|
|
121
|
+
runs-on: ubuntu-latest
|
|
122
|
+
|
|
123
|
+
steps:
|
|
124
|
+
- uses: actions/checkout@v4
|
|
125
|
+
|
|
126
|
+
- name: Install uv and Python
|
|
127
|
+
uses: astral-sh/setup-uv@v6
|
|
128
|
+
with:
|
|
129
|
+
python-version: "3.15"
|
|
130
|
+
|
|
131
|
+
- name: Check that tag version matches pyproject.toml
|
|
132
|
+
run: |
|
|
133
|
+
TAG_VERSION="${GITHUB_REF#refs/tags/v}"
|
|
134
|
+
FILE_VERSION="$(uv version --short)"
|
|
135
|
+
|
|
136
|
+
echo "Git tag version: $TAG_VERSION"
|
|
137
|
+
echo "pyproject.toml version: $FILE_VERSION"
|
|
138
|
+
|
|
139
|
+
if [ "$TAG_VERSION" != "$FILE_VERSION" ]; then
|
|
140
|
+
echo "❌ Tag version ($TAG_VERSION) does not match pyproject.toml version ($FILE_VERSION)"
|
|
141
|
+
exit 1
|
|
142
|
+
fi
|
|
143
|
+
|
|
144
|
+
- name: Build package
|
|
145
|
+
run: |
|
|
146
|
+
uv build
|
|
147
|
+
|
|
148
|
+
#List out what's in the package
|
|
149
|
+
tar -tzf dist/*.tar.gz
|
|
150
|
+
unzip -l dist/*.whl
|
|
151
|
+
|
|
152
|
+
- name: Publish to PyPI
|
|
153
|
+
# Prevent forks from blindly trying this.
|
|
154
|
+
if: ${{ github.repository == 'nsmithuk/pyjwt-aws-kms' }}
|
|
155
|
+
env:
|
|
156
|
+
UV_PUBLISH_TOKEN: ${{ secrets.PYPI_API_TOKEN }}
|
|
157
|
+
run: |
|
|
158
|
+
if [ -z "$UV_PUBLISH_TOKEN" ]; then
|
|
159
|
+
echo "❌ The PYPI_API_TOKEN secret is not set"
|
|
160
|
+
exit 1
|
|
161
|
+
fi
|
|
162
|
+
uv publish --trusted-publishing never
|
|
163
|
+
|
|
164
|
+
- name: Create coverage badge
|
|
165
|
+
# Prevent forks from blindly trying this. Skipped until a gist is configured.
|
|
166
|
+
if: ${{ github.repository == 'nsmithuk/pyjwt-aws-kms' && vars.COVERAGE_GIST_ID != '' }}
|
|
167
|
+
uses: schneegans/dynamic-badges-action@v1.7.0
|
|
168
|
+
with:
|
|
169
|
+
auth: ${{ secrets.GIST_SECRET }}
|
|
170
|
+
gistID: ${{ vars.COVERAGE_GIST_ID }}
|
|
171
|
+
filename: pyjwt-aws-kms.json
|
|
172
|
+
label: Coverage
|
|
173
|
+
message: "${{ needs.test.outputs.coverage }}%"
|
|
174
|
+
color: green
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
3.12
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Neil Smith
|
|
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.
|
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: pyjwt-aws-kms
|
|
3
|
+
Version: 0.2.0
|
|
4
|
+
Summary: Sign JWTs with PyJWT using asymmetric keys held in AWS KMS
|
|
5
|
+
Project-URL: Homepage, https://github.com/nsmithuk/pyjwt-aws-kms
|
|
6
|
+
Project-URL: Repository, https://github.com/nsmithuk/pyjwt-aws-kms
|
|
7
|
+
Project-URL: Issues, https://github.com/nsmithuk/pyjwt-aws-kms/issues
|
|
8
|
+
Author-email: Neil Smith <neil@nsmith.net>
|
|
9
|
+
License-Expression: MIT
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Requires-Python: >=3.12
|
|
12
|
+
Requires-Dist: boto3>=1.44.6
|
|
13
|
+
Requires-Dist: pyjwt[crypto]>=2.15.1
|
|
14
|
+
Description-Content-Type: text/markdown
|
|
15
|
+
|
|
16
|
+
# pyjwt-aws-kms
|
|
17
|
+
|
|
18
|
+
Sign JSON Web Tokens with [PyJWT](https://pyjwt.readthedocs.io/) using an asymmetric key held in
|
|
19
|
+
[AWS KMS](https://aws.amazon.com/kms/). The private key never leaves KMS: your application sends
|
|
20
|
+
KMS the data to sign and gets a signature back.
|
|
21
|
+
|
|
22
|
+
```python
|
|
23
|
+
import jwt
|
|
24
|
+
from pyjwt_aws_kms import KMSKey, register_algorithms
|
|
25
|
+
|
|
26
|
+
register_algorithms()
|
|
27
|
+
|
|
28
|
+
key = KMSKey("alias/my-signing-key")
|
|
29
|
+
token = jwt.encode(
|
|
30
|
+
{"sub": "user-123"}, key, algorithm="ES256", headers={"kid": key.key_id}
|
|
31
|
+
)
|
|
32
|
+
claims = jwt.decode(token, key, algorithms=["ES256"])
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
- Requires Python 3.12+ and PyJWT 2.15.1+ (with the `cryptography` extra, which is installed for you).
|
|
36
|
+
- Signing calls KMS. Verification does not: it uses the key's public half, fetched once and cached.
|
|
37
|
+
- Asymmetric algorithms only. HMAC (HS256/384/512) is not supported yet.
|
|
38
|
+
|
|
39
|
+
## Install
|
|
40
|
+
|
|
41
|
+
```sh
|
|
42
|
+
pip install pyjwt-aws-kms
|
|
43
|
+
# or
|
|
44
|
+
uv add pyjwt-aws-kms
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## Usage
|
|
48
|
+
|
|
49
|
+
### 1. Register the algorithms
|
|
50
|
+
|
|
51
|
+
Call `register_algorithms()` once at start-up, before you encode or decode:
|
|
52
|
+
|
|
53
|
+
```python
|
|
54
|
+
from pyjwt_aws_kms import register_algorithms
|
|
55
|
+
|
|
56
|
+
register_algorithms()
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
This extends PyJWT's standard algorithms (`RS256`, `ES256`, ...) so they also accept a KMS key.
|
|
60
|
+
Anything else keeps working exactly as before: if you pass a normal key (a PEM string, a
|
|
61
|
+
`cryptography` key object, a `PyJWK`), PyJWT handles it as it always did. You can mix KMS and
|
|
62
|
+
local keys in the same application.
|
|
63
|
+
|
|
64
|
+
### 2. Create a `KMSKey`
|
|
65
|
+
|
|
66
|
+
```python
|
|
67
|
+
from pyjwt_aws_kms import KMSKey
|
|
68
|
+
|
|
69
|
+
key = KMSKey("alias/my-signing-key")
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
The first argument is anything KMS accepts as a `KeyId`: a key ID, a key ARN, an alias name or an
|
|
73
|
+
alias ARN.
|
|
74
|
+
|
|
75
|
+
A `KMSKey` creates a default boto3 KMS client on first use. To control the region, credentials,
|
|
76
|
+
retries or endpoint, pass your own:
|
|
77
|
+
|
|
78
|
+
```python
|
|
79
|
+
import boto3
|
|
80
|
+
|
|
81
|
+
client = boto3.client("kms", region_name="eu-west-2")
|
|
82
|
+
key = KMSKey("alias/my-signing-key", client)
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
Reuse one `KMSKey` per KMS key rather than creating one per request, so the cached public key is
|
|
86
|
+
reused.
|
|
87
|
+
|
|
88
|
+
> **Signing with `ES256` or `ES256K`** needs `kms:GetPublicKey` as well as `kms:Sign`, unless you
|
|
89
|
+
> opt out with `KMSKey(..., check_curve_on_sign=False)`. See
|
|
90
|
+
> [Signing with ES256 and ES256K](#signing-with-es256-and-es256k).
|
|
91
|
+
|
|
92
|
+
### 3. Sign and verify
|
|
93
|
+
|
|
94
|
+
Use the key as the `key` argument of `jwt.encode` and `jwt.decode`, with an `algorithm` that
|
|
95
|
+
matches the KMS key's type (see below):
|
|
96
|
+
|
|
97
|
+
```python
|
|
98
|
+
token = jwt.encode(
|
|
99
|
+
{"sub": "user-123", "iss": "https://example.com"},
|
|
100
|
+
key,
|
|
101
|
+
algorithm="RS256",
|
|
102
|
+
headers={"kid": key.key_id},
|
|
103
|
+
)
|
|
104
|
+
|
|
105
|
+
claims = jwt.decode(token, key, algorithms=["RS256"], issuer="https://example.com")
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
All the usual PyJWT behaviour (claim validation, `options`, `PyJWT` instances) is unchanged.
|
|
109
|
+
|
|
110
|
+
`kid` is not set automatically. If you want a key identifier in the header, pass it as above.
|
|
111
|
+
|
|
112
|
+
### Verifying without KMS
|
|
113
|
+
|
|
114
|
+
Because KMS keys are asymmetric, anyone with the public key can verify your tokens. They don't need
|
|
115
|
+
AWS access or this library. You can fetch the public key once and give it to them (or publish it
|
|
116
|
+
yourself, for example in a JWKS endpoint):
|
|
117
|
+
|
|
118
|
+
```python
|
|
119
|
+
public_key = key.public_key() # a cryptography public key object
|
|
120
|
+
jwt.decode(token, public_key, algorithms=["RS256"])
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
## Supported algorithms
|
|
124
|
+
|
|
125
|
+
The JWT `algorithm` must match the KMS key's key spec:
|
|
126
|
+
|
|
127
|
+
| JWT `alg` | KMS key spec | KMS signing algorithm |
|
|
128
|
+
|-------------------------|---------------------------------------------|------------------------------------------|
|
|
129
|
+
| `RS256` `RS384` `RS512` | `RSA_2048` / `RSA_3072` / `RSA_4096` | `RSASSA_PKCS1_V1_5_SHA_256` / `384` / `512` |
|
|
130
|
+
| `PS256` `PS384` `PS512` | `RSA_2048` / `RSA_3072` / `RSA_4096` | `RSASSA_PSS_SHA_256` / `384` / `512` |
|
|
131
|
+
| `ES256` | `ECC_NIST_P256` | `ECDSA_SHA_256` |
|
|
132
|
+
| `ES384` | `ECC_NIST_P384` | `ECDSA_SHA_384` |
|
|
133
|
+
| `ES512` | `ECC_NIST_P521` | `ECDSA_SHA_512` |
|
|
134
|
+
| `ES256K` | `ECC_SECG_P256K1` | `ECDSA_SHA_256` |
|
|
135
|
+
| `EdDSA` | `ECC_NIST_EDWARDS25519` | `ED25519_SHA_512` |
|
|
136
|
+
|
|
137
|
+
The KMS key's usage must be `SIGN_VERIFY`. Signing with an algorithm that doesn't match the key (for
|
|
138
|
+
example `RS256` with an EC key) makes KMS reject the request, and boto3 raises its usual
|
|
139
|
+
`ClientError`. `ES256` and `ES256K` are checked before KMS is called, as described next.
|
|
140
|
+
|
|
141
|
+
### Signing with ES256 and ES256K
|
|
142
|
+
|
|
143
|
+
`ES256` and `ES256K` use the same KMS signing algorithm (`ECDSA_SHA_256`), so KMS can't tell them
|
|
144
|
+
apart. Given an `ECC_SECG_P256K1` key and `ES256` (or an `ECC_NIST_P256` key and `ES256K`), it would
|
|
145
|
+
sign without complaint, producing a token that correct verifiers reject.
|
|
146
|
+
|
|
147
|
+
To prevent that, the library checks the key's curve before signing with either algorithm, and raises
|
|
148
|
+
`jwt.InvalidKeyError` if it doesn't match. This needs **`kms:GetPublicKey` as well as `kms:Sign`**.
|
|
149
|
+
The public key is fetched once per `KMSKey`, then cached.
|
|
150
|
+
|
|
151
|
+
If you're sure the key matches the algorithm, you can opt out and sign with only `kms:Sign`:
|
|
152
|
+
|
|
153
|
+
```python
|
|
154
|
+
key = KMSKey("alias/my-signing-key", check_curve_on_sign=False)
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
Opting out only affects signing. Verification always checks the key type and curve. The setting has
|
|
158
|
+
no effect on other algorithms, which never fetch the public key to sign.
|
|
159
|
+
|
|
160
|
+
Not supported: HMAC (`HS*`), RSA-OAEP / encryption keys, ML-DSA, and JWK export of a `KMSKey`
|
|
161
|
+
itself (export `key.public_key()` instead). JWK import/export of ordinary keys through the
|
|
162
|
+
registered algorithms keeps working.
|
|
163
|
+
|
|
164
|
+
## Permissions
|
|
165
|
+
|
|
166
|
+
The IAM principal needs, on the KMS key:
|
|
167
|
+
|
|
168
|
+
| To do this | Permission |
|
|
169
|
+
|------------|------------|
|
|
170
|
+
| Sign (`jwt.encode`) with `ES256` or `ES256K` | `kms:Sign` and `kms:GetPublicKey` (called once per `KMSKey`, then cached) |
|
|
171
|
+
| Sign with `ES256` or `ES256K`, using `check_curve_on_sign=False` | `kms:Sign` |
|
|
172
|
+
| Sign with any other algorithm | `kms:Sign` |
|
|
173
|
+
| Verify with the `KMSKey` (`jwt.decode`) | `kms:GetPublicKey` (called once per `KMSKey`, then cached) |
|
|
174
|
+
|
|
175
|
+
Example policy statement:
|
|
176
|
+
|
|
177
|
+
```json
|
|
178
|
+
{
|
|
179
|
+
"Effect": "Allow",
|
|
180
|
+
"Action": ["kms:Sign", "kms:GetPublicKey"],
|
|
181
|
+
"Resource": "arn:aws:kms:eu-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab"
|
|
182
|
+
}
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
A service that only issues tokens needs just `kms:Sign`, unless it uses `ES256` or `ES256K` without
|
|
186
|
+
[opting out of the curve check](#signing-with-es256-and-es256k). One that only verifies needs just
|
|
187
|
+
`kms:GetPublicKey`, or no AWS access at all if you hand it the public key.
|
|
188
|
+
|
|
189
|
+
IAM resource ARNs refer to the key, not its alias, so use the key ARN in policies even if your
|
|
190
|
+
code uses an alias.
|
|
191
|
+
|
|
192
|
+
## How signing works
|
|
193
|
+
|
|
194
|
+
- **Hashing is done locally.** For every algorithm except EdDSA, the library hashes the JWT's
|
|
195
|
+
signing input and sends KMS the digest (`MessageType=DIGEST`). KMS therefore never sees your
|
|
196
|
+
token contents, and there is no size limit on the payload.
|
|
197
|
+
- **EdDSA** is the exception: KMS has no digest mode for Ed25519, so the raw signing input is sent
|
|
198
|
+
(`MessageType=RAW`). KMS limits that to 4096 bytes, so a larger token raises `ValueError`
|
|
199
|
+
before any request is made.
|
|
200
|
+
- **ECDSA** signatures come back from KMS in DER form. They are converted to the fixed-width
|
|
201
|
+
`r || s` form that JWS requires.
|
|
202
|
+
- **Verification** is done locally with `cryptography`, using the public key from
|
|
203
|
+
`GetPublicKey`. It does not call KMS's `Verify` API, so it costs nothing and isn't subject to KMS
|
|
204
|
+
request quotas. The key family and (for ECDSA) curve are checked against the algorithm, so a
|
|
205
|
+
token can't be verified with a key of the wrong type.
|
|
206
|
+
|
|
207
|
+
## Using your own `PyJWT` / `PyJWS` instance
|
|
208
|
+
|
|
209
|
+
By default `register_algorithms()` registers on PyJWT's global registry, which is what
|
|
210
|
+
`jwt.encode` and `jwt.decode` use. To keep it contained, register on a `PyJWS` instance instead:
|
|
211
|
+
|
|
212
|
+
```python
|
|
213
|
+
import jwt
|
|
214
|
+
from pyjwt_aws_kms import register_algorithms
|
|
215
|
+
|
|
216
|
+
jws = jwt.PyJWS()
|
|
217
|
+
register_algorithms(jws)
|
|
218
|
+
|
|
219
|
+
jws.encode(b'{"sub":"x"}', key, algorithm="ES256")
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
Calling `register_algorithms()` more than once is safe, as is calling it while other threads are
|
|
223
|
+
encoding or decoding. On a `PyJWS(algorithms=[...])` instance, only the enabled algorithms are
|
|
224
|
+
wrapped.
|
|
225
|
+
|
|
226
|
+
## Local development against Local KMS
|
|
227
|
+
|
|
228
|
+
[Local KMS](https://github.com/nsmithuk/local-kms) emulates KMS for development and tests. Point
|
|
229
|
+
the boto3 client at it:
|
|
230
|
+
|
|
231
|
+
```python
|
|
232
|
+
client = boto3.client(
|
|
233
|
+
"kms",
|
|
234
|
+
endpoint_url="http://localhost:8080",
|
|
235
|
+
region_name="eu-west-2",
|
|
236
|
+
aws_access_key_id="local", # any value; Local KMS doesn't check them
|
|
237
|
+
aws_secret_access_key="local",
|
|
238
|
+
)
|
|
239
|
+
key = KMSKey("alias/seed_ecc_nist_p256_sign_verify", client)
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
> **Note:** use Local KMS 4.0.0-rc.2 or later. Earlier releases signed `PS256`/`PS384`/`PS512` with
|
|
243
|
+
> the wrong RSA-PSS salt length, so those tokens failed verification in standard libraries.
|
|
244
|
+
|
|
245
|
+
## Development
|
|
246
|
+
|
|
247
|
+
The project uses [uv](https://docs.astral.sh/uv/) and hatchling.
|
|
248
|
+
|
|
249
|
+
```sh
|
|
250
|
+
uv sync # create .venv and install dependencies
|
|
251
|
+
uv run pytest # unit tests (+ functional tests if Local KMS is running)
|
|
252
|
+
uv run ruff check .
|
|
253
|
+
uv run mypy src
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
There are two kinds of test:
|
|
257
|
+
|
|
258
|
+
- **Unit tests** (`tests/test_kms.py`) use an in-process fake KMS client, so they need nothing
|
|
259
|
+
running.
|
|
260
|
+
- **Functional tests** (`tests/test_local_kms.py`) run against a real KMS endpoint and are skipped
|
|
261
|
+
if none is reachable.
|
|
262
|
+
|
|
263
|
+
To run the functional tests against Local KMS, start it with the seed file, then run pytest:
|
|
264
|
+
|
|
265
|
+
```sh
|
|
266
|
+
./run-local-kms.sh # needs Docker; listens on 127.0.0.1:8080
|
|
267
|
+
uv run pytest tests/test_local_kms.py
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
To run the same tests against real AWS KMS, set `REAL_KMS=true` and provide credentials the usual
|
|
271
|
+
way (environment variables, a profile, SSO). The region defaults to `eu-west-2`, or set
|
|
272
|
+
`AWS_REGION`:
|
|
273
|
+
|
|
274
|
+
```sh
|
|
275
|
+
REAL_KMS=true uv run pytest tests/test_local_kms.py
|
|
276
|
+
```
|
|
277
|
+
|
|
278
|
+
The aliases listed in `CASES` in `tests/test_local_kms.py` (for example
|
|
279
|
+
`alias/seed_rsa_2048_sign_verify`) must exist in that account and region. They match the keys
|
|
280
|
+
seeded into the Local KMS image. The identity needs `kms:DescribeKey`, `kms:GetPublicKey` and
|
|
281
|
+
`kms:Sign` on them.
|
|
282
|
+
|
|
283
|
+
## Licence
|
|
284
|
+
|
|
285
|
+
[MIT](LICENSE)
|