pkce-std 0.2.1

Handling Proof Key for Code Exchange.
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
//! PKCE code verifiers.
//!
//! The [`Verifier<'_>`] type represents PKCE code verifiers, which are strings
//! that consist of valid characters (see [`string`]) and have certain lengths
//! (see [`length`]).
//!
//! # Examples
//!
//! Generating random verifiers:
//!
//! ```
//! use pkce_std::{length::Length, verifier::Verifier};
//!
//! let length = Length::default();
//!
//! let verifier = Verifier::generate(length);
//! let other = Verifier::generate(length);
//!
//! assert_ne!(verifier, other);
//! ```
//!
//! Generating verifiers from random bytes:
//!
//! ```
//! use pkce_std::{count::Count, verifier::Verifier};
//!
//! let count = Count::default();
//!
//! let verifier = Verifier::generate_encode(count);
//! let other = Verifier::generate_encode(count);
//!
//! assert_ne!(verifier, other);
//! ```
//!
//! [`Verifier<'_>`]: Verifier

use std::{
    borrow::Cow,
    fmt,
    hash::{Hash, Hasher},
};

use const_macros::{const_map_err, const_none, const_ok, const_try};
use constant_time_eq::constant_time_eq;

#[cfg(feature = "static")]
use into_static::IntoStatic;

#[cfg(feature = "diagnostics")]
use miette::Diagnostic;

#[cfg(feature = "serde")]
use serde::{Deserialize, Deserializer, Serialize, Serializer, de};

use thiserror::Error;

use crate::{
    challenge::Challenge,
    check::string::{self, const_check_str},
    count::{self, Count},
    encoding, generate,
    length::{self, Length},
    method::Method,
};

/// Represents the error message for invalid verifiers.
pub const ERROR: &str = "invalid verifier; check the length and characters";

/// Represents errors that can occur when constructing verifiers.
///
/// There are two cases when constructing can fail:
///
/// - [`Length::check`] fails, which means that the length of the string is invalid;
/// - [`string::check`] fails, which means the string contains invalid characters.
#[derive(Debug, Error)]
#[cfg_attr(feature = "diagnostics", derive(Diagnostic))]
pub enum Error {
    /// Invalid verifier length.
    #[error("invalid verifier length")]
    #[cfg_attr(
        feature = "diagnostics",
        diagnostic(
            code(pkce_std::verifier::length),
            help("check the length of the verifier")
        )
    )]
    Length(#[from] length::Error),

    /// Invalid character(s) in verifier.
    #[error("verifier contains invalid character(s)")]
    #[cfg_attr(
        feature = "diagnostics",
        diagnostic(
            code(pkce_std::verifier::check),
            help("make sure the verifier is composed of valid characters only")
        )
    )]
    String(#[from] string::Error),
}

/// Represents PKCE code verifiers.
///
/// Refer to the [module] documentation for more information.
///
/// # Examples
///
/// ```
/// use pkce_std::verifier::Verifier;
///
/// let string = "dGhhbmtzIGZvciByZWFkaW5nIGRvY3MhIH4gbmVraXQ";
///
/// let expected = Verifier::borrowed(string).unwrap();
///
/// let bytes = "thanks for reading docs! ~ nekit";
///
/// let verifier = Verifier::encode(bytes).unwrap();
///
/// // `verifier` and `expected` are compared in constant time!
/// assert_eq!(verifier, expected);
/// ```
///
/// [module]: self
#[derive(Debug, Clone)]
pub struct Verifier<'v> {
    value: Cow<'v, str>,
}

#[cfg(feature = "serde")]
impl Serialize for Verifier<'_> {
    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        self.get().serialize(serializer)
    }
}

#[cfg(feature = "serde")]
impl<'de> Deserialize<'de> for Verifier<'_> {
    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        let value = Cow::deserialize(deserializer)?;

        Self::new(value).map_err(de::Error::custom)
    }
}

impl fmt::Display for Verifier<'_> {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.get().fmt(formatter)
    }
}

impl Verifier<'_> {
    /// Returns the borrowed string.
    pub fn get(&self) -> &str {
        self.value.as_ref()
    }
}

impl AsRef<str> for Verifier<'_> {
    fn as_ref(&self) -> &str {
        self.get()
    }
}

impl PartialEq for Verifier<'_> {
    fn eq(&self, other: &Self) -> bool {
        constant_time_eq(self.get().as_bytes(), other.get().as_bytes())
    }
}

