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
19 changes: 17 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions examples/axum/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Shield Axum Example

1. Start the server:

```shell
cargo run
```

2. Go to http://localhost:8080.
2 changes: 1 addition & 1 deletion examples/axum/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ async fn main() {
.init();

// Configuration
let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 3000);
let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080);

// Initialize sessions
let session_store = MemoryStore::default();
Expand Down
2 changes: 0 additions & 2 deletions examples/dioxus-axum/.env.example

This file was deleted.

2 changes: 0 additions & 2 deletions examples/dioxus-axum/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ server = [
"dep:shield-dioxus-axum",
"dep:shield-memory",
"dep:shield-oidc",
"dep:shield-workos",
"dep:tokio",
"dep:tower-sessions",
"dioxus/server",
Expand All @@ -36,7 +35,6 @@ shield-dioxus.workspace = true
shield-dioxus-axum = { workspace = true, optional = true }
shield-memory = { workspace = true, optional = true }
shield-oidc = { workspace = true, features = ["native-tls"], optional = true }
shield-workos = { workspace = true, optional = true }
tokio = { workspace = true, features = ["rt-multi-thread"], optional = true }
tower-sessions = { workspace = true, optional = true }
tracing.workspace = true
15 changes: 15 additions & 0 deletions examples/dioxus-axum/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Shield Dioxus Axum Example

1. Install binaries:

```shell
cargo install --locked dioxus-cli
```

2. Start the development server:

```shell
dx serve --platform web
```

3. Go to http://localhost:8080.
49 changes: 15 additions & 34 deletions examples/dioxus-axum/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ async fn main() {
use shield_dioxus_axum::{AxumDioxusIntegration, ShieldLayer};
use shield_memory::{MemoryStorage, User};
use shield_oidc::{Keycloak, OidcMethod};
use shield_workos::{WorkosMethod, WorkosOauthProvider, WorkosOptions};
use tokio::net::TcpListener;
use tower_sessions::{Expiry, MemoryStore, SessionManagerLayer, cookie::time::Duration};
use tracing::{Level, info};
Expand All @@ -46,39 +45,21 @@ async fn main() {
let storage = MemoryStorage::new();
let shield = Shield::new(
storage.clone(),
[
Some(Arc::new(
OidcMethod::new(storage).with_providers([Keycloak::builder(
"keycloak",
"http://localhost:18080/realms/Shield",
"client1",
)
.client_secret("xcpQsaGbRILTljPtX4npjmYMBjKrariJ")
.redirect_url(format!(
"http://localhost:{}/api/auth/oidc/sign-in-callback/keycloak",
dioxus::cli_config::devserver_raw_addr()
.map(|addr| addr.port())
.unwrap_or_else(|| addr.port())
))
.build()]),
) as Arc<dyn ErasedMethod>),
env::var("WORKOS_API_KEY").ok().map(|api_key| {
Arc::new(
WorkosMethod::from_api_key(&api_key).with_options(
WorkosOptions::builder()
.oauth_providers(vec![
WorkosOauthProvider::AppleOAuth,
WorkosOauthProvider::GoogleOAuth,
WorkosOauthProvider::MicrosoftOAuth,
])
.build(),
),
) as Arc<dyn ErasedMethod>
}),
]
.into_iter()
.flatten()
.collect(),
vec![
OidcMethod::new(storage).with_providers([Keycloak::builder(
"keycloak",
"http://localhost:18080/realms/Shield",
"client1",
)
.client_secret("xcpQsaGbRILTljPtX4npjmYMBjKrariJ")
.redirect_url(format!(
"http://localhost:{}/api/auth/oidc/sign-in-callback/keycloak",
dioxus::cli_config::devserver_raw_addr()
.map(|addr| addr.port())
.unwrap_or_else(|| addr.port())
))
.build()]),
],
ShieldOptions::default(),
);
let shield_layer = ShieldLayer::new(shield.clone());
Expand Down
15 changes: 15 additions & 0 deletions examples/leptos-actix/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Shield Leptos Actix Example

1. Install binaries:

```shell
cargo install --locked cargo-leptos
```

2. Start the development server:

```shell
cargo letpos watch
```

3. Go to http://localhost:8080.
15 changes: 15 additions & 0 deletions examples/leptos-axum/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Shield Leptos Axum Example

1. Install binaries:

```shell
cargo install --locked cargo-leptos
```

2. Start the development server:

```shell
cargo letpos watch
```

3. Go to http://localhost:8080.
2 changes: 2 additions & 0 deletions examples/workos/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
WORKOS_API_KEY = change
WORKOS_CLIENT_ID = change
39 changes: 39 additions & 0 deletions examples/workos/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
[package]
name = "shield-examples-workos"
description = "Example with WorkOS."
publish = false

authors.workspace = true
edition.workspace = true
license.workspace = true
repository.workspace = true
version.workspace = true

[features]
# TODO: Enabling these triggers multiple RustSec advisories.
# desktop = ["dioxus/desktop"]
# mobile = ["dioxus/mobile"]
server = [
"dep:axum",
"dep:shield-dioxus-axum",
"dep:shield-memory",
"dep:shield-workos",
"dep:tokio",
"dep:tower-sessions",
"dioxus/server",
"shield-dioxus/server",
]
web = ["dioxus/web"]

[dependencies]
axum = { workspace = true, optional = true }
dioxus = { workspace = true, features = ["fullstack", "router"] }
shield.workspace = true
shield-bootstrap = { workspace = true, features = ["dioxus"] }
shield-dioxus.workspace = true
shield-dioxus-axum = { workspace = true, optional = true }
shield-memory = { workspace = true, optional = true }
shield-workos = { workspace = true, optional = true }
tokio = { workspace = true, features = ["rt-multi-thread"], optional = true }
tower-sessions = { workspace = true, optional = true }
tracing.workspace = true
11 changes: 11 additions & 0 deletions examples/workos/Dioxus.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[application]

[web.app]
title = "Shield WorkOS Example"

[web.resource]
style = []
script = []

[web.resource.dev]
script = []
24 changes: 24 additions & 0 deletions examples/workos/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Shield WorkOS Example

1. Install binaries:

```shell
cargo install --locked dioxus-cli
cargo install --locked dotenvy
```

2. Copy the default environment variables:

```shell
cp .env.example .env
```

3. Configure the environment variables in `.env`.

4. Start the development server:

```shell
dotenvy dx serve --platform web
```

5. Go to http://localhost:8080.
30 changes: 30 additions & 0 deletions examples/workos/src/app.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use dioxus::{document::Stylesheet, prelude::*};
use shield_dioxus::ShieldRouter;

use crate::home::Home;

#[derive(Clone, Debug, PartialEq, Routable)]
#[rustfmt::skip]
enum Route {
#[route("/")]
Home {},
#[child("/auth")]
Auth {
child: ShieldRouter
},
}

#[component]
pub fn App() -> Element {
rsx! {
Stylesheet {
href: "https://cdn.jsdelivr.net/npm/bootstrap@5.3.7/dist/css/bootstrap.min.css",
integrity: "sha384-LN+7fdVzj6u52u30Kp6M/trliBMCMKTyK833zpbD+pXdCLuTusPj697FH4R/5mcr",
crossorigin: "anonymous"
}

main {
Router::<Route> {}
}
}
}
8 changes: 8 additions & 0 deletions examples/workos/src/home.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use dioxus::prelude::*;

#[component]
pub fn Home() -> Element {
rsx! {
h1 { "Shield WorkOS Example" }
}
}
86 changes: 86 additions & 0 deletions examples/workos/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
mod app;
mod home;

use crate::app::App;

#[cfg(not(feature = "server"))]
fn main() {
use shield_bootstrap::BootstrapDioxusStyle;

dioxus::LaunchBuilder::new()
.with_context(BootstrapDioxusStyle::default().context())
.launch(App)
}

#[cfg(feature = "server")]
#[tokio::main]
async fn main() {
use std::{env, sync::Arc};

use axum::Router;
use dioxus::{
cli_config::fullstack_address_or_localhost,
prelude::{DioxusRouterExt, *},
};
use shield::{ErasedMethod, Method, Shield, ShieldOptions};
use shield_bootstrap::BootstrapDioxusStyle;
use shield_dioxus_axum::{AxumDioxusIntegration, ShieldLayer};
use shield_memory::{MemoryStorage, User};
use shield_workos::{WorkosMethod, WorkosOauthProvider, WorkosOptions};
use tokio::net::TcpListener;
use tower_sessions::{Expiry, MemoryStore, SessionManagerLayer, cookie::time::Duration};
use tracing::{Level, info};

// Initialize Dioxus
dioxus::logger::init(Level::DEBUG).unwrap();
let addr = fullstack_address_or_localhost();

// Initialize sessions
let session_store = MemoryStore::default();
let session_layer = SessionManagerLayer::new(session_store)
.with_secure(false)
.with_expiry(Expiry::OnInactivity(Duration::minutes(10)));

// Initialize Shield
let storage = MemoryStorage::new();
let shield = Shield::new(
storage.clone(),
vec![Arc::new(WorkosMethod::from_api_key(
&env::var("WORKOS_API_KEY").expect("Missing `WORKOS_API_KEY`."),
&env::var("WORKOS_CLIENT_ID").expect("Missing `WORKOS_CLIENT_ID`."),
WorkosOptions::builder()
.oauth_providers(vec![
WorkosOauthProvider::AppleOAuth,
WorkosOauthProvider::GoogleOAuth,
WorkosOauthProvider::MicrosoftOAuth,
])
.redirect_url(format!(
"http://localhost:{}/api/auth/workos/sign-in-callback",
addr.port()
))
.build(),
))],
ShieldOptions::default(),
);
let shield_layer = ShieldLayer::new(shield.clone());

// Initialize router
let router = Router::new()
.serve_dioxus_application(
ServeConfig::builder()
.context(AxumDioxusIntegration::<User>::default().context())
.context(BootstrapDioxusStyle::default().context())
.build()
.unwrap(),
App,
)
.layer(shield_layer)
.layer(session_layer);

// Start app
info!("listening on http://{}", &addr);
let listener = TcpListener::bind(&addr).await.unwrap();
axum::serve(listener, router.into_make_service())
.await
.unwrap();
}
Loading