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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
use super::html::Page;
use super::{
COOKIE, Error, HOST, OAuth2, PERSIST_COOKIE, PendingAuth, Result, Services, Tokens, User, totp,
};
use rikka::{Redirect, Request};
use url::Url;
/// an established cas session.
///
/// obtained from [`PendingAuth::finish`] after a first login, from
/// [`CAS::restore`] with persisted values, or from [`CAS::temporary`] when
/// only a raw session cookie is known.
#[derive(Debug, Clone)]
pub struct CAS {
/// `lemonldap` session cookie used to perform requests.
pub cookie: String,
/// the `llngconnection` persistence cookie, needed by [`CAS::restore`].
pub connection: String,
/// totp key generated by the persistence cookie, also needed to restore.
pub key: String,
}
impl CAS {
/// rebuild a session from its three persisted parts.
pub fn new(
cookie: impl Into<String>,
connection: impl Into<String>,
key: impl Into<String>,
) -> Self {
Self {
cookie: cookie.into(),
connection: connection.into(),
key: key.into(),
}
}
/// use an existing `lemonldap` cookie for temporary access. such a
/// session cannot be restored.
pub fn temporary(cookie: impl Into<String>) -> Self {
Self::new(cookie, "", "")
}
/// start a new authentication by submitting credentials, returning a
/// [`PendingAuth`] to continue with 2fa.
///
/// check [`PendingAuth::solved`] first, the portal sometimes skips the
/// challenge. solve with one of the available methods and call
/// [`PendingAuth::finish`] to obtain the session.
pub async fn initialize(username: &str, password: &str) -> Result<PendingAuth> {
let token = Self::csrf_token().await?;
let response = Request::builder(HOST)
.post()
.form(body(&[
("password", password),
("stayconnected", "1"),
("token", token.as_str()),
("user", username),
]))
.send()
.await?;
Ok(PendingAuth::from_html(&response.text()))
}
/// re-authenticate without manually solving 2fa, using a persistence
/// cookie and its totp key.
///
/// both values come from a previous [`PendingAuth::finish`]: the
/// [`CAS::connection`] and [`CAS::key`] fields of the session it
/// returned.
pub async fn restore(
username: &str,
password: &str,
llngconnection: &str,
key: &str,
) -> Result<CAS> {
let token = Self::csrf_token().await?;
let response = Request::builder(HOST)
.post()
.form(body(&[
("password", password),
("token", token.as_str()),
("user", username),
]))
.cookie(PERSIST_COOKIE, llngconnection)
.send()
.await?;
let token = Page::parse(&response.text())
.token()
.ok_or(Error::NoCasToken)?;
let fingerprint = format!("TOTP_{}", totp::generate(key)?);
let response = Request::builder(format!("{HOST}/checkbrowser"))
.post()
.form(body(&[
("fg", fingerprint.as_str()),
("token", token.as_str()),
("usetotp", "1"),
]))
.cookie(PERSIST_COOKIE, llngconnection)
.redirect(Redirect::Manual)
.send()
.await?;
let lemonldap = response
.set_cookie_value(COOKIE)
.ok_or_else(|| Error::Api("bad persistence".into()))?;
Ok(CAS::new(lemonldap, llngconnection, key))
}
/// authorize a user through the `/oauth2` route, returning the callback url.
///
/// `state` is echoed back as a query parameter of the callback url.
/// enabling `challenge` sends a plain pkce code challenge and requires
/// the same flag on [`CAS::tokenize`]. a consent page shown on first
/// authorization is confirmed automatically.
///
/// # example
///
/// ```no_run
/// use unilim_cas::{CAS, OAuth2};
///
/// # async fn oauth2(cas: CAS) -> unilim_cas::Result<()> {
/// let client = OAuth2::new(
/// "client-id",
/// "https://service.example/callback",
/// vec!["openid".into(), "profile".into(), "email".into()],
/// );
///
/// let callback = cas.authorize(&client, false, "state").await?;
/// let tokens = cas.tokenize(&callback, &client, false).await?;
/// let user = cas.userinfo(&tokens).await?;
/// # Ok(())
/// # }
/// ```
pub async fn authorize(&self, client: &OAuth2, challenge: bool, state: &str) -> Result<Url> {
let scopes = client.scopes.join(" ");
let mut url = Url::parse(&format!("{HOST}/oauth2/authorize"))?;
{
let mut query = url.query_pairs_mut();
query
.append_pair("redirect_uri", &client.callback)
.append_pair("client_id", &client.identifier)
.append_pair("response_type", "code")
.append_pair("scope", &scopes)
.append_pair("state", state);
if challenge {
query
.append_pair("code_challenge_method", "plain")
.append_pair("code_challenge", "literateink");
}
}
let response = Request::builder(url.as_str())
.redirect(Redirect::Manual)
.cookie(COOKIE, &self.cookie)
.send()
.await?;
let mut location = response.location();
// a consent page may show up and must be confirmed to obtain the redirect.
if response.status == 200
&& location.is_none()
&& let Some(confirm) = Page::parse(&response.text()).confirm()
{
let mut pairs = vec![
("client_id", client.identifier.as_str()),
("confirm", confirm.as_str()),
("redirect_uri", client.callback.as_str()),
("response_type", "code"),
("scope", scopes.as_str()),
// base64 of "https://cas.unilim.fr/oauth2".
("url", "aHR0cHM6Ly9jYXMudW5pbGltLmZyL29hdXRoMg=="),
];
if challenge {
pairs.extend([
("code_challenge", "literateink"),
("code_challenge_method", "plain"),
]);
}
let response = Request::builder(url.as_str())
.post()
.redirect(Redirect::Manual)
.cookie(COOKIE, &self.cookie)
.form(body(&pairs))
.send()
.await?;
location = response.location();
}
parse_location(location)
}
/// authenticate to a cas-fronted `service`, returning the ticket url.
///
/// the returned url is the login route of the service with a
/// `ticket=ST-...` query parameter. requesting it grants an
/// authenticated session on the service.
///
/// # example
///
/// ```no_run
/// use unilim_cas::{CAS, Services};
///
/// # async fn ticket(cas: CAS) -> unilim_cas::Result<()> {
/// let url = cas.service(Services::CommunityIut).await?;
/// // > https://community-iut.unilim.fr/login/index.php?authCAS=CAS&ticket=ST-XXXXX
/// println!("{url}");
/// # Ok(())
/// # }
/// ```
pub async fn service(&self, service: Services) -> Result<Url> {
let mut url = Url::parse(&format!("{HOST}/cas/login"))?;
url.query_pairs_mut()
.append_pair("service", service.url())
.append_pair("gateway", "true");
let response = Request::builder(url.as_str())
.redirect(Redirect::Manual)
.cookie(COOKIE, &self.cookie)
.send()
.await?;
parse_location(response.location())
}
/// exchange the `code` of an authorized `callback` url for tokens.
///
/// `callback` is the url returned by [`CAS::authorize`], and `challenge`
/// must match the value used there.
pub async fn tokenize(
&self,
callback: &Url,
client: &OAuth2,
challenge: bool,
) -> Result<Tokens> {
let code = callback
.query_pairs()
.find_map(|(key, value)| (key == "code").then_some(value))
.ok_or_else(|| Error::Api("no code found".into()))?;
let mut pairs = vec![
("client_id", client.identifier.as_str()),
("code", &code),
("grant_type", "authorization_code"),
("redirect_uri", client.callback.as_str()),
];
if challenge {
pairs.push(("code_verifier", "literateink"));
}
let response = Request::builder(format!("{HOST}/oauth2/token"))
.post()
.form(body(&pairs))
.send()
.await?;
Ok(response.json()?)
}
/// retrieve user information from the access token.
pub async fn userinfo(&self, tokens: &Tokens) -> Result<User> {
let response = Request::builder(format!("{HOST}/oauth2/userinfo"))
.header("authorization", format!("Bearer {}", tokens.access_token))
.send()
.await?;
if response.status != 200 {
return Err(Error::Api("invalid access token".into()));
}
Ok(response.json()?)
}
/// the portal sometimes shows an info page dismissed by refreshing, so
/// fetching the csrf token is retried a few times.
async fn csrf_token() -> Result<String> {
const MAX_RETRIES: usize = 5;
for _ in 0..MAX_RETRIES {
let response = Request::builder(HOST).send().await?;
if let Some(token) = Page::parse(&response.text()).token() {
return Ok(token);
}
}
Err(Error::NoCasToken)
}
}
fn body(pairs: &[(&str, &str)]) -> Vec<(String, String)> {
pairs
.iter()
.map(|&(key, value)| (key.to_owned(), value.to_owned()))
.collect()
}
fn parse_location(location: Option<String>) -> Result<Url> {
let location = location.ok_or_else(|| Error::Api("location header not found".into()))?;
Ok(Url::parse(&location)?)
}