Skip to main content

Key

Struct Key 

Source
pub struct Key { /* private fields */ }
Expand description

A validated key, ready to sign and verify tokens.

The fields are fixed at construction: derived crypto material is cached on first use, so a key that could be mutated would sign with stale material. Build one from a Jwk, from Key::generate, or by parsing with Key::from_str, then use the builders to derive a new key rather than editing an existing one.

Implementations§

Source§

impl Key

Source

pub fn export(&self) -> Jwk

The serializable Jwk behind this key, cloned so editing it can’t reach the original.

The inverse of Jwk::import. Use it to derive a variant: export, edit, import again. Reading a single field needs no clone, since a Key derefs to its Jwk.

Source

pub fn from_str(s: &str) -> Result<Self>

Parse a key from a string, auto-detecting JSON or base64url encoding.

Source

pub fn from_file<P: AsRef<StdPath>>(path: P) -> Result<Self>

Load a key from a file, auto-detecting JSON or base64url encoding.

Source

pub fn to_str(&self) -> Result<String>

Encode the key as base64url-encoded JSON.

Source

pub fn to_file<P: AsRef<StdPath>>(&self, path: P) -> Result<()>

Write the key to a file as base64url-encoded JSON.

Source

pub fn to_public(&self) -> Result<Self>

Derive a verify-only copy of this key, dropping the private material.

Fails for symmetric (oct) keys, which have no public half, and for a key that cannot verify in the first place.

Source

pub fn verify(&self, token: &str) -> Result<Claims>

Verify a token’s signature with this key and return its claims.

Rejects an expired token (the exp claim) and one that grants nothing. Scoping the claims to a connection path is a separate step; see Claims::authorize.

Source

pub fn sign(&self, payload: &Claims) -> Result<String>

Sign the claims with this key, returning the encoded token.

Source

pub fn generate(algorithm: Algorithm, id: Option<KeyId>) -> Result<Self>

Generate a key pair for the given algorithm, returning the private and public keys.

Source

pub fn with_scope(self, scope: Scope) -> Result<Self>

Derive a key with an authorization scope attached, capping what its tokens may grant.

The scope is validated here, and it is the only way to set one, so a key can never carry a scope that permits nothing.

Source

pub fn with_operations( self, operations: impl IntoIterator<Item = KeyOperation>, ) -> Self

Derive a key restricted to the given operations.

Trait Implementations§

Source§

impl Clone for Key

Source§

fn clone(&self) -> Key

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Key

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Deref for Key

Read-only access to the underlying Jwk fields (key.algorithm, key.kid, …).

Deliberately no DerefMut: handing out &mut Jwk would let a caller change the algorithm or key material behind the cached crypto material, which is the bug this split exists to prevent.

Source§

type Target = Jwk

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<'de> Deserialize<'de> for Key

Source§

fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl From<&Key> for Jwk

Source§

fn from(key: &Key) -> Self

Converts to this type from the input type.
Source§

impl Serialize for Key

Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl TryFrom<Jwk> for Key

Source§

type Error = Error

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

fn try_from(jwk: Jwk) -> Result<Self>

Performs the conversion.

Auto Trait Implementations§

§

impl !Freeze for Key

§

impl RefUnwindSafe for Key

§

impl Send for Key

§

impl Sync for Key

§

impl Unpin for Key

§

impl UnsafeUnpin for Key

§

impl UnwindSafe for Key

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<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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
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