Debian Codegraph @

third_party/rust/authenticator/src/errors.rs

raw
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 pub use crate::ctap2::commands::{client_pin::PinError, CommandError};
6 pub use crate::transport::errors::HIDError;
7 use std::fmt;
8 use std::io;
9 use std::sync::mpsc;
10
11 // This composite error type is patterned from Phil Daniels' blog:
12 // https://www.philipdaniels.com/blog/2019/defining-rust-error-types/
13
14 #[derive(Debug)]
15 pub enum UnsupportedOption {
16 CredProtect,
17 EmptyAllowList,
18 MaxPinLength,
19 PubCredParams,
20 ResidentKey,
21 UserVerification,
22 }
23
24 #[derive(Debug)]
25 pub enum AuthenticatorError {
26 // Errors from external libraries...
27 Io(io::Error),
28 // Errors raised by us...
29 InvalidRelyingPartyInput,
30 NoConfiguredTransports,
31 Platform,
32 InternalError(String),
33 U2FToken(U2FTokenError),
34 Custom(String),
35 VersionMismatch(&'static str, u32),
36 HIDError(HIDError),
37 CryptoError,
38 PinError(PinError),
39 UnsupportedOption(UnsupportedOption),
40 CancelledByUser,
41 CredentialExcluded,
42 }
43
44 impl std::error::Error for AuthenticatorError {}
45
46 impl fmt::Display for AuthenticatorError {
47 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
48 match *self {
49 AuthenticatorError::Io(ref err) => err.fmt(f),
50 AuthenticatorError::InvalidRelyingPartyInput => {
51 write!(f, "invalid input from relying party")
52 }
53 AuthenticatorError::NoConfiguredTransports => write!(
54 f,
55 "no transports were configured in the authenticator service"
56 ),
57 AuthenticatorError::Platform => write!(f, "unknown platform error"),
58 AuthenticatorError::InternalError(ref err) => write!(f, "internal error: {err}"),
59 AuthenticatorError::U2FToken(ref err) => {
60 write!(f, "A u2f token error occurred {err:?}")
61 }
62 AuthenticatorError::Custom(ref err) => write!(f, "A custom error occurred {err:?}"),
63 AuthenticatorError::VersionMismatch(manager, version) => {
64 write!(f, "{manager} expected arguments of version CTAP{version}")
65 }
66 AuthenticatorError::HIDError(ref e) => write!(f, "Device error: {e}"),
67 AuthenticatorError::CryptoError => {
68 write!(f, "The cryptography implementation encountered an error")
69 }
70 AuthenticatorError::PinError(ref e) => write!(f, "PIN Error: {e}"),
71 AuthenticatorError::UnsupportedOption(ref e) => {
72 write!(f, "Unsupported option: {e:?}")
73 }
74 AuthenticatorError::CancelledByUser => {
75 write!(f, "Cancelled by user.")
76 }
77 AuthenticatorError::CredentialExcluded => {
78 write!(f, "Credential excluded.")
79 }
80 }
81 }
82 }
83
84 impl From<io::Error> for AuthenticatorError {
85 fn from(err: io::Error) -> AuthenticatorError {
86 AuthenticatorError::Io(err)
87 }
88 }
89
90 impl From<HIDError> for AuthenticatorError {
91 fn from(err: HIDError) -> AuthenticatorError {
92 AuthenticatorError::HIDError(err)
93 }
94 }
95
96 impl From<CommandError> for AuthenticatorError {
97 fn from(err: CommandError) -> AuthenticatorError {
98 AuthenticatorError::HIDError(HIDError::Command(err))
99 }
100 }
101
102 impl<T> From<mpsc::SendError<T>> for AuthenticatorError {
103 fn from(err: mpsc::SendError<T>) -> AuthenticatorError {
104 AuthenticatorError::InternalError(err.to_string())
105 }
106 }
107
108 #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
109 pub enum U2FTokenError {
110 Unknown = 1,
111 NotSupported = 2,
112 InvalidState = 3,
113 ConstraintError = 4,
114 NotAllowed = 5,
115 }
116
117 impl U2FTokenError {
118 fn as_str(&self) -> &str {
119 match *self {
120 U2FTokenError::Unknown => "unknown",
121 U2FTokenError::NotSupported => "not supported",
122 U2FTokenError::InvalidState => "invalid state",
123 U2FTokenError::ConstraintError => "constraint error",
124 U2FTokenError::NotAllowed => "not allowed",
125 }
126 }
127 }
128
129 impl std::fmt::Display for U2FTokenError {
130 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
131 write!(f, "{}", self.as_str())
132 }
133 }
134
135 impl std::error::Error for U2FTokenError {}