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
16 changes: 16 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,22 @@ sea-orm = "1.1.2"
sea-orm-migration = "1.1.2"
serde = "1.0.215"
serde_json = "1.0.133"
shield = { path = "./packages/core/shield", version = "0.0.4" }
shield-actix = { path = "./packages/integrations/shield-actix", version = "0.0.4" }
shield-axum = { path = "./packages/integrations/shield-axum", version = "0.0.4" }
shield-credentials = { path = "./packages/methods/shield-credentials", version = "0.0.4" }
shield-diesel = { path = "./packages/storage/shield-diesel", version = "0.0.4" }
shield-email = { path = "./packages/methods/shield-email", version = "0.0.4" }
shield-leptos = { path = "./packages/integrations/shield-leptos", version = "0.0.4" }
shield-leptos-actix = { path = "./packages/integrations/shield-leptos-actix", version = "0.0.4" }
shield-leptos-axum = { path = "./packages/integrations/shield-leptos-axum", version = "0.0.4" }
shield-memory = { path = "./packages/storage/shield-memory", version = "0.0.4" }
shield-oauth = { path = "./packages/methods/shield-oauth", version = "0.0.4" }
shield-oidc = { path = "./packages/methods/shield-oidc", version = "0.0.4" }
shield-sea-orm = { path = "./packages/storage/shield-sea-orm", version = "0.0.4" }
shield-sqlx = { path = "./packages/storage/shield-sqlx", version = "0.0.4" }
shield-tower = { path = "./packages/integrations/shield-tower", version = "0.0.4" }
shield-webauthn = { path = "./packages/methods/shield-webauthn", version = "0.0.4" }
tokio = "1.42.0"
tower-layer = "0.3.3"
tower-service = "0.3.3"
Expand Down
14 changes: 6 additions & 8 deletions examples/leptos-actix/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,11 @@ leptos.workspace = true
leptos_actix = { workspace = true, optional = true }
leptos_meta.workspace = true
leptos_router.workspace = true
shield = { path = "../../packages/core/shield" }
shield-leptos = { path = "../../packages/integrations/shield-leptos" }
shield-leptos-actix = { path = "../../packages/integrations/shield-leptos-actix", optional = true }
shield-memory = { path = "../../packages/storage/shield-memory", optional = true }
shield-oidc = { path = "../../packages/providers/shield-oidc", features = [
"native-tls",
], optional = true }
shield.workspace = true
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 }
tracing.workspace = true
tracing-subscriber.workspace = true
wasm-bindgen.workspace = true
Expand All @@ -49,7 +47,7 @@ ssr = [
"leptos/ssr",
"leptos_meta/ssr",
"leptos_router/ssr",
"shield-memory/provider-oidc",
"shield-memory/method-oidc",
]

