From cdd87d367f5504a08297187d1b812a20d64334dd Mon Sep 17 00:00:00 2001 From: Miguel Veloso Date: Wed, 25 Apr 2018 12:58:55 +0100 Subject: [PATCH] Fix swagger-ui redirection uri to current one as of v.3.x; Fix stored values for existing deployments --- .../Identity.API/Configuration/Config.cs | 12 ++++---- .../Data/ConfigurationDbContextSeed.cs | 30 +++++++++++++++++-- 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/src/Services/Identity/Identity.API/Configuration/Config.cs b/src/Services/Identity/Identity.API/Configuration/Config.cs index 3bc705995..301cccba2 100644 --- a/src/Services/Identity/Identity.API/Configuration/Config.cs +++ b/src/Services/Identity/Identity.API/Configuration/Config.cs @@ -162,7 +162,7 @@ namespace Microsoft.eShopOnContainers.Services.Identity.API.Configuration AllowedGrantTypes = GrantTypes.Implicit, AllowAccessTokensViaBrowser = true, - RedirectUris = { $"{clientsUrl["LocationsApi"]}/swagger/o2c.html" }, + RedirectUris = { $"{clientsUrl["LocationsApi"]}/swagger/oauth2-redirect.html" }, PostLogoutRedirectUris = { $"{clientsUrl["LocationsApi"]}/swagger/" }, AllowedScopes = @@ -177,7 +177,7 @@ namespace Microsoft.eShopOnContainers.Services.Identity.API.Configuration AllowedGrantTypes = GrantTypes.Implicit, AllowAccessTokensViaBrowser = true, - RedirectUris = { $"{clientsUrl["MarketingApi"]}/swagger/o2c.html" }, + RedirectUris = { $"{clientsUrl["MarketingApi"]}/swagger/oauth2-redirect.html" }, PostLogoutRedirectUris = { $"{clientsUrl["MarketingApi"]}/swagger/" }, AllowedScopes = @@ -192,7 +192,7 @@ namespace Microsoft.eShopOnContainers.Services.Identity.API.Configuration AllowedGrantTypes = GrantTypes.Implicit, AllowAccessTokensViaBrowser = true, - RedirectUris = { $"{clientsUrl["BasketApi"]}/swagger/o2c.html" }, + RedirectUris = { $"{clientsUrl["BasketApi"]}/swagger/oauth2-redirect.html" }, PostLogoutRedirectUris = { $"{clientsUrl["BasketApi"]}/swagger/" }, AllowedScopes = @@ -207,7 +207,7 @@ namespace Microsoft.eShopOnContainers.Services.Identity.API.Configuration AllowedGrantTypes = GrantTypes.Implicit, AllowAccessTokensViaBrowser = true, - RedirectUris = { $"{clientsUrl["OrderingApi"]}/swagger/o2c.html" }, + RedirectUris = { $"{clientsUrl["OrderingApi"]}/swagger/oauth2-redirect.html" }, PostLogoutRedirectUris = { $"{clientsUrl["OrderingApi"]}/swagger/" }, AllowedScopes = @@ -222,7 +222,7 @@ namespace Microsoft.eShopOnContainers.Services.Identity.API.Configuration AllowedGrantTypes = GrantTypes.Implicit, AllowAccessTokensViaBrowser = true, - RedirectUris = { $"{clientsUrl["MobileShoppingAgg"]}/swagger/o2c.html" }, + RedirectUris = { $"{clientsUrl["MobileShoppingAgg"]}/swagger/oauth2-redirect.html" }, PostLogoutRedirectUris = { $"{clientsUrl["MobileShoppingAgg"]}/swagger/" }, AllowedScopes = @@ -237,7 +237,7 @@ namespace Microsoft.eShopOnContainers.Services.Identity.API.Configuration AllowedGrantTypes = GrantTypes.Implicit, AllowAccessTokensViaBrowser = true, - RedirectUris = { $"{clientsUrl["WebShoppingAgg"]}/swagger/o2c.html" }, + RedirectUris = { $"{clientsUrl["WebShoppingAgg"]}/swagger/oauth2-redirect.html" }, PostLogoutRedirectUris = { $"{clientsUrl["WebShoppingAgg"]}/swagger/" }, AllowedScopes = diff --git a/src/Services/Identity/Identity.API/Data/ConfigurationDbContextSeed.cs b/src/Services/Identity/Identity.API/Data/ConfigurationDbContextSeed.cs index a5a4383bd..3547c053a 100644 --- a/src/Services/Identity/Identity.API/Data/ConfigurationDbContextSeed.cs +++ b/src/Services/Identity/Identity.API/Data/ConfigurationDbContextSeed.cs @@ -1,7 +1,10 @@ using IdentityServer4.EntityFramework.DbContexts; +using IdentityServer4.EntityFramework.Entities; using IdentityServer4.EntityFramework.Mappers; +using Microsoft.EntityFrameworkCore; using Microsoft.eShopOnContainers.Services.Identity.API.Configuration; using Microsoft.Extensions.Configuration; +using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; @@ -30,16 +33,37 @@ namespace Microsoft.eShopOnContainers.Services.Identity.API.Data { foreach (var client in Config.GetClients(clientUrls)) { - await context.Clients.AddAsync(client.ToEntity()); + context.Clients.Add(client.ToEntity()); } await context.SaveChangesAsync(); } + // Checking always for old redirects to fix existing deployments + // to use new swagger-ui redirect uri as of v3.0.0 + // There should be no problem for new ones + // ref: https://github.com/dotnet-architecture/eShopOnContainers/issues/586 + else + { + List oldRedirects = (await context.Clients.Include(c => c.RedirectUris).ToListAsync()) + .SelectMany(c => c.RedirectUris) + .Where(ru => ru.RedirectUri.EndsWith("/o2c.html")) + .ToList(); + + if (oldRedirects.Any()) + { + foreach (var ru in oldRedirects) + { + ru.RedirectUri = ru.RedirectUri.Replace("/o2c.html", "/oauth2-redirect.html"); + context.Update(ru.Client); + } + await context.SaveChangesAsync(); + } + } if (!context.IdentityResources.Any()) { foreach (var resource in Config.GetResources()) { - await context.IdentityResources.AddAsync(resource.ToEntity()); + context.IdentityResources.Add(resource.ToEntity()); } await context.SaveChangesAsync(); } @@ -48,7 +72,7 @@ namespace Microsoft.eShopOnContainers.Services.Identity.API.Data { foreach (var api in Config.GetApis()) { - await context.ApiResources.AddAsync(api.ToEntity()); + context.ApiResources.Add(api.ToEntity()); } await context.SaveChangesAsync();