Provide mechanism to validate API keys. Re-organise structure to reflect new responsibility

This commit is contained in:
Robert Marshall 2020-04-19 18:16:42 +01:00
parent 87ae65316f
commit 9519bc623b
27 changed files with 245 additions and 36 deletions

View file

@ -0,0 +1,43 @@
using System;
using System.Threading.Tasks;
using System.Linq;
using Dapper;
using Robware.Auth.API;
using Robware.Data.Users;
namespace Robware.Data.API {
public class ApiKeyRepository : IApiKeys {
private readonly IDatabaseProvider _dbProvider;
public ApiKeyRepository(IDatabaseProvider dbProvider) {
_dbProvider = dbProvider;
}
public async Task<ApiKey> Get(string key) {
const string query = "SELECT * FROM api_keys WHERE api_key=@key";
using (var connection = _dbProvider.NewConnection()) {
connection.Open();
var result = await connection.QueryAsync<ApiKeyState>(query, new {key});
if (!result.Any())
throw new ApiKeyNotFoundException(key);
var dbKey = result.Single();
return new ApiKey {
Key = dbKey.Api_Key,
Enabled = dbKey.Enabled,
IssueTimestamp = dbKey.IssueTimestamp,
Name = dbKey.Name
};
}
}
}
public class ApiKeyState {
public string Name { get; set; }
public DateTime IssueTimestamp { get; set; }
public string Api_Key { get; set; }
public bool Enabled { get; set; }
}
}