impl Eq for Verifier<'_> {}

impl Hash for Verifier<'_> {
    fn hash<H: Hasher>(&self, hasher: &mut H) {
        self.get().hash(hasher);
    }
}

impl Verifier<'_> {
    /// Generates random [`Self`] with specified length.
    pub fn generate(length: Length) -> Self {
        // SAFETY: `generate::string(length)` creates valid values for `Self`,
        // meaning that their length is exactly `length` and they consist of valid characters.
        unsafe { Self::owned_unchecked(generate::string(length)) }
    }

    /// Generates random [`Self`] with default length.
    pub fn generate_default() -> Self {
        Self::generate(Length::default())
    }

    /// Generates `count` random bytes length and encodes them into [`Self`].
    pub fn generate_encode(count: Count) -> Self {
        // SAFETY: `generate::bytes(count)` creates valid values for `Self::encode_unchecked`,
        // meaning that their length is exactly `count`.
        unsafe { Self::encode_unchecked(generate::bytes(count)) }
    }

    /// Generates random bytes of default length and encodes them into [`Self`].
    pub fn generate_encode_default() -> Self {
        Self::generate_encode(Count::default())
    }
}

impl Verifier<'_> {
    /// Computes the [`Challenge`] of [`Self`] with the given [`Method`].
    pub fn challenge_using(&self, method: Method) -> Challenge {
        Challenge::create_using(method, self)
    }

    /// Computes the [`Challenge`] of [`Self`] with the default [`Method`].
    pub fn challenge(&self) -> Challenge {
        self.challenge_using(Method::default())
    }

    /// Verifies the given [`Challenge`] against [`Self`].
    pub fn verify(&self, challenge: &Challenge) -> bool {
        let expected = self.challenge_using(challenge.method());

        challenge == &expected
    }
}

impl<'v> Verifier<'v> {
    /// Constructs [`Self`], provided that the given value is valid.
    ///
    /// # Errors
    ///
    /// See [`Self::check`] for more information.
    pub fn new(value: Cow<'v, str>) -> Result<Self, Error> {
        Self::check(value.as_ref())?;

        // SAFETY: `value` consists of valid characters
        // and its length is in the valid range for `Self`
        Ok(unsafe { Self::new_unchecked(value) })
    }

    /// Constructs [`Self`] without checking the value.
    ///
    /// # Safety
    ///
    /// The caller must ensure that `value` is valid for [`Self`].
    ///
    /// The value can be checked using [`Self::check`].
    pub const unsafe fn new_unchecked(value: Cow<'v, str>) -> Self {
        Self { value }
    }

    /// Constructs [`Self`] from borrowed `value`, provided it is valid.
    ///
    /// # Errors
    ///
    /// See [`Self::new`] for more information.
    pub fn borrowed(value: &'v str) -> Result<Self, Error> {
        Self::new(Cow::Borrowed(value))
    }

    /// Constructs [`Self`] from borrowed `value` without checking it.
    ///
    /// # Safety
    ///
    /// See [`Self::new_unchecked`] for more information.
    pub const unsafe fn borrowed_unchecked(value: &'v str) -> Self {
        // SAFETY: this function is `unsafe`, so the caller must ensure
        // that `value` is valid for `Self`
        unsafe { Self::new_unchecked(Cow::Borrowed(value)) }
    }

    /// Constructs [`Self`] from owned `value`, provided it is valid.
    ///
    /// # Errors
    ///
    /// See [`Self::new`] for more information.
    pub fn owned(value: String) -> Result<Self, Error> {
        Self::new(Cow::Owned(value))
    }

    /// Constructs [`Self`] from owned `value` without checking it.
    ///
    /// # Safety
    ///
    /// See [`Self::new_unchecked`] for more information.
    pub const unsafe fn owned_unchecked(value: String) -> Self {
        // SAFETY: this function is `unsafe`, so the caller must ensure
        // that `value` is valid for `Self`
        unsafe { Self::new_unchecked(Cow::Owned(value)) }
    }

    /// Similar to [`borrowed`], but can be used in `const` contexts.
    ///
    /// # Note
    ///
    /// One may need to increase the recursion limit when using longer strings.
    ///
    /// This is done via applying the `recursion_limit` attribute to the crate:
    ///
    /// ```
    /// #![recursion_limit = "256"]
    /// ```
    ///
    /// # Errors
    ///
    /// See [`const_check_str`] for more information.
    ///
    /// [`borrowed`]: Self::borrowed
    /// [`const_check_str`]: Self::const_check_str
    pub const fn const_borrowed(value: &'v str) -> Result<Self, Error> {
        const_try!(Self::const_check_str(value));

        // SAFETY: `value` is valid for `Self` here
        Ok(unsafe { Self::borrowed_unchecked(value) })
    }

