scram_2/
error.rs

1use std::{error, fmt};
2
3/// The SCRAM mechanism error cases.
4#[derive(Debug, PartialEq)]
5pub enum Error {
6    /// A message wasn't formatted as required. `Kind` contains further information.
7    ///
8    /// RFC5803 section 7 describes the format of the exchanged messages.
9    Protocol(Kind),
10    /// The server required a mandatory extension to be present that this client doesn't support.
11    UnsupportedExtension,
12    /// The server couldn't be validated. This usually means that the server didn't posess a stored
13    /// key to verify the credentials.
14    InvalidServer,
15    /// The server rejected the authentication request. `String` contains a message from the server.
16    Authentication(String),
17    /// The username supplied was not valid
18    InvalidUser(String),
19}
20
21/// The kinds of protocol errors.
22#[derive(Debug, PartialEq)]
23pub enum Kind {
24    /// The server responded with a nonce that doesn't start with our nonce.
25    InvalidNonce,
26    /// The content of the field `Field` is invalid.
27    InvalidField(Field),
28    /// The field `Field` was expected but not found.
29    ExpectedField(Field),
30}
31
32/// The fields used in the exchanged messages.
33#[derive(Debug, PartialEq)]
34pub enum Field {
35    /// Nonce
36    Nonce,
37    /// Salt
38    Salt,
39    /// Iterations
40    Iterations,
41    /// Verify or Error
42    VerifyOrError,
43    /// Channel Binding
44    ChannelBinding,
45    /// Authorization ID
46    Authzid,
47    /// Authcid
48    Authcid,
49    /// GS2Header
50    GS2Header,
51    /// Client Proof
52    Proof,
53}
54
55impl fmt::Display for Error {
56    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
57        use self::Error::*;
58        use self::Kind::*;
59        match *self {
60            Protocol(InvalidNonce) => write!(fmt, "Invalid nonce"),
61            Protocol(InvalidField(ref field)) => write!(fmt, "Invalid field {:?}", field),
62            Protocol(ExpectedField(ref field)) => write!(fmt, "Expected field {:?}", field),
63            UnsupportedExtension => write!(fmt, "Unsupported extension"),
64            InvalidServer => write!(fmt, "Server failed validation"),
65            InvalidUser(ref username) => write!(fmt, "Invalid user: '{}'", username),
66            Authentication(ref msg) => write!(fmt, "authentication error {}", msg),
67        }
68    }
69}
70
71impl error::Error for Error {
72    fn description(&self) -> &str {
73        use self::Error::*;
74        use self::Kind::*;
75        match *self {
76            Protocol(InvalidNonce) => "Invalid nonce",
77            Protocol(InvalidField(_)) => "Invalid field",
78            Protocol(ExpectedField(_)) => "Expected field",
79            UnsupportedExtension => "Unsupported extension",
80            InvalidServer => "Server failed validation",
81            InvalidUser(_) => "Invalid user",
82            Authentication(_) => "Unspecified error",
83        }
84    }
85}