Browse Source

Test other approach.

pull/617/head
Sebastian 5 years ago
parent
commit
0a04abd4a8
  1. 64
      backend/src/Squidex/Config/Authentication/IdentityServerServices.cs

64
backend/src/Squidex/Config/Authentication/IdentityServerServices.cs

@ -5,7 +5,6 @@
// All rights reserved. Licensed under the MIT license. // All rights reserved. Licensed under the MIT license.
// ========================================================================== // ==========================================================================
using System;
using IdentityServer4; using IdentityServer4;
using IdentityServer4.AccessTokenValidation; using IdentityServer4.AccessTokenValidation;
using IdentityServer4.Hosting.LocalApiAuthentication; using IdentityServer4.Hosting.LocalApiAuthentication;
@ -15,7 +14,6 @@ using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Squidex.Hosting; using Squidex.Hosting;
using Squidex.Web; using Squidex.Web;
@ -41,38 +39,37 @@ namespace Squidex.Config.Authentication
else else
{ {
authBuilder.AddLocalApi(); authBuilder.AddLocalApi();
authBuilder.Services.AddOptions<LocalApiAuthenticationOptions>()
authBuilder.Services.Configure<LocalApiAuthenticationOptions>((c, options) => .Configure<IUrlGenerator>((options, urlGenerator) =>
{ {
options.ClaimsIssuer = GetAuthorityUrl(c); options.ClaimsIssuer = urlGenerator.BuildUrl(Constants.IdentityServerPrefix, false);
options.ExpectedScope = Constants.ApiScope;
options.ExpectedScope = Constants.ApiScope; });
});
} }
authBuilder.Services.AddSingleton<IPostConfigureOptions<OpenIdConnectOptions>>(c => new PostConfigureOptions<OpenIdConnectOptions>(OpenIdConnectDefaults.AuthenticationScheme, options => authBuilder.AddOpenIdConnect();
{ authBuilder.Services.AddOptions<OpenIdConnectOptions>(OpenIdConnectDefaults.AuthenticationScheme)
if (!string.IsNullOrWhiteSpace(identityOptions.AuthorityUrl)) .Configure<IUrlGenerator>((options, urlGenerator) =>
{
options.Authority = identityOptions.AuthorityUrl;
}
else
{ {
options.Authority = GetAuthorityUrl(c); if (!string.IsNullOrWhiteSpace(identityOptions.AuthorityUrl))
} {
options.Authority = identityOptions.AuthorityUrl;
options.ClientId = Constants.InternalClientId; }
options.ClientSecret = Constants.InternalClientSecret; else
options.CallbackPath = "/signin-internal"; {
options.RequireHttpsMetadata = identityOptions.RequiresHttps; options.Authority = urlGenerator.BuildUrl(Constants.IdentityServerPrefix, false);
options.SaveTokens = true; }
options.Scope.Add(Constants.PermissionsScope);
options.Scope.Add(Constants.ProfileScope);
options.Scope.Add(Constants.RoleScope);
options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
}));
authBuilder.AddOpenIdConnect(); options.ClientId = Constants.InternalClientId;
options.ClientSecret = Constants.InternalClientSecret;
options.CallbackPath = "/signin-internal";
options.RequireHttpsMetadata = identityOptions.RequiresHttps;
options.SaveTokens = true;
options.Scope.Add(Constants.PermissionsScope);
options.Scope.Add(Constants.ProfileScope);
options.Scope.Add(Constants.RoleScope);
options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
});
authBuilder.AddPolicyScheme(Constants.ApiSecurityScheme, Constants.ApiSecurityScheme, options => authBuilder.AddPolicyScheme(Constants.ApiSecurityScheme, Constants.ApiSecurityScheme, options =>
{ {
@ -89,12 +86,5 @@ namespace Squidex.Config.Authentication
return authBuilder; return authBuilder;
} }
private static string GetAuthorityUrl(IServiceProvider services)
{
var urlGenerator = services.GetRequiredService<IUrlGenerator>();
return urlGenerator.BuildUrl(Constants.IdentityServerPrefix, false);
}
} }
} }

Loading…
Cancel
Save