Skip to content

Commit

Permalink
Shield SeaORM: Add OIDC provider
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielleHuisman committed Dec 27, 2024
1 parent 01684fc commit db73523
Show file tree
Hide file tree
Showing 18 changed files with 327 additions and 44 deletions.
11 changes: 11 additions & 0 deletions Cargo.lock

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

8 changes: 3 additions & 5 deletions examples/sea-orm/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
use sea_orm::{Database, DbErr};
use sea_orm_migration::migrator::MigratorTrait;
use shield_sea_orm::migrations::Migrator;
use shield_sea_orm::{migrations::Migrator, SeaOrmStorage};

pub async fn run() -> Result<(), DbErr> {
pub async fn initialize() -> Result<SeaOrmStorage, DbErr> {
let database = Database::connect("sqlite::memory:").await?;

Migrator::up(&database, None).await?;

database.close().await?;

Ok(())
Ok(SeaOrmStorage::new(database))
}
1 change: 1 addition & 0 deletions packages/core/shield/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ version.workspace = true

[dependencies]
async-trait.workspace = true
chrono.workspace = true
futures.workspace = true
serde.workspace = true
serde_json.workspace = true
Expand Down
2 changes: 2 additions & 0 deletions packages/core/shield/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ pub enum StorageError {
#[error(transparent)]
Configuration(#[from] ConfigurationError),
#[error("{0}")]
Validation(String),
#[error("{0}")]
Engine(String),
}

Expand Down
14 changes: 14 additions & 0 deletions packages/core/shield/src/user.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
use chrono::{DateTime, Utc};

#[derive(Clone, Debug)]
pub struct EmailAddress {
pub id: String,
pub email: String,
pub is_primary: bool,
pub is_verified: bool,
pub verification_token: Option<String>,
pub verification_token_expired_at: Option<DateTime<Utc>>,
pub verified_at: Option<DateTime<Utc>>,
pub user_id: String,
}

pub trait User: Send + Sync {
fn id(&self) -> String;
}
Expand Down
1 change: 0 additions & 1 deletion packages/integrations/shield-leptos/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
pub mod context;
mod integration;
pub mod routes;
mod user;

pub use integration::*;
12 changes: 0 additions & 12 deletions packages/integrations/shield-leptos/src/user.rs

This file was deleted.

8 changes: 4 additions & 4 deletions packages/providers/shield-oidc/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ pub struct UpdateOidcConnection {
pub id: String,
pub token_type: Option<String>,
pub access_token: Option<String>,
pub refresh_token: Option<String>,
pub id_token: Option<String>,
pub expired_at: Option<DateTime<Utc>>,
pub scopes: Option<Vec<String>>,
pub refresh_token: Option<Option<String>>,
pub id_token: Option<Option<String>>,
pub expired_at: Option<Option<DateTime<Utc>>>,
pub scopes: Option<Option<Vec<String>>>,
}
2 changes: 2 additions & 0 deletions packages/providers/shield-oidc/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,6 @@ pub trait OidcStorage<U: User>: Storage<U> + Sync {
&self,
connection: UpdateOidcConnection,
) -> Result<OidcConnection, StorageError>;

async fn delete_oidc_connection(&self, connection_id: &str) -> Result<(), StorageError>;
}
12 changes: 9 additions & 3 deletions packages/storage/shield-memory/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,23 @@ version.workspace = true
[dependencies]
async-trait.workspace = true
shield = { path = "../../core/shield" }
shield-credentials = { path = "../../providers/shield-credentials", optional = true }
shield-email = { path = "../../providers/shield-email", optional = true }
shield-oauth = { path = "../../providers/shield-oauth", optional = true }
shield-oidc = { path = "../../providers/shield-oidc", optional = true }
shield-webauthn = { path = "../../providers/shield-webauthn", optional = true }

[features]
default = []
all-providers = [
"provider-credentials",
"provider-email",
"provider-oauth",
"provider-oidc",
"provider-webauthn",
]
provider-email = []
provider-oauth = []
provider-credentials = ["dep:shield-credentials"]
provider-email = ["dep:shield-email"]
provider-oauth = ["dep:shield-oauth"]
provider-oidc = ["dep:shield-oidc"]
provider-webauthn = []
provider-webauthn = ["dep:shield-webauthn"]
4 changes: 4 additions & 0 deletions packages/storage/shield-memory/src/providers/oidc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,8 @@ impl OidcStorage<User> for MemoryStorage {
) -> Result<OidcConnection, StorageError> {
todo!("update_oidc_connection")
}

async fn delete_oidc_connection(&self, _connection_id: &str) -> Result<(), StorageError> {
todo!("delete_oidc_connection")
}
}
26 changes: 17 additions & 9 deletions packages/storage/shield-memory/src/storage.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
use std::{
collections::HashMap,
sync::{Arc, Mutex},
};
use std::sync::{Arc, Mutex};

