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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
use std::collections::HashMap;
use std::time::Duration;
use failure::Fail;
use reqwest::Url;
use crate::FEDORA_USER_AGENT;
const FEDORA_OPENID_API: &str = "https://id.fedoraproject.org/api/v1/";
#[derive(Debug)]
pub struct OpenIDClientBuilder {
login_url: Url,
timeout: Option<Duration>,
user_agent: Option<String>,
}
#[derive(Debug, Fail)]
#[fail(display = "Failed to initialize session: {}", error)]
pub struct BuilderError {
error: reqwest::Error,
}
impl From<reqwest::Error> for BuilderError {
fn from(error: reqwest::Error) -> Self {
Self { error }
}
}
impl OpenIDClientBuilder {
pub fn new(login_url: Url) -> Self {
OpenIDClientBuilder {
login_url,
timeout: None,
user_agent: None,
}
}
pub fn timeout(mut self, timeout: Duration) -> Self {
self.timeout = Some(timeout);
self
}
pub fn user_agent(mut self, user_agent: String) -> Self {
self.user_agent = Some(user_agent);
self
}
pub fn build(self) -> Result<OpenIDClient, BuilderError> {
let timeout = match self.timeout {
Some(timeout) => timeout,
None => Duration::from_secs(60),
};
let user_agent = match self.user_agent {
Some(user_agent) => user_agent,
None => String::from(FEDORA_USER_AGENT),
};
let mut headers = reqwest::header::HeaderMap::new();
headers.insert(
reqwest::header::USER_AGENT,
reqwest::header::HeaderValue::from_str(FEDORA_USER_AGENT).unwrap(),
);
headers.insert(
reqwest::header::ACCEPT,
reqwest::header::HeaderValue::from_str("application/json").unwrap(),
);
let session = reqwest::Client::builder()
.default_headers(headers)
.timeout(timeout)
.cookie_store(true)
.redirect(reqwest::RedirectPolicy::none())
.build()?;
Ok(OpenIDClient {
session,
login_url: self.login_url,
user_agent,
authenticated: false,
})
}
}
#[derive(Debug)]
pub struct OpenIDClient {
session: reqwest::Client,
login_url: Url,
user_agent: String,
authenticated: bool,
}
#[derive(Debug, Fail)]
pub enum ClientError {
#[fail(display = "Failed to contact OpenID provider: {}", error)]
RequestError { error: reqwest::Error },
#[fail(display = "Failed to parse redirection URL: {}", error)]
UrlParsingError { error: reqwest::UrlError },
#[fail(display = "{}", error)]
RedirectionError { error: String },
#[fail(display = "Failed to authenticate with OpenID service: {}", error)]
AuthenticationError { error: reqwest::Error },
}
impl From<reqwest::Error> for ClientError {
fn from(error: reqwest::Error) -> Self {
ClientError::RequestError { error }
}
}
impl From<reqwest::UrlError> for ClientError {
fn from(error: reqwest::UrlError) -> Self {
ClientError::UrlParsingError { error }
}
}
impl OpenIDClient {
pub fn login(&mut self, username: String, password: String) -> Result<(), ClientError> {
let mut url = self.login_url.clone();
let mut state: HashMap<String, String> = HashMap::new();
loop {
let response = self.session.get(url.clone()).send()?;
let status = response.status();
let args = url.query_pairs();
for (key, value) in args {
state.insert(key.to_string(), value.to_string());
}
if status.is_redirection() {
let header: &reqwest::header::HeaderValue = match response
.headers().get("location") {
Some(value) => value,
None => return Err(ClientError::RedirectionError {
error: String::from("No redirect URL provided in HTTP redirect headers.")
}),
};
let string = match header.to_str() {
Ok(string) => string,
Err(_) => return Err(ClientError::RedirectionError {
error: String::from("Failed to decode redirect URL.")
}),
};
url = Url::parse(string)?;
} else {
break;
}
}
state.insert(String::from("username"), username);
state.insert(String::from("password"), password);
state.insert(
String::from("auth_module"),
String::from("fedoauth.auth.fas.Auth_FAS"),
);
state.insert(String::from("auth_flow"), String::from("fedora"));
if !state.contains_key("openid.mode") {
state.insert(String::from("openid.mode"), String::from("checkid_setup"));
}
let mut _response = match self.session.post(FEDORA_OPENID_API).form(&state).send() {
Ok(response) => response,
Err(error) => return Err(ClientError::AuthenticationError { error }),
};
self.authenticated = true;
Ok(())
}
pub fn authenticated(&self) -> bool {
self.authenticated
}
pub fn session(&self) -> &reqwest::Client {
&self.session
}
}