pub struct KeySet {
pub keys: Vec<Key>,
}Expand description
A JSON Web Key Set (RFC 7517 Section 5).
A KeySet contains a collection of keys that can be looked up by various
criteria such as key ID (kid), algorithm, or key use.
§RFC Compliance
Per RFC 7517 Section 5:
“Implementations SHOULD ignore JWKs within a JWK Set that use ‘kty’ (key type) values that are not understood by them”
This implementation follows this guidance by silently skipping keys with
unknown kty values during deserialization rather than failing.
§Examples
Parse a JWKS from JSON:
use jwk_simple::KeySet;
let json = r#"{
"keys": [
{
"kty": "RSA",
"kid": "key-1",
"use": "sig",
"n": "0vx7agoebGcQSuuPiLJXZptN9nndrQmbXEps2aiAFbWhM78LhWx4cbbfAAtVT86zwu1RK7aPFFxuhDR1L6tSoc_BJECPebWKRXjBZCiFV4n3oknjhMstn64tZ_2W-5JsGY4Hc5n9yBXArwl93lqt7_RN5w6Cf0h4QyQ5v-65YGjQR0_FDW2QvzqY368QQMicAtaSqzs8KJZgnYb9c7d0zgdAZHzu6qMQvRL5hajrn1n91CbOpbISD08qNLyrdkt-bFTWhAI4vMQFh6WeZu0fM4lFd2NcRwr3XPksINHaQ-G_xBniIqbw0Ls1jF44-csFCur-kEgU8awapJzKnqDKgw",
"e": "AQAB"
}
]
}"#;
let jwks: KeySet = serde_json::from_str(json).unwrap();
assert_eq!(jwks.len(), 1);Keys with unknown kty values are silently skipped:
use jwk_simple::KeySet;
let json = r#"{
"keys": [
{"kty": "UNKNOWN", "data": "ignored"},
{"kty": "oct", "k": "AQAB"}
]
}"#;
let jwks: KeySet = serde_json::from_str(json).unwrap();
assert_eq!(jwks.len(), 1); // Only the "oct" key is includedFields§
§keys: Vec<Key>The collection of keys.
Implementations§
Source§impl KeySet
impl KeySet
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new empty KeySet.
§Examples
use jwk_simple::KeySet;
let jwks = KeySet::new();
assert!(jwks.is_empty());Sourcepub fn from_keys(keys: Vec<Key>) -> Self
pub fn from_keys(keys: Vec<Key>) -> Self
Creates a KeySet from a vector of keys.
§Examples
use jwk_simple::{KeySet, Key};
let keys = vec![]; // Would contain Key instances
let jwks = KeySet::from_keys(keys);Sourcepub fn add_key(&mut self, key: Key)
pub fn add_key(&mut self, key: Key)
Adds a key to the set.
§Examples
use jwk_simple::{KeySet, Key};
let mut jwks = KeySet::new();
// jwks.add_key(some_jwk);Sourcepub fn remove_by_kid(&mut self, kid: &str) -> Option<Key>
pub fn remove_by_kid(&mut self, kid: &str) -> Option<Key>
Sourcepub fn find_by_kid(&self, kid: &str) -> Option<&Key>
pub fn find_by_kid(&self, kid: &str) -> Option<&Key>
Finds a key by its ID (kid).
§Arguments
kid- The key ID to look for.
§Returns
A reference to the key, or None if not found.
§Examples
use jwk_simple::KeySet;
let json = r#"{"keys": [{"kty": "oct", "kid": "my-key", "k": "AQAB"}]}"#;
let jwks: KeySet = serde_json::from_str(json).unwrap();
let key = jwks.find_by_kid("my-key");
assert!(key.is_some());
let missing = jwks.find_by_kid("unknown");
assert!(missing.is_none());Sourcepub fn find_by_alg(&self, alg: &Algorithm) -> Vec<&Key>
pub fn find_by_alg(&self, alg: &Algorithm) -> Vec<&Key>
Finds all keys with the specified algorithm.
§Arguments
alg- The algorithm to filter by.
§Returns
A vector of references to matching keys.
§Examples
use jwk_simple::{KeySet, Algorithm};
let json = r#"{"keys": [{"kty": "RSA", "alg": "RS256", "n": "AQAB", "e": "AQAB"}]}"#;
let jwks: KeySet = serde_json::from_str(json).unwrap();
let rs256_keys = jwks.find_by_alg(&Algorithm::Rs256);
assert_eq!(rs256_keys.len(), 1);Sourcepub fn find_by_kty(&self, kty: KeyType) -> Vec<&Key>
pub fn find_by_kty(&self, kty: KeyType) -> Vec<&Key>
Sourcepub fn find_by_use(&self, key_use: KeyUse) -> Vec<&Key>
pub fn find_by_use(&self, key_use: KeyUse) -> Vec<&Key>
Finds all keys with the specified use.
§Arguments
key_use- The key use to filter by.
§Returns
A vector of references to matching keys.
§Examples
use jwk_simple::{KeySet, KeyUse};
let json = r#"{"keys": [{"kty": "RSA", "use": "sig", "n": "AQAB", "e": "AQAB"}]}"#;
let jwks: KeySet = serde_json::from_str(json).unwrap();
let signing_keys = jwks.find_by_use(KeyUse::Signature);
assert_eq!(signing_keys.len(), 1);Sourcepub fn signing_keys(&self) -> Vec<&Key>
pub fn signing_keys(&self) -> Vec<&Key>
Finds all signing keys.
A key is considered a signing key if:
- It has
use: "sig", OR - It has no
usespecified (default behavior for signature keys)
§Returns
A vector of references to signing keys.
Sourcepub fn encryption_keys(&self) -> Vec<&Key>
pub fn encryption_keys(&self) -> Vec<&Key>
Sourcepub fn first_signing_key(&self) -> Option<&Key>
pub fn first_signing_key(&self) -> Option<&Key>
Returns the first signing key, if any.
This is a convenience method for cases where only one signing key is expected.
§Examples
use jwk_simple::KeySet;
let json = r#"{"keys": [{"kty": "RSA", "use": "sig", "n": "AQAB", "e": "AQAB"}]}"#;
let jwks: KeySet = serde_json::from_str(json).unwrap();
let key = jwks.first_signing_key().expect("expected a signing key");Sourcepub fn first(&self) -> Option<&Key>
pub fn first(&self) -> Option<&Key>
Returns the first key, if any.
§Examples
use jwk_simple::KeySet;
let jwks = KeySet::new();
assert!(jwks.first().is_none());