Load api keys from database. Create api keys.
This commit is contained in:
parent
3331775a0b
commit
8718d81f7a
4 changed files with 61 additions and 12 deletions
|
@ -5,6 +5,7 @@ using Microsoft.Extensions.Logging;
|
||||||
using NSubstitute;
|
using NSubstitute;
|
||||||
using Robware.Api.Auth.Controllers;
|
using Robware.Api.Auth.Controllers;
|
||||||
using Robware.Auth.API;
|
using Robware.Auth.API;
|
||||||
|
using System;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
namespace Robware.Api.Auth.Tests.Controllers {
|
namespace Robware.Api.Auth.Tests.Controllers {
|
||||||
|
@ -14,7 +15,8 @@ namespace Robware.Api.Auth.Tests.Controllers {
|
||||||
var logger = Substitute.For<ILogger<ApiController>>();
|
var logger = Substitute.For<ILogger<ApiController>>();
|
||||||
var apiKeyValidator = Substitute.For<IApiKeyValidator>();
|
var apiKeyValidator = Substitute.For<IApiKeyValidator>();
|
||||||
apiKeyValidator.Validate("key").Returns(true);
|
apiKeyValidator.Validate("key").Returns(true);
|
||||||
var controller = new ApiController(logger, apiKeyValidator);
|
var apiKeyRepository = Substitute.For<IApiKeys>();
|
||||||
|
var controller = new ApiController(logger, apiKeyValidator, apiKeyRepository);
|
||||||
(await controller.Validate("key")).Should().BeOfType<OkResult>();
|
(await controller.Validate("key")).Should().BeOfType<OkResult>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -23,8 +25,28 @@ namespace Robware.Api.Auth.Tests.Controllers {
|
||||||
var logger = Substitute.For<ILogger<ApiController>>();
|
var logger = Substitute.For<ILogger<ApiController>>();
|
||||||
var apiKeyValidator = Substitute.For<IApiKeyValidator>();
|
var apiKeyValidator = Substitute.For<IApiKeyValidator>();
|
||||||
apiKeyValidator.Validate("key").Returns(false);
|
apiKeyValidator.Validate("key").Returns(false);
|
||||||
var controller = new ApiController(logger, apiKeyValidator);
|
var apiKeyRepository = Substitute.For<IApiKeys>();
|
||||||
|
var controller = new ApiController(logger, apiKeyValidator, apiKeyRepository);
|
||||||
(await controller.Validate("key")).Should().BeOfType<UnauthorizedResult>();
|
(await controller.Validate("key")).Should().BeOfType<UnauthorizedResult>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Create_WithName_ReturnsApiKeyJson() {
|
||||||
|
var logger = Substitute.For<ILogger<ApiController>>();
|
||||||
|
var apiKeyValidator = Substitute.For<IApiKeyValidator>();
|
||||||
|
var apiKeyRepository = Substitute.For<IApiKeys>();
|
||||||
|
|
||||||
|
var expectedKey = new ApiKey {
|
||||||
|
Name = "test",
|
||||||
|
Key = "test",
|
||||||
|
IssueTimestamp = DateTime.Now,
|
||||||
|
Enabled = true
|
||||||
|
};
|
||||||
|
|
||||||
|
apiKeyRepository.Create("test").Returns(expectedKey);
|
||||||
|
|
||||||
|
var controller = new ApiController(logger, apiKeyValidator, apiKeyRepository);
|
||||||
|
controller.Create("test").Result.Value.Should().BeEquivalentTo(expectedKey);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,4 +1,5 @@
|
||||||
using System.Threading.Tasks;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
using System.Threading.Tasks;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using Robware.Auth.API;
|
using Robware.Auth.API;
|
||||||
|
@ -9,13 +10,19 @@ namespace Robware.Api.Auth.Controllers {
|
||||||
public class ApiController : ControllerBase {
|
public class ApiController : ControllerBase {
|
||||||
private readonly ILogger<ApiController> _logger;
|
private readonly ILogger<ApiController> _logger;
|
||||||
private readonly IApiKeyValidator _apiKeyValidator;
|
private readonly IApiKeyValidator _apiKeyValidator;
|
||||||
|
private readonly IApiKeys _apiKeyRepository;
|
||||||
|
|
||||||
public ApiController(ILogger<ApiController> logger, IApiKeyValidator apiKeyValidator) {
|
public ApiController(ILogger<ApiController> logger, IApiKeyValidator apiKeyValidator, IApiKeys apiKeyRepository) {
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
_apiKeyValidator = apiKeyValidator;
|
_apiKeyValidator = apiKeyValidator;
|
||||||
|
_apiKeyRepository = apiKeyRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet(nameof(Validate))]
|
[HttpGet(nameof(Validate))]
|
||||||
public async Task<ActionResult> Validate(string key) => await _apiKeyValidator.Validate(key) ? (ActionResult) Ok() : Unauthorized();
|
public async Task<ActionResult> Validate(string key) => await _apiKeyValidator.Validate(key) ? (ActionResult) Ok() : Unauthorized();
|
||||||
|
|
||||||
|
[HttpPost(nameof(Create))]
|
||||||
|
[Authorize]
|
||||||
|
public async Task<ActionResult<ApiKey>> Create(string name) => await _apiKeyRepository.Create(name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
namespace Robware.Auth.API {
|
namespace Robware.Auth.API {
|
||||||
public interface IApiKeys {
|
public interface IApiKeys {
|
||||||
Task<ApiKey> Get(string key);
|
Task<ApiKey> Get(string apiKey);
|
||||||
|
Task<ApiKey> Create(string name);
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,19 +1,38 @@
|
||||||
|
using MongoDB.Driver;
|
||||||
using Robware.Auth.API;
|
using Robware.Auth.API;
|
||||||
|
using Robware.Data.ApiKeys;
|
||||||
|
using Robware.Data.ApiKeys.State;
|
||||||
using System;
|
using System;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace Robware.Data {
|
namespace Robware.Data {
|
||||||
public class ApiKeyRepository : IApiKeys {
|
public class ApiKeyRepository : IApiKeys {
|
||||||
public async Task<ApiKey> Get(string key) {
|
private readonly IMongoCollection<ApiKeyState> _collection;
|
||||||
if (key=="denied")
|
|
||||||
throw new ApiKeyNotFoundException(key);
|
|
||||||
|
|
||||||
return new ApiKey {
|
public ApiKeyRepository(IMongoDatabase database) {
|
||||||
Name = "Hardcoded key",
|
_collection = database.GetCollection<ApiKeyState>("api-keys");
|
||||||
Key = key,
|
}
|
||||||
|
|
||||||
|
public async Task<ApiKey> Get(string apiKey) {
|
||||||
|
var result = (await _collection.FindAsync(key => key.Key == apiKey)).FirstOrDefault();
|
||||||
|
|
||||||
|
if (result == null)
|
||||||
|
throw new ApiKeyNotFoundException(apiKey);
|
||||||
|
|
||||||
|
return new DatabaseApiKey(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<ApiKey> Create(string name) {
|
||||||
|
var apiKey = new ApiKey {
|
||||||
|
Name = name,
|
||||||
|
Key = Guid.NewGuid().ToString(),
|
||||||
Enabled = true,
|
Enabled = true,
|
||||||
IssueTimestamp = DateTime.Now
|
IssueTimestamp = DateTime.Now
|
||||||
};
|
};
|
||||||
|
|
||||||
|
await _collection.InsertOneAsync(new ApiKeyState(apiKey));
|
||||||
|
|
||||||
|
return apiKey;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Add table
Add a link
Reference in a new issue