gel-jwt 0.1.4

JWT implementation for the Gel database.
Documentation
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
use crate::{
    bare_key::{SerializedKey, SerializedKeys},
    key::*,
    Any, KeyError, OpaqueValidationFailureReason, SignatureError, SigningContext,
    ValidationContext, ValidationError,
};
use std::{
    collections::{BTreeSet, HashMap, HashSet},
    fmt::Debug,
};

pub(crate) trait IsKey {
    type Inner: std::hash::Hash + Eq + Debug + Clone;

    fn inner(&self) -> &Self::Inner;
    fn key_type(inner: &Self::Inner) -> KeyType;
    fn from_inner(kid: Option<String>, inner: Self::Inner) -> Self;
    fn into_inner(self) -> (Option<String>, Self::Inner);
    fn get_serialized_key(key: SerializedKey) -> Option<Self>
    where
        Self: Sized;
    fn to_serialized_key(kid: Option<&str>, inner: &Self::Inner) -> SerializedKey;
    fn from_pem(pem: &str) -> Result<Vec<Result<Self, KeyError>>, KeyError>
    where
        Self: Sized;
    fn to_pem(inner: &Self::Inner) -> String;
    fn encoding_key(inner: &Self::Inner) -> Option<&jsonwebtoken::EncodingKey>;
    fn decoding_key(inner: &Self::Inner) -> &jsonwebtoken::DecodingKey;
}

/// A collection of [`Key`] or [`PublicKey`] objects.
#[allow(private_bounds)]
pub struct KeyRegistry<K: IsKey> {
    // TODO: this could probably be optimized, especially if we can
    // generate key signatures.
    /// Map from key identifier (kid) to ordinal
    named_keys: HashMap<String, usize>,
    /// Set of ordinals for unnamed keys
    unnamed_keys: HashSet<usize>,
    /// Map from key to ordinal/kid for quick lookup
    key_to_ordinal: HashMap<K::Inner, (usize, Option<String>)>,
    /// Set of active ordinals
    active_keys: BTreeSet<usize>,
    /// Next ordinal to use for a new key
    next: usize,
}

impl<K: IsKey> Default for KeyRegistry<K> {
    fn default() -> Self {
        Self {
            named_keys: HashMap::default(),
            unnamed_keys: HashSet::default(),
            key_to_ordinal: HashMap::default(),
            active_keys: BTreeSet::default(),
            next: 0,
        }
    }
}

impl KeyRegistry<PrivateKey> {
    /// Create a new private key registry.
    pub fn private() -> Self {
        Self::default()
    }
}

impl KeyRegistry<PublicKey> {
    /// Create a new public key registry.
    pub fn public() -> Self {
        Self::default()
    }
}

impl KeyRegistry<Key> {
    /// Create a new key registry that may contain both private and public keys.
    pub fn new() -> Self {
        Self::default()
    }
}

#[allow(private_bounds)]
impl<K: IsKey> KeyRegistry<K> {
    /// Clear the registry.
    pub fn clear(&mut self) {
        *self = Self::default();
    }

    pub fn into_keys(self) -> impl Iterator<Item = K> {
        self.key_to_ordinal
            .into_iter()
            .map(|(key, (_, kid))| K::from_inner(kid, key))
    }

    /// Add a key to the registry. If the key already exists, it will be
    /// replaced. If the key specifies the same kid as another key already
    /// added, the new key will replace the old one.
    ///
    /// Adding a key, even if it already exists, will make it the active key.
    pub fn add_key(&mut self, key: K) {
        self.remove_key(&key);

        let (kid, inner) = key.into_inner();

        // If the kid still exists, we need to remove that key too
        if let Some(kid) = &kid {
            if self.named_keys.contains_key(kid) {
                self.remove_kid(kid);
            }
        }

        // Key is new, add it to the registry
        let ordinal = self.next;
        self.next += 1;
        self.key_to_ordinal.insert(inner, (ordinal, kid.clone()));
        self.active_keys.insert(ordinal);

        if let Some(kid) = kid {
            self.named_keys.insert(kid, ordinal);
        } else {
            self.unnamed_keys.insert(ordinal);
        }
    }

    /// Remove a key from the registry by its key.
    pub fn remove_key(&mut self, key: &K) {
        let inner = key.inner();
        if let Some((ordinal, kid)) = self.key_to_ordinal.remove(inner) {
            if let Some(kid) = kid {
                self.named_keys.remove(&kid);
            } else {
                self.unnamed_keys.remove(&ordinal);
            }
            self.active_keys.remove(&ordinal);
        }
    }

    /// Remove a key from the registry by its kid. Note: O(N).
    pub fn remove_kid(&mut self, kid: &str) -> bool {
        if let Some(ordinal) = self.named_keys.remove(kid) {
            self.active_keys.remove(&ordinal);
            self.key_to_ordinal.retain(|_, &mut (v, _)| v != ordinal);
            true
        } else {
            false
        }
    }

