openid-client 1.0.0-alpha.7

OpenID client for Rust
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
459
460
461
462
463
use crate::errors::OpenIdError;
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};

/// # JwkType
/// Represents the JwkType
#[derive(PartialEq, Copy, Clone, Debug, Serialize, Deserialize)]
pub enum JwkType {
    /// OCT - Shared Key
    #[serde(rename = "oct")]
    OCT,
    /// RSA
    #[serde(rename = "RSA")]
    RSA,
    /// Elliptic curve
    #[serde(rename = "EC")]
    EC,
    /// OKP
    #[serde(rename = "OKP")]
    OKP,
}

impl JwkType {
    /// Creates [JwkType] from algorithm
    pub fn from_alg_str(alg: &str) -> Option<JwkType> {
        match alg {
            "HS256" | "HS384" | "HS512" => Some(Self::OCT),
            "RS256" | "RS384" | "RS512" | "PS256" | "PS384" | "PS512" => Some(Self::RSA),
            "ES256" | "ES384" | "ES512" | "ES256K" => Some(Self::EC),
            "EdDSA" => Some(Self::OKP),
            _ => None,
        }
    }

    /// Creates [JwkType] from key type
    pub fn from_kty_str(alg: &str) -> Option<JwkType> {
        match alg {
            "oct" => Some(Self::OCT),
            "RSA" => Some(Self::RSA),
            "EC" => Some(Self::EC),
            "OKP" => Some(Self::OKP),
            _ => None,
        }
    }
}

impl JwkType {
    /// Gets the key type as string
    pub fn get_kty(&self) -> &'static str {
        match self {
            JwkType::OCT => "oct",
            JwkType::RSA => "RSA",
            JwkType::EC => "EC",
            JwkType::OKP => "OKP",
        }
    }
}

/// Represents a JSON Web Key (JWK).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Jwk {
    /// The key fields as a JSON map (key-value pairs).
    #[serde(flatten)]
    pub(crate) params: Map<String, Value>,
}

/// Represents a JSON Web Key Set (JWKS) as returned by the jwks_uri endpoint.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JwksResponse {
    /// The array of JSON Web Keys
    pub keys: Vec<Jwk>,
}

impl TryFrom<&str> for Jwk {
    type Error = OpenIdError;

    fn try_from(value: &str) -> Result<Self, Self::Error> {
        let map: Map<String, Value> =
            serde_json::from_str(value).map_err(|e| OpenIdError::new_error(e.to_string()))?;

        Ok(Jwk { params: map })
    }
}

impl Jwk {
    /// Construct new [Jwk]
    pub fn new(key_type: JwkType, params: Option<Map<String, Value>>) -> Self {
        let mut params = params.unwrap_or_default();

        params.insert(
            "kty".to_owned(),
            Value::String(key_type.get_kty().to_owned()),
        );

        Self { params }
    }

    /// Returns the [JwkType] of the key
    pub fn key_type(&self) -> Option<JwkType> {
        self.params
            .get("kty")
            .and_then(|kty| kty.as_str().map(JwkType::from_kty_str))
            .flatten()
    }

    /// Clones the innner map
    pub fn as_map(&self) -> Map<String, Value> {
        self.params.clone()
    }

    /// Get a parameter of [Jwk]
    pub fn get_param(&self, key: &str) -> Option<&Value> {
        self.params.get(key)
    }

    /// Create a [Jwk] from a symmetric key
    pub fn from_symmetric_key(key: &[u8]) -> Self {
        let mut params = Map::new();

        params.insert("k".to_string(), Value::String(base64_url::encode(key)));
        params.insert("kty".to_string(), Value::String("oct".to_owned()));

        Self { params }
    }

