Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
215 changes: 129 additions & 86 deletions Cargo.lock

Large diffs are not rendered by default.

7 changes: 3 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,9 @@ axum = "0.8.1"
bon = "3.3.2"
chrono = "0.4.39"
console_error_panic_hook = "0.1.2"
dioxus = "0.7.0-rc.3"
dioxus-html = "0.7.0-rc.3"
dioxus-server = "0.7.0-rc.3"
futures = "0.3.31"
dioxus = "0.7.3"
dioxus-html = "0.7.3"
dioxus-server = "0.7.3"
http = "1.2.0"
jsonwebtoken = "10.3.0"
leptos = "0.8.3"
Expand Down
1 change: 1 addition & 0 deletions deny.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ wildcards = "deny"
[licenses]
allow = [
"Apache-2.0",
"BSD-2-Clause",
"BSD-3-Clause",
"BSL-1.0",
"CC0-1.0",
Expand Down
2 changes: 1 addition & 1 deletion examples/axum/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ shield.workspace = true
shield-axum = { workspace = true, features = ["utoipa"] }
shield-email = { workspace = true, features = ["sender-tracing"] }
shield-memory = { workspace = true, features = ["method-email", "method-oidc"] }
shield-oidc = { workspace = true, features = ["native-tls"] }
shield-oidc.workspace = true
time = "0.3.47"
tokio = { workspace = true, features = ["macros", "rt-multi-thread"] }
tower-sessions.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion examples/dioxus-axum/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ shield-email = { workspace = true, features = [
"sender-tracing",
], optional = true }
shield-memory = { workspace = true, optional = true }
shield-oidc = { workspace = true, features = ["native-tls"], optional = true }
shield-oidc = { workspace = true, optional = true }
tokio = { workspace = true, features = [
"macros",
"rt-multi-thread",
Expand Down
2 changes: 1 addition & 1 deletion examples/leptos-actix/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ shield-email = { workspace = true, features = [
shield-leptos.workspace = true
shield-leptos-actix = { workspace = true, optional = true }
shield-memory = { workspace = true, optional = true }
shield-oidc = { workspace = true, features = ["native-tls"], optional = true }
shield-oidc = { workspace = true, optional = true }
tracing.workspace = true
tracing-subscriber.workspace = true
wasm-bindgen.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion examples/leptos-axum/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ shield-email = { workspace = true, features = [
shield-leptos.workspace = true
shield-leptos-axum = { workspace = true, optional = true }
shield-memory = { workspace = true, optional = true }
shield-oidc = { workspace = true, features = ["native-tls"], optional = true }
shield-oidc = { workspace = true, optional = true }
time = "0.3.47"
tokio = { workspace = true, features = [
"macros",
Expand Down
4 changes: 2 additions & 2 deletions examples/sea-orm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ version.workspace = true
[dependencies]
sea-orm = { workspace = true, features = [
"macros",
"runtime-tokio-native-tls",
"runtime-tokio-rustls",
"sqlx-mysql",
"sqlx-postgres",
"sqlx-sqlite",
] }
sea-orm-migration = { workspace = true, features = [
"runtime-tokio-native-tls",
"runtime-tokio-rustls",
"sqlx-mysql",
"sqlx-postgres",
"sqlx-sqlite",
Expand Down
2 changes: 1 addition & 1 deletion packages/core/shield/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ async-trait.workspace = true
bon.workspace = true
chrono = { workspace = true, features = ["serde"] }
convert_case = "0.11.0"
futures.workspace = true
ordered_hash_map = "0.5.0"
serde = { workspace = true, features = ["derive"] }
serde_json.workspace = true
thiserror.workspace = true
Expand Down
60 changes: 24 additions & 36 deletions packages/core/shield/src/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,28 @@ use crate::{
error::ShieldError,
form::Form,
provider::Provider,
request::Request,
request::{Request, RequestMethod},
response::Response,
session::{BaseSession, MethodSession},
};
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
#[cfg(feature = "utoipa")]
use utoipa::openapi::HttpMethod;

#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum ActionMethod {
Get,
Post,
Put,
Delete,
Options,
Head,
Patch,
Trace,
}

#[cfg(feature = "utoipa")]
impl From<ActionMethod> for HttpMethod {
fn from(value: ActionMethod) -> Self {
match value {
ActionMethod::Get => Self::Get,
ActionMethod::Post => Self::Post,
ActionMethod::Put => Self::Put,
ActionMethod::Delete => Self::Delete,
ActionMethod::Options => Self::Options,
ActionMethod::Head => Self::Head,
ActionMethod::Patch => Self::Patch,
ActionMethod::Trace => Self::Trace,
}
}
#[async_trait]
pub trait Action: Send + Sync {
fn id(&self) -> &'static str;

fn name(&self) -> &'static str;

fn openapi_summary(&self) -> &'static str;

fn openapi_description(&self) -> &'static str;

fn method(&self) -> RequestMethod;

async fn forms(&self) -> Result<Vec<Form>, ShieldError>;

async fn call(&self, session: &BaseSession, request: Request) -> Result<Response, ShieldError>;
}

// TODO: Think of a better name.
Expand All @@ -48,6 +35,7 @@ impl From<ActionMethod> for HttpMethod {
pub struct ActionForms {
pub id: String,
pub name: String,
pub forms: Vec<Form>,
pub method_forms: Vec<ActionMethodForm>,
}

Expand All @@ -70,7 +58,7 @@ pub struct ActionProviderForm {
}

#[async_trait]
pub trait Action<P: Provider, S>: ErasedAction + Send + Sync {
pub trait MethodAction<P: Provider, S>: ErasedMethodAction + Send + Sync {
fn id(&self) -> String;

fn name(&self) -> String;
Expand All @@ -79,7 +67,7 @@ pub trait Action<P: Provider, S>: ErasedAction + Send + Sync {

fn openapi_description(&self) -> &'static str;

fn method(&self) -> ActionMethod;
fn method(&self) -> RequestMethod;

fn condition(&self, _provider: &P, _session: &MethodSession<S>) -> Result<bool, ShieldError> {
Ok(true)
Expand All @@ -96,7 +84,7 @@ pub trait Action<P: Provider, S>: ErasedAction + Send + Sync {
}

#[async_trait]
pub trait ErasedAction: Send + Sync {
pub trait ErasedMethodAction: Send + Sync {
fn erased_id(&self) -> String;

fn erased_name(&self) -> String;
Expand All @@ -105,7 +93,7 @@ pub trait ErasedAction: Send + Sync {

fn erased_openapi_description(&self) -> &'static str;

fn erased_method(&self) -> ActionMethod;
fn erased_method(&self) -> RequestMethod;

fn erased_condition(
&self,
Expand All @@ -129,10 +117,10 @@ pub trait ErasedAction: Send + Sync {
}

#[macro_export]
macro_rules! erased_action {
macro_rules! erased_method_action {
($action:ident $(, < $( $generic_name:ident : $generic_type:ident ),+ > )*) => {
#[async_trait]
impl $( < $( $generic_name: $generic_type + 'static ),+ > )* $crate::ErasedAction for $action $( < $( $generic_name ),+ > )* {
impl $( < $( $generic_name: $generic_type + 'static ),+ > )* $crate::ErasedMethodAction for $action $( < $( $generic_name ),+ > )* {
fn erased_id(&self) -> String {
self.id()
}
Expand All @@ -149,7 +137,7 @@ macro_rules! erased_action {
self.openapi_description()
}

fn erased_method(&self) -> $crate::ActionMethod {
fn erased_method(&self) -> $crate::RequestMethod {
self.method()
}

Expand Down
56 changes: 32 additions & 24 deletions packages/core/shield/src/actions/sign_out.rs
Original file line number Diff line number Diff line change
@@ -1,53 +1,61 @@
use async_trait::async_trait;

use crate::{
action::Action,
error::ShieldError,
form::{Form, Input, InputType, InputTypeSubmit, InputValue},
provider::Provider,
session::MethodSession,
request::{Request, RequestMethod},
response::{Response, ResponseType},
session::{BaseSession, SessionAction},
};

const ACTION_ID: &str = "sign-out";
const ACTION_NAME: &str = "Sign out";

// TODO: Sign out should be a global action that is independent of the method.
// TODO: Add hooks, so the method can still perform custom sign out.

pub struct SignOutAction;

impl SignOutAction {
pub fn id() -> String {
ACTION_ID.to_owned()
#[async_trait]
impl Action for SignOutAction {
fn id(&self) -> &'static str {
ACTION_ID
}

fn name(&self) -> &'static str {
ACTION_NAME
}

pub fn name() -> String {
ACTION_NAME.to_owned()
fn openapi_summary(&self) -> &'static str {
"Sign out"
}

pub fn condition<P: Provider, S>(
provider: &P,
session: &MethodSession<S>,
) -> Result<bool, ShieldError> {
Ok(session
.base
.authentication
.as_ref()
.is_some_and(|authentication| {
authentication.method_id == provider.method_id()
&& authentication.provider_id == provider.id()
}))
fn openapi_description(&self) -> &'static str {
"Sign out."
}

pub async fn forms<P: Provider>(_provider: P) -> Result<Vec<Form>, ShieldError> {
fn method(&self) -> RequestMethod {
RequestMethod::Post
}

async fn forms(&self) -> Result<Vec<Form>, ShieldError> {
Ok(vec![Form {
inputs: vec![Input {
name: "submit".to_owned(),
label: None,
r#type: InputType::Submit(InputTypeSubmit {}),
value: Some(InputValue::String {
value: Self::name(),
value: self.name().to_owned(),
}),
addon_start: None,
addon_end: None,
}],
}])
}

async fn call(
&self,
_session: &BaseSession,
_request: Request,
) -> Result<Response, ShieldError> {
Ok(Response::new(ResponseType::Default).session_action(SessionAction::Unauthenticate))
}
}
20 changes: 10 additions & 10 deletions packages/core/shield/src/method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use async_trait::async_trait;
use serde::{Serialize, de::DeserializeOwned};

use crate::{
ErasedAction,
action::Action,
ErasedMethodAction,
action::MethodAction,
error::{SessionError, ShieldError},
provider::Provider,
};
Expand All @@ -17,12 +17,12 @@ pub trait Method: Send + Sync {

fn id(&self) -> String;

fn actions(&self) -> Vec<Box<dyn Action<Self::Provider, Self::Session>>>;
fn actions(&self) -> Vec<Box<dyn MethodAction<Self::Provider, Self::Session>>>;

fn action_by_id(
&self,
action_id: &str,
) -> Option<Box<dyn Action<Self::Provider, Self::Session>>> {
) -> Option<Box<dyn MethodAction<Self::Provider, Self::Session>>> {
self.actions()
.into_iter()
.find(|action| action.id() == action_id)
Expand All @@ -46,9 +46,9 @@ pub trait Method: Send + Sync {
pub trait ErasedMethod: Send + Sync {
fn erased_id(&self) -> String;

fn erased_actions(&self) -> Vec<Box<dyn ErasedAction>>;
fn erased_actions(&self) -> Vec<Box<dyn ErasedMethodAction>>;

fn erased_action_by_id(&self, action_id: &str) -> Option<Box<dyn ErasedAction>>;
fn erased_action_by_id(&self, action_id: &str) -> Option<Box<dyn ErasedMethodAction>>;

async fn erased_providers(
&self,
Expand All @@ -74,19 +74,19 @@ macro_rules! erased_method {
self.id()
}

fn erased_actions(&self) -> Vec<Box<dyn $crate::ErasedAction>> {
fn erased_actions(&self) -> Vec<Box<dyn $crate::ErasedMethodAction>> {
self.actions()
.into_iter()
.map(|action| action as Box<dyn $crate::ErasedAction>)
.map(|action| action as Box<dyn $crate::ErasedMethodAction>)
.collect()
}

fn erased_action_by_id(
&self,
action_id: &str,
) -> Option<Box<dyn $crate::ErasedAction>> {
) -> Option<Box<dyn $crate::ErasedMethodAction>> {
self.action_by_id(action_id)
.map(|action| action as Box<dyn $crate::ErasedAction>)
.map(|action| action as Box<dyn $crate::ErasedMethodAction>)
}

async fn erased_providers(
Expand Down
12 changes: 10 additions & 2 deletions packages/core/shield/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,18 @@ pub struct ActionFormsPathParams {
#[cfg_attr(feature = "utoipa", derive(utoipa::IntoParams))]
#[serde(rename_all = "camelCase")]
pub struct ActionPathParams {
/// ID of the method.
pub method_id: String,
/// ID of the action.
pub action_id: String,
}

#[derive(Deserialize)]
#[cfg_attr(feature = "utoipa", derive(utoipa::IntoParams))]
#[serde(rename_all = "camelCase")]
pub struct MethodActionPathParams {
/// ID of the action.
pub action_id: String,
/// ID of the method.
pub method_id: String,
/// ID of provider (optional).
pub provider_id: Option<String>,
}
Loading
Loading