Skip to main content

KeySigner

Struct KeySigner 

Source
pub struct KeySigner<S> { /* private fields */ }
Expand description

A Signer over one key of a KeyStore.

S is the handle the signer holds the store by: a reference (&InMemoryKeyStore, &dyn KeyStore) for a signer that lives next to the store, or an Arc<dyn KeyStore> for one that outlives the scope it was made in, moved into a spawned task or kept in shared application state. Every such handle implements KeyStore itself.

The signer looks the key up once, when it is created, and keeps the public Jwk so that a caller building a DID document or a JWS header never round-trips to the store for it (KeySigner::public_jwk). A caller that already has the public key, for instance stored next to the DID document it published, skips that lookup with KeySigner::from_public_jwk. Signing goes to the store on every call.

Signer::kid is the key id, unless KeySigner::with_kid set another value, such as the DID URL of the verification method the key backs.

§Examples

use identity_kit::jose::{CompactJws, DefaultVerifier, JwsAlgorithm, JwsHeader, KeyType, encode_compact};
use identity_kit::keys::{InMemoryKeyStore, KeySigner, KeySpec, KeyStore};

let store = InMemoryKeyStore::new();
let key = store.generate(KeySpec::new(KeyType::Ed25519, JwsAlgorithm::EdDSA)).await?;
let signer = KeySigner::new(&store, key, JwsAlgorithm::EdDSA).await?;

let header = JwsHeader::new(JwsAlgorithm::EdDSA).with_kid(signer.key_id().as_str());
let token = encode_compact(&header, b"{}", &signer).await?;
let jws = CompactJws::decode(&token)?;
jws.verify(&DefaultVerifier, signer.public_jwk())?;

A signer over an Arc<dyn KeyStore> is 'static and moves into a spawned task:

use std::sync::Arc;

use identity_kit::jose::{JwsAlgorithm, JwsHeader, KeyType, encode_compact};
use identity_kit::keys::{InMemoryKeyStore, KeySigner, KeySpec, KeyStore};

let store: Arc<dyn KeyStore> = Arc::new(InMemoryKeyStore::new());
let key = store.generate(KeySpec::new(KeyType::P256, JwsAlgorithm::ES256)).await?;
let signer = KeySigner::new(Arc::clone(&store), key, JwsAlgorithm::ES256).await?;

let token = tokio::spawn(async move {
    encode_compact(&JwsHeader::new(JwsAlgorithm::ES256), b"{}", &signer).await
})
.await??;
assert_eq!(token.split('.').count(), 3);

Implementations§

Source§

impl<S: KeyStore> KeySigner<S>

Source

pub async fn new( store: S, key: KeyId, alg: JwsAlgorithm, ) -> Result<Self, KeyStoreError>

A signer over key of store, signing with alg.

§Errors

KeyStoreError::UnsupportedAlgorithm when the store does not advertise alg, KeyStoreError::NotFound when the key does not exist, KeyStoreError::AlgorithmMismatch when the key’s public JWK names another algorithm, and whatever else the store reports.

Source

pub fn from_public_jwk( store: S, key: KeyId, alg: JwsAlgorithm, public: Jwk, ) -> Result<Self, KeyStoreError>

A signer over key of store, signing with alg, whose public key the caller already has. The store is not asked for anything until the first signature.

The caller vouches that public is the public key of key: the signer cannot tell, and with a wrong one it makes signatures that verify against no key the caller publishes.

§Errors

KeyStoreError::UnsupportedAlgorithm when the store does not advertise alg, KeyStoreError::AlgorithmMismatch when public names another algorithm, and KeyStoreError::InvalidKey when public carries private parameters or is not a key for alg (its kty and crv).

§Examples
use identity_kit::jose::{JwsAlgorithm, KeyType};
use identity_kit::keys::{InMemoryKeyStore, KeySigner, KeySpec, KeyStore};

let store = InMemoryKeyStore::new();
let key = store.generate(KeySpec::new(KeyType::Ed25519, JwsAlgorithm::EdDSA)).await?;
// Fetched once and kept, for instance next to the DID document.
let public = store.public_jwk(&key).await?;

let signer = KeySigner::from_public_jwk(&store, key, JwsAlgorithm::EdDSA, public)?;
Source

pub fn with_kid(self, kid: impl Into<String>) -> Self

This signer with Signer::kid set to kid instead of the key id.

Source

pub fn store(&self) -> &S

The store the key lives in.

Source

pub fn key_id(&self) -> &KeyId

The key this signer signs with.

Source

pub fn public_jwk(&self) -> &Jwk

The public key, as the store reported it when the signer was created, or as given to KeySigner::from_public_jwk.

Trait Implementations§

Source§

impl<S: Clone> Clone for KeySigner<S>

Source§

fn clone(&self) -> Self

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<S> Debug for KeySigner<S>

Source§

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

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

impl<S: KeyStore> Signer for KeySigner<S>

Source§

fn alg(&self) -> JwsAlgorithm

The algorithm every signature of this signer uses.
Source§

fn kid(&self) -> Option<&str>

The key id to put in a header, if the signer has one.
Source§

fn sign<'life0, 'life1, 'async_trait>( &'life0 self, signing_input: &'life1 [u8], ) -> Pin<Box<dyn Future<Output = Result<Vec<u8>, SignError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Signs signing_input. Read more

Auto Trait Implementations§

§

impl<S> Freeze for KeySigner<S>
where S: Freeze,

§

impl<S> RefUnwindSafe for KeySigner<S>
where S: RefUnwindSafe,

§

impl<S> Send for KeySigner<S>
where S: Send,

§

impl<S> Sync for KeySigner<S>
where S: Sync,

§

impl<S> Unpin for KeySigner<S>
where S: Unpin,

§

impl<S> UnsafeUnpin for KeySigner<S>
where S: UnsafeUnpin,

§

impl<S> UnwindSafe for KeySigner<S>
where S: UnwindSafe,

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<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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + 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: Sized + 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 = !

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

fn try_from(value: U) -> Result<T, !>

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<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