    /// Get the number of keys in the registry.
    pub fn len(&self) -> usize {
        self.key_to_ordinal.len()
    }

    /// Check if the registry is empty.
    pub fn is_empty(&self) -> bool {
        self.key_to_ordinal.is_empty()
    }

    /// Add keys from a JWKSet.
    pub fn add_from_jwkset(&mut self, jwkset: &str) -> Result<usize, KeyError> {
        let loaded: SerializedKeys =
            serde_json::from_str(jwkset).map_err(|_| KeyError::InvalidJson)?;
        let mut added = 0;
        for key in loaded.keys {
            if let Some(key) = K::get_serialized_key(key) {
                self.add_key(key);
                added += 1;
            } else {
                // TODO: log unknown or invalid key
            }
        }
        Ok(added)
    }

    /// Add keys from a PEM file.
    pub fn add_from_pem(&mut self, pem: &str) -> Result<usize, KeyError> {
        let keys = K::from_pem(pem)?;
        let mut added = 0;
        for key in keys {
            if let Ok(key) = key {
                self.add_key(key);
                added += 1;
            } else {
                // TODO: log unknown or invalid key
            }
        }
        Ok(added)
    }

    /// Add keys from a source string which can be either a JWK set or a PEM file with
    /// 1 or more keys.
    pub fn add_from_any(&mut self, source: &str) -> Result<usize, KeyError> {
        let source = source.trim();
        if source.is_empty() {
            return Ok(0);
        }

        // Get the first non-whitespace character
        let first_char = source.chars().next().unwrap_or_default();
        if first_char == '{' {
            self.add_from_jwkset(source)
        } else if first_char == '-' {
            self.add_from_pem(source)
        } else {
            Err(KeyError::UnsupportedKeyType(format!(
                "Expected JWK set or PEM file, got {first_char}"
            )))
        }
    }

    pub fn to_pem(&self) -> String {
        let mut pem = String::new();
        for (k, (_, _)) in &self.key_to_ordinal {
            pem.push_str(&K::to_pem(k));
        }
        pem
    }

    pub fn to_json(&self) -> Result<String, KeyError> {
        serde_json::to_string(&SerializedKeys {
            keys: self
                .key_to_ordinal
                .iter()
                .map(|(k, (_, kid))| K::to_serialized_key(kid.as_deref(), k))
                .collect(),
        })
        .map_err(|_| KeyError::EncodeError)
    }

    /// Get the active key and kid.
    fn active_key(&self) -> Option<(Option<&str>, &K::Inner)> {
        if let Some(&i) = self.active_keys.last() {
            for (k, &(v, ref kid)) in &self.key_to_ordinal {
                if v == i {
                    if let Some(kid) = kid {
                        return Some((Some(kid.as_str()), k));
                    } else {
                        return Some((None, k));
                    }
                }
            }
        }
        None
    }

    /// Decode a token without validating the signature.
    pub fn unsafely_decode_without_validation(
        &self,
        token: &str,
    ) -> Result<HashMap<String, Any>, ValidationError> {
        let mut validation = jsonwebtoken::Validation::new(jsonwebtoken::Algorithm::default());
        validation.insecure_disable_signature_validation();
        validation.required_spec_claims.clear();
        validation.validate_exp = false;
        validation.validate_nbf = false;
        validation.validate_aud = false;
        let decoding_key = jsonwebtoken::DecodingKey::from_secret(b"");

        let decoded =
            jsonwebtoken::decode::<HashMap<String, Any>>(token, &decoding_key, &validation)
                .map_err(|e| {
                    ValidationError::Invalid(OpaqueValidationFailureReason::Failure(format!(
                        "{:?}",
                        e.kind()
                    )))
                })?;

        Ok(decoded.claims)
    }

    pub fn validate(
        &self,
        token: &str,
        ctx: &ValidationContext,
    ) -> Result<HashMap<String, Any>, ValidationError> {
        // If we have a named key that matches, use that.
        if !self.named_keys.is_empty() {
            if let Ok(header) = jsonwebtoken::decode_header(token) {
                if let Some(header_kid) = header.kid {
                    for (key, (_, kid)) in &self.key_to_ordinal {
                        if kid.as_deref() == Some(header_kid.as_str()) {
                            return validate_token(
                                K::key_type(key),
                                K::decoding_key(key),
                                None,
                                token,
                                ctx,
                            );
                        }
                    }
                }
            }
        }

        let mut result = None;
        for (key, _) in self.key_to_ordinal.iter() {
            let last_result =
                validate_token(K::key_type(key), K::decoding_key(key), None, token, ctx);
            match last_result {
                Ok(result) => return Ok(result),
                Err(e) => result = Some(e),
            }
        }
        Err(result.unwrap_or(OpaqueValidationFailureReason::NoAppropriateKey.into()))
    }

