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
//! Failure modes of identity operations.

use std::fmt;

use actix_session::{SessionGetError, SessionInsertError};
use actix_web::{cookie::time::error::ComponentRange, http::StatusCode, ResponseError};

/// Error that can occur during login attempts.
#[derive(Debug)]
pub struct LoginError(SessionInsertError);

impl fmt::Display for LoginError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl std::error::Error for LoginError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        Some(&self.0)
    }
}

impl ResponseError for LoginError {
    fn status_code(&self) -> StatusCode {
        StatusCode::UNAUTHORIZED
    }
}

impl From<SessionInsertError> for LoginError {
    fn from(error: SessionInsertError) -> Self {
        Self(error)
    }
}

/// Error encountered when working with a session that has expired.
#[derive(Debug)]
pub struct SessionExpiryError(ComponentRange);

impl fmt::Display for SessionExpiryError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("The given session has expired and is no longer valid")
    }
}

impl std::error::Error for SessionExpiryError {}

/// The identity information has been lost.
///
/// Seeing this error in user code indicates a bug in actix-identity.
#[derive(Debug)]
#[non_exhaustive]
pub struct LostIdentityError;

impl fmt::Display for LostIdentityError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(
            "The identity information in the current session has disappeared \
            after having been successfully validated. This is likely to be a bug.",
        )
    }
}

impl std::error::Error for LostIdentityError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        Some(self)
    }
}

/// There is no identity information attached to the current session.
#[derive(Debug)]
#[non_exhaustive]
pub struct MissingIdentityError;

impl fmt::Display for MissingIdentityError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("There is no identity information attached to the current session.")
    }
}

impl std::error::Error for MissingIdentityError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        Some(self)
    }
}

/// Errors that can occur while retrieving an identity.
#[derive(Debug)]
#[non_exhaustive]
pub enum GetIdentityError {
    /// The session has expired.
    SessionExpiryError(SessionExpiryError),

    /// No identity is found in a session.
    MissingIdentityError(MissingIdentityError),

    /// Failed to accessing the session store.
    SessionGetError(SessionGetError),

    /// Identity info was lost after being validated.
    ///
    /// Seeing this error indicates a bug in actix-identity.
    LostIdentityError(LostIdentityError),
}

impl fmt::Display for GetIdentityError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::SessionExpiryError(err) => write!(f, "{err}"),
            Self::MissingIdentityError(err) => write!(f, "{err}"),
            Self::SessionGetError(err) => write!(f, "{err}"),
            Self::LostIdentityError(err) => write!(f, "{err}"),
        }
    }
}

impl std::error::Error for GetIdentityError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::SessionExpiryError(err) => Some(err),
            Self::MissingIdentityError(err) => Some(err),
            Self::SessionGetError(err) => Some(err),
            Self::LostIdentityError(err) => Some(err),
        }
    }
}

impl ResponseError for GetIdentityError {
    fn status_code(&self) -> StatusCode {
        StatusCode::UNAUTHORIZED
    }
}

impl From<LostIdentityError> for GetIdentityError {
    fn from(error: LostIdentityError) -> Self {
        Self::LostIdentityError(error)
    }
}

impl From<MissingIdentityError> for GetIdentityError {
    fn from(error: MissingIdentityError) -> Self {
        Self::MissingIdentityError(error)
    }
}

impl From<ComponentRange> for GetIdentityError {
    fn from(error: ComponentRange) -> Self {
        Self::SessionExpiryError(SessionExpiryError(error))
    }
}

impl From<SessionGetError> for GetIdentityError {
    fn from(source: SessionGetError) -> Self {
        Self::SessionGetError(source)
    }
}