1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
//! A helper library for working with JWT's for Okta.
//!
//! The purpose of this library is to help with the
//! verification of access and ID tokens issued by Okta.
//! See [`Verifier`] for more examples, and a
//! [tide](https://github.com/http-rs/tide) middleware
//! implementation in the repository under the examples directory.
//!
//! ### Minimal example
//!
//! ```no_run
//! use okta_jwt_verifier::{Verifier, DefaultClaims};
//!
//! #[async_std::main]
//! async fn main() -> anyhow::Result<()> {
//! let token = "token";
//! let issuer = "https://your.domain/oauth2/default";
//!
//! Verifier::new(&issuer)
//! .await?
//! .verify::<DefaultClaims>(&token)
//! .await?;
//! Ok(())
//! }
//!```
#![forbid(unsafe_code, future_incompatible)]
#![deny(
missing_docs,
missing_debug_implementations,
missing_copy_implementations,
nonstandard_style,
unused_qualifications
)]
#[cfg(not(any(feature = "client-surf", feature = "client-reqwest")))]
compile_error!("Either feature \"client-surf\" or \"client-reqwest\" must be enabled for this crate.");
#[cfg(all(feature = "client-surf", feature = "client-reqwest"))]
compile_error!("Only either feature \"client-surf\" or \"client-reqwest\" must be enabled for this crate, not both.");
#[cfg(all(feature = "cache-surf", not(feature = "client-surf")))]
compile_error!(
"Feature \"cache-surf\" requires that \"client-surf\" be enabled."
);
#[cfg(all(feature = "cache-reqwest", not(feature = "client-reqwest")))]
compile_error!(
"Feature \"cache-reqwest\" requires that \"client-reqwest\" be enabled."
);
use std::collections::{HashMap, HashSet};
use anyhow::{bail, Result};
use jsonwebkey::JsonWebKey;
use jsonwebtoken::{TokenData, Validation};
use serde::{de::DeserializeOwned, Deserialize, Serialize};
#[cfg(feature = "cache-surf")]
use http_cache_surf::{
CACacheManager, Cache, CacheMode, HttpCache, HttpCacheOptions,
};
#[cfg(feature = "cache-reqwest")]
use http_cache_reqwest::{
CACacheManager, Cache, CacheMode, HttpCache, HttpCacheOptions,
};
const DEFAULT_ENDPOINT: &str = "/v1/keys";
/// Describes the default claims inside a decoded token
#[derive(Debug, Serialize, Deserialize)]
pub struct DefaultClaims {
/// The Issuer Identifier of the response.
/// This value is the unique identifier for the Authorization Server instance.
pub iss: String,
/// The subject of the token.
pub sub: String,
/// Array of scopes that are granted to this access token.
pub scp: Option<Vec<String>>,
/// Client ID of the client that requested the access token.
pub cid: Option<String>,
/// A unique identifier for the user.
/// It isn't included in the access token if there is no user bound to it.
pub uid: Option<String>,
/// The time the access token expires, represented in Unix time (seconds).
pub exp: u64,
/// The time the access token was issued, represented in Unix time (seconds).
pub iat: u64,
}
// Describes the key retrieved from upstream
#[derive(Clone, Debug, Serialize, Deserialize)]
struct Jwk {
// The "kty" (key type) parameter identifies the cryptographic algorithm
// family used with the key, such as "RSA" or "EC".
kty: String,
// The "alg" (algorithm) parameter identifies the algorithm intended for
// use with the key.
alg: String,
// The "kid" (key ID) parameter is used to match a specific key. This
// is used, for instance, to choose among a set of keys within a Jwk Set
// during key rollover. The structure of the "kid" value is
// unspecified. When "kid" values are used within a Jwk Set, different
// keys within the Jwk Set SHOULD use distinct "kid" values.
kid: String,
// The "use" (public key use) parameter identifies the intended use of
// the public key. The "use" parameter is employed to indicate whether
// a public key is used for encrypting data or verifying the signature
// on data.
#[serde(rename = "use")]
uses: String,
// RSA public exponent is used on signed / encoded data to decode the original value
e: String,
// RSA modulus is the product of two prime numbers used to generate the key pair
n: String,
}
// Container for keys
#[derive(Clone, Debug, Serialize, Deserialize)]
struct Jwks {
inner: HashMap<String, Jwk>,
}
// Describes issuer keys response
#[derive(Debug, Deserialize)]
struct KeyResponse {
keys: Vec<Jwk>,
}
// Needed for the cid verification workaround
#[derive(Debug, Serialize, Deserialize)]
struct ClientId {
cid: String,
}
impl Jwks {
// Attempts to retrieve a key by given id
pub fn where_id(&self, kid: &str) -> Option<&Jwk> {
self.inner.get(kid)
}
}
/// Describes optional config when creating a new Verifier
#[derive(Debug)]
pub struct Config {
/// The endpoint to retrieve json web keys from
pub keys_endpoint: Option<String>,
}
impl Default for Config {
fn default() -> Self {
Self { keys_endpoint: Some(DEFAULT_ENDPOINT.into()) }
}
}
/// Attempts to retrieve the keys from an Okta issuer,
/// decode and verify a given access/ID token, and
/// deserialize the requested claims.
#[derive(Debug, Clone)]
pub struct Verifier {
issuer: String,
cid: Option<String>,
leeway: Option<u64>,
aud: Option<HashSet<String>>,
keys: Jwks,
}
impl Verifier {
/// `new` constructs an instance of Verifier and attempts
/// to retrieve the keys from the specified issuer.
pub async fn new(issuer: &str) -> Result<Self> {
let keys = get(issuer, DEFAULT_ENDPOINT).await?;
Ok(Self {
issuer: issuer.to_string(),
cid: None,
leeway: None,
aud: None,
keys,
})
}
/// `configure` constructs an instance of Verifier and attempts
/// to retrieve the keys from the specified issuer while specifying extra config.
pub async fn new_with_config(issuer: &str, config: Config) -> Result<Self> {
let mut endpoint = DEFAULT_ENDPOINT.to_owned();
if let Some(keys_endpoint) = config.keys_endpoint {
endpoint = keys_endpoint
}
let keys = get(issuer, &endpoint).await?;
Ok(Self {
issuer: issuer.to_string(),
cid: None,
leeway: None,
aud: None,
keys,
})
}
/// `verify` will attempt to validate a passed access
/// or ID token. Upon a successful validation it will then
/// attempt to deserialize the requested claims. A [`DefaultClaims`]
/// struct has been provided for use or to serve as an example
/// for constructing a custom claim struct.
///
/// ```no_run
/// use okta_jwt_verifier::{Verifier, DefaultClaims};
///
/// #[async_std::main]
/// async fn main() -> anyhow::Result<()> {
/// let token = "token";
/// let issuer = "https://your.domain/oauth2/default";
///
/// Verifier::new(&issuer)
/// .await?
/// .verify::<DefaultClaims>(&token)
/// .await?;
/// Ok(())
/// }
///```
pub async fn verify<T>(&self, token: &str) -> Result<TokenData<T>>
where
T: DeserializeOwned,
{
let kid: String = self.key_id(token)?;
let jwk: Option<&Jwk> = self.keys.where_id(&kid);
match jwk {
Some(key_jwk) => self.decode::<T>(token, key_jwk).await,
None => bail!("No matching key found!"),
}
}
/// `client_id` can be used to require cid claim verification.
///
/// ```no_run
/// use okta_jwt_verifier::{Verifier, DefaultClaims};
///
/// #[async_std::main]
/// async fn main() -> anyhow::Result<()> {
/// let token = "token";
/// let issuer = "https://your.domain/oauth2/default";
///
/// Verifier::new(&issuer)
/// .await?
/// .client_id("Bl3hStrINgiD")
/// .verify::<DefaultClaims>(&token)
/// .await?;
/// Ok(())
/// }
///```
pub fn client_id(mut self, cid: &str) -> Self {
self.cid = Some(cid.to_string());
self
}
/// `audience` is for setting multiple aud values
/// to check against.
///
/// ```no_run
/// use okta_jwt_verifier::{Verifier, DefaultClaims};
/// use std::collections::HashSet;
///
/// #[async_std::main]
/// async fn main() -> anyhow::Result<()> {
/// let token = "token";
/// let issuer = "https://your.domain/oauth2/default";
/// let mut aud = HashSet::new();
/// aud.insert("api://default".to_string());
/// aud.insert("api://admin".to_string());
///
/// Verifier::new(&issuer)
/// .await?
/// .audience(aud)
/// .verify::<DefaultClaims>(&token)
/// .await?;
/// Ok(())
/// }
///```
pub fn audience(mut self, audience: HashSet<String>) -> Self {
self.aud = Some(audience);
self
}
/// `add_audience` helps to make adding a single
/// aud entry easier.
///
/// ```no_run
/// use okta_jwt_verifier::{Verifier, DefaultClaims};
///
/// #[async_std::main]
/// async fn main() -> anyhow::Result<()> {
/// let token = "token";
/// let issuer = "https://your.domain/oauth2/default";
///
/// Verifier::new(&issuer)
/// .await?
/// .add_audience("api://default")
/// .verify::<DefaultClaims>(&token)
/// .await?;
/// Ok(())
/// }
///```
pub fn add_audience(mut self, audience: &str) -> Self {
if let Some(mut a) = self.aud.clone() {
a.insert(audience.to_string());
} else {
let mut a = HashSet::new();
a.insert(audience.to_string());
self.aud = Some(a);
}
self
}
/// `leeway` is for overriding the default leeway
/// of 120 seconds, this is to help deal with clock skew.
///
/// ```no_run
/// use okta_jwt_verifier::{Verifier, DefaultClaims};
///
/// #[async_std::main]
/// async fn main() -> anyhow::Result<()> {
/// let token = "token";
/// let issuer = "https://your.domain/oauth2/default";
///
/// Verifier::new(&issuer)
/// .await?
/// .leeway(60)
/// .verify::<DefaultClaims>(&token)
/// .await?;
/// Ok(())
/// }
///```
pub fn leeway(mut self, leeway: u64) -> Self {
self.leeway = Some(leeway);
self
}
// Attempts to retrieve a key id for a given token
fn key_id(&self, token: &str) -> Result<String> {
let header = jsonwebtoken::decode_header(token)?;
if header.kid.is_some() {
Ok(header.kid.unwrap())
} else {
bail!("No key id found!")
}
}
// Attempts to decode the passed token and deserialize the claims
async fn decode<T>(
&self,
token: &str,
key_jwk: &Jwk,
) -> Result<TokenData<T>>
where
T: DeserializeOwned,
{
let key: JsonWebKey = serde_json::to_string(key_jwk)?.parse()?;
let mut validation = Validation::new(jsonwebtoken::Algorithm::RS256);
if let Some(cid) = &self.cid {
// This isn't ideal but what we have to do for now
let cid_tdata = jsonwebtoken::decode::<ClientId>(
token,
&key.key.to_decoding_key(),
&validation,
)?;
if &cid_tdata.claims.cid != cid {
bail!("client_id validation failed!")
}
}
if let Some(secs) = self.leeway {
validation.leeway = secs;
} else {
// default PT2M
validation.leeway = 120;
}
validation.aud = self.aud.clone();
let mut iss = HashSet::new();
iss.insert(self.issuer.clone());
validation.iss = Some(iss);
let tdata = jsonwebtoken::decode::<T>(
token,
&key.key.to_decoding_key(),
&validation,
)?;
Ok(tdata)
}
}
// Attempts to retrieve the keys from the issuer
async fn get(issuer: &str, keys_endpoint: &str) -> Result<Jwks> {
let url = format!(
"{issuer}{keys_endpoint}",
issuer = &issuer,
keys_endpoint = &keys_endpoint
);
let keys = remote_fetch(&url).await?;
let mut keymap = Jwks { inner: HashMap::new() };
for key in keys {
keymap.inner.insert(key.kid.clone(), key);
}
Ok(keymap)
}
// Builds a default surf client
#[cfg(all(feature = "client-surf", not(feature = "cache-surf")))]
fn build_surf_client() -> surf::Client {
surf::Client::new()
}
// Builds a surf client configured to use a disk cache
#[cfg(all(feature = "client-surf", feature = "cache-surf"))]
fn build_surf_client() -> surf::Client {
surf::Client::new().with(Cache(HttpCache {
mode: CacheMode::Default,
manager: CACacheManager::default(),
options: HttpCacheOptions::default(),
}))
}
#[cfg(feature = "client-surf")]
async fn remote_fetch(url: &str) -> Result<Vec<Jwk>> {
let req = surf::get(url);
let client = build_surf_client();
let mut res = match client.send(req).await {
Ok(r) => r,
Err(e) => {
bail!(e)
}
};
let KeyResponse { keys } = match res.body_json().await {
Ok(k) => k,
Err(e) => {
bail!(e)
}
};
Ok(keys)
}
// Builds a default reqwest client
#[cfg(all(feature = "client-reqwest", not(feature = "cache-reqwest")))]
fn build_reqwest_client() -> reqwest_middleware::ClientWithMiddleware {
reqwest_middleware::ClientBuilder::new(reqwest::Client::new()).build()
}
// Builds a reqwest client configured to use a disk cache
#[cfg(all(feature = "client-reqwest", feature = "cache-reqwest"))]
fn build_reqwest_client() -> reqwest_middleware::ClientWithMiddleware {
reqwest_middleware::ClientBuilder::new(reqwest::Client::new())
.with(Cache(HttpCache {
mode: CacheMode::Default,
manager: CACacheManager::default(),
options: HttpCacheOptions::default(),
}))
.build()
}
#[cfg(feature = "client-reqwest")]
async fn remote_fetch(url: &str) -> Result<Vec<Jwk>> {
let client = build_reqwest_client();
let res = client.get(url).send().await?;
let KeyResponse { keys } = res.json().await?;
Ok(keys)
}
#[cfg(test)]
mod tests {
use super::*;
use jwt_simple::prelude::*;
#[cfg(feature = "client-surf")]
use async_std::test as async_test;
#[cfg(feature = "client-reqwest")]
use tokio::test as async_test;
#[derive(Debug, serde::Serialize)]
struct Res {
keys: Vec<Jwk>,
}
// Pulled test data from https://github.com/jedisct1/rust-jwt-simple/blob/master/src/lib.rs
const RSA_KP_PEM: &str = r"
-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEAyqq0N5u8Jvl+BLH2VMP/NAv/zY9T8mSq0V2Gk5Ql5H1a+4qi
3viorUXG3AvIEEccpLsW85ps5+I9itp74jllRjA5HG5smbb+Oym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9cDJTt5cRs51V7pKnVEr6sy0uvDhe
u+G091HuhwY9ak0WNtPwfJ8CgYEAuRdoyZQQso7x/Bj0tiHGW7EOB2n+LRiErj6g
odspmNIH8zrtHXF9bnEHT++VCDpSs34ztuZpywnHS2SBoHH4HD0MJlszksbqbbDM
1bk3+1bUIlEF/Hyk1jljn3QTB0tJ4y1dwweaH9NvVn7DENW9cr/aePGnJwA4Lq3G
fq/IPlUCgYAuqgJQ4ztOq0EaB75xgqtErBM57A/+lMWS9eD/euzCEO5UzWVaiIJ+
nNDmx/jvSrxA1Ih8TEHjzv4ezLFYpaJrTst4Mjhtx+csXRJU9a2W6HMXJ4Kdn8rk
PBziuVURslNyLdlFsFlm/kfvX+4Cxrbb+pAGETtRTgmAoCDbvuDGRQ==
-----END RSA PRIVATE KEY-----
";
const KEY_ID: &str = "12345";
const RSA_MOD: &str = r"yqq0N5u8Jvl-BLH2VMP_NAv_zY9T8mSq0V2Gk5Ql5H1a-4qi3viorUXG3AvIEEccpLsW85ps5-I9itp74jllRjA5HG5smbb-Oym0m2Hovfj6qP_1m1drQg8oth6tNmupNqVzlGGWZLsSCBLuMa3pFaPhoxl9lGU3XJIQ1_evMkOb98I3hHb4ELn3WGtNlAVkbP20R8sSii_zFjPqrG_NbSPLyAl1ctbG2d8RllQF1uRIqYQj85yx73hqQCMpYWU3d9QzpkLf_C35_79qNnSKa3t0cyDKinOY7JGIwh8DWAa4pfEzgg56yLcilYSSohXeaQV0nR8-rm9J8GUYXjPK7w";
#[async_test]
async fn can_verify_token() -> Result<()> {
let mut server = mockito::Server::new();
let key_pair = RS256KeyPair::from_pem(RSA_KP_PEM)?.with_key_id(KEY_ID);
let jsonwk = Jwk {
kty: "RSA".to_string(),
alg: "RS256".to_string(),
kid: KEY_ID.to_string(),
uses: "sig".to_string(),
e: "AQAB".to_string(),
n: RSA_MOD.to_string(),
};
let claims = Claims::create(Duration::from_hours(2))
.with_issuer(server.url())
.with_subject("test");
let token = key_pair.sign(claims)?;
let res = Res { keys: vec![jsonwk] };
let m = server
.mock("GET", DEFAULT_ENDPOINT)
.with_status(200)
.with_body(serde_json::to_string(&res)?)
.create();
let verifier = Verifier::new(&server.url()).await?;
m.assert();
verifier.verify::<DefaultClaims>(&token).await?;
Ok(())
}
#[async_test]
async fn can_verify_token_with_config() -> Result<()> {
let mut server = mockito::Server::new();
let key_pair = RS256KeyPair::from_pem(RSA_KP_PEM)?.with_key_id(KEY_ID);
let jsonwk = Jwk {
kty: "RSA".to_string(),
alg: "RS256".to_string(),
kid: KEY_ID.to_string(),
uses: "sig".to_string(),
e: "AQAB".to_string(),
n: RSA_MOD.to_string(),
};
let config: Config =
Config { keys_endpoint: Some("/oauth2/v1/keys".to_owned()) };
let claims = Claims::create(Duration::from_hours(2))
.with_issuer(server.url())
.with_subject("test");
let token = key_pair.sign(claims)?;
let res = Res { keys: vec![jsonwk] };
let m = server
.mock("GET", "/oauth2/v1/keys")
.with_status(200)
.with_body(serde_json::to_string(&res)?)
.create();
let verifier = Verifier::new_with_config(&server.url(), config).await?;
m.assert();
verifier.verify::<DefaultClaims>(&token).await?;
Ok(())
}
#[async_test]
async fn can_verify_token_with_empty_config() -> Result<()> {
let mut server = mockito::Server::new();
let key_pair = RS256KeyPair::from_pem(RSA_KP_PEM)?.with_key_id(KEY_ID);
let jsonwk = Jwk {
kty: "RSA".to_string(),
alg: "RS256".to_string(),
kid: KEY_ID.to_string(),
uses: "sig".to_string(),
e: "AQAB".to_string(),
n: RSA_MOD.to_string(),
};
let config: Config = Config::default();
let claims = Claims::create(Duration::from_hours(2))
.with_issuer(server.url())
.with_subject("test");
let token = key_pair.sign(claims)?;
let res = Res { keys: vec![jsonwk] };
let m = server
.mock("GET", DEFAULT_ENDPOINT)
.with_status(200)
.with_body(serde_json::to_string(&res)?)
.create();
let verifier = Verifier::new_with_config(&server.url(), config).await?;
m.assert();
verifier.verify::<DefaultClaims>(&token).await?;
Ok(())
}
}