Skip to content

Commit

Permalink
Shield SeaORM: Add OAuth and OIDC providers
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielleHuisman committed Dec 11, 2024
1 parent cb08457 commit ee186ca
Show file tree
Hide file tree
Showing 16 changed files with 969 additions and 20 deletions.
2 changes: 1 addition & 1 deletion packages/storage/shield-seaorm/development.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Development

```
sea-orm-cli migrate generate -d packages/storage/shield-seaorm/src/migrations <name>
sea-orm-cli migrate generate -d packages/storage/shield-seaorm/src/migrations/<directory> <name>
sea-orm-cli migrate fresh -u "sqlite:///tmp/shield-seaorm.sqlite?mode=rwc" -d ./examples/seaorm
Expand Down
12 changes: 12 additions & 0 deletions packages/storage/shield-seaorm/src/entities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,15 @@ pub mod entity;

#[cfg(feature = "provider-email")]
pub mod email_auth_token;

#[cfg(feature = "provider-oauth")]
pub mod oauth_provider;
#[cfg(feature = "provider-oauth")]
pub mod oauth_provider_connection;

#[cfg(feature = "provider-oidc")]
pub mod oidc_provider;
#[cfg(feature = "provider-oidc")]
pub mod oidc_provider_connection;

// TODO: Use features to ensure all databases are supported (e.g. for enums).
59 changes: 59 additions & 0 deletions packages/storage/shield-seaorm/src/entities/oauth_provider.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.2
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)]
#[sea_orm(table_name = "oauth_provider")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
pub id: Uuid,
pub created_at: DateTimeUtc,
pub updated_at: DateTimeUtc,
pub name: String,
pub slug: Option<String>,
#[sea_orm(column_type = "custom(\"enum_text\")")]
pub r#type: String,
#[sea_orm(column_type = "custom(\"enum_text\")")]
pub visibility: String,
#[sea_orm(column_type = "Text")]
pub client_id: String,
#[sea_orm(column_type = "Text", nullable)]
pub client_secret: Option<String>,
#[sea_orm(column_type = "Text", nullable)]
pub scopes: Option<String>,
#[sea_orm(column_type = "Text", nullable)]
pub redirect_url: Option<String>,
#[sea_orm(column_type = "Text", nullable)]
pub authorization_url: Option<String>,
#[sea_orm(column_type = "Text", nullable)]
pub authorization_url_params: Option<String>,
#[sea_orm(column_type = "Text", nullable)]
pub token_url: Option<String>,
#[sea_orm(column_type = "Text", nullable)]
pub token_url_params: Option<String>,
#[sea_orm(column_type = "Text", nullable)]
pub introspection_url: Option<String>,
#[sea_orm(column_type = "Text", nullable)]
pub introspection_url_params: Option<String>,
#[sea_orm(column_type = "Text", nullable)]
pub revocation_url: Option<String>,
#[sea_orm(column_type = "Text", nullable)]
pub revocation_url_params: Option<String>,
#[sea_orm(column_type = "custom(\"enum_text\")")]
pub pcke_code_challenge: String,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(has_many = "super::oauth_provider_connection::Entity")]
OauthProviderConnection,
}

impl Related<super::oauth_provider_connection::Entity> for Entity {
fn to() -> RelationDef {
Relation::OauthProviderConnection.def()
}
}

impl ActiveModelBehavior for ActiveModel {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.2
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)]
#[sea_orm(table_name = "oauth_provider_connection")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
pub id: Uuid,
pub created_at: DateTimeUtc,
pub updated_at: DateTimeUtc,
#[sea_orm(column_type = "Text")]
pub identifier: String,
#[sea_orm(column_type = "Text")]
pub token_type: String,
#[sea_orm(column_type = "Text")]
pub access_token: String,
#[sea_orm(column_type = "Text", nullable)]
pub refresh_token: Option<String>,
pub expired_at: Option<DateTimeUtc>,
#[sea_orm(column_type = "Text", nullable)]
pub scopes: Option<String>,
pub provider_id: Uuid,
pub user_id: Uuid,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(
belongs_to = "super::oauth_provider::Entity",
from = "Column::ProviderId",
to = "super::oauth_provider::Column::Id",
on_update = "Cascade",
on_delete = "Cascade"
)]
OauthProvider,
#[sea_orm(
belongs_to = "super::user::Entity",
from = "Column::UserId",
to = "super::user::Column::Id",
on_update = "Cascade",
on_delete = "Cascade"
)]
User,
}

impl Related<super::oauth_provider::Entity> for Entity {
fn to() -> RelationDef {
Relation::OauthProvider.def()
}
}

impl Related<super::user::Entity> for Entity {
fn to() -> RelationDef {
Relation::User.def()
}
}

impl ActiveModelBehavior for ActiveModel {}
67 changes: 67 additions & 0 deletions packages/storage/shield-seaorm/src/entities/oidc_provider.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.2
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)]
#[sea_orm(table_name = "oidc_provider")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
pub id: Uuid,
pub created_at: DateTimeUtc,
pub updated_at: DateTimeUtc,
pub name: String,
pub slug: Option<String>,
#[sea_orm(column_type = "custom(\"enum_text\")")]
pub r#type: String,
#[sea_orm(column_type = "custom(\"enum_text\")")]
pub visibility: String,
#[sea_orm(column_type = "Text")]
pub client_id: String,
#[sea_orm(column_type = "Text", nullable)]
pub client_secret: Option<String>,
#[sea_orm(column_type = "Text", nullable)]
pub scopes: Option<String>,
#[sea_orm(column_type = "Text", nullable)]
pub redirect_url: Option<String>,
#[sea_orm(column_type = "Text", nullable)]
pub issuer_url: Option<String>,
#[sea_orm(column_type = "Text", nullable)]
pub authorization_url: Option<String>,
#[sea_orm(column_type = "Text", nullable)]
pub authorization_url_params: Option<String>,
#[sea_orm(column_type = "Text", nullable)]
pub token_url: Option<String>,
#[sea_orm(column_type = "Text", nullable)]
pub token_url_params: Option<String>,
#[sea_orm(column_type = "Text", nullable)]
pub introspection_url: Option<String>,
#[sea_orm(column_type = "Text", nullable)]
pub introspection_url_params: Option<String>,
#[sea_orm(column_type = "Text", nullable)]
pub revocation_url: Option<String>,
#[sea_orm(column_type = "Text", nullable)]
pub revocation_url_params: Option<String>,
#[sea_orm(column_type = "Text", nullable)]
pub user_info_url: Option<String>,
#[sea_orm(column_type = "Text", nullable)]
pub json_web_key_set_url: Option<String>,
#[sea_orm(column_type = "JsonBinary", nullable)]
pub json_web_key_set: Option<Json>,
#[sea_orm(column_type = "custom(\"enum_text\")")]
pub pcke_code_challenge: String,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(has_many = "super::oidc_provider_connection::Entity")]
OidcProviderConnection,
}

impl Related<super::oidc_provider_connection::Entity> for Entity {
fn to() -> RelationDef {
Relation::OidcProviderConnection.def()
}
}

impl ActiveModelBehavior for ActiveModel {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.2
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)]
#[sea_orm(table_name = "oidc_provider_connection")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
pub id: Uuid,
pub created_at: DateTimeUtc,
pub updated_at: DateTimeUtc,
#[sea_orm(column_type = "Text")]
pub identifier: String,
#[sea_orm(column_type = "Text")]
pub token_type: String,
#[sea_orm(column_type = "Text")]
pub access_token: String,
#[sea_orm(column_type = "Text", nullable)]
pub refresh_token: Option<String>,
#[sea_orm(column_type = "Text", nullable)]
pub id_token: Option<String>,
pub expired_at: Option<DateTimeUtc>,
#[sea_orm(column_type = "Text", nullable)]
pub scopes: Option<String>,
pub provider_id: Uuid,
pub user_id: Uuid,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(
belongs_to = "super::oidc_provider::Entity",
from = "Column::ProviderId",
to = "super::oidc_provider::Column::Id",
on_update = "Cascade",
on_delete = "Cascade"
)]
OidcProvider,
#[sea_orm(
belongs_to = "super::user::Entity",
from = "Column::UserId",
to = "super::user::Column::Id",
on_update = "Cascade",
on_delete = "Cascade"
)]
User,
}

impl Related<super::oidc_provider::Entity> for Entity {
fn to() -> RelationDef {
Relation::OidcProvider.def()
}
}

impl Related<super::user::Entity> for Entity {
fn to() -> RelationDef {
Relation::User.def()
}
}

impl ActiveModelBehavior for ActiveModel {}
10 changes: 10 additions & 0 deletions packages/storage/shield-seaorm/src/entities/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,13 @@ pub use super::entity::Entity;

#[cfg(feature = "provider-email")]
pub use super::email_auth_token::Entity as EmailAuthToken;

#[cfg(feature = "provider-oauth")]
pub use super::oauth_provider::Entity as OauthProvider;
#[cfg(feature = "provider-oauth")]
pub use super::oauth_provider_connection::Entity as OauthProviderConnection;

#[cfg(feature = "provider-oidc")]
pub use super::oidc_provider::Entity as OidcProvider;
#[cfg(feature = "provider-oidc")]
pub use super::oidc_provider_connection::Entity as OidcProviderConnection;
20 changes: 20 additions & 0 deletions packages/storage/shield-seaorm/src/entities/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ pub enum Relation {
#[cfg(not(feature = "entity"))]
#[sea_orm(has_many = "super::email_address::Entity")]
EmailAddress,
#[cfg(feature = "provider-oauth")]
#[sea_orm(has_many = "super::oauth_provider_connection::Entity")]
OauthProviderConnection,
#[cfg(feature = "provider-oidc")]
#[sea_orm(has_many = "super::oidc_provider_connection::Entity")]
OidcProviderConnection,
}

#[cfg(feature = "entity")]
Expand All @@ -47,4 +53,18 @@ impl Related<super::email_address::Entity> for Entity {
}
}

#[cfg(feature = "provider-oauth")]
impl Related<super::oauth_provider_connection::Entity> for Entity {
fn to() -> RelationDef {
Relation::OauthProviderConnection.def()
}
}

#[cfg(feature = "provider-oidc")]
impl Related<super::oidc_provider_connection::Entity> for Entity {
fn to() -> RelationDef {
Relation::OidcProviderConnection.def()
}
}

impl ActiveModelBehavior for ActiveModel {}
20 changes: 7 additions & 13 deletions packages/storage/shield-seaorm/src/migrations.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
use core::CoreMigrator;
pub mod core;
pub mod providers;

use async_trait::async_trait;
use sea_orm_migration::{MigrationTrait, MigratorTrait};

mod core;
mod providers;
use self::{core::CoreMigrator, providers::ProvidersMigrator};

pub struct Migrator;

#[async_trait]
impl MigratorTrait for Migrator {
fn migrations() -> Vec<Box<dyn MigrationTrait>> {
#[allow(unused_mut)]
let mut migrations = CoreMigrator::migrations();

#[cfg(feature = "provider-email")]
{
use providers::email::ProviderEmailMigrator;
migrations.extend(ProviderEmailMigrator::migrations());
}

migrations
CoreMigrator::migrations()
.into_iter()
.chain(ProvidersMigrator::migrations())
.collect()
}
}
6 changes: 3 additions & 3 deletions packages/storage/shield-seaorm/src/migrations/core.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
mod m20241210_203135_create_user;

use async_trait::async_trait;
use sea_orm_migration::{MigrationTrait, MigratorTrait};

mod m20241210_203135_create_user;

pub struct CoreMigrator;

#[async_trait]
impl MigratorTrait for CoreMigrator {
fn migrations() -> Vec<Box<dyn MigrationTrait>> {
vec![Box::new(m20241210_203135_create_user::Migration)]
vec![Box::new(self::m20241210_203135_create_user::Migration)]
}
}
Loading

0 comments on commit ee186ca

Please sign in to comment.