0
0
mirror of https://github.com/tursodatabase/libsql.git synced 2025-06-08 11:54:16 +00:00
Files
.cargo
.config
.github
bindings
bottomless
bottomless-cli
docker-compose
docs
libsql
libsql-ffi
libsql-hrana
libsql-replication
libsql-server
assets
perf
scripts
gen_certs.py
gen_jwt.py
gen_jwt_test_assets.py
install-deps.sh
prometheus_docker.yml
run_prometheus_docker.sh
src
tests
Cargo.toml
README.md
build.rs
local-test-envs
output.sql
libsql-shell
libsql-sqlite3
libsql-storage
libsql-storage-server
libsql-sys
libsql-wal
tools
vendored
xtask
.dockerignore
.env
.gitignore
.gitmodules
CODE_OF_CONDUCT.md
CONTRIBUTING.md
Cargo.lock
Cargo.toml
Dockerfile
Dockerfile.dev
LICENSE.md
README-libsql.md
README.md
docker-entrypoint.sh
docker-wrapper.sh
fly.toml
rust-toolchain.toml
libsql/libsql-server/scripts/gen_jwt_test_assets.py
David McCullars 7a16a8d1c2 Support multiple jwt keys for authentication ()
* Add auth::parsers::tests::parse_jwt_key* tests

* Support multiple JWT keys

* Get rid of the JWT key(s) enum and use Vec

* Add gen_jwt_test_assets.py script for generating example JWTs
2024-06-11 11:09:40 +00:00

53 lines
1.9 KiB
Python
Executable File

#!/usr/bin/env python3
"""utility that generates Ed25519 key and a JWT for testing
the public key is stored in jwt_key.pem (in PEM format) and jwt_key.base64 (raw
base64 format) and the JWT is printed to stdout
"""
import base64
import datetime
import jwt
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PrivateKey
def update_example(name, namespaces):
privkey = Ed25519PrivateKey.generate()
pubkey = privkey.public_key()
pubkey_pem = pubkey.public_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PublicFormat.SubjectPublicKeyInfo,
)
pubkey_base64 = base64.b64encode(
pubkey.public_bytes(
encoding=serialization.Encoding.Raw,
format=serialization.PublicFormat.Raw,
),
altchars=b"-_",
)
while pubkey_base64[-1] == ord("="):
pubkey_base64 = pubkey_base64[:-1]
privkey_pem = privkey.private_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PrivateFormat.PKCS8,
encryption_algorithm=serialization.NoEncryption(),
)
exp = datetime.datetime.now(datetime.timezone.utc) + datetime.timedelta(days=100_000)
claims = {
"p": { "ro": { "ns": namespaces } },
"exp": int(exp.timestamp()),
}
token = jwt.encode(claims, privkey_pem, "EdDSA")
open(f"libsql-server/assets/test/auth/{name}.key", "wb").write(privkey_pem)
open(f"libsql-server/assets/test/auth/{name}.pem", "wb").write(pubkey_pem)
open(f"libsql-server/assets/test/auth/{name}.jwt", "wb").write(token.encode())
open(f"libsql-server/assets/test/auth/combined123.pem", "ab").write(pubkey_pem)
open(f"libsql-server/assets/test/auth/combined123.pem", "wb").write("".encode())
update_example("example1", ["example1a", "example1b", "example1c"])
update_example("example2", ["example2d"])
update_example("example3", ["example3e", "example3f"])