totp-rs 6.0.0

RFC-compliant TOTP implementation with ease of use as a goal and additional QoL features.
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
#[cfg(feature = "otpauth")]
use {crate::algorithm::UnsupportedAlgorithm, alloc::string::String, url::ParseError};

/// Errors produced when building or parsing a [`Totp`](crate::Totp).
///
/// Variant naming follows a fixed convention:
/// * `Invalid<Field>`: the field was read but its value is not acceptable.
/// * `<Field>Parse` / `<Field>Decode`: the raw input could not be interpreted
///   (numeric parse vs. percent-decoding, respectively).
/// * `<Field>Mismatch`: two inputs contradict each other.
/// * `<Field>NotSet`: a required input is absent.
/// * otherwise, a descriptive name (e.g. `SecretTooShort`, `UrlTooLong`).
#[derive(Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum TotpError {
    // === Parameter validation errors ===
    /// Digits must be 6, 7, or 8.
    InvalidDigits { digits: u8 },
    /// Step is zero.
    InvalidStepZero,
    /// Secret is shorter than 128 bits (16 bytes).
    SecretTooShort { bits: usize },
    /// Secret was not set in builder.
    SecretNotSet,

    // === URL parsing errors (otpauth feature) ===
    #[cfg(feature = "otpauth")]
    /// Algorithm is not recognized.
    InvalidAlgorithm { cause: UnsupportedAlgorithm },
    #[cfg(feature = "otpauth")]
    /// Digits parameter is not a valid number.
    DigitsParse { digits: String },
    #[cfg(feature = "otpauth")]
    /// URL parsing failed.
    UrlParse(ParseError),
    #[cfg(feature = "otpauth")]
    /// Scheme must be "otpauth".
    InvalidScheme { scheme: String },
    #[cfg(feature = "otpauth")]
    /// Host must be "totp" (or "steam" with steam feature).
    InvalidHost { host: String },
    #[cfg(feature = "otpauth")]
    /// Secret parameter is not a valid non-padded base32 string.
    InvalidSecret,
    #[cfg(feature = "otpauth")]
    /// Step parameter is not a valid number.
    StepParse { step: String },

    // === Account/Issuer errors (otpauth feature) ===
    #[cfg(feature = "otpauth")]
    /// Account name is required to build an otpauth URL but was not set.
    AccountNameNotSet,
    #[cfg(feature = "otpauth")]
    /// Account name contains invalid character ':'.
    InvalidAccountName { account_name: String },
    #[cfg(feature = "otpauth")]
    /// Account name URL decoding failed.
    AccountNameDecode { account_name: String },
    #[cfg(feature = "otpauth")]
    /// Issuer contains invalid character ':'.
    InvalidIssuer { issuer: String },
    #[cfg(feature = "otpauth")]
    /// Issuer URL decoding failed.
    IssuerDecode { issuer: String },
    #[cfg(feature = "otpauth")]
    /// Issuer in path differs from issuer in query parameter.
    IssuerMismatch { path: String, query: String },

    // === QR Code Errors (qr feature) ===
    #[cfg(feature = "qr")]
    /// The generated URL is too long to encode as a QR code.
    UrlTooLong { url: String },
}

