-
Notifications
You must be signed in to change notification settings - Fork 378
/
Copy patherrors.rs
66 lines (63 loc) · 2.59 KB
/
errors.rs
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
use tonic::Status;
#[derive(thiserror::Error, Debug, PartialEq)]
pub enum AuthError {
#[error("The `Authorization` HTTP header is required but was not specified")]
HttpAuthHeaderMissing,
#[error("The `Authorization` HTTP header has invalid value")]
HttpAuthHeaderInvalid,
#[error("The authentication scheme in the `Authorization` HTTP header is not supported")]
HttpAuthHeaderUnsupportedScheme,
#[error("The `Basic` HTTP authentication scheme is not allowed")]
BasicNotAllowed,
#[error("The `Basic` HTTP authentication credentials were rejected")]
BasicRejected,
#[error("Authentication is required but no JWT was specified")]
JwtMissing,
#[error("Authentication using a JWT is not allowed")]
JwtNotAllowed,
#[error("The JWT is invalid")]
JwtInvalid,
#[error("The JWT has expired")]
JwtExpired,
#[error("The JWT is immature (not valid yet)")]
JwtImmature,
#[error("Auth string does not conform to '<scheme> <token>' form")]
AuthStringMalformed,
#[error("Expected authorization header but none given")]
AuthHeaderNotFound,
#[error("Expected authorization proxy header but none given")]
AuthProxyHeaderNotFound,
#[error("Failed to parse auth proxy header")]
AuthProxyHeaderInvalid,
#[error("Non-ASCII auth header")]
AuthHeaderNonAscii,
#[error("Authentication failed")]
Other,
}
impl AuthError {
pub fn code(&self) -> &'static str {
match self {
Self::HttpAuthHeaderMissing => "AUTH_HTTP_HEADER_MISSING",
Self::HttpAuthHeaderInvalid => "AUTH_HTTP_HEADER_INVALID",
Self::HttpAuthHeaderUnsupportedScheme => "AUTH_HTTP_HEADER_UNSUPPORTED_SCHEME",
Self::BasicNotAllowed => "AUTH_BASIC_NOT_ALLOWED",
Self::BasicRejected => "AUTH_BASIC_REJECTED",
Self::JwtMissing => "AUTH_JWT_MISSING",
Self::JwtNotAllowed => "AUTH_JWT_NOT_ALLOWED",
Self::JwtInvalid => "AUTH_JWT_INVALID",
Self::JwtExpired => "AUTH_JWT_EXPIRED",
Self::JwtImmature => "AUTH_JWT_IMMATURE",
Self::AuthStringMalformed => "AUTH_HEADER_MALFORMED",
Self::AuthHeaderNotFound => "AUTH_HEADER_NOT_FOUND",
Self::AuthProxyHeaderNotFound => "AUTH_PROXY_HEADER_NOT_FOUND",
Self::AuthProxyHeaderInvalid => "AUTH_PROXY_HEADER_INVALID",
Self::AuthHeaderNonAscii => "AUTH_HEADER_MALFORMED",
Self::Other => "AUTH_FAILED",
}
}
}
impl From<AuthError> for Status {
fn from(e: AuthError) -> Self {
Status::unauthenticated(format!("AuthError: {}", e))
}
}