    /// Get the publick key of jwk. Discards unknown parameters.
    ///
    /// This method extracts the parameters but does not check the validity of the values.
    pub fn extract_public_key_jwk(&self) -> Option<Jwk> {
        match self.key_type() {
            Some(JwkType::RSA) => {
                let n = self.params.get("n")?;
                let e = self.params.get("e")?;

                let mut public_key = Map::new();
                public_key.insert("kty".to_string(), Value::String("RSA".to_string()));
                public_key.insert("n".to_string(), n.clone());
                public_key.insert("e".to_string(), e.clone());

                if let Some(use_param) = self.params.get("use") {
                    public_key.insert("use".to_string(), use_param.clone());
                }
                if let Some(alg) = self.params.get("alg") {
                    public_key.insert("alg".to_string(), alg.clone());
                }
                if let Some(kid) = self.params.get("kid") {
                    public_key.insert("kid".to_string(), kid.clone());
                }

                Some(Jwk::new(JwkType::RSA, Some(public_key)))
            }
            Some(JwkType::EC) => {
                let crv = self.params.get("crv")?;
                let x = self.params.get("x")?;
                let y = self.params.get("y")?;

                let mut public_key = Map::new();
                public_key.insert("kty".to_string(), Value::String("EC".to_string()));
                public_key.insert("crv".to_string(), crv.clone());
                public_key.insert("x".to_string(), x.clone());
                public_key.insert("y".to_string(), y.clone());

                if let Some(use_param) = self.params.get("use") {
                    public_key.insert("use".to_string(), use_param.clone());
                }
                if let Some(alg) = self.params.get("alg") {
                    public_key.insert("alg".to_string(), alg.clone());
                }
                if let Some(kid) = self.params.get("kid") {
                    public_key.insert("kid".to_string(), kid.clone());
                }

                Some(Jwk::new(JwkType::EC, Some(public_key)))
            }
            Some(JwkType::OKP) => {
                let crv = self.params.get("crv")?;
                let x = self.params.get("x")?;

                let mut public_key = Map::new();
                public_key.insert("kty".to_string(), Value::String("OKP".to_string()));
                public_key.insert("crv".to_string(), crv.clone());
                public_key.insert("x".to_string(), x.clone());

                if let Some(use_param) = self.params.get("use") {
                    public_key.insert("use".to_string(), use_param.clone());
                }
                if let Some(alg) = self.params.get("alg") {
                    public_key.insert("alg".to_string(), alg.clone());
                }
                if let Some(kid) = self.params.get("kid") {
                    public_key.insert("kid".to_string(), kid.clone());
                }

                Some(Jwk::new(JwkType::OKP, Some(public_key)))
            }
            Some(JwkType::OCT) => None,
            None => None,
        }
    }

    /// Get the private key of jwk. Discards unknown parameters.
    ///
    /// This method extracts the parameters but does not check the validity of the values.
    pub fn extract_private_key_jwk(&self) -> Option<Jwk> {
        match self.key_type() {
            Some(JwkType::RSA) => {
                let n = self.params.get("n")?;
                let e = self.params.get("e")?;
                let d = self.params.get("d")?;

                let mut private_key = Map::new();
                private_key.insert("kty".to_string(), Value::String("RSA".to_string()));
                private_key.insert("n".to_string(), n.clone());
                private_key.insert("e".to_string(), e.clone());
                private_key.insert("d".to_string(), d.clone());

                if let Some(p) = self.params.get("p") {
                    private_key.insert("p".to_string(), p.clone());
                }
                if let Some(q) = self.params.get("q") {
                    private_key.insert("q".to_string(), q.clone());
                }
                if let Some(dp) = self.params.get("dp") {
                    private_key.insert("dp".to_string(), dp.clone());
                }
                if let Some(dq) = self.params.get("dq") {
                    private_key.insert("dq".to_string(), dq.clone());
                }
                if let Some(qi) = self.params.get("qi") {
                    private_key.insert("qi".to_string(), qi.clone());
                }
                if let Some(oth) = self.params.get("oth") {
                    private_key.insert("oth".to_string(), oth.clone());
                }
                if let Some(r) = self.params.get("r") {
                    private_key.insert("r".to_string(), r.clone());
                }
                if let Some(t) = self.params.get("t") {
                    private_key.insert("t".to_string(), t.clone());
                }

                if let Some(use_param) = self.params.get("use") {
                    private_key.insert("use".to_string(), use_param.clone());
                }
                if let Some(alg) = self.params.get("alg") {
                    private_key.insert("alg".to_string(), alg.clone());
                }
                if let Some(kid) = self.params.get("kid") {
                    private_key.insert("kid".to_string(), kid.clone());
                }

                Some(Jwk::new(JwkType::RSA, Some(private_key)))
            }
            Some(JwkType::EC) => {
                let crv = self.params.get("crv")?;
                let x = self.params.get("x")?;
                let y = self.params.get("y")?;
                let d = self.params.get("d")?;

                let mut private_key = Map::new();
                private_key.insert("kty".to_string(), Value::String("EC".to_string()));
                private_key.insert("crv".to_string(), crv.clone());
                private_key.insert("x".to_string(), x.clone());
                private_key.insert("y".to_string(), y.clone());
                private_key.insert("d".to_string(), d.clone());

                if let Some(use_param) = self.params.get("use") {
                    private_key.insert("use".to_string(), use_param.clone());
                }
                if let Some(alg) = self.params.get("alg") {
                    private_key.insert("alg".to_string(), alg.clone());
                }
                if let Some(kid) = self.params.get("kid") {
                    private_key.insert("kid".to_string(), kid.clone());
                }

                Some(Jwk::new(JwkType::EC, Some(private_key)))
            }
            Some(JwkType::OKP) => {
                let crv = self.params.get("crv")?;
                let x = self.params.get("x")?;
                let d = self.params.get("d")?;

                let mut private_key = Map::new();
                private_key.insert("kty".to_string(), Value::String("OKP".to_string()));
                private_key.insert("crv".to_string(), crv.clone());
                private_key.insert("x".to_string(), x.clone());
                private_key.insert("d".to_string(), d.clone());

                if let Some(use_param) = self.params.get("use") {
                    private_key.insert("use".to_string(), use_param.clone());
                }
                if let Some(alg) = self.params.get("alg") {
                    private_key.insert("alg".to_string(), alg.clone());
                }
                if let Some(kid) = self.params.get("kid") {
                    private_key.insert("kid".to_string(), kid.clone());
                }

                Some(Jwk::new(JwkType::OKP, Some(private_key)))
            }
            Some(JwkType::OCT) => None,
            None => None,
        }
    }