impl core::fmt::Display for TotpError {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            TotpError::SecretTooShort { bits } => write!(
                f,
                "The length of the shared secret MUST be at least 128 bits, got {} bits",
                bits
            ),
            TotpError::InvalidDigits { digits } => {
                #[cfg(not(feature = "steam"))]
                {
                    write!(f, "Digits must be 6, 7, or 8, not {}", digits)
                }
                #[cfg(feature = "steam")]
                {
                    write!(
                        f,
                        "Digits must be 6, 7, or 8 (or exactly 5 for the Steam algorithm), not {}",
                        digits
                    )
                }
            }
            TotpError::InvalidStepZero => write!(f, "Step cannot be 0."),
            TotpError::SecretNotSet => write!(
                f,
                "Secret was not set in builder. Consider using the `gen_secret` feature"
            ),
            #[cfg(feature = "otpauth")]
            TotpError::UrlParse(e) => write!(f, "Error parsing URL: {}", e),
            #[cfg(feature = "otpauth")]
            TotpError::InvalidAlgorithm { cause } => write!(f, "{}", cause),
            #[cfg(feature = "otpauth")]
            TotpError::InvalidScheme { scheme } => {
                write!(f, "Scheme must be \"otpauth\", not \"{}\"", scheme)
            }
            #[cfg(feature = "otpauth")]
            TotpError::InvalidHost { host } => {
                #[cfg(not(feature = "steam"))]
                {
                    write!(f, "Host must be \"totp\", not \"{}\"", host)
                }
                #[cfg(feature = "steam")]
                {
                    write!(f, "Host must be \"totp\" or \"steam\", not \"{}\"", host)
                }
            }
            #[cfg(feature = "otpauth")]
            TotpError::StepParse { step } => {
                write!(f, "Could not parse step \"{}\" as a number", step)
            }
            #[cfg(feature = "otpauth")]
            TotpError::DigitsParse { digits } => {
                write!(f, "Could not parse digits \"{}\" as a number", digits)
            }
            #[cfg(feature = "otpauth")]
            TotpError::AccountNameNotSet => {
                write!(f, "Account name is required to build an otpauth URL")
            }
            #[cfg(feature = "otpauth")]
            TotpError::InvalidAccountName { account_name } => {
                write!(
                    f,
                    "Account name cannot contain ':', found in \"{}\"",
                    account_name
                )
            }
            #[cfg(feature = "otpauth")]
            TotpError::InvalidSecret => {
                write!(f, "Could not parse secret as an unpadded base32 string")
            }
            #[cfg(feature = "otpauth")]
            TotpError::AccountNameDecode { account_name } => {
                write!(f, "Could not URL-decode account name \"{}\"", account_name)
            }
            #[cfg(feature = "otpauth")]
            TotpError::InvalidIssuer { issuer } => {
                write!(f, "Issuer cannot contain ':', found in \"{}\"", issuer)
            }
            #[cfg(feature = "otpauth")]
            TotpError::IssuerDecode { issuer } => {
                write!(f, "Could not URL-decode issuer \"{}\"", issuer)
            }
            #[cfg(feature = "otpauth")]
            TotpError::IssuerMismatch { path, query } => write!(
                f,
                "Issuer mismatch: path contains \"{}\" but query parameter contains \"{}\"",
                path, query
            ),
            #[cfg(feature = "qr")]
            TotpError::UrlTooLong { url } => write!(
                f,
                "Could not generate a QR code: the generated URL is too long to encode: {url}"
            ),
        }
    }
}

impl core::error::Error for TotpError {
    fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
        match self {
            #[cfg(feature = "otpauth")]
            TotpError::InvalidAlgorithm { cause } => Some(cause),
            #[cfg(feature = "otpauth")]
            TotpError::UrlParse(e) => Some(e),
            _ => None,
        }
    }
}

#[cfg(test)]
mod test {
    use super::TotpError;

    // === Parameter validation errors ===

    #[test]
    #[cfg(not(feature = "steam"))]
    fn invalid_digits() {
        let error = TotpError::InvalidDigits { digits: 5 };
        assert_eq!(error.to_string(), "Digits must be 6, 7, or 8, not 5");
    }

    #[test]
    #[cfg(not(feature = "steam"))]
    fn invalid_digits_high() {
        let error = TotpError::InvalidDigits { digits: 10 };
        assert_eq!(error.to_string(), "Digits must be 6, 7, or 8, not 10");
    }

    #[test]
    #[cfg(feature = "steam")]
    fn invalid_digits_steam() {
        let error = TotpError::InvalidDigits { digits: 6 };
        assert_eq!(
            error.to_string(),
            "Digits must be 6, 7, or 8 (or exactly 5 for the Steam algorithm), not 6"
        );
    }

    #[test]
    fn invalid_step_zero() {
        let error = TotpError::InvalidStepZero;
        assert_eq!(error.to_string(), "Step cannot be 0.");
    }

    #[test]
    fn secret_too_short() {
        let error = TotpError::SecretTooShort { bits: 64 };
        assert_eq!(
            error.to_string(),
            "The length of the shared secret MUST be at least 128 bits, got 64 bits"
        );
    }

    // === URL parsing errors (otpauth feature) ===

    #[test]
    #[cfg(feature = "otpauth")]
    fn invalid_algorithm() {
        let cause = crate::Algorithm::try_from("MD5".to_string()).unwrap_err();
        let error = TotpError::InvalidAlgorithm { cause };
        assert_eq!(error.to_string(), "Unsupported Algorithm: MD5");
    }

    #[test]
    #[cfg(feature = "otpauth")]
    fn digits_parse() {
        let error = TotpError::DigitsParse {
            digits: "six".to_string(),
        };
        assert_eq!(
            error.to_string(),
            "Could not parse digits \"six\" as a number"
        );
    }

    #[test]
    #[cfg(feature = "otpauth")]
    fn url_parse() {
        let error = TotpError::UrlParse(url::ParseError::EmptyHost);
        assert_eq!(error.to_string(), "Error parsing URL: empty host");
    }

    #[test]
    #[cfg(feature = "otpauth")]
    fn invalid_scheme() {
        let error = TotpError::InvalidScheme {
            scheme: "https".to_string(),
        };
        assert_eq!(
            error.to_string(),
            "Scheme must be \"otpauth\", not \"https\""
        );
    }

