This repository has been archived by the owner on Aug 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add admin password and logout endpoints
- Loading branch information
Showing
28 changed files
with
774 additions
and
123 deletions.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
.sqlx/query-2880480077b654e38b63f423ab40680697a500ffe1af1d1b39108910594b581b.json
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
INSERT INTO USERS (user_id, username, password_hash) | ||
VALUES ( | ||
'77b7e761-d41a-447d-bcf9-00211196a577', | ||
'admin', | ||
'$argon2id$v=19$m=15000,t=2,p=1' | ||
'$7WOA4CkjTTaLhVrzBVrFTQ$x+dO/frQlVo0CNxRso+ds0bZyrP5TPm+8b68jYn/eWY' | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
use crate::{ | ||
session::state::TypedSession, | ||
utils::{e500, HttpError}, | ||
}; | ||
use axum::response::Redirect; | ||
use axum_messages::Messages; | ||
|
||
#[tracing::instrument(skip(session, messages))] | ||
pub(super) async fn log_out( | ||
session: TypedSession, | ||
messages: Messages, | ||
) -> Result<Redirect, HttpError<anyhow::Error>> { | ||
if session.get_user_id().await.map_err(e500)?.is_some() { | ||
session.flush().await.map_err(e500)?; | ||
messages.info("You have successfully logged out."); | ||
} | ||
|
||
Ok(Redirect::to("/login")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,20 @@ | ||
use crate::app_state::AppState; | ||
use axum::{routing::get, Router}; | ||
use axum::{ | ||
routing::{get, post}, | ||
Router, | ||
}; | ||
use dashboard::admin_dashboard; | ||
use logout::log_out; | ||
use password::{change_password, change_password_form}; | ||
|
||
mod dashboard; | ||
mod logout; | ||
mod password; | ||
|
||
pub fn router() -> Router<AppState> { | ||
Router::new().route("/admin/dashboard", get(admin_dashboard)) | ||
Router::new() | ||
.route("/admin/dashboard", get(admin_dashboard)) | ||
.route("/admin/password", get(change_password_form)) | ||
.route("/admin/password", post(change_password)) | ||
.route("/admin/logout", post(log_out)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
use crate::{ | ||
app_state::AppState, | ||
authentication::{ | ||
change_password as auth_change_password, validate_credentials, AuthError, Credentials, | ||
}, | ||
routes::admin::dashboard::get_username, | ||
session::extract::SessionUserId, | ||
utils::{e500, HttpError}, | ||
}; | ||
use axum::{extract::State, response::Redirect, Form}; | ||
use axum_messages::Messages; | ||
use secrecy::{ExposeSecret, Secret}; | ||
use serde::Deserialize; | ||
|
||
#[tracing::instrument(skip(app_state, user_id, form))] | ||
pub(in crate::routes::admin) async fn change_password( | ||
State(app_state): State<AppState>, | ||
SessionUserId(user_id): SessionUserId, | ||
messages: Messages, | ||
Form(form): Form<FormData>, | ||
) -> Result<Redirect, HttpError<anyhow::Error>> { | ||
if form.new_password.expose_secret() != form.new_password_check.expose_secret() { | ||
messages.error( | ||
"You have entered two different new passwords - \ | ||
the field values must match.", | ||
); | ||
return Ok(Redirect::to("/admin/password")); | ||
} | ||
|
||
let credentials = get_username(&app_state.db_pool, user_id) | ||
.await | ||
.map(|username| Credentials { | ||
username, | ||
password: form.current_password, | ||
}) | ||
.map_err(e500)?; | ||
|
||
if let Err(e) = validate_credentials(&app_state.db_pool, credentials).await { | ||
return match e { | ||
AuthError::InvalidCredentials(_) => { | ||
messages.error("The current password is incorrect."); | ||
Ok(Redirect::to("/admin/password")) | ||
} | ||
AuthError::UnexpectedError(_) => Err(e500(e.into())), | ||
}; | ||
} | ||
|
||
if let Err(e) = validate_password(&form.new_password) { | ||
messages.error(e); | ||
return Ok(Redirect::to("/admin/password")); | ||
} | ||
|
||
auth_change_password(&app_state.db_pool, user_id, form.new_password) | ||
.await | ||
.map_err(e500)?; | ||
|
||
messages.info("Your password has been changed."); | ||
|
||
Ok(Redirect::to("/admin/password")) | ||
} | ||
|
||
#[derive(Deserialize)] | ||
pub(in crate::routes::admin) struct FormData { | ||
current_password: Secret<String>, | ||
new_password: Secret<String>, | ||
new_password_check: Secret<String>, | ||
} | ||
|
||
fn validate_password(password: &Secret<String>) -> Result<(), &'static str> { | ||
let password = password.expose_secret(); | ||
|
||
if password.len() < 12 { | ||
return Err("Password must be at least 12 characters long."); | ||
} | ||
|
||
if password.len() > 128 { | ||
return Err("Passwords must be at most 128 characters long."); | ||
} | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
use askama_axum::Template; | ||
use axum_messages::Messages; | ||
|
||
pub(in crate::routes::admin) async fn change_password_form( | ||
messages: Messages, | ||
) -> ChangePasswordForm<'static> { | ||
let flashes = messages.map(|m| m.message).collect(); | ||
|
||
ChangePasswordForm { | ||
title: "Change Password", | ||
current_password_label: "Current Password", | ||
current_password_placeholder: "Enter current password", | ||
new_password_label: "New Password", | ||
new_password_placeholder: "Enter new password", | ||
new_password_check_label: "Confirm New Password", | ||
new_password_check_placeholder: "Type the new password again", | ||
change_password_button: "Change password", | ||
back_link: "Back", | ||
flashes, | ||
} | ||
} | ||
|
||
#[derive(Template)] | ||
#[template(path = "web/change_password_form.html")] | ||
pub(in crate::routes::admin) struct ChangePasswordForm<'a> { | ||
title: &'a str, | ||
current_password_label: &'a str, | ||
current_password_placeholder: &'a str, | ||
new_password_label: &'a str, | ||
new_password_placeholder: &'a str, | ||
new_password_check_label: &'a str, | ||
new_password_check_placeholder: &'a str, | ||
change_password_button: &'a str, | ||
back_link: &'a str, | ||
flashes: Vec<String>, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
mod action; | ||
mod form; | ||
|
||
pub(super) use action::change_password; | ||
pub(super) use form::change_password_form; |
Oops, something went wrong.