[package.metadata.leptos]
Expand Down
4 changes: 2 additions & 2 deletions examples/leptos-actix/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ async fn main() -> std::io::Result<()> {
use shield_examples_leptos_actix::app::*;
use shield_leptos_actix::{ShieldMiddleware, provide_actix_integration};
use shield_memory::{MemoryStorage, User};
use shield_oidc::{Keycloak, OidcProvider};
use shield_oidc::{Keycloak, OidcMethod};
use tracing::{info, level_filters::LevelFilter};

// Initialize tracing
Expand Down Expand Up @@ -44,7 +44,7 @@ async fn main() -> std::io::Result<()> {
let shield = Shield::new(
shield_storage.clone(),
vec![Arc::new(
OidcProvider::new(shield_storage).with_subproviders([Keycloak::builder(
OidcMethod::new(shield_storage).with_providers([Keycloak::builder(
"keycloak",
"http://localhost:18080/realms/Shield",
"client1",
Expand Down
14 changes: 6 additions & 8 deletions examples/leptos-axum/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,13 @@ leptos.workspace = true
leptos_axum = { workspace = true, optional = true }
leptos_meta.workspace = true
leptos_router.workspace = true
shield = { path = "../../packages/core/shield" }
shield-leptos = { path = "../../packages/integrations/shield-leptos" }
shield-leptos-axum = { path = "../../packages/integrations/shield-leptos-axum", features = [
shield.workspace = true
shield-leptos.workspace = true
shield-leptos-axum = { workspace = true, features = [
"utoipa",
], optional = true }
shield-memory = { path = "../../packages/storage/shield-memory", optional = true }
shield-oidc = { path = "../../packages/providers/shield-oidc", features = [
"native-tls",
], optional = true }
shield-memory = { workspace = true, optional = true }
shield-oidc = { workspace = true, features = ["native-tls"], optional = true }
time = "0.3.37"
tokio = { workspace = true, features = ["rt-multi-thread"], optional = true }
tower-sessions = { workspace = true, optional = true }
Expand All @@ -52,7 +50,7 @@ ssr = [
"leptos/ssr",
"leptos_meta/ssr",
"leptos_router/ssr",
"shield-memory/provider-oidc",
"shield-memory/method-oidc",
]

[package.metadata.leptos]
Expand Down
4 changes: 2 additions & 2 deletions examples/leptos-axum/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ async fn main() {
use shield_examples_leptos_axum::app::*;
use shield_leptos_axum::{AuthRoutes, ShieldLayer, auth_required, provide_axum_integration};
use shield_memory::{MemoryStorage, User};
use shield_oidc::{Keycloak, OidcProvider};
use shield_oidc::{Keycloak, OidcMethod};
use time::Duration;
use tokio::net::TcpListener;
use tower_sessions::{Expiry, MemoryStore, SessionManagerLayer};
Expand Down Expand Up @@ -40,7 +40,7 @@ async fn main() {
let shield = Shield::new(
storage.clone(),
vec![Arc::new(
OidcProvider::new(storage).with_subproviders([Keycloak::builder(
OidcMethod::new(storage).with_providers([Keycloak::builder(
"keycloak",
"http://localhost:18080/realms/Shield",
"client1",
Expand Down
5 changes: 1 addition & 4 deletions examples/sea-orm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,5 @@ sea-orm-migration = { workspace = true, features = [
"sqlx-postgres",
"sqlx-sqlite",
] }
shield-sea-orm = { path = "../../packages/storage/shield-sea-orm", features = [
"all-providers",
"utoipa",
] }
shield-sea-orm = { workspace = true, features = ["all-methods", "utoipa"] }
tokio = { workspace = true, features = ["macros", "rt-multi-thread"] }
14 changes: 10 additions & 4 deletions packages/core/shield/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
use thiserror::Error;

#[derive(Debug, Error)]
pub enum MethodError {
#[error("method `{0}` not found")]
MethodNotFound(String),
}

#[derive(Debug, Error)]
pub enum ProviderError {
#[error("provider is missing")]
ProviderMissing,
#[error("provider `{0}` not found")]
ProviderNotFound(String),
#[error("subprovider is missing")]
SubproviderMissing,
#[error("subprovider `{0}` not found")]
SubproviderNotFound(String),
}

#[derive(Debug, Error)]
Expand Down Expand Up @@ -45,6 +49,8 @@ pub enum SessionError {
#[derive(Debug, Error)]

pub enum ShieldError {
#[error(transparent)]
Method(#[from] MethodError),
#[error(transparent)]
Provider(#[from] ProviderError),
#[error(transparent)]
Expand Down
2 changes: 2 additions & 0 deletions packages/core/shield/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod error;
mod form;
mod method;
mod options;
mod provider;
mod request;
Expand All @@ -12,6 +13,7 @@ mod user;

pub use error::*;
pub use form::*;
pub use method::*;
pub use options::*;
pub use provider::*;
pub use request::*;
Expand Down
118 changes: 118 additions & 0 deletions packages/core/shield/src/method.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
use async_trait::async_trait;

use crate::{
error::ShieldError,
options::ShieldOptions,
provider::Provider,
request::{SignInCallbackRequest, SignInRequest, SignOutRequest},
response::Response,
session::Session,
};

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

async fn providers(&self) -> Result<Vec<Box<dyn Provider>>, ShieldError>;

async fn provider_by_id(
&self,
provider_id: &str,
) -> Result<Option<Box<dyn Provider>>, ShieldError>;

async fn sign_in(
&self,
request: SignInRequest,
session: Session,
options: &ShieldOptions,
) -> Result<Response, ShieldError>;

async fn sign_in_callback(
&self,
request: SignInCallbackRequest,
session: Session,
options: &ShieldOptions,
) -> Result<Response, ShieldError>;

async fn sign_out(
&self,
request: SignOutRequest,
session: Session,
options: &ShieldOptions,
) -> Result<Response, ShieldError>;
}

#[cfg(test)]
pub(crate) mod tests {
use async_trait::async_trait;

use crate::{
ShieldOptions,
error::ShieldError,
provider::Provider,
request::{SignInCallbackRequest, SignInRequest, SignOutRequest},
response::Response,
session::Session,
};

use super::Method;

pub const TEST_METHOD_ID: &str = "test";

#[derive(Default)]
pub struct TestMethod {
id: Option<&'static str>,
}

impl TestMethod {
pub fn with_id(mut self, id: &'static str) -> Self {
self.id = Some(id);
self
}
}

#[async_trait]
impl Method for TestMethod {
fn id(&self) -> String {
self.id.unwrap_or(TEST_METHOD_ID).to_owned()
}

async fn providers(&self) -> Result<Vec<Box<dyn Provider>>, ShieldError> {
Ok(vec![])
}

async fn provider_by_id(
&self,
_provider_id: &str,
) -> Result<Option<Box<dyn Provider>>, ShieldError> {
Ok(None)
}

async fn sign_in(
&self,
_request: SignInRequest,
_session: Session,
_options: &ShieldOptions,
) -> Result<Response, ShieldError> {
todo!("redirect back?")
}

async fn sign_in_callback(
&self,
_request: SignInCallbackRequest,
_session: Session,
_options: &ShieldOptions,
) -> Result<Response, ShieldError> {
todo!("redirect back?")
}

async fn sign_out(
&self,
_request: SignOutRequest,
_session: Session,
_options: &ShieldOptions,
) -> Result<Response, ShieldError> {
todo!("redirect back?")
}
}
}
Loading
Loading