    #[test]
    #[cfg(all(feature = "otpauth", not(feature = "steam")))]
    fn invalid_host() {
        let error = TotpError::InvalidHost {
            host: "hotp".to_string(),
        };
        assert_eq!(error.to_string(), "Host must be \"totp\", not \"hotp\"");
    }

    #[test]
    #[cfg(all(feature = "otpauth", feature = "steam"))]
    fn invalid_host_steam() {
        let error = TotpError::InvalidHost {
            host: "hotp".to_string(),
        };
        assert_eq!(
            error.to_string(),
            "Host must be \"totp\" or \"steam\", not \"hotp\""
        );
    }

    #[test]
    #[cfg(feature = "otpauth")]
    fn invalid_secret() {
        let error = TotpError::InvalidSecret;
        assert_eq!(
            error.to_string(),
            "Could not parse secret as an unpadded base32 string"
        );
    }

    #[test]
    #[cfg(feature = "otpauth")]
    fn step_parse() {
        let error = TotpError::StepParse {
            step: "thirty".to_string(),
        };
        assert_eq!(
            error.to_string(),
            "Could not parse step \"thirty\" as a number"
        );
    }

    // === Account/Issuer errors (otpauth feature) ===

    #[test]
    #[cfg(feature = "otpauth")]
    fn account_name_not_set() {
        let error = TotpError::AccountNameNotSet;
        assert_eq!(
            error.to_string(),
            "Account name is required to build an otpauth URL"
        );
    }

    #[test]
    #[cfg(feature = "otpauth")]
    fn invalid_account_name() {
        let error = TotpError::InvalidAccountName {
            account_name: "user:name".to_string(),
        };
        assert_eq!(
            error.to_string(),
            "Account name cannot contain ':', found in \"user:name\""
        );
    }

    #[test]
    #[cfg(feature = "otpauth")]
    fn account_name_decode() {
        let error = TotpError::AccountNameDecode {
            account_name: "bad%ZZencoding".to_string(),
        };
        assert_eq!(
            error.to_string(),
            "Could not URL-decode account name \"bad%ZZencoding\""
        );
    }

    #[test]
    #[cfg(feature = "otpauth")]
    fn invalid_issuer() {
        let error = TotpError::InvalidIssuer {
            issuer: "Iss:uer".to_string(),
        };
        assert_eq!(
            error.to_string(),
            "Issuer cannot contain ':', found in \"Iss:uer\""
        );
    }

    #[test]
    #[cfg(feature = "otpauth")]
    fn issuer_decode() {
        let error = TotpError::IssuerDecode {
            issuer: "bad%ZZissuer".to_string(),
        };
        assert_eq!(
            error.to_string(),
            "Could not URL-decode issuer \"bad%ZZissuer\""
        );
    }

    #[test]
    #[cfg(feature = "otpauth")]
    fn issuer_mismatch() {
        let error = TotpError::IssuerMismatch {
            path: "Google".to_string(),
            query: "Github".to_string(),
        };
        assert_eq!(
            error.to_string(),
            "Issuer mismatch: path contains \"Google\" but query parameter contains \"Github\""
        );
    }

    // === Error trait implementation ===

    #[test]
    fn error_source_none() {
        use core::error::Error;

        let error = TotpError::SecretTooShort { bits: 64 };
        assert!(error.source().is_none());

        let error = TotpError::InvalidDigits { digits: 5 };
        assert!(error.source().is_none());

        let error = TotpError::InvalidStepZero;
        assert!(error.source().is_none());
    }

    #[test]
    #[cfg(feature = "otpauth")]
    fn error_source_url_parse() {
        use core::error::Error;

        let parse_error = url::ParseError::EmptyHost;
        let error = TotpError::UrlParse(parse_error);

        let source = error.source().expect("source should be Some");
        assert_eq!(source.to_string(), "empty host");
    }

    #[test]
    #[cfg(feature = "otpauth")]
    fn error_source_invalid_algorithm() {
        use core::error::Error;

        let cause = crate::Algorithm::try_from("MD5".to_string()).unwrap_err();
        let error = TotpError::InvalidAlgorithm { cause };

        let source = error.source().expect("source should be Some");
        assert_eq!(source.to_string(), "Unsupported Algorithm: MD5");
    }

    #[test]
    #[cfg(feature = "otpauth")]
    fn error_source_none_otpauth_variants() {
        use core::error::Error;

        let error = TotpError::InvalidScheme {
            scheme: "https".to_string(),
        };
        assert!(error.source().is_none());

        let error = TotpError::IssuerMismatch {
            path: "A".to_string(),
            query: "B".to_string(),
        };
        assert!(error.source().is_none());
    }
}