refactor(ConsumerApi): umbenannt in Consumer mit LoginDto.
- ConsumerApi.Name umbenannt in Id - Eigenschaft audience hinzugefügt.
This commit is contained in:
parent
47aeb49a40
commit
0a61586e39
@ -19,7 +19,7 @@ namespace DigitalData.Auth.API.Controllers
|
||||
{
|
||||
private readonly IJwtSignatureHandler<UserReadDto> _userSignatureHandler;
|
||||
|
||||
private readonly IJwtSignatureHandler<ConsumerApi> _apiSignatureHandler;
|
||||
private readonly IJwtSignatureHandler<Consumer> _consumerSignatureHandler;
|
||||
|
||||
private readonly AuthApiParams _apiParams;
|
||||
|
||||
@ -31,9 +31,9 @@ namespace DigitalData.Auth.API.Controllers
|
||||
|
||||
private readonly IDirectorySearchService _dirSearchService;
|
||||
|
||||
private readonly IConsumerApiService _consumerApiService;
|
||||
private readonly IConsumerService _consumerService;
|
||||
|
||||
public AuthController(IJwtSignatureHandler<UserReadDto> userSignatureHandler, IOptions<AuthApiParams> cookieParamsOptions, ICryptoFactory cryptoFactory, ILogger<AuthController> logger, IUserService userService, IDirectorySearchService dirSearchService, IConsumerApiService consumerApiService, IJwtSignatureHandler<ConsumerApi> apiSignatureHandler)
|
||||
public AuthController(IJwtSignatureHandler<UserReadDto> userSignatureHandler, IOptions<AuthApiParams> cookieParamsOptions, ICryptoFactory cryptoFactory, ILogger<AuthController> logger, IUserService userService, IDirectorySearchService dirSearchService, IConsumerService consumerService, IJwtSignatureHandler<Consumer> apiSignatureHandler)
|
||||
{
|
||||
_apiParams = cookieParamsOptions.Value;
|
||||
_userSignatureHandler = userSignatureHandler;
|
||||
@ -41,8 +41,8 @@ namespace DigitalData.Auth.API.Controllers
|
||||
_logger = logger;
|
||||
_userService = userService;
|
||||
_dirSearchService = dirSearchService;
|
||||
_consumerApiService = consumerApiService;
|
||||
_apiSignatureHandler = apiSignatureHandler;
|
||||
_consumerService = consumerService;
|
||||
_consumerSignatureHandler = apiSignatureHandler;
|
||||
}
|
||||
|
||||
private async Task<IActionResult> CreateTokenAsync(LogInDto login, string consumerRoute, bool cookie = true)
|
||||
@ -72,9 +72,9 @@ namespace DigitalData.Auth.API.Controllers
|
||||
return Ok(token);
|
||||
}
|
||||
|
||||
private async Task<IActionResult> CreateTokenAsync(ConsumerApiLogin login, bool cookie = true)
|
||||
private async Task<IActionResult> CreateTokenAsync(ConsumerLogin login, bool cookie = true)
|
||||
{
|
||||
var api = await _consumerApiService.ReadByNameAsync(login.Name);
|
||||
var api = await _consumerService.ReadByIdAsync(login.Id);
|
||||
|
||||
if (api is null || api.Password != login.Password)
|
||||
return Unauthorized();
|
||||
@ -82,7 +82,7 @@ namespace DigitalData.Auth.API.Controllers
|
||||
if (!_cryptoFactory.TokenDescriptors.TryGet(_apiParams.Issuer, _apiParams.DefaultConsumer.Audience, out var descriptor))
|
||||
return StatusCode(StatusCodes.Status500InternalServerError);
|
||||
|
||||
var token = _apiSignatureHandler.WriteToken(api, descriptor);
|
||||
var token = _consumerSignatureHandler.WriteToken(api, descriptor);
|
||||
|
||||
//set cookie
|
||||
if (cookie)
|
||||
@ -112,7 +112,7 @@ namespace DigitalData.Auth.API.Controllers
|
||||
|
||||
[HttpPost("login")]
|
||||
[AllowAnonymous]
|
||||
public async Task<IActionResult> Login([FromForm] ConsumerApiLogin login)
|
||||
public async Task<IActionResult> Login([FromForm] ConsumerLogin login)
|
||||
{
|
||||
try
|
||||
{
|
||||
@ -155,7 +155,7 @@ namespace DigitalData.Auth.API.Controllers
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> CreateTokenViaBody([FromBody] ConsumerApiLogin login, [FromQuery] bool cookie = false)
|
||||
public async Task<IActionResult> CreateTokenViaBody([FromBody] ConsumerLogin login, [FromQuery] bool cookie = false)
|
||||
{
|
||||
try
|
||||
{
|
||||
|
||||
@ -1,4 +0,0 @@
|
||||
namespace DigitalData.Auth.API.Dto
|
||||
{
|
||||
public record ConsumerApiLogin(string Name, string Password);
|
||||
}
|
||||
4
src/DigitalData.Auth.API/Dto/ConsumerLogin.cs
Normal file
4
src/DigitalData.Auth.API/Dto/ConsumerLogin.cs
Normal file
@ -0,0 +1,4 @@
|
||||
namespace DigitalData.Auth.API.Dto
|
||||
{
|
||||
public record ConsumerLogin(string Id, string Password);
|
||||
}
|
||||
4
src/DigitalData.Auth.API/Entities/Consumer.cs
Normal file
4
src/DigitalData.Auth.API/Entities/Consumer.cs
Normal file
@ -0,0 +1,4 @@
|
||||
namespace DigitalData.Auth.API.Entities
|
||||
{
|
||||
public record Consumer(string Id, string Password, string Audience);
|
||||
}
|
||||
@ -1,4 +0,0 @@
|
||||
namespace DigitalData.Auth.API.Entities
|
||||
{
|
||||
public record ConsumerApi(string Name, string Password);
|
||||
}
|
||||
@ -13,7 +13,7 @@ using Microsoft.OpenApi.Models;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
builder.Configuration.AddJsonFile("consumers-api.json", true, true);
|
||||
builder.Configuration.AddJsonFile("consumer-repository.json", true, true);
|
||||
|
||||
var config = builder.Configuration;
|
||||
|
||||
@ -23,9 +23,9 @@ var apiParams = config.Get<AuthApiParams>() ?? throw new InvalidOperationExcepti
|
||||
builder.Services.Configure<AuthApiParams>(config);
|
||||
builder.Services.AddConsumerApiService(config);
|
||||
builder.Services.AddCryptoFactory(config.GetSection("CryptParams"));
|
||||
builder.Services.AddJwtSignatureHandler<ConsumerApi>(api => new Dictionary<string, object>
|
||||
builder.Services.AddJwtSignatureHandler<Consumer>(api => new Dictionary<string, object>
|
||||
{
|
||||
{ JwtRegisteredClaimNames.Sub, api.Name },
|
||||
{ JwtRegisteredClaimNames.Sub, api.Id },
|
||||
{ JwtRegisteredClaimNames.Iat, DateTimeOffset.UtcNow.ToUnixTimeSeconds() }
|
||||
});
|
||||
builder.Services.AddJwtSignatureHandler<UserReadDto>(user => new Dictionary<string, object>
|
||||
|
||||
@ -1,19 +0,0 @@
|
||||
using DigitalData.Auth.API.Entities;
|
||||
using DigitalData.Auth.API.Services.Contracts;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace DigitalData.Auth.API.Services
|
||||
{
|
||||
public class ConfiguredConsumerApiService : IConsumerApiService
|
||||
{
|
||||
private readonly IEnumerable<ConsumerApi> _consumerAPIs;
|
||||
public ConfiguredConsumerApiService(IOptions<IEnumerable<ConsumerApi>> options)
|
||||
{
|
||||
_consumerAPIs = options.Value;
|
||||
}
|
||||
|
||||
public Task<ConsumerApi?> ReadByNameAsync(string name) => Task.Run(() => _consumerAPIs.FirstOrDefault(api => api.Name == name));
|
||||
|
||||
public async Task<bool> VerifyAsync(string name, string password) => (await ReadByNameAsync(name))?.Password == password;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,19 @@
|
||||
using DigitalData.Auth.API.Entities;
|
||||
using DigitalData.Auth.API.Services.Contracts;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace DigitalData.Auth.API.Services
|
||||
{
|
||||
public class ConfiguredConsumerService : IConsumerService
|
||||
{
|
||||
private readonly IEnumerable<Consumer> _consumerAPIs;
|
||||
public ConfiguredConsumerService(IOptions<IEnumerable<Consumer>> options)
|
||||
{
|
||||
_consumerAPIs = options.Value;
|
||||
}
|
||||
|
||||
public Task<Consumer?> ReadByIdAsync(string id) => Task.Run(() => _consumerAPIs.FirstOrDefault(api => api.Id == id));
|
||||
|
||||
public async Task<bool> VerifyAsync(string id, string password) => (await ReadByIdAsync(id))?.Password == password;
|
||||
}
|
||||
}
|
||||
@ -1,11 +0,0 @@
|
||||
using DigitalData.Auth.API.Entities;
|
||||
|
||||
namespace DigitalData.Auth.API.Services.Contracts
|
||||
{
|
||||
public interface IConsumerApiService
|
||||
{
|
||||
public Task<ConsumerApi?> ReadByNameAsync(string name);
|
||||
|
||||
public Task<bool> VerifyAsync(string name, string password);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,11 @@
|
||||
using DigitalData.Auth.API.Entities;
|
||||
|
||||
namespace DigitalData.Auth.API.Services.Contracts
|
||||
{
|
||||
public interface IConsumerService
|
||||
{
|
||||
public Task<Consumer?> ReadByIdAsync(string id);
|
||||
|
||||
public Task<bool> VerifyAsync(string id, string password);
|
||||
}
|
||||
}
|
||||
@ -8,9 +8,9 @@ namespace DigitalData.Auth.API.Services
|
||||
{
|
||||
public static IServiceCollection AddConsumerApiService(this IServiceCollection services, IConfiguration configuration, string key = "ConsumerAPIs")
|
||||
{
|
||||
var consumerApis = configuration.GetSection(key).Get<IEnumerable<ConsumerApi>>() ?? throw new InvalidOperationException($"No Consumer list found in {key} in configuration.");
|
||||
services.AddSingleton(Options.Create(consumerApis));
|
||||
services.AddSingleton<IConsumerApiService, ConfiguredConsumerApiService>();
|
||||
var consumers = configuration.GetSection(key).Get<IEnumerable<Consumer>>() ?? throw new InvalidOperationException($"No Consumer list found in {key} in configuration.");
|
||||
services.AddSingleton(Options.Create(consumers));
|
||||
services.AddSingleton<IConsumerService, ConfiguredConsumerService>();
|
||||
return services;
|
||||
}
|
||||
}
|
||||
|
||||
19
src/DigitalData.Auth.API/consumer-repository.json
Normal file
19
src/DigitalData.Auth.API/consumer-repository.json
Normal file
@ -0,0 +1,19 @@
|
||||
{
|
||||
"ConsumerAPIs": [
|
||||
{
|
||||
"Id": "auth",
|
||||
"Audience": "auth.digitaldata.works",
|
||||
"Password": "aQ9z!2@TgY7b#fHcD3pLmV1$wX"
|
||||
},
|
||||
{
|
||||
"Id": "work-flow",
|
||||
"Audience": "work-flow.digitaldata.works",
|
||||
"Password": "t3B|aiJ'i-snLzNRj3B{9=&:lM5P@'iL"
|
||||
},
|
||||
{
|
||||
"Id": "user-manager",
|
||||
"Audience": "user-manager.digitaldata.works",
|
||||
"Password": "a098Hvu1-y29ep{KPQO]#>8TK+fk{O`_d"
|
||||
}
|
||||
]
|
||||
}
|
||||
@ -1,12 +0,0 @@
|
||||
{
|
||||
"ConsumerAPIs": [
|
||||
{
|
||||
"Name": "WorkFlow.API",
|
||||
"Password": "t3B|aiJ'i-snLzNRj3B{9=&:lM5P@'iL"
|
||||
},
|
||||
{
|
||||
"Name": "DigitalData.UserManager.API",
|
||||
"Password": "a098Hvu1-y29ep{KPQO]#>8TK+fk{O`_d"
|
||||
}
|
||||
]
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user