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,10 @@
using System;
namespace Robware.Auth.API {
public class ApiKey {
public string Name { get; set; }
public DateTime IssueTimestamp { get; set; }
public string Key { get; set; }
public bool Enabled { get; set; }
}
}

View file

@ -0,0 +1,9 @@
using System;
namespace Robware.Auth.API {
public class ApiKeyNotFoundException :Exception {
public ApiKeyNotFoundException(string key) : base("Could not find API key " + key) {
}
}
}

View file

@ -0,0 +1,22 @@
using System;
using System.Threading.Tasks;
namespace Robware.Auth.API {
public class ApiKeyValidator : IApiKeyValidator {
private readonly IApiKeys _apiKeys;
public ApiKeyValidator(IApiKeys apiKeys) {
_apiKeys = apiKeys;
}
public async Task<bool> Validate(string key) {
try {
var apiKey = await _apiKeys.Get(key);
return apiKey.Enabled;
}
catch (ApiKeyNotFoundException) {
return false;
}
}
}
}

View file

@ -0,0 +1,7 @@
using System.Threading.Tasks;
namespace Robware.Auth.API {
public interface IApiKeyValidator {
Task<bool> Validate(string key);
}
}

View file

@ -0,0 +1,7 @@
using System.Threading.Tasks;
namespace Robware.Auth.API {
public interface IApiKeys {
Task<ApiKey> Get(string key);
}
}

View file

@ -1,7 +1,6 @@
using System;
using System.Threading.Tasks;
using System.Threading.Tasks;
namespace Robware.Auth {
namespace Robware.Auth.Users {
public class Authenticator : IAuthenticator {
private readonly IUsers _users;
private readonly ICryptographyProvider _crypto;

View file

@ -1,7 +1,7 @@
using System.Security.Cryptography;
using System.Text;
namespace Robware.Auth {
namespace Robware.Auth.Users {
public class CryptographyProvider : ICryptographyProvider {
public string Encrypt(string input) {
using (var sha256 = SHA256.Create()) {

View file

@ -1,6 +1,6 @@
using System.Threading.Tasks;
namespace Robware.Auth {
namespace Robware.Auth.Users {
public interface IAuthenticator {
Task<(AuthenticationResult Result, User User)> Authenticate(string username, string password);
}

View file

@ -1,4 +1,4 @@
namespace Robware.Auth {
namespace Robware.Auth.Users {
public interface ICryptographyProvider {
string Encrypt(string input);
}

View file

@ -1,6 +1,6 @@
using System.Threading.Tasks;
namespace Robware.Auth {
namespace Robware.Auth.Users {
public interface IUsers {
Task<User> GetByEmail(string email);
}

View file

@ -1,4 +1,4 @@
namespace Robware.Auth {
namespace Robware.Auth.Users {
public class User {
public string Username { get; protected set; }
public string Password { get; protected set; }

View file

@ -1,6 +1,6 @@
using System;
namespace Robware.Auth {
namespace Robware.Auth.Users {
public class UserNotFoundException : Exception {
public UserNotFoundException(string username) : base("Could not find user " + username) {