actix_web_httpauth/headers/www_authenticate/challenge/bearer/
errors.rs

1use std::fmt;
2
3use actix_web::http::StatusCode;
4
5/// Bearer authorization error types, described in [RFC 6750].
6///
7/// [RFC 6750]: https://tools.ietf.org/html/rfc6750#section-3.1
8#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
9pub enum Error {
10    /// The request is missing a required parameter, includes an unsupported parameter or parameter
11    /// value, repeats the same parameter, uses more than one method for including an access token,
12    /// or is otherwise malformed.
13    InvalidRequest,
14
15    /// The access token provided is expired, revoked, malformed, or invalid for other reasons.
16    InvalidToken,
17
18    /// The request requires higher privileges than provided by the access token.
19    InsufficientScope,
20}
21
22impl Error {
23    /// Returns [HTTP status code] suitable for current error type.
24    ///
25    /// [HTTP status code]: `actix_web::http::StatusCode`
26    #[allow(clippy::trivially_copy_pass_by_ref)]
27    pub fn status_code(&self) -> StatusCode {
28        match self {
29            Error::InvalidRequest => StatusCode::BAD_REQUEST,
30            Error::InvalidToken => StatusCode::UNAUTHORIZED,
31            Error::InsufficientScope => StatusCode::FORBIDDEN,
32        }
33    }
34
35    #[doc(hidden)]
36    #[allow(clippy::trivially_copy_pass_by_ref)]
37    pub fn as_str(&self) -> &str {
38        match self {
39            Error::InvalidRequest => "invalid_request",
40            Error::InvalidToken => "invalid_token",
41            Error::InsufficientScope => "insufficient_scope",
42        }
43    }
44}
45
46impl fmt::Display for Error {
47    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
48        f.write_str(self.as_str())
49    }
50}