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
|
use core::net::IpAddr;
use rocket::fairing::AdHoc;
use rocket::form::Form;
use rocket::http::{Cookie, CookieJar, Status};
use rocket::outcome::{try_outcome, IntoOutcome};
use rocket::request::{FromRequest, Outcome, Request};
use rocket::response::status::Unauthorized;
use rocket::serde::json::{self, Json};
use rocket::serde::{Deserialize, Serialize};
use rocket::State;
use std::collections::BTreeMap;
use std::sync::Mutex;
use std::time::Instant;
use time::Duration;
use crate::api_model;
#[derive(FromForm)]
struct Login<'r> {
username: &'r str,
password: &'r str,
}
#[derive(Debug, Deserialize)]
#[allow(dead_code)]
struct AuthConfig {
session_max_age_days: u32,
}
struct SessionsData {
active_ids: BTreeMap<u32, Instant>,
next_id: u32,
}
struct Sessions {
data: Mutex<SessionsData>,
}
#[derive(Debug, Deserialize, Serialize)]
pub struct Session {
pub user_id: u64,
session_id: u32,
remote: String,
}
#[derive(Debug)]
pub enum SessionError {
Invalid,
}
const SESSION_COOKIE: &str = "s";
fn validate(sessions: &State<Sessions>, session: &Session, request: &Request<'_>) -> bool {
match request.client_ip() {
Some(addr) => {
if session.remote == addr.to_string() {
{
let sessions_data = sessions.data.lock().unwrap();
match sessions_data.active_ids.get(&session.session_id) {
// We could remove the expired session here, but it will be cleaned
// next time anyone logs in anyway.
Some(&expire) => expire > Instant::now(),
None => false,
}
}
} else {
false
}
}
None => false,
}
}
#[rocket::async_trait]
impl<'r> FromRequest<'r> for Session {
type Error = SessionError;
async fn from_request(request: &'r Request<'_>) -> Outcome<Session, SessionError> {
let sessions = try_outcome!(request
.guard::<&State<Sessions>>()
.await
.map_error(|_| (Status::Unauthorized, SessionError::Invalid)));
request
.cookies()
.get_private(SESSION_COOKIE)
.and_then(|cookie| -> Option<Session> { json::from_str(cookie.value()).ok() })
.and_then(|session| {
if validate(&sessions, &session, request) {
Some(session)
} else {
None
}
})
.or_error((Status::Unauthorized, SessionError::Invalid))
}
}
fn new_session(
sessions: &State<Sessions>,
user_id: u64,
remote: String,
max_age: Duration,
) -> Session {
let session_id;
{
let mut sessions_data = sessions.data.lock().unwrap();
session_id = sessions_data.next_id;
sessions_data.next_id += 1;
let now = Instant::now();
// Remove expired sessions first
sessions_data
.active_ids
.retain(|_, &mut expire| expire > now);
sessions_data.active_ids.insert(session_id, now + max_age);
}
Session {
user_id: user_id,
session_id: session_id,
remote: remote,
}
}
#[post("/login", data = "<login>")]
fn login(
auth_config: &State<AuthConfig>,
sessions: &State<Sessions>,
ipaddr: IpAddr,
cookies: &CookieJar<'_>,
login: Form<Login<'_>>,
) -> Result<Json<api_model::StatusResponse>, Unauthorized<&'static str>> {
if login.username == "user" && login.password == "password" {
let max_age = Duration::days(i64::from(auth_config.session_max_age_days));
let session = new_session(&sessions, 1u64, ipaddr.to_string(), max_age);
let cookie = Cookie::build((SESSION_COOKIE, json::to_string(&session).unwrap()))
.path("/api")
.max_age(max_age)
.http_only(true)
.build();
cookies.add_private(cookie);
Ok(Json(api_model::StatusResponse {
ok: true,
error: None,
}))
} else {
Err(Unauthorized("Unknown username or password"))
}
}
#[get("/logout")]
fn logout(
session: Session,
sessions: &State<Sessions>,
cookies: &CookieJar<'_>,
) -> Json<api_model::StatusResponse> {
{
let mut sessions_data = sessions.data.lock().unwrap();
sessions_data.active_ids.remove(&session.session_id);
}
let cookie = Cookie::build((SESSION_COOKIE, ""))
.path("/api")
.http_only(true)
.build();
cookies.remove_private(cookie);
Json(api_model::StatusResponse {
ok: true,
error: None,
})
}
#[get("/status")]
fn status(_session: Session) -> Json<api_model::StatusResponse> {
Json(api_model::StatusResponse {
ok: true,
error: None,
})
}
#[catch(401)]
fn unauthorized() -> Json<api_model::StatusResponse> {
Json(api_model::StatusResponse {
ok: false,
error: Some("Unauthorized".to_string()),
})
}
pub fn stage(basepath: String) -> AdHoc {
AdHoc::on_ignite("Auth Stage", |rocket| async {
rocket
.manage(Sessions {
data: Mutex::new(SessionsData {
active_ids: BTreeMap::new(),
next_id: 1,
}),
})
.attach(AdHoc::config::<AuthConfig>())
.mount(basepath.clone(), routes![login, logout, status])
.register(basepath, catchers![unauthorized])
})
}
|