openidconnect 4.0.1

OpenID Connect library
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
use crate::types::jwk::JsonWebKey;
use crate::{AccessToken, AuthorizationCode};

use base64::prelude::BASE64_URL_SAFE_NO_PAD;
use base64::Engine;
use oauth2::helpers::deserialize_space_delimited_vec;
use rand::{thread_rng, Rng};
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use thiserror::Error;
use url::Url;

use std::fmt::Debug;
use std::hash::Hash;
use std::ops::Deref;

pub(crate) mod jwk;
pub(crate) mod jwks;
pub(crate) mod localized;

#[cfg(test)]
mod tests;

/// Client application type.
pub trait ApplicationType: Debug + DeserializeOwned + Serialize + 'static {}

/// How the Authorization Server displays the authentication and consent user interface pages to
/// the End-User.
pub trait AuthDisplay: AsRef<str> + Debug + DeserializeOwned + Serialize + 'static {}

/// Whether the Authorization Server should prompt the End-User for reauthentication and consent.
pub trait AuthPrompt: AsRef<str> + 'static {}

/// Claim name.
pub trait ClaimName: Debug + DeserializeOwned + Serialize + 'static {}

/// Claim type (e.g., normal, aggregated, or distributed).
pub trait ClaimType: Debug + DeserializeOwned + Serialize + 'static {}

/// Client authentication method.
pub trait ClientAuthMethod: Debug + DeserializeOwned + Serialize + 'static {}

/// Grant type.
pub trait GrantType: Debug + DeserializeOwned + Serialize + 'static {}

/// Error signing a message.
#[derive(Clone, Debug, Error, PartialEq, Eq)]
#[non_exhaustive]
pub enum SigningError {
    /// Failed to sign the message using the given key and parameters.
    #[error("Crypto error")]
    CryptoError,
    /// Unsupported signature algorithm.
    #[error("Unsupported signature algorithm: {0}")]
    UnsupportedAlg(String),
    /// An unexpected error occurred.
    #[error("Other error: {0}")]
    Other(String),
}

/// Response mode indicating how the OpenID Connect Provider should return the Authorization
/// Response to the Relying Party (client).
pub trait ResponseMode: Debug + DeserializeOwned + Serialize + 'static {}

/// Response type indicating the desired authorization processing flow, including what
/// parameters are returned from the endpoints used.
pub trait ResponseType: AsRef<str> + Debug + DeserializeOwned + Serialize + 'static {
    /// Converts this OpenID Connect response type to an [`oauth2::ResponseType`] used by the
    /// underlying [`oauth2`] crate.
    fn to_oauth2(&self) -> oauth2::ResponseType;
}

/// Subject identifier type returned by an OpenID Connect Provider to uniquely identify its users.
pub trait SubjectIdentifierType: Debug + DeserializeOwned + Serialize + 'static {}

new_type![
    /// Set of authentication methods or procedures that are considered to be equivalent to each
    /// other in a particular context.
    #[derive(Deserialize, Hash, Ord, PartialOrd, Serialize)]
    AuthenticationContextClass(String)
];
impl AsRef<str> for AuthenticationContextClass {
    fn as_ref(&self) -> &str {
        self
    }
}

new_type![
    /// Identifier for an authentication method (e.g., `password` or `totp`).
    ///
    /// Defining specific AMR identifiers is beyond the scope of the OpenID Connect Core spec.
    #[derive(Deserialize, Hash, Ord, PartialOrd, Serialize)]
    AuthenticationMethodReference(String)
];

new_type![
    /// Access token hash.
    #[derive(Deserialize, Hash, Ord, PartialOrd, Serialize)]
    AccessTokenHash(String)
    impl {
        /// Initialize a new access token hash from an [`AccessToken`] and signature algorithm.
        pub fn from_token<K>(
            access_token: &AccessToken,
            alg: &K::SigningAlgorithm,
            key: &K,
        ) -> Result<Self, SigningError>
        where
            K: JsonWebKey,
        {
            key.hash_bytes(access_token.secret().as_bytes(), alg)
                .map(|hash| Self::new(BASE64_URL_SAFE_NO_PAD.encode(&hash[0..hash.len() / 2])))
                .map_err(SigningError::UnsupportedAlg)
        }
    }
];

new_type![
    /// Country portion of address.
    #[derive(Deserialize, Hash, Ord, PartialOrd, Serialize)]
    AddressCountry(String)
];

new_type![
    /// Locality portion of address.
    #[derive(Deserialize, Hash, Ord, PartialOrd, Serialize)]
    AddressLocality(String)
];

new_type![
    /// Postal code portion of address.
    #[derive(Deserialize, Hash, Ord, PartialOrd, Serialize)]
    AddressPostalCode(String)
];

new_type![
    /// Region portion of address.
    #[derive(Deserialize, Hash, Ord, PartialOrd, Serialize)]
    AddressRegion(String)
];

