joy the admin area works with this commit

This commit is contained in:
Tom Gardham-Pallister 2017-10-25 08:06:41 +01:00
parent 4428982052
commit 09126911bd
4 changed files with 93 additions and 57 deletions

View File

@ -146,45 +146,25 @@ namespace Ocelot.DependencyInjection
var identityServerConfiguration = IdentityServerConfigurationCreator.GetIdentityServerConfiguration(); var identityServerConfiguration = IdentityServerConfigurationCreator.GetIdentityServerConfiguration();
if (identityServerConfiguration != null) if (identityServerConfiguration != null)
{
services.AddIdentityServer(identityServerConfiguration);
}
return services;
}
private static void AddIdentityServer(this IServiceCollection services, IIdentityServerConfiguration identityServerConfiguration)
{ {
services.TryAddSingleton<IIdentityServerConfiguration>(identityServerConfiguration); services.TryAddSingleton<IIdentityServerConfiguration>(identityServerConfiguration);
services.TryAddSingleton<IHashMatcher, HashMatcher>(); services.TryAddSingleton<IHashMatcher, HashMatcher>();
var identityServerBuilder = services var identityServerBuilder = services
.AddIdentityServer(options => { .AddIdentityServer()
options.IssuerUri = "Ocelot"; // .AddIdentityServer(options => {
}) // options.IssuerUri = "Ocelot";
.AddInMemoryApiResources(new List<ApiResource> // })
{ .AddInMemoryApiResources(Resources(identityServerConfiguration))
new ApiResource .AddInMemoryClients(Client(identityServerConfiguration))
{ .AddResourceOwnerValidator<OcelotResourceOwnerPasswordValidator>();
Name = identityServerConfiguration.ApiName,
Description = identityServerConfiguration.Description,
Enabled = identityServerConfiguration.Enabled,
DisplayName = identityServerConfiguration.ApiName,
Scopes = identityServerConfiguration.AllowedScopes.Select(x => new Scope(x)).ToList(),
ApiSecrets = new List<Secret>
{
new Secret
{
Value = identityServerConfiguration.ApiSecret.Sha256()
}
}
}
})
.AddInMemoryClients(new List<Client>
{
new Client
{
ClientId = identityServerConfiguration.ApiName,
AllowedGrantTypes = GrantTypes.ResourceOwnerPassword,
ClientSecrets = new List<Secret> {new Secret(identityServerConfiguration.ApiSecret.Sha256())},
AllowedScopes = identityServerConfiguration.AllowedScopes,
AccessTokenType = identityServerConfiguration.AccessTokenType,
Enabled = identityServerConfiguration.Enabled,
RequireClientSecret = identityServerConfiguration.RequireClientSecret
}
}).AddResourceOwnerValidator<OcelotResourceOwnerPasswordValidator>();
var whb = services.First(x => x.ServiceType == typeof(IWebHostBuilder)); var whb = services.First(x => x.ServiceType == typeof(IWebHostBuilder));
var urlFinder = new BaseUrlFinder((IWebHostBuilder)whb.ImplementationInstance); var urlFinder = new BaseUrlFinder((IWebHostBuilder)whb.ImplementationInstance);
@ -194,13 +174,14 @@ namespace Ocelot.DependencyInjection
services.AddAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme) services.AddAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme)
.AddIdentityServerAuthentication(o => .AddIdentityServerAuthentication(o =>
{ {
o.Authority = baseSchemeUrlAndPort + "admin"; //todo - this needs to come from the config so have to get it in here...
o.Authority = baseSchemeUrlAndPort + "/administration";
o.ApiName = identityServerConfiguration.ApiName; o.ApiName = identityServerConfiguration.ApiName;
o.RequireHttpsMetadata = identityServerConfiguration.RequireHttps; o.RequireHttpsMetadata = identityServerConfiguration.RequireHttps;
//o.AllowedScopes = identityServerConfiguration.AllowedScopes;
o.SupportedTokens = SupportedTokens.Both; o.SupportedTokens = SupportedTokens.Both;
o.ApiSecret = identityServerConfiguration.ApiSecret; o.ApiSecret = identityServerConfiguration.ApiSecret;
}); });
if (string.IsNullOrEmpty(identityServerConfiguration.CredentialsSigningCertificateLocation) || string.IsNullOrEmpty(identityServerConfiguration.CredentialsSigningCertificatePassword)) if (string.IsNullOrEmpty(identityServerConfiguration.CredentialsSigningCertificateLocation) || string.IsNullOrEmpty(identityServerConfiguration.CredentialsSigningCertificatePassword))
{ {
identityServerBuilder.AddDeveloperSigningCredential(); identityServerBuilder.AddDeveloperSigningCredential();
@ -212,7 +193,62 @@ namespace Ocelot.DependencyInjection
} }
} }
return services; private static List<ApiResource> Resources(IIdentityServerConfiguration identityServerConfiguration)
{
return new List<ApiResource>
{
new ApiResource("admin", "My API")
};
// return new List<ApiResource>
// {
// new ApiResource
// {
// Name = identityServerConfiguration.ApiName,
// Description = identityServerConfiguration.Description,
// Enabled = identityServerConfiguration.Enabled,
// DisplayName = identityServerConfiguration.ApiName,
// Scopes = identityServerConfiguration.AllowedScopes.Select(x => new Scope(x)).ToList(),
// ApiSecrets = new List<Secret>
// {
// new Secret
// {
// Value = identityServerConfiguration.ApiSecret.Sha256()
// }
// }
// }
// };
}
private static List<Client> Client(IIdentityServerConfiguration identityServerConfiguration)
{
return new List<Client>
{
// resource owner password grant client
new Client
{
ClientId = "admin",
AllowedGrantTypes = GrantTypes.ResourceOwnerPassword,
ClientSecrets =
{
new Secret("secret".Sha256())
},
AllowedScopes = { "admin" }
}
};
// return new List<Client>
// {
// new Client
// {
// ClientId = identityServerConfiguration.ApiName,
// AllowedGrantTypes = GrantTypes.ResourceOwnerPassword,
// ClientSecrets = new List<Secret> {new Secret(identityServerConfiguration.ApiSecret.Sha256())},
// AllowedScopes = identityServerConfiguration.AllowedScopes,
// AccessTokenType = identityServerConfiguration.AccessTokenType,
// Enabled = identityServerConfiguration.Enabled,
// RequireClientSecret = identityServerConfiguration.RequireClientSecret
// }
// };
} }
} }
} }

