From 5afb87bee006db92155b38cf152008f6ec54f0fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABlle=20Huisman?= Date: Sat, 18 Jan 2025 14:42:31 +0100 Subject: [PATCH] feat(shield): add icon URL to subprovider --- packages/core/shield/src/provider.rs | 3 ++ packages/core/shield/src/shield.rs | 1 + .../providers/shield-oauth/src/subprovider.rs | 5 +++ .../shield-oidc/src/builders/google.rs | 5 ++- .../shield-oidc/src/builders/keycloak.rs | 5 ++- .../providers/shield-oidc/src/subprovider.rs | 5 +++ .../src/entities/oauth_provider.rs | 2 + .../src/entities/oidc_provider.rs | 2 + .../src/migrations/providers/oauth.rs | 8 ++-- .../oauth/m20250118_133257_add_icon_url.rs | 37 +++++++++++++++++++ .../src/migrations/providers/oidc.rs | 8 ++-- .../oidc/m20250118_133731_add_icon_url.rs | 37 +++++++++++++++++++ .../shield-sea-orm/src/providers/oidc.rs | 1 + 13 files changed, 109 insertions(+), 10 deletions(-) create mode 100644 packages/storage/shield-sea-orm/src/migrations/providers/oauth/m20250118_133257_add_icon_url.rs create mode 100644 packages/storage/shield-sea-orm/src/migrations/providers/oidc/m20250118_133731_add_icon_url.rs diff --git a/packages/core/shield/src/provider.rs b/packages/core/shield/src/provider.rs index 57ae54e..06f24b0 100644 --- a/packages/core/shield/src/provider.rs +++ b/packages/core/shield/src/provider.rs @@ -46,6 +46,8 @@ pub trait Subprovider: Send + Sync { fn name(&self) -> String; + fn icon_url(&self) -> Option; + fn form(&self) -> Option
; } @@ -57,6 +59,7 @@ pub struct SubproviderVisualisation { pub provider_id: String, pub subprovider_id: Option, pub name: String, + pub icon_url: Option, } #[cfg(test)] diff --git a/packages/core/shield/src/shield.rs b/packages/core/shield/src/shield.rs index db15b57..2409a38 100644 --- a/packages/core/shield/src/shield.rs +++ b/packages/core/shield/src/shield.rs @@ -71,6 +71,7 @@ impl Shield { provider_id, subprovider_id, name: subprovider.name(), + icon_url: subprovider.icon_url(), } }) .collect() diff --git a/packages/providers/shield-oauth/src/subprovider.rs b/packages/providers/shield-oauth/src/subprovider.rs index 9625c08..610f69e 100644 --- a/packages/providers/shield-oauth/src/subprovider.rs +++ b/packages/providers/shield-oauth/src/subprovider.rs @@ -36,6 +36,7 @@ pub struct OauthSubprovider { pub(crate) revocation_url: Option, pub(crate) revocation_url_params: Option, pub(crate) pkce_code_challenge: OauthProviderPkceCodeChallenge, + pub(crate) icon_url: Option, } impl Subprovider for OauthSubprovider { @@ -51,6 +52,10 @@ impl Subprovider for OauthSubprovider { self.name.clone() } + fn icon_url(&self) -> Option { + self.icon_url.clone() + } + fn form(&self) -> Option { None } diff --git a/packages/providers/shield-oidc/src/builders/google.rs b/packages/providers/shield-oidc/src/builders/google.rs index 9e164d7..01f6543 100644 --- a/packages/providers/shield-oidc/src/builders/google.rs +++ b/packages/providers/shield-oidc/src/builders/google.rs @@ -1,5 +1,5 @@ use crate::subprovider::{ - oidc_subprovider_builder::{SetClientId, SetDiscoveryUrl, SetId, SetName}, + oidc_subprovider_builder::{SetClientId, SetDiscoveryUrl, SetIconUrl, SetId, SetName}, OidcSubprovider, OidcSubproviderBuilder, }; @@ -9,10 +9,11 @@ impl Google { pub fn builder( id: &str, client_id: &str, - ) -> OidcSubproviderBuilder>>> { + ) -> OidcSubproviderBuilder>>>> { OidcSubprovider::builder() .id(id) .name("Google") + .icon_url("https://authjs.dev/img/providers/google.svg") .client_id(client_id) .discovery_url("https://accounts.google.com") } diff --git a/packages/providers/shield-oidc/src/builders/keycloak.rs b/packages/providers/shield-oidc/src/builders/keycloak.rs index 60f4ea1..e50523e 100644 --- a/packages/providers/shield-oidc/src/builders/keycloak.rs +++ b/packages/providers/shield-oidc/src/builders/keycloak.rs @@ -1,5 +1,5 @@ use crate::subprovider::{ - oidc_subprovider_builder::{SetClientId, SetDiscoveryUrl, SetId, SetName}, + oidc_subprovider_builder::{SetClientId, SetDiscoveryUrl, SetIconUrl, SetId, SetName}, OidcSubprovider, OidcSubproviderBuilder, }; @@ -10,10 +10,11 @@ impl Keycloak { id: &str, discovery_url: &str, client_id: &str, - ) -> OidcSubproviderBuilder>>> { + ) -> OidcSubproviderBuilder>>>> { OidcSubprovider::builder() .id(id) .name("Keycloak") + .icon_url("https://authjs.dev/img/providers/keycloak.svg") .client_id(client_id) .discovery_url(discovery_url) } diff --git a/packages/providers/shield-oidc/src/subprovider.rs b/packages/providers/shield-oidc/src/subprovider.rs index d8076af..2fdf059 100644 --- a/packages/providers/shield-oidc/src/subprovider.rs +++ b/packages/providers/shield-oidc/src/subprovider.rs @@ -30,6 +30,7 @@ pub struct OidcSubprovider { pub id: String, pub name: String, pub slug: Option, + pub icon_url: Option, #[builder(default = OidcProviderVisibility::Public)] pub visibility: OidcProviderVisibility, pub client_id: String, @@ -140,6 +141,10 @@ impl Subprovider for OidcSubprovider { self.name.clone() } + fn icon_url(&self) -> Option { + self.icon_url.clone() + } + fn form(&self) -> Option { None } diff --git a/packages/storage/shield-sea-orm/src/entities/oauth_provider.rs b/packages/storage/shield-sea-orm/src/entities/oauth_provider.rs index 4991ae6..fc2e811 100644 --- a/packages/storage/shield-sea-orm/src/entities/oauth_provider.rs +++ b/packages/storage/shield-sea-orm/src/entities/oauth_provider.rs @@ -78,6 +78,8 @@ pub struct Model { #[sea_orm(column_type = "Text", nullable)] pub revocation_url_params: Option, pub pkce_code_challenge: OauthProviderPkceCodeChallenge, + #[sea_orm(column_type = "Text", nullable)] + pub icon_url: Option, } #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] diff --git a/packages/storage/shield-sea-orm/src/entities/oidc_provider.rs b/packages/storage/shield-sea-orm/src/entities/oidc_provider.rs index d1452c0..0bf6092 100644 --- a/packages/storage/shield-sea-orm/src/entities/oidc_provider.rs +++ b/packages/storage/shield-sea-orm/src/entities/oidc_provider.rs @@ -84,6 +84,8 @@ pub struct Model { #[sea_orm(column_type = "JsonBinary", nullable)] pub json_web_key_set: Option, pub pkce_code_challenge: OidcProviderPkceCodeChallenge, + #[sea_orm(column_type = "Text", nullable)] + pub icon_url: Option, } #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] diff --git a/packages/storage/shield-sea-orm/src/migrations/providers/oauth.rs b/packages/storage/shield-sea-orm/src/migrations/providers/oauth.rs index aa93eeb..2215e61 100644 --- a/packages/storage/shield-sea-orm/src/migrations/providers/oauth.rs +++ b/packages/storage/shield-sea-orm/src/migrations/providers/oauth.rs @@ -1,4 +1,5 @@ mod m20241211_095111_create_provider_oauth; +mod m20250118_133257_add_icon_url; use async_trait::async_trait; use sea_orm_migration::{MigrationTrait, MigratorTrait}; @@ -8,8 +9,9 @@ pub struct ProviderOauthMigrator; #[async_trait] impl MigratorTrait for ProviderOauthMigrator { fn migrations() -> Vec> { - vec![Box::new( - self::m20241211_095111_create_provider_oauth::Migration, - )] + vec![ + Box::new(self::m20241211_095111_create_provider_oauth::Migration), + Box::new(self::m20250118_133257_add_icon_url::Migration), + ] } } diff --git a/packages/storage/shield-sea-orm/src/migrations/providers/oauth/m20250118_133257_add_icon_url.rs b/packages/storage/shield-sea-orm/src/migrations/providers/oauth/m20250118_133257_add_icon_url.rs new file mode 100644 index 0000000..af6e765 --- /dev/null +++ b/packages/storage/shield-sea-orm/src/migrations/providers/oauth/m20250118_133257_add_icon_url.rs @@ -0,0 +1,37 @@ +use async_trait::async_trait; +use sea_orm_migration::prelude::*; + +#[derive(DeriveMigrationName)] +pub struct Migration; + +#[async_trait] +impl MigrationTrait for Migration { + async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager + .alter_table( + Table::alter() + .table(OauthProvider::Table) + .add_column(ColumnDef::new(OauthProvider::IconUrl).text()) + .to_owned(), + ) + .await + } + + async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager + .alter_table( + Table::alter() + .table(OauthProvider::Table) + .drop_column(OauthProvider::IconUrl) + .to_owned(), + ) + .await + } +} + +#[derive(DeriveIden)] +enum OauthProvider { + Table, + + IconUrl, +} diff --git a/packages/storage/shield-sea-orm/src/migrations/providers/oidc.rs b/packages/storage/shield-sea-orm/src/migrations/providers/oidc.rs index d373c52..ea78b68 100644 --- a/packages/storage/shield-sea-orm/src/migrations/providers/oidc.rs +++ b/packages/storage/shield-sea-orm/src/migrations/providers/oidc.rs @@ -1,4 +1,5 @@ mod m20241211_184751_create_provider_oidc; +mod m20250118_133731_add_icon_url; use async_trait::async_trait; use sea_orm_migration::{MigrationTrait, MigratorTrait}; @@ -8,8 +9,9 @@ pub struct ProviderOidcMigrator; #[async_trait] impl MigratorTrait for ProviderOidcMigrator { fn migrations() -> Vec> { - vec![Box::new( - self::m20241211_184751_create_provider_oidc::Migration, - )] + vec![ + Box::new(self::m20241211_184751_create_provider_oidc::Migration), + Box::new(self::m20250118_133731_add_icon_url::Migration), + ] } } diff --git a/packages/storage/shield-sea-orm/src/migrations/providers/oidc/m20250118_133731_add_icon_url.rs b/packages/storage/shield-sea-orm/src/migrations/providers/oidc/m20250118_133731_add_icon_url.rs new file mode 100644 index 0000000..d785ec5 --- /dev/null +++ b/packages/storage/shield-sea-orm/src/migrations/providers/oidc/m20250118_133731_add_icon_url.rs @@ -0,0 +1,37 @@ +use async_trait::async_trait; +use sea_orm_migration::prelude::*; + +#[derive(DeriveMigrationName)] +pub struct Migration; + +#[async_trait] +impl MigrationTrait for Migration { + async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager + .alter_table( + Table::alter() + .table(OidcProvider::Table) + .add_column(ColumnDef::new(OidcProvider::IconUrl).text()) + .to_owned(), + ) + .await + } + + async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager + .alter_table( + Table::alter() + .table(OidcProvider::Table) + .drop_column(OidcProvider::IconUrl) + .to_owned(), + ) + .await + } +} + +#[derive(DeriveIden)] +enum OidcProvider { + Table, + + IconUrl, +} diff --git a/packages/storage/shield-sea-orm/src/providers/oidc.rs b/packages/storage/shield-sea-orm/src/providers/oidc.rs index aba331e..5f2a2bf 100644 --- a/packages/storage/shield-sea-orm/src/providers/oidc.rs +++ b/packages/storage/shield-sea-orm/src/providers/oidc.rs @@ -179,6 +179,7 @@ impl TryFrom for OidcSubprovider { id: value.id.to_string(), name: value.name, slug: value.slug, + icon_url: value.icon_url, visibility: value.visibility.into(), client_id: value.client_id, client_secret: value.client_secret,