new_type![
    /// Audience claim value.
    #[derive(Deserialize, Hash, Ord, PartialOrd, Serialize)]
    Audience(String)
];

new_type![
    /// Authorization code hash.
    #[derive(Deserialize, Hash, Ord, PartialOrd, Serialize)]
    AuthorizationCodeHash(String)
    impl {
        /// Initialize a new authorization code hash from an [`AuthorizationCode`] and signature
        /// algorithm.
        pub fn from_code<K>(
            code: &AuthorizationCode,
            alg: &K::SigningAlgorithm,
            key: &K,
        ) -> Result<Self, SigningError>
        where
            K: JsonWebKey,
        {
            key.hash_bytes(code.secret().as_bytes(), alg)
                .map(|hash| Self::new(BASE64_URL_SAFE_NO_PAD.encode(&hash[0..hash.len() / 2])))
                .map_err(SigningError::UnsupportedAlg)
        }
    }
];

new_type![
    /// OpenID Connect client name.
    #[derive(Deserialize, Hash, Ord, PartialOrd, Serialize)]
    ClientName(String)
];

new_url_type![
    /// Client configuration endpoint URL.
    ClientConfigUrl
];

new_url_type![
    /// Client homepage URL.
    ClientUrl
];

new_type![
    /// Client contact e-mail address.
    #[derive(Deserialize, Hash, Ord, PartialOrd, Serialize)]
    ClientContactEmail(String)
];

new_url_type![
    /// URL for the [OpenID Connect RP-Initiated Logout 1.0](
    /// https://openid.net/specs/openid-connect-rpinitiated-1_0.html) end session endpoint.
    EndSessionUrl
];

new_type![
    /// End user's birthday, represented as an
    /// [ISO 8601:2004](https://www.iso.org/standard/40874.html) `YYYY-MM-DD` format.
    ///
    /// The year MAY be `0000`, indicating that it is omitted. To represent only the year, `YYYY`
    /// format is allowed. Note that depending on the underlying platform's date related function,
    /// providing just year can result in varying month and day, so the implementers need to take
    /// this factor into account to correctly process the dates.
    #[derive(Deserialize, Hash, Ord, PartialOrd, Serialize)]
    EndUserBirthday(String)
];

new_type![
    /// End user's e-mail address.
    #[derive(Deserialize, Hash, Ord, PartialOrd, Serialize)]
    EndUserEmail(String)
];

new_type![
    /// End user's family name.
    #[derive(Deserialize, Hash, Ord, PartialOrd, Serialize)]
    EndUserFamilyName(String)
];

new_type![
    /// End user's given name.
    #[derive(Deserialize, Hash, Ord, PartialOrd, Serialize)]
    EndUserGivenName(String)
];

new_type![
    /// End user's middle name.
    #[derive(Deserialize, Hash, Ord, PartialOrd, Serialize)]
    EndUserMiddleName(String)
];

new_type![
    /// End user's name.
    #[derive(Deserialize, Hash, Ord, PartialOrd, Serialize)]
    EndUserName(String)
];

new_type![
    /// End user's nickname.
    #[derive(Deserialize, Hash, Ord, PartialOrd, Serialize)]
    EndUserNickname(String)
];

new_type![
    /// End user's phone number.
    #[derive(Deserialize, Hash, Ord, PartialOrd, Serialize)]
    EndUserPhoneNumber(String)
];

new_type![
    /// URL of end user's profile picture.
    #[derive(Deserialize, Hash, Ord, PartialOrd, Serialize)]
    EndUserPictureUrl(String)
];

new_type![
    /// URL of end user's profile page.
    #[derive(Deserialize, Hash, Ord, PartialOrd, Serialize)]
    EndUserProfileUrl(String)
];

new_type![
    /// End user's time zone as a string from the
    /// [time zone database](https://www.iana.org/time-zones).
    #[derive(Deserialize, Hash, Ord, PartialOrd, Serialize)]
    EndUserTimezone(String)
];

new_type![
    /// URL of end user's website.
    #[derive(Deserialize, Hash, Ord, PartialOrd, Serialize)]
    EndUserWebsiteUrl(String)
];

new_type![
    /// End user's username.
    #[derive(Deserialize, Hash, Ord, PartialOrd, Serialize)]
    EndUserUsername(String)
];

new_type![
    /// Full mailing address, formatted for display or use on a mailing label.
    ///
    /// This field MAY contain multiple lines, separated by newlines. Newlines can be represented
    /// either as a carriage return/line feed pair (`"\r\n"`) or as a single line feed character
    /// (`"\n"`).
    #[derive(Deserialize, Hash, Ord, PartialOrd, Serialize)]
    FormattedAddress(String)
];

new_url_type![
    /// URI using the `https` scheme that a third party can use to initiate a login by the Relying
    /// Party.
    InitiateLoginUrl
];

