Skip to content

Commit

Permalink
feat: adding mfa verifying as a data route in data api
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasauler committed Jul 8, 2024
1 parent a4b18e2 commit e61c174
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 8 deletions.
2 changes: 1 addition & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
buildInputs = [
bacon
cargo-nextest
cargo-shuttle
# cargo-shuttle
jq
nixpkgs-fmt
openssl
Expand Down
2 changes: 1 addition & 1 deletion src/data/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
pub mod router;
mod service;
pub mod service;
19 changes: 19 additions & 0 deletions src/data/router/auth.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use std::sync::Arc;

use askama_axum::IntoResponse;
use axum::{extract::State, routing::post, Json, Router};

use crate::{auth::AuthSession, hypermedia::schema::auth::MfaTokenForm, AppState};

pub fn private_router() -> Router<Arc<AppState>> {
Router::new().route("/api/auth/mfa", post(mfa_verify))
}

async fn mfa_verify(
auth_session: AuthSession,
State(shared_state): State<Arc<AppState>>,
Json(mfa_token): Json<MfaTokenForm>,
) -> impl IntoResponse {
crate::hypermedia::service::auth::mfa_verify(auth_session, &shared_state.pool, mfa_token.token)
.await
}
2 changes: 1 addition & 1 deletion src/data/router.rs → src/data/router/expenses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::{
AppState,
};

pub fn data_router() -> Router<Arc<AppState>> {
pub fn router() -> Router<Arc<AppState>> {
Router::new()
.route("/api/expenses", get(get_expenses).post(insert_expense))
.route(
Expand Down
2 changes: 2 additions & 0 deletions src/data/router/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod auth;
pub mod expenses;
4 changes: 2 additions & 2 deletions src/hypermedia/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pub mod router;
mod schema;
mod service;
pub mod schema;
pub mod service;
7 changes: 4 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,14 @@ async fn axum(

let shared_state = Arc::new(AppState { pool, secret_store });
let router = Router::new()
.merge(data::router::data_router())
.merge(data::router::expenses::router())
.merge(hypermedia::router::expenses::router())
.route_layer(permission_required!(
Backend,
login_url = "/auth/mfa",
"restricted:read",
))
.merge(data::router::auth::private_router())
.merge(hypermedia::router::auth::private_router())
.route_layer(permission_required!(
Backend,
Expand Down Expand Up @@ -161,7 +162,7 @@ async fn axum(
if res.headers().get("content-security-policy").is_none() {
res.headers_mut().insert(
"content-security-policy",
generate_defaut_csp()
generate_default_csp()
.to_string()
.parse()
.unwrap_or_else(|_| {
Expand Down Expand Up @@ -231,7 +232,7 @@ fn generate_general_helmet_headers() -> Helmet {

/// Returns a default strict Content Security Policy.
/// It's used whenever a custom CSP is not set.
fn generate_defaut_csp() -> ContentSecurityPolicy<'static> {
fn generate_default_csp() -> ContentSecurityPolicy<'static> {
return ContentSecurityPolicy::new()
.default_src(vec!["'self'"])
.base_uri(vec!["'none'"])
Expand Down

0 comments on commit e61c174

Please sign in to comment.