JWT validation library for basebox (and maybe others :-) )
Synopsis
This lib was created to provide a straight forward, simple and reliable way to validate JWTs against a set of public keys loaded from a URL. We at basebox use it to validate OpenID Connect ID Tokens (which are JWTs) using the set of public keys published by the OpenID server (e.g. Keycloak).
It provides the following features:
- Download a set of public keys from a URL (a JSON Web Key Set)
- Provide an entry point to update the keyset if necessary
- Parse JWTs and validate them using the key(s) in the downloaded keyset.
And that's it.
Besides, we designed bbjwt to meet the following requirements:
- No unsecure code (openssl crate is not considered unsecure by us :-) )
- Never panic
- No lifetime specifiers in the API
- Asynchronous
- Thread safe
Algorithm Support
The following table shows all signing algorithms supported by bbjwt, along with some info about their usage in JWKs, JWTs etc.
| Name | JOSE "kty" | JOSE "alg" | JOSE "curve" |
|---|---|---|---|
| RSA256 | RSA | RS256 | |
| RSA384 | RSA | RS384 | |
| RSA512 | RSA | RS512 | |
| ES256 | EC | ES256 | P-256 |
| ES256 | EC | ES256 | secp256k1 |
| ES384 | EC | ES384 | P-384 |
| ES512 | EC | ES512 | P-521 (no typo) |
| Ed25519 | OKP | EdDSA | Ed25519 |
| Ed448 | OKP | EdDSA | Ed448 |
Encrypted JWTs are not supported.
BTW, if you have the choice, use Ed25519. It is safe and fast.
Building
bbjwt uses the openssl crate, so OpenSSL development libraries are required to build bbjwt. See the openssl crate's documentation for details.
Why yet another Rust JWT validation lib?
We tried various other Rust JWT libraries, but none worked for us. Problems were complicated APIs, lacking documentation and/or functionality. This is our attempt at doing better :-)
Usage
To validate JWTs, you have to have the issuer's public keys available. Using bbjwt, you can get them either by downloading them from a URL provided by the issuer, or you load them from a local buffer/file.
Download public keys from a URL
See the following example:
use KeyStore;
async
Using public keys from memory
When loading public keys from local file or buffer, you can either load a JWK JSON or a PEM encoded text. JWKs contain all required info to identify the type of key, but for PEM you need to use the function that corresponds to the type of key.
See the following example:
use ;
async
Validating JWTs
JWTs are passed as Base64 encoded strings; for details about this format, see e.g. https://jwt.io.
use ;
async
Copyright (c) 2022 basebox GmbH, all rights reserved.
License: MIT
Made with ❤️ and Emacs :-)