Skip to content

Commit 12e2fc0

Browse files
feat(shield-dioxus): add Shield for Dioxus (#125)
1 parent 3a0310f commit 12e2fc0

14 files changed

Lines changed: 1269 additions & 27 deletions

File tree

Cargo.lock

Lines changed: 1055 additions & 27 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ axum = "0.8.1"
2020
bon = "3.3.2"
2121
chrono = "0.4.39"
2222
console_error_panic_hook = "0.1.2"
23+
dioxus = "0.7.0-alpha.1"
2324
futures = "0.3.31"
2425
http = "1.2.0"
2526
leptos = "0.8.0-beta"
@@ -37,6 +38,8 @@ shield-actix = { path = "./packages/integrations/shield-actix", version = "0.0.4
3738
shield-axum = { path = "./packages/integrations/shield-axum", version = "0.0.4" }
3839
shield-credentials = { path = "./packages/methods/shield-credentials", version = "0.0.4" }
3940
shield-diesel = { path = "./packages/storage/shield-diesel", version = "0.0.4" }
41+
shield-dioxus = { path = "./packages/integrations/shield-dioxus", version = "0.0.4" }
42+
shield-dioxus-axum = { path = "./packages/integrations/shield-dioxus-axum", version = "0.0.4" }
4043
shield-email = { path = "./packages/methods/shield-email", version = "0.0.4" }
4144
shield-leptos = { path = "./packages/integrations/shield-leptos", version = "0.0.4" }
4245
shield-leptos-actix = { path = "./packages/integrations/shield-leptos-actix", version = "0.0.4" }
@@ -59,3 +62,15 @@ utoipa = { version = "5.3.1", features = ["chrono", "uuid"] }
5962
uuid = "1.11.0"
6063
wasm-bindgen = "0.2.100"
6164
wasm-tracing = "2.0.0"
65+
66+
[profile]
67+
68+
[profile.wasm-dev]
69+
inherits = "dev"
70+
opt-level = 1
71+
72+
[profile.server-dev]
73+
inherits = "dev"
74+
75+
[profile.android-dev]
76+
inherits = "dev"

deny.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ allow = [
1717
"Apache-2.0",
1818
"BSD-3-Clause",
1919
"BSL-1.0",
20+
"CC0-1.0",
2021
"CDLA-Permissive-2.0",
2122
"ISC",
2223
"MIT",

examples/dioxus-axum/Cargo.toml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
[package]
2+
name = "shield-examples-diouxs-axum"
3+
description = "Example with Dioxus and Axum."
4+
publish = false
5+
6+
authors.workspace = true
7+
edition.workspace = true
8+
license.workspace = true
9+
repository.workspace = true
10+
version.workspace = true
11+
12+
[features]
13+
default = ["server"]
14+
# TODO: Enabling these triggers multiple RustSec advisories.
15+
# desktop = ["dioxus/desktop"]
16+
# mobile = ["dioxus/mobile"]
17+
server = [
18+
"dep:axum",
19+
"dep:shield-dioxus-axum",
20+
"dep:shield-memory",
21+
"dep:shield-oidc",
22+
"dep:tokio",
23+
"dep:tower-sessions",
24+
"dioxus/server",
25+
"shield-memory/method-oidc",
26+
]
27+
web = ["dioxus/web"]
28+
29+
[dependencies]
30+
axum = { workspace = true, optional = true }
31+
dioxus = { workspace = true, features = ["router", "fullstack"] }
32+
shield.workspace = true
33+
shield-dioxus-axum = { workspace = true, optional = true }
34+
shield-memory = { workspace = true, optional = true }
35+
shield-oidc = { workspace = true, features = ["native-tls"], optional = true }
36+
tokio = { workspace = true, features = ["rt-multi-thread"], optional = true }
37+
tower-sessions = { workspace = true, optional = true }
38+
tracing.workspace = true

examples/dioxus-axum/Dioxus.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[application]
2+
3+
[web.app]
4+
title = "Shield Dioxus Axum Example"
5+
6+
[web.resource]
7+
style = []
8+
script = []
9+
10+
[web.resource.dev]
11+
script = []

examples/dioxus-axum/clippy.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
await-holding-invalid-types = [
2+
"generational_box::GenerationalRef",
3+
{ path = "generational_box::GenerationalRef", reason = "Reads should not be held over an await point. This will cause any writes to fail while the await is pending since the read borrow is still active." },
4+
"generational_box::GenerationalRefMut",
5+
{ path = "generational_box::GenerationalRefMut", reason = "Write should not be held over an await point. This will cause any reads or writes to fail while the await is pending since the write borrow is still active." },
6+
"dioxus_signals::Write",
7+
{ path = "dioxus_signals::Write", reason = "Write should not be held over an await point. This will cause any reads or writes to fail while the await is pending since the write borrow is still active." },
8+
]

examples/dioxus-axum/src/app.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
use dioxus::prelude::*;
2+
3+
use crate::home::Home;
4+
5+
#[derive(Debug, Clone, Routable, PartialEq)]
6+
#[rustfmt::skip]
7+
enum Route {
8+
#[route("/")]
9+
Home {},
10+
}
11+
12+
#[component]
13+
pub fn App() -> Element {
14+
rsx! {
15+
main {
16+
Router::<Route> {}
17+
}
18+
}
19+
}

examples/dioxus-axum/src/home.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
use dioxus::prelude::*;
2+
3+
#[component]
4+
pub fn Home() -> Element {
5+
rsx! {
6+
h1 { "Shield Dioxus Axum Example" }
7+
}
8+
}

examples/dioxus-axum/src/main.rs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
mod app;
2+
mod home;
3+
4+
use crate::app::App;
5+
6+
#[cfg(not(feature = "server"))]
7+
fn main() {
8+
dioxus::launch(App);
9+
}
10+
11+
#[cfg(feature = "server")]
12+
#[tokio::main]
13+
async fn main() {
14+
use std::sync::Arc;
15+
16+
use axum::Router;
17+
use dioxus::{
18+
cli_config::fullstack_address_or_localhost,
19+
prelude::{DioxusRouterExt, *},
20+
};
21+
use shield::{Shield, ShieldOptions};
22+
use shield_dioxus_axum::ShieldLayer;
23+
use shield_memory::MemoryStorage;
24+
use shield_oidc::{Keycloak, OidcMethod};
25+
use tokio::net::TcpListener;
26+
use tower_sessions::{Expiry, MemoryStore, SessionManagerLayer, cookie::time::Duration};
27+
use tracing::{Level, info};
28+
29+
// Initialize Dioxus
30+
let addr = fullstack_address_or_localhost();
31+
dioxus::logger::init(Level::DEBUG).unwrap();
32+
33+
// Initialize sessions
34+
let session_store = MemoryStore::default();
35+
let session_layer = SessionManagerLayer::new(session_store)
36+
.with_secure(false)
37+
.with_expiry(Expiry::OnInactivity(Duration::minutes(10)));
38+
39+
// Initialize Shield
40+
let storage = MemoryStorage::new();
41+
let shield = Shield::new(
42+
storage.clone(),
43+
vec![Arc::new(
44+
OidcMethod::new(storage).with_providers([Keycloak::builder(
45+
"keycloak",
46+
"http://localhost:18080/realms/Shield",
47+
"client1",
48+
)
49+
.client_secret("xcpQsaGbRILTljPtX4npjmYMBjKrariJ")
50+
.redirect_url(format!(
51+
"http://localhost:{}/api/auth/sign-in/callback/oidc/keycloak",
52+
addr.port()
53+
))
54+
.build()]),
55+
)],
56+
ShieldOptions::default(),
57+
);
58+
let shield_layer = ShieldLayer::new(shield.clone());
59+
60+
// Initialize router
61+
let router = Router::new()
62+
.serve_dioxus_application(ServeConfig::new().unwrap(), App)
63+
.layer(shield_layer)
64+
.layer(session_layer);
65+
66+
// Start app
67+
info!("listening on http://{}", &addr);
68+
let listener = TcpListener::bind(&addr).await.unwrap();
69+
axum::serve(listener, router.into_make_service())
70+
.await
71+
.unwrap();
72+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[package]
2+
name = "shield-dioxus-axum"
3+
description = "Dioxus Axum integration for Shield."
4+
5+
authors.workspace = true
6+
edition.workspace = true
7+
license.workspace = true
8+
repository.workspace = true
9+
version.workspace = true
10+
11+
[features]
12+
default = []
13+
utoipa = ["shield-axum/utoipa"]
14+
15+
[dependencies]
16+
shield-axum.workspace = true

0 commit comments

Comments
 (0)