trying to get identity server authing

This commit is contained in:
TomPallister 2017-02-19 13:59:17 +00:00
parent fa47663259
commit d236ed3018
5 changed files with 107 additions and 43 deletions

View File

@ -1,20 +1,24 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Ocelot.Services; using Ocelot.Services;
namespace Ocelot.Controllers namespace Ocelot.Controllers
{ {
[RouteAttribute("configuration")] [Authorize(Roles = "Admin")]
public class FileConfigurationController [Route("configuration")]
public class FileConfigurationController : Controller
{ {
private IGetFileConfiguration _getFileConfig; private readonly IGetFileConfiguration _getFileConfig;
public FileConfigurationController(IGetFileConfiguration getFileConfig) public FileConfigurationController(IGetFileConfiguration getFileConfig)
{ {
_getFileConfig = getFileConfig; _getFileConfig = getFileConfig;
} }
[HttpGet]
public IActionResult Get() public IActionResult Get()
{ {
var user = this.HttpContext.User;
return new OkObjectResult(_getFileConfig.Invoke().Data); return new OkObjectResult(_getFileConfig.Invoke().Data);
} }
} }

View File

@ -1,6 +1,10 @@
using System; using System;
using System.Collections.Generic;
using System.Net.Http; using System.Net.Http;
using System.Security.Claims;
using CacheManager.Core; using CacheManager.Core;
using IdentityServer4.Models;
using IdentityServer4.Test;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
@ -61,6 +65,53 @@ namespace Ocelot.DependencyInjection
public static IServiceCollection AddOcelot(this IServiceCollection services) public static IServiceCollection AddOcelot(this IServiceCollection services)
{ {
services.AddIdentityServer()
.AddTemporarySigningCredential()
.AddInMemoryApiResources(new List<ApiResource>
{
new ApiResource
{
Name = "admin",
Description = "Ocelot Administration",
Enabled = true,
DisplayName = "admin",
Scopes = new List<Scope>()
{
new Scope("admin"),
new Scope("openid"),
new Scope("offline_access")
},
ApiSecrets = new List<Secret>
{
new Secret
{
Value = "secret".Sha256()
}
}
}
})
.AddInMemoryClients(new List<Client>
{
new Client
{
ClientId = "admin",
AllowedGrantTypes = GrantTypes.ResourceOwnerPassword,
ClientSecrets = new List<Secret> {new Secret("secret".Sha256())},
AllowedScopes = new List<string> {"admin", "openid", "offline_access"},
AccessTokenType = AccessTokenType.Jwt,
Enabled = true,
RequireClientSecret = false
}
})
.AddTestUsers(new List<TestUser>
{
new TestUser
{
Username = "admin",
Password = "admin",
SubjectId = "admin",
}
});
services.AddMvcCore().AddJsonFormatters(); services.AddMvcCore().AddJsonFormatters();
services.AddLogging(); services.AddLogging();
services.AddSingleton<IGetFileConfiguration, GetFileConfiguration>(); services.AddSingleton<IGetFileConfiguration, GetFileConfiguration>();

View File

@ -1,4 +1,6 @@
using Microsoft.AspNetCore.Builder; using System.Collections.Generic;
using IdentityServer4.AccessTokenValidation;
using Microsoft.AspNetCore.Builder;
using Ocelot.Authentication.Middleware; using Ocelot.Authentication.Middleware;
using Ocelot.Cache.Middleware; using Ocelot.Cache.Middleware;
using Ocelot.Claims.Middleware; using Ocelot.Claims.Middleware;
@ -144,9 +146,21 @@ namespace Ocelot.Middleware
if(!string.IsNullOrEmpty(configuration.AdministrationPath)) if(!string.IsNullOrEmpty(configuration.AdministrationPath))
{ {
builder.Map(configuration.AdministrationPath, x => builder.Map(configuration.AdministrationPath, app =>
{ {
x.UseMvc(); app.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions
{
Authority = "http://localhost:5000/admin",
ApiName = "admin",
RequireHttpsMetadata = false,
AllowedScopes = new List<string>(),
SupportedTokens = SupportedTokens.Both,
ApiSecret = "secret"
});
app.UseIdentityServer();
app.UseMvc();
}); });
} }
} }

View File

@ -1,35 +1,36 @@
{ {
"version": "0.0.0-dev", "version": "0.0.0-dev",
"dependencies": { "dependencies": {
"Microsoft.AspNetCore.Server.IISIntegration": "1.1.0", "Microsoft.AspNetCore.Server.IISIntegration": "1.1.0",
"Microsoft.Extensions.Configuration.EnvironmentVariables": "1.1.0", "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.1.0",
"Microsoft.Extensions.Configuration.FileExtensions": "1.1.0", "Microsoft.Extensions.Configuration.FileExtensions": "1.1.0",
"Microsoft.Extensions.Configuration.Json": "1.1.0", "Microsoft.Extensions.Configuration.Json": "1.1.0",
"Microsoft.Extensions.Logging": "1.1.0", "Microsoft.Extensions.Logging": "1.1.0",
"Microsoft.Extensions.Logging.Console": "1.1.0", "Microsoft.Extensions.Logging.Console": "1.1.0",
"Microsoft.Extensions.Logging.Debug": "1.1.0", "Microsoft.Extensions.Logging.Debug": "1.1.0",
"Microsoft.Extensions.Options.ConfigurationExtensions": "1.1.0", "Microsoft.Extensions.Options.ConfigurationExtensions": "1.1.0",
"Microsoft.AspNetCore.Http": "1.1.0", "Microsoft.AspNetCore.Http": "1.1.0",
"System.Text.RegularExpressions": "4.3.0", "System.Text.RegularExpressions": "4.3.0",
"Microsoft.AspNetCore.Authentication.OAuth": "1.1.0", "Microsoft.AspNetCore.Authentication.OAuth": "1.1.0",
"Microsoft.AspNetCore.Authentication.JwtBearer": "1.1.0", "Microsoft.AspNetCore.Authentication.JwtBearer": "1.1.0",
"Microsoft.AspNetCore.Authentication.OpenIdConnect": "1.1.0", "Microsoft.AspNetCore.Authentication.OpenIdConnect": "1.1.0",
"Microsoft.AspNetCore.Authentication.Cookies": "1.1.0", "Microsoft.AspNetCore.Authentication.Cookies": "1.1.0",
"Microsoft.AspNetCore.Authentication.Google": "1.1.0", "Microsoft.AspNetCore.Authentication.Google": "1.1.0",
"Microsoft.AspNetCore.Authentication.Facebook": "1.1.0", "Microsoft.AspNetCore.Authentication.Facebook": "1.1.0",
"Microsoft.AspNetCore.Authentication.Twitter": "1.1.0", "Microsoft.AspNetCore.Authentication.Twitter": "1.1.0",
"Microsoft.AspNetCore.Authentication.MicrosoftAccount": "1.1.0", "Microsoft.AspNetCore.Authentication.MicrosoftAccount": "1.1.0",
"Microsoft.AspNetCore.Authentication": "1.1.0", "Microsoft.AspNetCore.Authentication": "1.1.0",
"IdentityServer4.AccessTokenValidation": "1.0.2", "IdentityServer4.AccessTokenValidation": "1.0.2",
"Microsoft.AspNetCore.Mvc": "1.1.0", "Microsoft.AspNetCore.Mvc": "1.1.0",
"Microsoft.AspNetCore.Server.Kestrel": "1.1.0", "Microsoft.AspNetCore.Server.Kestrel": "1.1.0",
"Microsoft.NETCore.App": "1.1.0", "Microsoft.NETCore.App": "1.1.0",
"CacheManager.Core": "0.9.2", "CacheManager.Core": "0.9.2",
"CacheManager.Microsoft.Extensions.Configuration": "0.9.2", "CacheManager.Microsoft.Extensions.Configuration": "0.9.2",
"CacheManager.Microsoft.Extensions.Logging": "0.9.2", "CacheManager.Microsoft.Extensions.Logging": "0.9.2",
"Consul": "0.7.2.1", "Consul": "0.7.2.1",
"Polly": "5.0.3" "Polly": "5.0.3",
}, "IdentityServer4": "1.0.1"
},
"runtimes": { "runtimes": {
"win10-x64": {}, "win10-x64": {},
"osx.10.11-x64": {}, "osx.10.11-x64": {},

View File

@ -1,10 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Net; using System.Net;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Ocelot.Configuration.File; using Ocelot.Configuration.File;
using TestStack.BDDfy; using TestStack.BDDfy;
using Xunit; using Xunit;
@ -13,7 +9,6 @@ namespace Ocelot.AcceptanceTests
{ {
public class AdministrationTests : IDisposable public class AdministrationTests : IDisposable
{ {
private IWebHost _builder;
private readonly Steps _steps; private readonly Steps _steps;
public AdministrationTests() public AdministrationTests()
@ -81,7 +76,6 @@ namespace Ocelot.AcceptanceTests
public void Dispose() public void Dispose()
{ {
_builder?.Dispose();
_steps.Dispose(); _steps.Dispose();
} }
} }