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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
use std::fmt;
use std::str;
use std::error::Error;
#[derive(Debug, Clone)]
pub enum AccessTokenProviderError {
BadAuthorizationRequest(AuthorizationRequestError),
Client(String),
Server(String),
Connection(String),
Parse(String),
Credentials(super::credentials::CredentialsError),
Other(String),
}
#[derive(Debug, Clone)]
pub struct AuthorizationRequestError {
pub error: AuthorizationServerErrorCode,
pub error_description: Option<String>,
pub error_uri: Option<String>,
}
impl fmt::Display for AuthorizationRequestError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut error_msg =
format!(
"An invalid request was sent to the authorization server. \
The error is \"{:?}\".",
self.error,
);
if let Some(ref msg) = self.error_description {
error_msg.push_str(&format!(" The message from the server is \"{}\".", msg));
}
if let Some(ref uri) = self.error_uri {
error_msg.push_str(&format!(" You can find more information at \"{}\".", uri));
}
write!(f, "{}", error_msg)
}
}
impl str::FromStr for AuthorizationServerErrorCode {
type Err = AccessTokenProviderError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"invalid_request" => Ok(AuthorizationServerErrorCode::InvalidRequest),
"invalid_client" => Ok(AuthorizationServerErrorCode::InvalidClient),
"invalid_grant" => Ok(AuthorizationServerErrorCode::InvalidGrant),
"unauthorized_client" => Ok(AuthorizationServerErrorCode::UnauthorizedClient),
"unsupported_grant_type" => Ok(AuthorizationServerErrorCode::UnsupportedGrantType),
"invalid_scope" => Ok(AuthorizationServerErrorCode::InvalidScope),
x => Err(AccessTokenProviderError::Other(
format!("'{}' is not a valid error kind.", x),
)),
}
}
}
#[derive(Debug, Clone)]
pub enum AuthorizationServerErrorCode {
InvalidRequest,
InvalidClient,
InvalidGrant,
UnauthorizedClient,
UnsupportedGrantType,
InvalidScope,
}
impl fmt::Display for AccessTokenProviderError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
AccessTokenProviderError::BadAuthorizationRequest(ref err) => {
write!(f, "Bad authorization request: {}", err)
}
AccessTokenProviderError::Client(ref msg) => write!(f, "Client error: {}", msg),
AccessTokenProviderError::Server(ref msg) => write!(f, "Server error: {}", msg),
AccessTokenProviderError::Connection(ref msg) => write!(f, "Connection error: {}", msg),
AccessTokenProviderError::Parse(ref msg) => write!(f, "Parse error: {}", msg),
AccessTokenProviderError::Credentials(ref inner) => {
write!(f, "Problem with credentials caused by {}", inner)
}
AccessTokenProviderError::Other(ref msg) => write!(f, "Other error {}", msg),
}
}
}
impl Error for AccessTokenProviderError {
fn description(&self) -> &str {
match *self {
AccessTokenProviderError::BadAuthorizationRequest(_) => {
"an invalid request was sent to the authorization server"
}
AccessTokenProviderError::Client(_) => "the request to the token service was invalid",
AccessTokenProviderError::Server(_) => "the token service returned an error",
AccessTokenProviderError::Connection(_) => "the connection broke",
AccessTokenProviderError::Parse(_) => {
"the response from the token service couldn't be parsed"
}
AccessTokenProviderError::Credentials(_) => "problem with the credentials",
AccessTokenProviderError::Other(_) => "something unexpected happened",
}
}
fn cause(&self) -> Option<&Error> {
match *self {
AccessTokenProviderError::Credentials(ref inner) => Some(inner),
_ => None,
}
}
}
impl From<AccessTokenProviderError> for ::token_manager::error::ErrorKind {
fn from(what: AccessTokenProviderError) -> ::token_manager::error::ErrorKind {
::token_manager::error::ErrorKind::AccessTokenProvider(what)
}
}
impl From<super::credentials::CredentialsError> for AccessTokenProviderError {
fn from(what: super::credentials::CredentialsError) -> AccessTokenProviderError {
AccessTokenProviderError::Credentials(what)
}
}
impl From<::std::io::Error> for AccessTokenProviderError {
fn from(what: ::std::io::Error) -> Self {
AccessTokenProviderError::Connection(what.to_string())
}
}
impl From<::std::str::Utf8Error> for AccessTokenProviderError {
fn from(what: ::std::str::Utf8Error) -> Self {
AccessTokenProviderError::Other(what.to_string())
}
}