    /// Checks if a [Jwk] is a valid public key structurally
    pub fn is_valid_public_key(&self) -> bool {
        match self.key_type() {
            Some(JwkType::RSA) => {
                self.params
                    .get("n")
                    .and_then(|v| v.as_str())
                    .is_some_and(|s| !s.is_empty())
                    && self
                        .params
                        .get("e")
                        .and_then(|v| v.as_str())
                        .is_some_and(|s| !s.is_empty())
            }
            Some(JwkType::EC) => {
                self.params
                    .get("crv")
                    .and_then(|v| v.as_str())
                    .is_some_and(|s| !s.is_empty())
                    && self
                        .params
                        .get("x")
                        .and_then(|v| v.as_str())
                        .is_some_and(|s| !s.is_empty())
                    && self
                        .params
                        .get("y")
                        .and_then(|v| v.as_str())
                        .is_some_and(|s| !s.is_empty())
            }
            Some(JwkType::OKP) => {
                self.params
                    .get("crv")
                    .and_then(|v| v.as_str())
                    .is_some_and(|s| !s.is_empty())
                    && self
                        .params
                        .get("x")
                        .and_then(|v| v.as_str())
                        .is_some_and(|s| !s.is_empty())
            }
            Some(JwkType::OCT) => false,
            None => false,
        }
    }

    /// Checks if a [Jwk] is a valid private key structurally
    pub fn is_valid_private_key(&self) -> bool {
        match self.key_type() {
            // Add validation for multiprime check?
            Some(JwkType::RSA) => {
                let has_basic_params = self
                    .params
                    .get("n")
                    .and_then(|v| v.as_str())
                    .is_some_and(|s| !s.is_empty())
                    && self
                        .params
                        .get("e")
                        .and_then(|v| v.as_str())
                        .is_some_and(|s| !s.is_empty())
                    && self
                        .params
                        .get("d")
                        .and_then(|v| v.as_str())
                        .is_some_and(|s| !s.is_empty());

                if !has_basic_params {
                    return false;
                }

                let has_p = self
                    .params
                    .get("p")
                    .and_then(|v| v.as_str())
                    .is_some_and(|s| !s.is_empty());
                let has_q = self
                    .params
                    .get("q")
                    .and_then(|v| v.as_str())
                    .is_some_and(|s| !s.is_empty());
                let has_dp = self
                    .params
                    .get("dp")
                    .and_then(|v| v.as_str())
                    .is_some_and(|s| !s.is_empty());
                let has_dq = self
                    .params
                    .get("dq")
                    .and_then(|v| v.as_str())
                    .is_some_and(|s| !s.is_empty());
                let has_qi = self
                    .params
                    .get("qi")
                    .and_then(|v| v.as_str())
                    .is_some_and(|s| !s.is_empty());

                let crt_count = [has_p, has_q, has_dp, has_dq, has_qi]
                    .iter()
                    .filter(|&&x| x)
                    .count();
                crt_count == 0 || crt_count == 5
            }
            Some(JwkType::EC) => {
                self.params
                    .get("crv")
                    .and_then(|v| v.as_str())
                    .is_some_and(|s| !s.is_empty())
                    && self
                        .params
                        .get("x")
                        .and_then(|v| v.as_str())
                        .is_some_and(|s| !s.is_empty())
                    && self
                        .params
                        .get("y")
                        .and_then(|v| v.as_str())
                        .is_some_and(|s| !s.is_empty())
                    && self
                        .params
                        .get("d")
                        .and_then(|v| v.as_str())
                        .is_some_and(|s| !s.is_empty())
            }
            Some(JwkType::OKP) => {
                self.params
                    .get("crv")
                    .and_then(|v| v.as_str())
                    .is_some_and(|s| !s.is_empty())
                    && self
                        .params
                        .get("x")
                        .and_then(|v| v.as_str())
                        .is_some_and(|s| !s.is_empty())
                    && self
                        .params
                        .get("d")
                        .and_then(|v| v.as_str())
                        .is_some_and(|s| !s.is_empty())
            }
            Some(JwkType::OCT) => self
                .params
                .get("k")
                .and_then(|v| v.as_str())
                .is_some_and(|s| !s.is_empty()),

            None => false,
        }
    }
}

/// JWK Set
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Jwks {
    /// List of Jwk
    pub keys: Vec<Jwk>,
}