5 releases (stable)

Uses old Rust 2015

4091.0.0 May 16, 2022
4084.0.0 May 16, 2022
1.0.1 Aug 15, 2021
1.0.0 Feb 13, 2019
0.1.0 Sep 4, 2018

#183 in Authentication

Download history 5618/week @ 2023-12-13 2896/week @ 2023-12-20 1374/week @ 2023-12-27 5018/week @ 2024-01-03 5282/week @ 2024-01-10 3198/week @ 2024-01-17 5035/week @ 2024-01-24 3486/week @ 2024-01-31 2902/week @ 2024-02-07 3548/week @ 2024-02-14 3314/week @ 2024-02-21 3584/week @ 2024-02-28 2982/week @ 2024-03-06 2870/week @ 2024-03-13 3700/week @ 2024-03-20 2287/week @ 2024-03-27

12,444 downloads per month
Used in 8 crates (7 directly)

GPL-3.0-or-later

29KB
292 lines

alcoholic_jwt

This is a library for validation of RS256 JWTs using keys from a JWKS. Nothing more, nothing less.

RS256 is the most commonly used asymmetric signature mechanism for JWTs, encountered in for example Google's or Aprila's APIs.

The name of the library stems from the potential side-effects of trying to use the other Rust libraries that are made for similar purposes.

Usage overview

You are retrieving JWTs from some authentication provider that uses RS256 signatures and provides its public keys in JWKS format.

Example for a token that provides the key ID used for signing in the kid claim:

extern crate alcoholic_jwt;

use alcoholic_jwt::{JWKS, Validation, validate, token_kid};

// The function implied here would usually perform an HTTP-GET
// on the JWKS-URL for an authentication provider and deserialize
// the result into the `alcoholic_jwt::JWKS`-struct.
let jwks: JWKS = jwks_fetching_function();

let token: String = some_token_fetching_function();

// Several types of built-in validations are provided:
let validations = vec![
  Validation::Issuer("auth.test.aprila.no".into()),
  Validation::SubjectPresent,
];

// If a JWKS contains multiple keys, the correct KID first
// needs to be fetched from the token headers.
let kid = token_kid(&token)
    .expect("Failed to decode token headers")
    .expect("No 'kid' claim present in token");

let jwk = jwks.find(&kid).expect("Specified key not found in set");

validate(token, jwk, validations).expect("Token validation has failed!");

Under the hood

This library aims to only use trustworthy off-the-shelf components to do the work. Cryptographic operations are provided by the openssl crate, JSON-serialisation is provided by serde_json.

Contributing

This project is developed in the TVL monorepo. To work on it, you can either use a local clone of the entire repository or clone just the alcoholic_jwt subtree:

https://code.tvl.fyi/depot.git:/net/alcoholic_jwt.git

Please follow the TVL contribution guidelines.

Dependencies

~2.4–3.5MB
~78K SLoC