Move login stuff to own module.
This commit is contained in:
parent
e5d66769dc
commit
0f0a7583cd
4 changed files with 74 additions and 60 deletions
|
@ -3,6 +3,9 @@ extern crate justerror;
|
||||||
|
|
||||||
pub mod db;
|
pub mod db;
|
||||||
pub mod generic_handlers;
|
pub mod generic_handlers;
|
||||||
|
pub mod login;
|
||||||
pub mod session_store;
|
pub mod session_store;
|
||||||
pub mod signup;
|
pub mod signup;
|
||||||
pub(crate) mod templates;
|
pub(crate) mod templates;
|
||||||
|
|
||||||
|
pub use signup::User;
|
||||||
|
|
67
src/login.rs
67
src/login.rs
|
@ -0,0 +1,67 @@
|
||||||
|
use argon2::PasswordVerifier;
|
||||||
|
use axum::{
|
||||||
|
extract::State,
|
||||||
|
http::StatusCode,
|
||||||
|
response::{IntoResponse, Response},
|
||||||
|
};
|
||||||
|
use axum_login::{secrecy::SecretVec, AuthUser, SqliteStore};
|
||||||
|
use sqlx::SqlitePool;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
use crate::{templates::LoginGet, User};
|
||||||
|
|
||||||
|
pub type AuthContext = axum_login::extractors::AuthContext<Uuid, User, SqliteStore<User>>;
|
||||||
|
|
||||||
|
impl AuthUser<Uuid> for User {
|
||||||
|
fn get_id(&self) -> Uuid {
|
||||||
|
self.id
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_password_hash(&self) -> SecretVec<u8> {
|
||||||
|
SecretVec::new(self.pwhash.as_bytes().to_vec())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//-************************************************************************
|
||||||
|
// Login error and success types
|
||||||
|
//-************************************************************************
|
||||||
|
|
||||||
|
#[Error]
|
||||||
|
pub struct LoginError(#[from] LoginErrorKind);
|
||||||
|
|
||||||
|
#[Error]
|
||||||
|
#[non_exhaustive]
|
||||||
|
pub enum LoginErrorKind {
|
||||||
|
BadPassword,
|
||||||
|
Unknown,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl IntoResponse for LoginError {
|
||||||
|
fn into_response(self) -> Response {
|
||||||
|
match self.0 {
|
||||||
|
LoginErrorKind::Unknown => (
|
||||||
|
StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
|
"An unknown error occurred; you cursed, brah?",
|
||||||
|
)
|
||||||
|
.into_response(),
|
||||||
|
_ => (StatusCode::BAD_REQUEST, format!("{self}")).into_response(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//-************************************************************************
|
||||||
|
// Login handlers
|
||||||
|
//-************************************************************************
|
||||||
|
|
||||||
|
/// Handle login queries
|
||||||
|
#[axum::debug_handler]
|
||||||
|
pub async fn post_login(
|
||||||
|
mut auth: AuthContext,
|
||||||
|
State(pool): State<SqlitePool>,
|
||||||
|
) -> Result<(), LoginError> {
|
||||||
|
Err(LoginErrorKind::Unknown.into())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn get_login() -> impl IntoResponse {
|
||||||
|
LoginGet::default()
|
||||||
|
}
|
|
@ -7,8 +7,9 @@ use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
|
||||||
use witch_watch::{
|
use witch_watch::{
|
||||||
db,
|
db,
|
||||||
generic_handlers::{handle_slash, handle_slash_redir},
|
generic_handlers::{handle_slash, handle_slash_redir},
|
||||||
|
login::{get_login, post_login},
|
||||||
session_store::SqliteSessionStore,
|
session_store::SqliteSessionStore,
|
||||||
signup::{get_create_user, get_login, handle_signup_success, post_create_user, post_login},
|
signup::{get_create_user, handle_signup_success, post_create_user},
|
||||||
};
|
};
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
|
|
|
@ -10,12 +10,11 @@ use axum::{
|
||||||
http::StatusCode,
|
http::StatusCode,
|
||||||
response::{IntoResponse, Response},
|
response::{IntoResponse, Response},
|
||||||
};
|
};
|
||||||
use axum_login::{secrecy::SecretVec, AuthUser, SqliteStore};
|
|
||||||
use sqlx::{query_as, SqlitePool};
|
use sqlx::{query_as, SqlitePool};
|
||||||
use unicode_segmentation::UnicodeSegmentation;
|
use unicode_segmentation::UnicodeSegmentation;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
use crate::templates::{CreateUser, LoginGet};
|
use crate::templates::CreateUser;
|
||||||
|
|
||||||
const CREATE_QUERY: &str =
|
const CREATE_QUERY: &str =
|
||||||
"insert into witches (id, username, displayname, email, pwhash) values ($1, $2, $3, $4, $5)";
|
"insert into witches (id, username, displayname, email, pwhash) values ($1, $2, $3, $4, $5)";
|
||||||
|
@ -28,7 +27,7 @@ pub struct User {
|
||||||
pub displayname: Option<String>,
|
pub displayname: Option<String>,
|
||||||
pub email: Option<String>,
|
pub email: Option<String>,
|
||||||
pub last_seen: Option<i64>,
|
pub last_seen: Option<i64>,
|
||||||
pwhash: String,
|
pub(crate) pwhash: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Display for User {
|
impl Display for User {
|
||||||
|
@ -44,18 +43,6 @@ impl Display for User {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type AuthContext = axum_login::extractors::AuthContext<Uuid, User, SqliteStore<User>>;
|
|
||||||
|
|
||||||
impl AuthUser<Uuid> for User {
|
|
||||||
fn get_id(&self) -> Uuid {
|
|
||||||
self.id
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_password_hash(&self) -> SecretVec<u8> {
|
|
||||||
SecretVec::new(self.pwhash.as_bytes().to_vec())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//-************************************************************************
|
//-************************************************************************
|
||||||
// Result types for user creation
|
// Result types for user creation
|
||||||
//-************************************************************************
|
//-************************************************************************
|
||||||
|
@ -199,50 +186,6 @@ pub async fn handle_signup_success(
|
||||||
resp
|
resp
|
||||||
}
|
}
|
||||||
|
|
||||||
//-************************************************************************
|
|
||||||
// Login error and success types
|
|
||||||
//-************************************************************************
|
|
||||||
|
|
||||||
#[Error]
|
|
||||||
pub struct LoginError(#[from] LoginErrorKind);
|
|
||||||
|
|
||||||
#[Error]
|
|
||||||
#[non_exhaustive]
|
|
||||||
pub enum LoginErrorKind {
|
|
||||||
BadPassword,
|
|
||||||
Unknown,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl IntoResponse for LoginError {
|
|
||||||
fn into_response(self) -> Response {
|
|
||||||
match self.0 {
|
|
||||||
LoginErrorKind::Unknown => (
|
|
||||||
StatusCode::INTERNAL_SERVER_ERROR,
|
|
||||||
"An unknown error occurred; you cursed, brah?",
|
|
||||||
)
|
|
||||||
.into_response(),
|
|
||||||
_ => (StatusCode::BAD_REQUEST, format!("{self}")).into_response(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//-************************************************************************
|
|
||||||
// Login handlers
|
|
||||||
//-************************************************************************
|
|
||||||
|
|
||||||
/// Handle login queries
|
|
||||||
#[axum::debug_handler]
|
|
||||||
pub async fn post_login(
|
|
||||||
mut auth: AuthContext,
|
|
||||||
State(pool): State<SqlitePool>,
|
|
||||||
) -> Result<(), LoginError> {
|
|
||||||
Err(LoginErrorKind::Unknown.into())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn get_login() -> impl IntoResponse {
|
|
||||||
LoginGet::default()
|
|
||||||
}
|
|
||||||
|
|
||||||
//-************************************************************************
|
//-************************************************************************
|
||||||
// private fns
|
// private fns
|
||||||
//-************************************************************************
|
//-************************************************************************
|
||||||
|
|
Loading…
Reference in a new issue