-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathauthentication.rs
More file actions
573 lines (496 loc) · 18.1 KB
/
authentication.rs
File metadata and controls
573 lines (496 loc) · 18.1 KB
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
use std::net::{IpAddr, Ipv4Addr};
use axum::{
extract::State,
response::{AppendHeaders, IntoResponse, Json},
};
use axum_extra::{TypedHeader, extract::CookieJar, headers::UserAgent};
use cookie::{Cookie, SameSite};
use hyper::StatusCode;
use serde::{Deserialize, Serialize};
use sqlx::SqlitePool;
use utoipa::ToSchema;
use utoipa_axum::{router::OpenApiRouter, routes};
use crate::{
APIError, AppState, ErrorResponse, SqlitePoolExt,
api::middleware::authentication::{
IncompleteUser, SECURE_COOKIES, SESSION_COOKIE_NAME, SESSION_LIFE_TIME,
error::AuthenticationError,
},
domain::role::Role,
error::ErrorReference,
infra::audit_log::{
AuditEvent, AuditService, UserDetails, UserLoggedInDetails, UserLoggedOutDetails,
UserLoginFailedDetails,
},
repository::{
session_repo,
session_repo::Session,
user_repo::{self, User, UserId},
},
};
impl From<AuthenticationError> for APIError {
fn from(err: AuthenticationError) -> Self {
APIError::Authentication(err)
}
}
pub fn router() -> OpenApiRouter<AppState> {
OpenApiRouter::default()
.routes(routes!(login))
.routes(routes!(account))
.routes(routes!(account_update))
.routes(routes!(initialised))
.routes(routes!(create_first_admin))
.routes(routes!(admin_exists))
.routes(routes!(logout))
}
#[derive(Debug, Serialize, Deserialize, ToSchema)]
#[serde(deny_unknown_fields)]
pub struct Credentials {
pub username: String,
pub password: String,
}
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, ToSchema)]
#[serde(deny_unknown_fields)]
pub struct LoginResponse {
pub user_id: UserId,
#[serde(skip_serializing_if = "Option::is_none")]
#[schema(nullable = false)]
pub fullname: Option<String>,
pub username: String,
pub role: Role,
pub needs_password_change: bool,
}
impl From<&User> for LoginResponse {
fn from(user: &User) -> Self {
Self {
user_id: user.id(),
fullname: user.fullname().map(|u| u.to_string()),
username: user.username().to_string(),
role: user.role(),
needs_password_change: user.needs_password_change(),
}
}
}
impl From<LoginResponse> for UserDetails {
fn from(user: LoginResponse) -> Self {
Self {
user_id: user.user_id,
fullname: user.fullname,
username: user.username,
role: user.role.to_string(),
}
}
}
/// Set default session cookie properties
pub(crate) fn set_default_cookie_properties(cookie: &mut Cookie) {
cookie.set_path("/");
cookie.set_http_only(true);
cookie.set_secure(SECURE_COOKIES);
cookie.set_same_site(SameSite::Strict);
}
/// Login endpoint, authenticates a user and creates a new session + session cookie
#[utoipa::path(
post,
path = "/api/login",
request_body = Credentials,
responses(
(status = 200, description = "The logged in user id and user name", body = LoginResponse),
(status = 401, description = "Invalid credentials", body = ErrorResponse),
(status = 403, description = "Forbidden", body = ErrorResponse),
(status = 500, description = "Internal server error", body = ErrorResponse),
),
)]
async fn login(
State(pool): State<SqlitePool>,
jar: CookieJar,
audit_service: AuditService,
user_agent: Option<TypedHeader<UserAgent>>,
Json(credentials): Json<Credentials>,
) -> Result<impl IntoResponse, APIError> {
let Credentials { username, password } = credentials;
let user_agent = user_agent.map(|ua| ua.to_string()).unwrap_or_default();
// Check the username + password combination, do not leak information about usernames etc.
// Log when the attempt fails
let user = match user_repo::authenticate(&pool, &username, &password).await {
Ok(u) => Ok(u),
Err(AuthenticationError::UserNotFound) | Err(AuthenticationError::InvalidPassword) => {
let mut tx = pool.begin_immediate().await?;
audit_service
.log(
&mut tx,
&AuditEvent::UserLoginFailed(UserLoginFailedDetails {
username,
user_agent: user_agent.clone(),
}),
None,
)
.await?;
tx.commit().await?;
Err(AuthenticationError::InvalidUsernameOrPassword)
}
e => e,
}?;
let mut tx = pool.begin_immediate().await?;
// Remove expired sessions, we do this after a login to prevent the necessity of periodical cleanup jobs
session_repo::delete_expired_sessions(&mut tx).await?;
// Remove possible old session for this user
session_repo::delete_user_session(&mut tx, user.id()).await?;
// Get the client IP address if available
let ip = audit_service
.get_ip()
.unwrap_or(IpAddr::V4(Ipv4Addr::UNSPECIFIED))
.to_string();
// Create a new session and cookie
let session = Session::create(user.id(), &user_agent, &ip, SESSION_LIFE_TIME);
session_repo::save(&mut tx, &session).await?;
// Log the login event
let logged_in_users_count = session_repo::count(&mut tx).await?;
audit_service
.with_user(user.clone())
.log(
&mut tx,
&AuditEvent::UserLoggedIn(UserLoggedInDetails {
user_agent: user_agent.to_string(),
logged_in_users_count,
}),
None,
)
.await?;
tx.commit().await?;
// Add the session cookie to the response
let mut cookie = session.get_cookie();
set_default_cookie_properties(&mut cookie);
let updated_jar = jar.add(cookie);
Ok((updated_jar, Json(LoginResponse::from(&user))))
}
#[derive(Debug, Serialize, Deserialize, ToSchema)]
#[serde(deny_unknown_fields)]
pub struct AccountUpdateRequest {
pub username: String,
pub password: String,
#[serde(skip_serializing_if = "Option::is_none")]
#[schema(nullable = false)]
pub fullname: Option<String>,
}
/// Get current logged-in user endpoint
#[utoipa::path(
get,
path = "/api/account",
responses(
(status = 200, description = "The current user name and id", body = LoginResponse),
(status = 401, description = "Invalid user session", body = ErrorResponse),
(status = 500, description = "Internal server error", body = ErrorResponse),
),
security(("cookie_auth" = ["administrator", "coordinator", "typist"])),
)]
async fn account(user: Option<User>) -> Result<impl IntoResponse, APIError> {
let user = user.ok_or(AuthenticationError::UserNotFound)?;
Ok(Json(LoginResponse::from(&user)))
}
/// Update the user's account with a new password and optionally new fullname
#[utoipa::path(
put,
path = "/api/account",
request_body = AccountUpdateRequest,
responses(
(status = 200, description = "The logged in user", body = LoginResponse),
(status = 400, description = "Bad request", body = ErrorResponse),
(status = 500, description = "Internal server error", body = ErrorResponse),
),
security(("cookie_auth" = ["administrator", "coordinator", "typist"])),
)]
async fn account_update(
IncompleteUser(user): IncompleteUser,
State(pool): State<SqlitePool>,
audit_service: AuditService,
Json(account): Json<AccountUpdateRequest>,
) -> Result<impl IntoResponse, APIError> {
let mut tx = pool.begin_immediate().await?;
if user.username() != account.username {
return Err(AuthenticationError::UserNotFound.into());
}
// Update the password
user_repo::update_password(&mut tx, user.id(), &account.username, &account.password).await?;
// Update the fullname
if let Some(fullname) = account.fullname {
user_repo::update_fullname(&mut tx, user.id(), &fullname).await?;
}
let Some(updated_user) = user_repo::get_by_username(&mut tx, user.username()).await? else {
return Err(AuthenticationError::UserNotFound.into());
};
let response = LoginResponse::from(&updated_user);
audit_service
.log(
&mut tx,
&AuditEvent::UserAccountUpdated(response.clone().into()),
None,
)
.await?;
tx.commit().await?;
Ok(Json(response))
}
/// Check whether the application is initialised (an admin user exists + has logged in at least once)
#[utoipa::path(
get,
path = "/api/initialised",
responses(
(status = 204, description = "The application is initialised"),
(status = 418, description = "The application is not initialised", body = ErrorResponse),
(status = 500, description = "Internal server error", body = ErrorResponse),
),
)]
async fn initialised(State(pool): State<SqlitePool>) -> Result<impl IntoResponse, APIError> {
let mut conn = pool.acquire().await?;
if user_repo::has_active_users(&mut conn).await? {
Ok(StatusCode::NO_CONTENT)
} else {
Err(AuthenticationError::NotInitialised.into())
}
}
#[derive(Serialize, Deserialize, ToSchema)]
#[serde(deny_unknown_fields)]
pub struct CreateFirstAdminRequest {
pub username: String,
pub fullname: String,
pub temp_password: String,
}
/// Create the first admin user, only allowed if no users exist yet
#[utoipa::path(
post,
path = "/api/initialise/first-admin",
request_body = CreateFirstAdminRequest,
responses(
(status = 201, description = "First admin user created", body = User),
(status = 403, description = "Forbidden, an active user already exists", body = ErrorResponse),
(status = 409, description = "Conflict (username already exists)", body = ErrorResponse),
(status = 500, description = "Internal server error", body = ErrorResponse),
),
)]
async fn create_first_admin(
State(pool): State<SqlitePool>,
audit_service: AuditService,
Json(admin_request): Json<CreateFirstAdminRequest>,
) -> Result<(StatusCode, Json<User>), APIError> {
let mut tx = pool.begin_immediate().await?;
if user_repo::has_active_users(&mut tx).await? {
return Err(AuthenticationError::AlreadyInitialised.into());
}
// Delete any existing admin user
let users = user_repo::list(&mut tx, None).await?;
for user in users {
if user.role() == Role::Administrator {
user_repo::delete(&mut tx, user.id()).await?;
audit_service
.log(&mut tx, &AuditEvent::UserDeleted(user.into()), None)
.await?;
}
}
// Create the first admin user
let user = user_repo::create(
&mut tx,
&admin_request.username,
Some(&admin_request.fullname),
&admin_request.temp_password,
false,
Role::Administrator,
)
.await?;
audit_service
.log(&mut tx, &AuditEvent::UserCreated(user.clone().into()), None)
.await?;
tx.commit().await?;
Ok((StatusCode::CREATED, Json(user)))
}
#[utoipa::path(
get,
path = "/api/initialise/admin-exists",
responses(
(status = 204, description = "First admin user exists"),
(status = 403, description = "Forbidden, the application is already initialised", body = ErrorResponse),
(status = 404, description = "No admin user exists", body = ErrorResponse),
(status = 500, description = "Internal server error", body = ErrorResponse),
),
)]
async fn admin_exists(State(pool): State<SqlitePool>) -> Result<StatusCode, APIError> {
let mut conn = pool.acquire().await?;
if user_repo::has_active_users(&mut conn).await? {
return Err(AuthenticationError::AlreadyInitialised.into());
}
if user_repo::admin_exists(&mut conn).await? {
return Ok(StatusCode::NO_CONTENT);
}
Err(APIError::NotFound(
"No admin user exists".into(),
ErrorReference::UserNotFound,
))
}
/// Logout endpoint, deletes the session cookie
#[utoipa::path(
post,
path = "/api/logout",
responses(
(status = 204, description = "Successful logout, or user was already logged out"),
(status = 500, description = "Internal server error", body = ErrorResponse),
),
)]
async fn logout(
State(pool): State<SqlitePool>,
audit_service: AuditService,
jar: CookieJar,
) -> Result<impl IntoResponse, APIError> {
// Ask browser to remove cookies and storage data
// https://owasp.org/www-project-secure-headers/#clear-site-data
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Clear-Site-Data
const CLEAR_SITE_DATA_HEADER: (&str, &str) = ("Clear-Site-Data", r#""cookies","storage""#);
let Some(mut cookie) = jar.get(SESSION_COOKIE_NAME).cloned() else {
// no cookie found, return OK
return Ok((AppendHeaders([CLEAR_SITE_DATA_HEADER]), jar, StatusCode::OK));
};
// Get the session key from the cookie
let session_key = cookie.value();
// Log audit event when a valid session exists
let mut tx = pool.begin_immediate().await?;
if let Some(session) = session_repo::get_by_key(&mut tx, session_key)
.await
.ok()
.flatten()
{
// Log the logout event
audit_service
.log(
&mut tx,
&AuditEvent::UserLoggedOut(UserLoggedOutDetails {
session_duration: session.duration().as_secs(),
}),
None,
)
.await?;
}
// Remove session from the database
session_repo::delete(&mut tx, session_key).await?;
// Commit the transaction
tx.commit().await?;
// Set cookie parameters, these are not present in the request, and have to match in order to clear the cookie
set_default_cookie_properties(&mut cookie);
let updated_jar = jar.remove(cookie);
Ok((
AppendHeaders([CLEAR_SITE_DATA_HEADER]),
updated_jar,
StatusCode::NO_CONTENT,
))
}
#[cfg(test)]
mod tests {
use test_log::test;
use super::*;
use crate::repository::user_repo::update_last_activity_at;
#[test(tokio::test)]
async fn test_set_default_cookie_properties() {
let mut cookie = Cookie::new("test-cookie", "");
set_default_cookie_properties(&mut cookie);
assert_eq!(cookie.path().unwrap(), "/");
assert!(cookie.http_only().unwrap());
assert_eq!(cookie.secure().unwrap(), SECURE_COOKIES);
assert_eq!(cookie.same_site().unwrap(), SameSite::Strict);
}
#[test(sqlx::test)]
async fn test_initialised(pool: SqlitePool) {
let result = initialised(State(pool.clone())).await;
assert!(matches!(
result,
Err(APIError::Authentication(
AuthenticationError::NotInitialised
))
));
// Create an admin user to mark the application as initialised
let mut conn = pool.acquire().await.unwrap();
let admin = user_repo::create(
&mut conn,
"admin",
Some("Admin User"),
"admin_password",
false,
Role::Administrator,
)
.await
.unwrap();
admin.update_last_activity_at(&mut conn).await.unwrap();
let result = initialised(State(pool)).await;
assert!(result.is_ok());
let response = result.unwrap().into_response();
assert_eq!(response.status(), StatusCode::NO_CONTENT);
}
#[test(sqlx::test)]
async fn test_create_first_admin(pool: SqlitePool) {
let admin_request = CreateFirstAdminRequest {
username: "admin".to_string(),
fullname: "Admin User".to_string(),
temp_password: "admin_password".to_string(),
};
let response = create_first_admin(
State(pool.clone()),
AuditService::new(None, None),
Json(admin_request),
)
.await;
assert!(response.is_ok());
let (status, user) = response.unwrap();
assert_eq!(status, StatusCode::CREATED);
assert_eq!(user.username(), "admin");
// Create a new user again is allowed as long as the admin has not logged in yet
let admin_request = CreateFirstAdminRequest {
username: "admin2".to_string(),
fullname: "Admin User 2".to_string(),
temp_password: "admin_password".to_string(),
};
let response = create_first_admin(
State(pool.clone()),
AuditService::new(None, None),
Json(admin_request),
)
.await;
assert!(response.is_ok());
let (status, user) = response.unwrap();
assert_eq!(status, StatusCode::CREATED);
assert_eq!(user.username(), "admin2");
// Not allowed to create a new user after the first admin has logged in
let mut conn = pool.acquire().await.unwrap();
update_last_activity_at(&mut conn, user.id()).await.unwrap();
let admin_request = CreateFirstAdminRequest {
username: "admin2".to_string(),
fullname: "Admin User 3".to_string(),
temp_password: "admin_password".to_string(),
};
let response = create_first_admin(
State(pool.clone()),
AuditService::new(None, None),
Json(admin_request),
)
.await;
assert!(response.is_err());
}
#[test(sqlx::test)]
async fn test_admin_exists(pool: SqlitePool) {
// No admin user exists yet
let response = admin_exists(State(pool.clone())).await;
assert!(response.is_err());
assert!(matches!(response, Err(APIError::NotFound(_, _))));
// Create an admin user
let admin_request = CreateFirstAdminRequest {
username: "admin".to_string(),
fullname: "Admin User".to_string(),
temp_password: "admin_password".to_string(),
};
let response = create_first_admin(
State(pool.clone()),
AuditService::new(None, None),
Json(admin_request),
)
.await;
assert!(response.is_ok());
let (status, _user) = response.unwrap();
assert_eq!(status, StatusCode::CREATED);
let response = admin_exists(State(pool.clone())).await;
assert_eq!(response.unwrap(), StatusCode::NO_CONTENT);
}
}