Skip to content

Commit

Permalink
Refactor authentication stuff into own module.
Browse files Browse the repository at this point in the history
  • Loading branch information
ISibboI committed Aug 28, 2023
1 parent ef61f0e commit 5b1fc09
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 32 deletions.
38 changes: 38 additions & 0 deletions backend/rvoc-backend/src/web/authentication.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use axum::{
http::{Request, StatusCode},
middleware::Next,
response::{IntoResponse, Response},
};
use typed_session_axum::ReadableSession;

use super::{session::RVocSessionData, user::model::Username};

pub async fn ensure_logged_in<B>(mut request: Request<B>, next: Next<B>) -> Response {
let session: &ReadableSession<RVocSessionData> = request.extensions().get().unwrap();

match session.data() {
RVocSessionData::Anonymous => return StatusCode::UNAUTHORIZED.into_response(),
RVocSessionData::LoggedIn(username) => {
let username = username.clone();
request.extensions_mut().insert(LoggedInUser(username));
}
}

next.run(request).await
}

/// If this extension is found, it means that the request was made by the contained username.
#[derive(Debug, Clone)]
pub struct LoggedInUser(Username);

impl From<LoggedInUser> for String {
fn from(value: LoggedInUser) -> Self {
value.0.into()
}
}

impl AsRef<str> for LoggedInUser {
fn as_ref(&self) -> &str {
self.0.as_ref()
}
}
36 changes: 6 additions & 30 deletions backend/rvoc-backend/src/web/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,28 @@ use std::{convert::Infallible, fmt::Display, sync::Arc};

use axum::{
error_handling::HandleErrorLayer,
http::{Request, StatusCode},
middleware::{self, Next},
response::{IntoResponse, Response},
http::StatusCode,
middleware,
response::IntoResponse,
routing::{delete, get, post},
Extension, Router,
};
use tower::ServiceBuilder;
use tracing::{debug, error, info, instrument};
use typed_session_axum::{ReadableSession, SessionLayer, SessionLayerError};
use typed_session_axum::{SessionLayer, SessionLayerError};

use crate::{
configuration::Configuration,
database::RVocAsyncDatabaseConnectionPool,
error::{RVocError, RVocResult, UserError},
web::{
authentication::ensure_logged_in,
session::{RVocSessionData, RVocSessionStoreConnector},
user::{create_account, delete_account},
},
};

use self::user::model::Username;

mod authentication;
mod session;
mod user;

Expand Down Expand Up @@ -83,30 +83,6 @@ async fn hello_world() -> &'static str {
"Hello World!"
}

async fn ensure_logged_in<B>(mut request: Request<B>, next: Next<B>) -> Response {
let session: &ReadableSession<RVocSessionData> = request.extensions().get().unwrap();

match session.data() {
RVocSessionData::Anonymous => return StatusCode::UNAUTHORIZED.into_response(),
RVocSessionData::LoggedIn(username) => {
let username = username.clone();
request.extensions_mut().insert(LoggedInUser(username));
}
}

next.run(request).await
}

/// If this extension is found, it means that the request was made by the contained username.
#[derive(Debug, Clone)]
pub struct LoggedInUser(Username);

impl From<LoggedInUser> for String {
fn from(value: LoggedInUser) -> Self {
value.0.into()
}
}

impl IntoResponse for RVocError {
fn into_response(self) -> axum::response::Response {
if let RVocError::UserError(user_error) = self {
Expand Down
4 changes: 2 additions & 2 deletions backend/rvoc-backend/src/web/user/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use self::{
password_hash::PasswordHash,
};

use super::{LoggedInUser, WebConfiguration, WebDatabaseConnectionPool};
use super::{authentication::LoggedInUser, WebConfiguration, WebDatabaseConnectionPool};

pub mod model;
pub mod password_hash;
Expand Down Expand Up @@ -82,7 +82,7 @@ pub async fn delete_account(
use diesel_async::RunQueryDsl;

match diesel::delete(users)
.filter(name.eq(username.0.as_ref()))
.filter(name.eq(username.as_ref()))
.execute(database_connection)
.await
{
Expand Down

0 comments on commit 5b1fc09

Please sign in to comment.