fix formatting

This commit is contained in:
Evan Almloff committed 2023-07-08 10:56:10 -07:00
1 parent fd98b51dfe
commit 7c2bf4c5ed
3 files changed
+64 -35

No files matched your search

@@ -28,6 +28,7 @@ sqlx = { version = "0.7.0", features = [
], optional = true } ], optional = true }
anyhow = "1.0.71" anyhow = "1.0.71"
http = { version = "0.2.9", optional = true } http = { version = "0.2.9", optional = true }
tower = { version = "0.4.13", optional = true }
[dependencies.axum_session] [dependencies.axum_session]
version = "0.3.0" version = "0.3.0"
@@ -41,7 +42,7 @@ optional = true
[features] [features]
default = [] default = []
ssr = ["axum", "tokio", "dioxus-fullstack/axum", "tower-http", "simple_logger", "async-trait", "sqlx", "axum_session", "axum_session_auth", "http"] ssr = ["axum", "tokio", "dioxus-fullstack/axum", "tower-http", "simple_logger", "async-trait", "sqlx", "axum_session", "axum_session_auth", "http", "tower"]
web = ["dioxus-web"] web = ["dioxus-web"]
[profile.release] [profile.release]
@@ -1,9 +1,18 @@
use async_trait::async_trait; use async_trait::async_trait;
use axum::{http::Method, routing::get, Router, response::{Response, IntoResponse}}; use axum::{
http::Method,
response::{IntoResponse, Response},
routing::get,
Router,
};
use axum_session::{SessionConfig, SessionLayer, SessionSqlitePool, SessionStore}; use axum_session::{SessionConfig, SessionLayer, SessionSqlitePool, SessionStore};
use axum_session_auth::*; use axum_session_auth::*;
use core::pin::Pin;
use dioxus_fullstack::prelude::*;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use sqlx::sqlite::{SqliteConnectOptions, SqlitePool, SqlitePoolOptions}; use sqlx::sqlite::{SqliteConnectOptions, SqlitePool, SqlitePoolOptions};
use std::error::Error;
use std::future::Future;
use std::{collections::HashSet, net::SocketAddr, str::FromStr}; use std::{collections::HashSet, net::SocketAddr, str::FromStr};
#[derive(Debug, Clone, Serialize, Deserialize)] #[derive(Debug, Clone, Serialize, Deserialize)]
@@ -183,17 +192,29 @@ pub async fn connect_to_database() -> SqlitePool {
.unwrap() .unwrap()
} }
pub struct Session(pub axum_session_auth::AuthSession<crate::auth::User, i64, axum_session_auth::SessionSqlitePool, sqlx::SqlitePool>); pub struct Session(
pub axum_session_auth::AuthSession<
crate::auth::User,
i64,
axum_session_auth::SessionSqlitePool,
sqlx::SqlitePool,
>,
);
impl std::ops::Deref for Session { impl std::ops::Deref for Session {
type Target = axum_session_auth::AuthSession<crate::auth::User, i64, axum_session_auth::SessionSqlitePool, sqlx::SqlitePool>; type Target = axum_session_auth::AuthSession<
crate::auth::User,
i64,
axum_session_auth::SessionSqlitePool,
sqlx::SqlitePool,
>;
fn deref(&self) -> &Self::Target { fn deref(&self) -> &Self::Target {
&self.0 &self.0
} }
} }
impl std::ops::DerefMut for Session { impl std::ops::DerefMut for Session {
fn deref_mut(&mut self) -> &mut Self::Target { fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0 &mut self.0
} }
@@ -211,19 +232,31 @@ impl std::fmt::Display for AuthSessionLayerNotFound {
impl std::error::Error for AuthSessionLayerNotFound {} impl std::error::Error for AuthSessionLayerNotFound {}
impl IntoResponse for AuthSessionLayerNotFound { impl IntoResponse for AuthSessionLayerNotFound {
fn into_response(self) -> Response { fn into_response(self) -> Response {
(http::status::StatusCode::INTERNAL_SERVER_ERROR, "AuthSessionLayer was not found").into_response() (
} http::status::StatusCode::INTERNAL_SERVER_ERROR,
"AuthSessionLayer was not found",
)
.into_response()
} }
}
#[async_trait] #[async_trait]
impl<S: std::marker::Sync + std::marker::Send> axum::extract::FromRequestParts<S> for Session { impl<S: std::marker::Sync + std::marker::Send> axum::extract::FromRequestParts<S> for Session {
type Rejection = AuthSessionLayerNotFound; type Rejection = AuthSessionLayerNotFound;
async fn from_request_parts(parts: &mut http::request::Parts, state: &S) -> Result<Self, Self::Rejection> { async fn from_request_parts(
axum_session_auth::AuthSession::<crate::auth::User, i64, axum_session_auth::SessionSqlitePool, sqlx::SqlitePool>::from_request_parts(parts, state) parts: &mut http::request::Parts,
.await state: &S,
.map(Session) ) -> Result<Self, Self::Rejection> {
.map_err(|_| AuthSessionLayerNotFound) axum_session_auth::AuthSession::<
crate::auth::User,
i64,
axum_session_auth::SessionSqlitePool,
sqlx::SqlitePool,
>::from_request_parts(parts, state)
.await
.map(Session)
.map_err(|_| AuthSessionLayerNotFound)
} }
} }
@@ -61,8 +61,7 @@ fn main() {
>::new(Some(pool)) >::new(Some(pool))
.with_config(auth_config), .with_config(auth_config),
) )
.layer(axum_session::SessionLayer::new(session_store)) .layer(axum_session::SessionLayer::new(session_store));
;
// run it // run it
let addr = std::net::SocketAddr::from(([127, 0, 0, 1], 3000)); let addr = std::net::SocketAddr::from(([127, 0, 0, 1], 3000));
@@ -123,39 +122,35 @@ fn app(cx: Scope) -> Element {
#[server(GetUserName)] #[server(GetUserName)]
pub async fn get_user_name( pub async fn get_user_name(
#[extract] #[extract] session: crate::auth::Session,
session : crate::auth::Session
) -> Result<String, ServerFnError> { ) -> Result<String, ServerFnError> {
Ok(session.0.current_user.unwrap().username Ok(session.0.current_user.unwrap().username.to_string())
.to_string())
} }
#[server(Login)] #[server(Login)]
pub async fn login( pub async fn login(#[extract] auth: crate::auth::Session) -> Result<(), ServerFnError> {
#[extract]
auth: crate::auth::Session
) -> Result<(), ServerFnError> {
auth.login_user(2); auth.login_user(2);
Ok(()) Ok(())
} }
#[server(Permissions)] #[server(Permissions)]
pub async fn get_permissions( pub async fn get_permissions(
#[extract] #[extract] method: axum::http::Method,
method: axum::http::Method, #[extract] auth: crate::auth::Session,
#[extract]
auth: crate::auth::Session,
) -> Result<String, ServerFnError> { ) -> Result<String, ServerFnError> {
let current_user = auth.current_user.clone().unwrap_or_default(); let current_user = auth.current_user.clone().unwrap_or_default();
// lets check permissions only and not worry about if they are anon or not // lets check permissions only and not worry about if they are anon or not
if !axum_session_auth::Auth::<crate::auth::User, i64, sqlx::SqlitePool>::build([axum::http::Method::POST], false) if !axum_session_auth::Auth::<crate::auth::User, i64, sqlx::SqlitePool>::build(
.requires(axum_session_auth::Rights::any([ [axum::http::Method::POST],
axum_session_auth::Rights::permission("Category::View"), false,
axum_session_auth::Rights::permission("Admin::View"), )
])) .requires(axum_session_auth::Rights::any([
.validate(&current_user, &method, None) axum_session_auth::Rights::permission("Category::View"),
.await axum_session_auth::Rights::permission("Admin::View"),
]))
.validate(&current_user, &method, None)
.await
{ {
return Ok(format!( return Ok(format!(
"User {}, Does not have permissions needed to view this page please login", "User {}, Does not have permissions needed to view this page please login",