From 3ef362f7f249326c0379348de36e089eae5078d5 Mon Sep 17 00:00:00 2001 From: Galip Tolga Erdem Date: Fri, 21 Oct 2022 23:08:15 -0400 Subject: [PATCH] Added angular client --- apps/angular/src/environments/environment.ts | 5 +- .../KeycloakDataSeeder.cs | 66 ++++++++++++++----- 2 files changed, 54 insertions(+), 17 deletions(-) diff --git a/apps/angular/src/environments/environment.ts b/apps/angular/src/environments/environment.ts index 9c3c768d..243cea14 100644 --- a/apps/angular/src/environments/environment.ts +++ b/apps/angular/src/environments/environment.ts @@ -9,11 +9,12 @@ export const environment = { name: 'EShopOnAbp', }, oAuthConfig: { - issuer: 'https://localhost:44330', + issuer: 'http://localhost:8080/realms/master', redirectUri: baseUrl, clientId: 'Web', responseType: 'code', - scope: 'offline_access openid profile email phone AccountService IdentityService AdministrationService CatalogService OrderingService', + scope: 'offline_access openid profile email phone', + // scope: 'offline_access openid profile email phone AccountService IdentityService AdministrationService CatalogService OrderingService', //TODO: Update when https://github.com/AnderssonPeter/Keycloak.Net/pull/5 is merged //requireHttps: true, }, apis: { diff --git a/shared/EShopOnAbp.Keycloak.DbMigrator/KeycloakDataSeeder.cs b/shared/EShopOnAbp.Keycloak.DbMigrator/KeycloakDataSeeder.cs index 223178e8..fbc82386 100644 --- a/shared/EShopOnAbp.Keycloak.DbMigrator/KeycloakDataSeeder.cs +++ b/shared/EShopOnAbp.Keycloak.DbMigrator/KeycloakDataSeeder.cs @@ -100,7 +100,48 @@ public class KeyCloakDataSeeder : IDataSeedContributor, ITransientDependency private async Task CreateClientsAsync() { await CreatePublicWebClientAsync(); - await CreateSwaggerClientAsync(); + await CreateSwaggerClientAsync(); // TODO: Test when Volo.Abp.Swashbuckle v6.0.1 is released (https://github.com/abpframework/abp/pull/14409) + await CreateWebClientAsync(); + } + + private async Task CreateWebClientAsync() + { + var webClient = (await _keycloakClient.GetClientsAsync(_keycloakOptions.RealmName, clientId: "Web")) + .FirstOrDefault(); + + if (webClient == null) + { + var webRootUrl = _configuration[$"Clients:Web:RootUrl"]; + webClient = new Client + { + ClientId = "Web", + Name = "Angular Back-Office Web Application", + Protocol = "openid-connect", + Enabled = true, + BaseUrl = webRootUrl, + RedirectUris = new List + { + $"{webRootUrl.TrimEnd('/')}" + }, + FrontChannelLogout = true, + PublicClient = true + }; + webClient.Attributes = new Dictionary + { + { "post.logout.redirect.uris", $"{webRootUrl.TrimEnd('/')}" } + }; + + await _keycloakClient.CreateClientAsync(_keycloakOptions.RealmName, webClient); + //TODO: Update when //https://github.com/AnderssonPeter/Keycloak.Net/pull/5 is merged + // await AddOptionalClientScopesAsync( + // "PublicWeb", + // new List + // { + // "AccountService", "AdministrationService", "IdentityService", "BasketService", "CatalogService", + // "OrderingService", "PaymentService", "CmskitService" + // } + // ); + } } private async Task CreateSwaggerClientAsync() @@ -127,7 +168,6 @@ public class KeyCloakDataSeeder : IDataSeedContributor, ITransientDependency Name = "Swagger Client Application", Protocol = "openid-connect", Enabled = true, - // BaseUrl = "https://localhost:44335/", RedirectUris = new List { $"{webGatewaySwaggerRootUrl}/swagger/oauth2-redirect.html", // WebGateway redirect uri @@ -144,10 +184,6 @@ public class KeyCloakDataSeeder : IDataSeedContributor, ITransientDependency FrontChannelLogout = true, PublicClient = true }; - swaggerClient.Attributes = new Dictionary - { - { "post.logout.redirect.uris", "https://localhost:44335/signout-callback-oidc" } - }; await _keycloakClient.CreateClientAsync(_keycloakOptions.RealmName, swaggerClient); } @@ -181,16 +217,16 @@ public class KeyCloakDataSeeder : IDataSeedContributor, ITransientDependency }; await _keycloakClient.CreateClientAsync(_keycloakOptions.RealmName, publicWebClient); + //TODO: Update when //https://github.com/AnderssonPeter/Keycloak.Net/pull/5 is merged + // await AddOptionalClientScopesAsync( + // "PublicWeb", + // new List + // { + // "AccountService", "AdministrationService", "IdentityService", "BasketService", "CatalogService", + // "OrderingService", "PaymentService", "CmskitService" + // } + // ); } - - await AddOptionalClientScopesAsync( - "PublicWeb", - new List - { - "AccountService", "AdministrationService", "IdentityService", "BasketService", "CatalogService", - "OrderingService", "PaymentService", "CmskitService" - } - ); } private async Task AddOptionalClientScopesAsync(string clientName, List scopes)