Skip to main content

KeySelector

Struct KeySelector 

Source
pub struct KeySelector<'a> { /* private fields */ }
Expand description

Policy-bound strict selector for a KeySet.

Implementations§

Source§

impl<'a> KeySelector<'a>

Source

pub fn select(&self, matcher: KeyMatcher<'_>) -> Result<&'a Key, SelectionError>

Selects exactly one key using strict cryptographic suitability checks.

Conceptually, strict selection applies the same per-key validation layers as Key::validate_for_use, but within a JWKS search and with extra policy rules such as verification allowlists, declared-alg matching, and ambiguity handling.

When kid is present in the matcher, candidate-level validation failures are surfaced with specific diagnostics (AlgorithmMismatch, IntentMismatch, InvalidKey, KeySuitabilityFailed, IncompatibleKeyType) using deterministic precedence.

When kid is not present, candidates that fail per-key checks are skipped and selection resolves by surviving cardinality (AmbiguousSelection / NoMatchingKey).

In kid-less mode, candidate-level mismatch diagnostics are intentionally suppressed. Early policy errors still surface (UnknownAlgorithm, UnknownOperation, OperationAlgorithmMismatch, allowlist failures).

Error precedence is deterministic:

  1. UnknownAlgorithm
  2. UnknownOperation
  3. OperationAlgorithmMismatch
  4. EmptyVerifyAllowlist / AlgorithmNotAllowed (verify only)
  5. Candidate evaluation
  6. If multiple candidates survive: AmbiguousSelection

If kid is present and no candidate survives, the most specific error is returned in this order: AlgorithmMismatch -> IntentMismatch -> InvalidKey -> KeySuitabilityFailed -> IncompatibleKeyType -> NoMatchingKey.

If kid is absent and no candidate survives, candidate-level diagnostics are suppressed and selection returns NoMatchingKey.

If kid is omitted and selection returns NoMatchingKey, use KeySet::find for discovery diagnostics to inspect broad candidates.

§Examples

Verify selection with an explicit allowlist:

use jwk_simple::{Algorithm, KeyMatcher, KeyOperation, KeySet};

let json = r#"{"keys": [
  {"kty": "RSA", "kid": "my-kid", "use": "sig", "alg": "RS256", "n": "0vx7agoebGcQSuuPiLJXZptN9nndrQmbXEps2aiAFbWhM78LhWx4cbbfAAtVT86zwu1RK7aPFFxuhDR1L6tSoc_BJECPebWKRXjBZCiFV4n3oknjhMstn64tZ_2W-5JsGY4Hc5n9yBXArwl93lqt7_RN5w6Cf0h4QyQ5v-65YGjQR0_FDW2QvzqY368QQMicAtaSqzs8KJZgnYb9c7d0zgdAZHzu6qMQvRL5hajrn1n91CbOpbISD08qNLyrdkt-bFTWhAI4vMQFh6WeZu0fM4lFd2NcRwr3XPksINHaQ-G_xBniIqbw0Ls1jF44-csFCur-kEgU8awapJzKnqDKgw", "e": "AQAB"}
]}"#;
let jwks: KeySet = serde_json::from_str(json).unwrap();

let key = jwks
  .selector(&[Algorithm::Rs256, Algorithm::Es256])
  .select(KeyMatcher::new(KeyOperation::Verify, Algorithm::Rs256).with_kid("my-kid"))
  .unwrap();
assert_eq!(key.kid(), Some("my-kid"));

Sign selection (allowlist is not consulted for signing):

use jwk_simple::{Algorithm, KeyMatcher, KeyOperation, KeySet};

let json = r#"{"keys": [
  {"kty": "EC", "kid": "sign-kid", "use": "sig", "alg": "ES256", "crv": "P-256", "x": "MKBCTNIcKUSDii11ySs3526iDZ8AiTo7Tu6KPAqv7D4", "y": "4Etl6SRW2YiLUrN5vfvVHuhp7x8PxltmWWlbbM4IFyM", "d": "870MB6gfuTJ4HtUnUvYMyJpr5eUZNP4Bk43bVdj3eAE"}
]}"#;
let jwks: KeySet = serde_json::from_str(json).unwrap();

let key = jwks
  .selector(&[])
  .select(KeyMatcher::new(KeyOperation::Sign, Algorithm::Es256).with_kid("sign-kid"))
  .unwrap();
assert_eq!(key.kid(), Some("sign-kid"));

Trait Implementations§

Source§

impl<'a> Clone for KeySelector<'a>

Source§

fn clone(&self) -> KeySelector<'a>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a> Debug for KeySelector<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for KeySelector<'a>

§

impl<'a> RefUnwindSafe for KeySelector<'a>

§

impl<'a> Send for KeySelector<'a>

§

impl<'a> Sync for KeySelector<'a>

§

impl<'a> Unpin for KeySelector<'a>

§

impl<'a> UnsafeUnpin for KeySelector<'a>

§

impl<'a> UnwindSafe for KeySelector<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<'a, T, E> AsTaggedExplicit<'a, E> for T
where T: 'a,

Source§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self, E>

Source§

impl<'a, T, E> AsTaggedImplicit<'a, E> for T
where T: 'a,

Source§

fn implicit( self, class: Class, constructed: bool, tag: u32, ) -> TaggedParser<'a, Implicit, Self, E>

Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more