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
//! The signing party: which identifiers it controls and the keys that sign for them.
//!
//! Every protocol that signs on behalf of a party needs the same three things: an identifier for
//! `iss` or `sub`, a `kid` for the header and a [`Signer`]. Which identifier depends on what the
//! other side accepts, a DID of some method or the RFC 7638 thumbprint of a bare key, and on the
//! algorithm both sides agreed on. The protocol negotiates that pair (SIOPv2 matches
//! `subject_syntax_types_supported` against what the wallet offers, OID4VCI the
//! `proof_signing_alg_values_supported`), using [`Identity::capabilities`] for the identity's
//! side; the identity then answers for the chosen pair:
//!
//! - [`Identity::identifier`]: the `iss`/`sub` value, a [`Did`] or a thumbprint;
//! - [`Identity::public_jwk`]: the public key, for `cnf.jwk`, `sub_jwk` or a `jwk` header;
//! - [`Identity::signer`]: a [`Signer`] whose [`kid`](Signer::kid) is the absolute DID URL of the
//! verification method for a DID, and `None` for a thumbprint, where the caller puts the
//! public key in the header instead.
//!
//! All three name the same key for the same pair. A party may hold a key per algorithm for one
//! kind, so the identifier takes the algorithm too: a `did:key`, a `did:jwk` or a thumbprint is
//! derived from the key and differs between them.
//!
//! [`KeyStoreIdentity`] is the implementation the crate ships, over any [`KeyStore`] and the
//! [`DidProducer`]s registered with it. There is no hook for an external signer: a remote or
//! hardware signer is a [`KeyStore`] (ADR 0006).
//!
//! # Examples
//!
//! ```
//! # #[cfg(all(feature = "in-memory", feature = "did-key"))]
//! # tokio::runtime::Builder::new_current_thread().build()?.block_on(async {
//! use std::sync::Arc;
//!
//! use identity_kit::did::{DidMethod, VerificationRelationship};
//! use identity_kit::identity::{Identity, IdentityKind, KeyStoreIdentity};
//! use identity_kit::jose::{CompactJws, DefaultVerifier, JwsAlgorithm, JwsHeader, KeyType, encode_compact};
//! use identity_kit::keys::{InMemoryKeyStore, KeySpec, KeyStore};
//! use identity_kit::methods::DidKey;
//! use identity_kit::resolver::Resolver;
//!
//! let store = Arc::new(InMemoryKeyStore::new());
//! let key = store.generate(KeySpec::new(KeyType::Ed25519, JwsAlgorithm::EdDSA)).await?;
//! let identity = KeyStoreIdentity::new(store).with_producer(DidKey::default());
//!
//! // The protocol settled on did:key and EdDSA.
//! let kind = IdentityKind::Did(DidMethod::new("key")?);
//! let did = identity.bind(kind.clone(), JwsAlgorithm::EdDSA, key).await?;
//! assert!(did.to_string().starts_with("did:key:z6Mk"));
//!
//! let signer = identity.signer(&kind, JwsAlgorithm::EdDSA).await?;
//! let kid = signer.kid().expect("a DID signer names its verification method");
//! let header = JwsHeader::new(JwsAlgorithm::EdDSA).with_kid(kid);
//! let token = encode_compact(&header, br#"{"iss":"me"}"#, &*signer).await?;
//!
//! // A verifier finds the key through the DID in `kid`.
//! let resolver = Resolver::builder().with(DidKey::default()).build();
//! let url = kid.parse()?;
//! let key = resolver
//! .resolve_public_key(&url, Some(VerificationRelationship::AssertionMethod))
//! .await?;
//! CompactJws::decode(&token)?.verify(&DefaultVerifier, &key)?;
//! # Ok::<(), Box<dyn std::error::Error>>(())
//! # })?;
//! # Ok::<(), Box<dyn std::error::Error>>(())
//! ```
//!
//! [`KeyStore`]: crate::keys::KeyStore
//! [`DidProducer`]: crate::resolver::DidProducer
use fmt;
use Arc;
use crate;
use crate;
use crateKeyStoreError;
use crateProduceError;
pub use KeyStoreIdentity;
/// The kind of identifier the other side accepts.
///
/// # Examples
///
/// ```
/// use identity_kit::did::DidMethod;
/// use identity_kit::identity::IdentityKind;
///
/// let kind = IdentityKind::Did(DidMethod::new("key")?);
/// assert_eq!(kind.to_string(), "did:key");
/// assert_eq!(IdentityKind::JwkThumbprint.to_string(), "JWK thumbprint");
/// # Ok::<(), identity_kit::did::DidError>(())
/// ```
/// The identifier of an [`Identity`] for one kind and algorithm; [`Display`](fmt::Display) gives
/// the `iss` or `sub` value.
///
/// # Examples
///
/// ```
/// use identity_kit::did::Did;
/// use identity_kit::identity::Identifier;
///
/// let did = Identifier::Did(Did::parse("did:example:123")?);
/// assert_eq!(did.to_string(), "did:example:123");
/// assert!(did.as_did().is_some());
///
/// // RFC 7638 ยง3.1.
/// let thumbprint = Identifier::JwkThumbprint("NzbLsXh8uDCcd-6MNwXF4W_7noWXFZAfHkxZsRGC9Xs".into());
/// assert_eq!(thumbprint.to_string(), "NzbLsXh8uDCcd-6MNwXF4W_7noWXFZAfHkxZsRGC9Xs");
/// assert!(thumbprint.as_did().is_none());
/// # Ok::<(), identity_kit::did::DidError>(())
/// ```
/// Why an [`Identity`] cannot answer for a kind and algorithm.
/// A party that signs: the identifiers it controls and the keys behind them.
///
/// See the [module documentation](self) for how a protocol uses it. Every method answers for a
/// `(kind, alg)` pair; for one pair, [`identifier`](Identity::identifier),
/// [`public_jwk`](Identity::public_jwk) and [`signer`](Identity::signer) name the same key.
forward_identity!;