    /// Similar to [`const_borrowed`], but errors are discarded.
    ///
    /// [`const_borrowed`]: Self::const_borrowed
    pub const fn const_borrowed_ok(value: &'v str) -> Option<Self> {
        const_none!(const_ok!(Self::const_check_str(value)));

        // SAFETY: `value` is valid for `Self` here
        Some(unsafe { Self::borrowed_unchecked(value) })
    }

    /// Constantly checks if the given string is valid for [`Self`].
    ///
    /// # Note
    ///
    /// One may need to increase the recursion limit when checking longer strings.
    ///
    /// This is done via applying the `recursion_limit` attribute to the crate:
    ///
    /// ```
    /// #![recursion_limit = "256"]
    /// ```
    ///
    /// # Errors
    ///
    /// Returns [`enum@Error`] if the string is invalid, which means either:
    ///
    /// - the length of the string is invalid (see [`Length::check`]);
    /// - the string contains invalid character(s) (see [`string::check`]).
    pub const fn const_check_str(string: &str) -> Result<(), Error> {
        const_try!(const_map_err!(Length::check(string.len()) => Error::Length));

        const_try!(const_map_err!(const_check_str(string) => Error::String));

        Ok(())
    }

    /// Checks if the given string is valid for [`Self`].
    ///
    /// # Errors
    ///
    /// Returns [`enum@Error`] if the string is invalid, which means either:
    ///
    /// - the length of the string is invalid (see [`Length::check`]);
    /// - the string contains invalid character(s) (see [`string::check`]).
    pub fn check_str(string: &str) -> Result<(), Error> {
        Length::check(string.len())?;

        string::check_str(string)?;

        Ok(())
    }

    /// Similar to [`check_str`], except it is generic over [`AsRef<str>`].
    ///
    /// # Errors
    ///
    /// Any [`enum@Error`] returned by [`check_str`] is propagated.
    ///
    /// [`check_str`]: Self::check_str
    pub fn check<S: AsRef<str>>(value: S) -> Result<(), Error> {
        Self::check_str(value.as_ref())
    }

    /// Consumes [`Self`] and returns the contained string.
    pub fn take(self) -> Cow<'v, str> {
        self.value
    }
}

impl Verifier<'_> {
    /// Encodes the given `bytes` into [`Self`], provided `bytes` has valid length.
    ///
    /// # Errors
    ///
    /// Returns [`count::Error`] if the length of `bytes` is invalid.
    pub fn encode<B: AsRef<[u8]>>(bytes: B) -> Result<Self, count::Error> {
        Count::check(bytes.as_ref().len())?;

        // SAFETY: `bytes` has length in the valid range for `Self::encode_unchecked`
        Ok(unsafe { Self::encode_unchecked(bytes) })
    }

    /// Encodes the given `bytes` into [`Self`] without checking `bytes` length.
    ///
    /// # Safety
    ///
    /// The caller must ensure that `bytes` has valid length.
    ///
    /// The `bytes` can be checked using [`Count::check`] on its length.
    pub unsafe fn encode_unchecked<B: AsRef<[u8]>>(bytes: B) -> Self {
        let string = encoding::encode(bytes);

        // SAFETY: this function is `unsafe`, so the caller must ensure that `bytes`
        // has length in the valid range for encoding, which produces valid values for `Self`
        unsafe { Self::owned_unchecked(string) }
    }
}

/// Constructs [`Verifier`] from `value`, panicking if it is invalid.
#[macro_export]
macro_rules! const_borrowed_verifier {
    ($value: expr) => {
        $crate::verifier::Verifier::const_borrowed_ok($value).expect($crate::verifier::ERROR)
    };
}

/// An alias for [`Verifier<'static>`].
#[cfg(feature = "static")]
pub type StaticVerifier = Verifier<'static>;

#[cfg(feature = "static")]
impl IntoStatic for Verifier<'_> {
    type Static = StaticVerifier;

    fn into_static(self) -> Self::Static {
        // SAFETY: calling `into_static` does not change `value` validity
        unsafe { Self::Static::new_unchecked(self.value.into_static()) }
    }
}