    pub fn sign(
        &self,
        claims: HashMap<String, Any>,
        ctx: &SigningContext,
    ) -> Result<String, SignatureError> {
        let (kid, key) = self.active_key().ok_or(SignatureError::NoAppropriateKey)?;
        let encoding_key = K::encoding_key(key).ok_or(SignatureError::NoAppropriateKey)?;
        sign_token(K::key_type(key), encoding_key, kid, claims, ctx)
    }
}

impl KeyRegistry<PrivateKey> {
    pub fn can_sign(&self) -> bool {
        self.has_private_keys() || self.has_symmetric_keys()
    }

    pub fn can_validate(&self) -> bool {
        self.has_public_keys() || self.has_symmetric_keys()
    }

    pub fn has_private_keys(&self) -> bool {
        !self.is_empty()
    }

    pub fn has_public_keys(&self) -> bool {
        self.key_to_ordinal
            .iter()
            .any(|(k, _)| k.bare_key.key_type() != KeyType::HS256)
    }

    pub fn has_symmetric_keys(&self) -> bool {
        self.key_to_ordinal
            .iter()
            .any(|(k, _)| k.bare_key.key_type() == KeyType::HS256)
    }

    #[cfg(feature = "keygen")]
    pub fn generate_key(&mut self, kid: Option<String>, key_type: KeyType) -> Result<(), KeyError> {
        let key = PrivateKey::generate(kid, key_type)?;
        self.add_key(key);
        Ok(())
    }
}

impl KeyRegistry<PublicKey> {
    pub fn can_sign(&self) -> bool {
        self.has_private_keys() || self.has_symmetric_keys()
    }

    pub fn can_validate(&self) -> bool {
        self.has_public_keys() || self.has_symmetric_keys()
    }

    pub fn has_public_keys(&self) -> bool {
        !self.is_empty()
    }

    pub fn has_private_keys(&self) -> bool {
        false
    }

    pub fn has_symmetric_keys(&self) -> bool {
        false
    }
}

impl KeyRegistry<Key> {
    pub fn can_sign(&self) -> bool {
        self.has_private_keys() || self.has_symmetric_keys()
    }

    pub fn can_validate(&self) -> bool {
        self.has_public_keys() || self.has_symmetric_keys()
    }

    pub fn has_private_keys(&self) -> bool {
        for k in self.key_to_ordinal.keys() {
            if let KeyInner::Private(_) = k {
                return true;
            }
        }
        false
    }

    pub fn has_public_keys(&self) -> bool {
        for k in self.key_to_ordinal.keys() {
            if let KeyInner::Public(_) = k {
                return true;
            }
            if let KeyInner::Private(k) = k {
                if k.bare_key.key_type() != KeyType::HS256 {
                    return true;
                }
            }
        }
        false
    }

    pub fn has_symmetric_keys(&self) -> bool {
        for k in self.key_to_ordinal.keys() {
            if let KeyInner::Private(k) = k {
                if k.bare_key.key_type() == KeyType::HS256 {
                    return true;
                }
            }
        }
        false
    }

    /// Export the registry as a PEM file containing only the public keys.
    /// This will fail if the registry contains symmetric keys.
    pub fn to_pem_public(&self) -> Result<String, KeyError> {
        let mut pem = String::new();
        for (k, (_, _)) in &self.key_to_ordinal {
            match k {
                KeyInner::Private(k) => {
                    pem.push_str(&k.bare_key.to_pem_public()?);
                }
                KeyInner::Public(k) => {
                    pem.push_str(&k.bare_key.to_pem());
                }
            }
        }
        Ok(pem)
    }

    /// Export the registry as a JSON object containing only the public keys.
    /// This will fail if the registry contains symmetric keys.
    pub fn to_json_public(&self) -> Result<String, KeyError> {
        let mut keys = Vec::new();
        for (k, (_, kid)) in &self.key_to_ordinal {
            match k {
                KeyInner::Private(k) => {
                    keys.push(SerializedKey::Public(
                        kid.clone(),
                        k.bare_key.to_public()?.clone_key(),
                    ));
                }
                KeyInner::Public(k) => {
                    keys.push(SerializedKey::Public(kid.clone(), k.bare_key.clone_key()));
                }
            }
        }
        serde_json::to_string(&SerializedKeys { keys }).map_err(|_| KeyError::EncodeError)
    }

    #[cfg(feature = "keygen")]
    pub fn generate_key(&mut self, kid: Option<String>, key_type: KeyType) -> Result<(), KeyError> {
        let key = PrivateKey::generate(kid, key_type)?;
        self.add_key(key.into());
        Ok(())
    }
}