use async_trait::async_trait;
use shield::{Storage, StorageError};
use shield::{Storage, StorageError, User as _};

use crate::user::User;

pub const MEMORY_STORAGE_ID: &str = "memory";

#[derive(Clone, Debug, Default)]
pub struct MemoryStorage {
pub(crate) users: Arc<Mutex<HashMap<String, User>>>,
pub(crate) users: Arc<Mutex<Vec<User>>>,
#[cfg(feature = "provider-oidc")]
pub(crate) oidc: crate::providers::oidc::OidcMemoryStorage,
}
Expand All @@ -34,11 +31,22 @@ impl Storage<User> for MemoryStorage {
.users
.lock()
.map_err(|err| StorageError::Engine(err.to_string()))?
.get(user_id)
.iter()
.find(|user| user.id() == user_id)
.cloned())
}

async fn user_by_email(&self, _email: &str) -> Result<Option<User>, StorageError> {
todo!("user_by_email")
async fn user_by_email(&self, email: &str) -> Result<Option<User>, StorageError> {
Ok(self
.users
.lock()
.map_err(|err| StorageError::Engine(err.to_string()))?
.iter()
.find(|user| {
user.email_addresses
.iter()
.any(|email_address| email_address.email == email)
})
.cloned())
}
}
3 changes: 3 additions & 0 deletions packages/storage/shield-memory/src/user.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use shield::EmailAddress;

#[derive(Clone, Debug)]
pub struct User {
id: String,
pub(crate) email_addresses: Vec<EmailAddress>,
}

impl shield::User for User {
Expand Down
16 changes: 12 additions & 4 deletions packages/storage/shield-sea-orm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,26 @@ async-trait.workspace = true
sea-orm.workspace = true
sea-orm-migration.workspace = true
serde = { workspace = true, features = ["derive"] }
serde_json.workspace = true
shield = { path = "../../core/shield" }
shield-credentials = { path = "../../providers/shield-credentials", optional = true }
shield-email = { path = "../../providers/shield-email", optional = true }
shield-oauth = { path = "../../providers/shield-oauth", optional = true }
shield-oidc = { path = "../../providers/shield-oidc", optional = true }
shield-webauthn = { path = "../../providers/shield-webauthn", optional = true }

[features]
default = []
entity = []
all-providers = [
"provider-credentials",
"provider-email",
"provider-oauth",
"provider-oidc",
"provider-webauthn",
]
provider-email = []
provider-oauth = []
provider-oidc = []
provider-webauthn = []
provider-credentials = ["dep:shield-credentials"]
provider-email = ["dep:shield-email"]
provider-oauth = ["dep:shield-oauth"]
provider-oidc = ["dep:shield-oidc"]
provider-webauthn = ["dep:shield-webauthn"]
1 change: 1 addition & 0 deletions packages/storage/shield-sea-orm/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pub mod base;
pub mod entities;
pub mod migrations;
mod providers;
mod storage;

pub use storage::*;
2 changes: 2 additions & 0 deletions packages/storage/shield-sea-orm/src/providers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#[cfg(feature = "provider-oidc")]
pub mod oidc;
Loading

0 comments on commit db73523

Please sign in to comment.