new_url_type![
    /// URL using the `https` scheme with no query or fragment component that the OP asserts as its
    /// Issuer Identifier.
    IssuerUrl
    impl {
        /// Parse a string as a URL, with this URL as the base URL.
        ///
        /// See [`Url::parse`].
        pub fn join(&self, suffix: &str) -> Result<Url, url::ParseError> {
            if let Some('/') = self.1.chars().next_back() {
                Url::parse(&(self.1.clone() + suffix))
            } else {
                Url::parse(&(self.1.clone() + "/" + suffix))
            }
        }
    }
];

new_secret_type![
    /// Hint about the login identifier the End-User might use to log in.
    ///
    /// The use of this parameter is left to the OpenID Connect Provider's discretion.
    #[derive(Clone, Deserialize, Serialize)]
    LoginHint(String)
];

new_secret_type![
    /// Hint about the logout identifier the End-User might use to log out.
    ///
    /// The use of this parameter is left to the OpenID Connect Provider's discretion.
    #[derive(Clone, Deserialize, Serialize)]
    LogoutHint(String)
];

new_url_type![
    /// URL that references a logo for the Client application.
    LogoUrl
];

new_secret_type![
    /// String value used to associate a client session with an ID Token, and to mitigate replay
    /// attacks.
    #[derive(Clone, Deserialize, Serialize)]
    Nonce(String)
    impl {
        /// Generate a new random, base64-encoded 128-bit nonce.
        pub fn new_random() -> Self {
            Nonce::new_random_len(16)
        }
        /// Generate a new random, base64-encoded nonce of the specified length.
        ///
        /// # Arguments
        ///
        /// * `num_bytes` - Number of random bytes to generate, prior to base64-encoding.
        pub fn new_random_len(num_bytes: u32) -> Self {
            let random_bytes: Vec<u8> = (0..num_bytes).map(|_| thread_rng().gen::<u8>()).collect();
            Nonce::new(BASE64_URL_SAFE_NO_PAD.encode(random_bytes))
        }
    }
];

new_url_type![
    /// URL providing the OpenID Connect Provider's data usage policies for client applications.
    OpPolicyUrl
];

new_url_type![
    /// URL providing the OpenID Connect Provider's Terms of Service.
    OpTosUrl
];

new_url_type![
    /// URL providing a client application's data usage policy.
    PolicyUrl
];

new_url_type![
    /// The post logout redirect URL, which should be passed to the end session endpoint
    /// of providers implementing [OpenID Connect RP-Initiated Logout 1.0](
    /// https://openid.net/specs/openid-connect-rpinitiated-1_0.html).
    PostLogoutRedirectUrl
];

new_secret_type![
    /// Access token used by a client application to access the Client Registration endpoint.
    #[derive(Clone, Deserialize, Serialize)]
    RegistrationAccessToken(String)
];

new_url_type![
    /// URL of the Client Registration endpoint.
    RegistrationUrl
];

new_url_type![
    /// URL used to pass request parameters as JWTs by reference.
    RequestUrl
];

/// Informs the Authorization Server of the desired authorization processing flow, including what
/// parameters are returned from the endpoints used.
///
/// See [OAuth 2.0 Multiple Response Type Encoding Practices](
///     http://openid.net/specs/oauth-v2-multiple-response-types-1_0.html#ResponseTypesAndModes)
/// for further details.
#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize)]
pub struct ResponseTypes<RT: ResponseType>(
    #[serde(
        deserialize_with = "deserialize_space_delimited_vec",
        serialize_with = "crate::helpers::serialize_space_delimited_vec"
    )]
    Vec<RT>,
);
impl<RT: ResponseType> ResponseTypes<RT> {
    /// Create a new [`ResponseTypes<RT>`] to wrap the given [`Vec<RT>`].
    pub fn new(s: Vec<RT>) -> Self {
        ResponseTypes::<RT>(s)
    }
}
impl<RT: ResponseType> Deref for ResponseTypes<RT> {
    type Target = Vec<RT>;
    fn deref(&self) -> &Vec<RT> {
        &self.0
    }
}

new_url_type![
    /// URL for retrieving redirect URIs that should receive identical pairwise subject identifiers.
    SectorIdentifierUrl
];

new_url_type![
    /// URL for developer documentation for an OpenID Connect Provider.
    ServiceDocUrl
];

new_type![
    /// A user's street address.
    ///
    /// Full street address component, which MAY include house number, street name, Post Office Box,
    /// and multi-line extended street address information. This field MAY contain multiple lines,
    /// separated by newlines. Newlines can be represented either as a carriage return/line feed
    /// pair (`\r\n`) or as a single line feed character (`\n`).
    #[derive(Deserialize, Hash, Ord, PartialOrd, Serialize)]
    StreetAddress(String)
];

new_type![
    /// Locally unique and never reassigned identifier within the Issuer for the End-User, which is
    /// intended to be consumed by the client application.
    #[derive(Deserialize, Hash, Ord, PartialOrd, Serialize)]
    SubjectIdentifier(String)
];

new_url_type![
    /// URL for the relying party's Terms of Service.
    ToSUrl
];