View File

@ -181,12 +181,12 @@ namespace Ocelot.Middleware
if(!string.IsNullOrEmpty(configuration.AdministrationPath) && identityServerConfiguration != null) if(!string.IsNullOrEmpty(configuration.AdministrationPath) && identityServerConfiguration != null)
{ {
builder.Map(configuration.AdministrationPath, app => builder.Map(configuration.AdministrationPath, app =>
{ {
app.UseMvc(); Console.WriteLine("SETTING UP ADMIN AREA");
app.UseIdentityServer(); app.UseIdentityServer();
app.UseAuthentication(); app.UseAuthentication();
app.UseMvc();
}); });
} }
} }

View File

@ -34,7 +34,7 @@ namespace Ocelot.ManualTest
{ {
x.WithMicrosoftLogging(log => x.WithMicrosoftLogging(log =>
{ {
log.AddConsole(LogLevel.Debug); //log.AddConsole(LogLevel.Debug);
}) })
.WithDictionaryHandle(); .WithDictionaryHandle();
}; };

View File

@ -312,6 +312,6 @@
"GlobalConfiguration": { "GlobalConfiguration": {
"RequestIdKey": "OcRequestId", "RequestIdKey": "OcRequestId",
"AdministrationPath": "/admin" "AdministrationPath": "/administration"
} }
} }