Provide mechanism to validate API keys. Re-organise structure to reflect new responsibility
This commit is contained in:
parent
87ae65316f
commit
9519bc623b
27 changed files with 245 additions and 36 deletions
10
src/Robware.Auth/API/ApiKey.cs
Normal file
10
src/Robware.Auth/API/ApiKey.cs
Normal 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; }
|
||||
}
|
||||
}
|
9
src/Robware.Auth/API/ApiKeyNotFoundException.cs
Normal file
9
src/Robware.Auth/API/ApiKeyNotFoundException.cs
Normal 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) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
22
src/Robware.Auth/API/ApiKeyValidator.cs
Normal file
22
src/Robware.Auth/API/ApiKeyValidator.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
7
src/Robware.Auth/API/IApiKeyValidator.cs
Normal file
7
src/Robware.Auth/API/IApiKeyValidator.cs
Normal file
|
@ -0,0 +1,7 @@
|
|||
using System.Threading.Tasks;
|
||||
|
||||
namespace Robware.Auth.API {
|
||||
public interface IApiKeyValidator {
|
||||
Task<bool> Validate(string key);
|
||||
}
|
||||
}
|
7
src/Robware.Auth/API/IApiKeys.cs
Normal file
7
src/Robware.Auth/API/IApiKeys.cs
Normal file
|
@ -0,0 +1,7 @@
|
|||
using System.Threading.Tasks;
|
||||
|
||||
namespace Robware.Auth.API {
|
||||
public interface IApiKeys {
|
||||
Task<ApiKey> Get(string key);
|
||||
}
|
||||
}
|
|
@ -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;
|
|
@ -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()) {
|
|
@ -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);
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
namespace Robware.Auth {
|
||||
namespace Robware.Auth.Users {
|
||||
public interface ICryptographyProvider {
|
||||
string Encrypt(string input);
|
||||
}
|
|
@ -1,6 +1,6 @@
|
|||
using System.Threading.Tasks;
|
||||
|
||||
namespace Robware.Auth {
|
||||
namespace Robware.Auth.Users {
|
||||
public interface IUsers {
|
||||
Task<User> GetByEmail(string email);
|
||||
}
|
|
@ -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; }
|
|
@ -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) {
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue