Add FingerprintManager
This commit is contained in:
25
Akari.Prototype.Server/Services/AuthManager.cs
Normal file
25
Akari.Prototype.Server/Services/AuthManager.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Akari.Prototype.Server.Services
|
||||
{
|
||||
public class AuthManager : IAuthManager
|
||||
{
|
||||
private readonly ILogger<AuthManager> _logger;
|
||||
private readonly IKeyManager _keyManager;
|
||||
|
||||
public AuthManager(ILogger<AuthManager> logger, IKeyManager keyManager)
|
||||
{
|
||||
_logger = logger;
|
||||
_keyManager = keyManager;
|
||||
}
|
||||
|
||||
public void Auth(byte[] token, string name)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
71
Akari.Prototype.Server/Services/FingerprintManager.cs
Normal file
71
Akari.Prototype.Server/Services/FingerprintManager.cs
Normal file
@@ -0,0 +1,71 @@
|
||||
using Akari.Prototype.Server.Options;
|
||||
using Akari.Prototype.Server.Utils;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text.Json;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Akari.Prototype.Server.Services
|
||||
{
|
||||
public class FingerprintManager : IFingerprintManager
|
||||
{
|
||||
public const string FingerprintsPath = "fingerprints.json";
|
||||
|
||||
private readonly ILogger<FingerprintManager> _logger;
|
||||
private readonly IOptions<AkariOptions> _akariOptions;
|
||||
private readonly IAuthManager _authManager;
|
||||
|
||||
/// <summary>
|
||||
/// Map token hash to user-friendly name
|
||||
/// </summary>
|
||||
private IDictionary<string, string> _fingerprintNames;
|
||||
|
||||
public FingerprintManager(ILogger<FingerprintManager> logger, IOptions<AkariOptions> akariOptions,
|
||||
IAuthManager authManager)
|
||||
{
|
||||
_logger = logger;
|
||||
_akariOptions = akariOptions;
|
||||
_authManager = authManager;
|
||||
|
||||
LoadFingerprints();
|
||||
}
|
||||
|
||||
private void LoadFingerprints()
|
||||
{
|
||||
var path = AkariPath.GetPath(FingerprintsPath);
|
||||
|
||||
// Create new
|
||||
if (!File.Exists(path))
|
||||
{
|
||||
_fingerprintNames = new Dictionary<string, string>();
|
||||
|
||||
File.WriteAllText(path, JsonSerializer.Serialize(_fingerprintNames));
|
||||
}
|
||||
// Load
|
||||
else
|
||||
{
|
||||
_fingerprintNames = JsonSerializer.Deserialize<IDictionary<string, string>>(File.ReadAllText(path));
|
||||
}
|
||||
}
|
||||
|
||||
public async Task VerifyToken(byte[] token)
|
||||
{
|
||||
var hash = Security.Argon2idHash(token);
|
||||
|
||||
_logger.LogDebug($"Verifying hash: {hash}");
|
||||
|
||||
if (!_fingerprintNames.TryGetValue(hash, out var name))
|
||||
{
|
||||
_logger.LogDebug($"Unknown hash, aborting: {hash}");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
_authManager.Auth(token, name);
|
||||
}
|
||||
}
|
||||
}
|
||||
12
Akari.Prototype.Server/Services/IAuthManager.cs
Normal file
12
Akari.Prototype.Server/Services/IAuthManager.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Akari.Prototype.Server.Services
|
||||
{
|
||||
public interface IAuthManager
|
||||
{
|
||||
void Auth(byte[] token, string name);
|
||||
}
|
||||
}
|
||||
12
Akari.Prototype.Server/Services/IFingerprintManager.cs
Normal file
12
Akari.Prototype.Server/Services/IFingerprintManager.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Akari.Prototype.Server.Services
|
||||
{
|
||||
public interface IFingerprintManager
|
||||
{
|
||||
Task VerifyToken(byte[] vs);
|
||||
}
|
||||
}
|
||||
11
Akari.Prototype.Server/Services/IKeyManager.cs
Normal file
11
Akari.Prototype.Server/Services/IKeyManager.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Akari.Prototype.Server.Services
|
||||
{
|
||||
public interface IKeyManager
|
||||
{
|
||||
}
|
||||
}
|
||||
11
Akari.Prototype.Server/Services/KeyManager.cs
Normal file
11
Akari.Prototype.Server/Services/KeyManager.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Akari.Prototype.Server.Services
|
||||
{
|
||||
public class KeyManager : IKeyManager
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -17,20 +17,23 @@ namespace Akari.Prototype.Server.Services
|
||||
public class TcpProviderService : BackgroundService
|
||||
{
|
||||
public const int BufferLength = 4096;
|
||||
public const int MaxTokenLength = 192;
|
||||
public const int MaxTokenLength = 24;
|
||||
public const int ReceiveTimeout = 500_000;
|
||||
|
||||
private readonly ILogger<TcpProviderService> _logger;
|
||||
private readonly TcpProviderOptions _options;
|
||||
private readonly IHostApplicationLifetime _hostApplicationLifetime;
|
||||
private readonly IFingerprintManager _fingerprintManager;
|
||||
private Task _backgroundTask;
|
||||
private TcpListener _listener;
|
||||
|
||||
public TcpProviderService(ILogger<TcpProviderService> logger, IOptions<TcpProviderOptions> options, IHostApplicationLifetime hostApplicationLifetime)
|
||||
public TcpProviderService(ILogger<TcpProviderService> logger, IOptions<TcpProviderOptions> options,
|
||||
IHostApplicationLifetime hostApplicationLifetime, IFingerprintManager fingerprintManager)
|
||||
{
|
||||
_logger = logger;
|
||||
_options = options.Value;
|
||||
_hostApplicationLifetime = hostApplicationLifetime;
|
||||
_fingerprintManager = fingerprintManager;
|
||||
}
|
||||
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
@@ -117,8 +120,9 @@ namespace Akari.Prototype.Server.Services
|
||||
position += read;
|
||||
}
|
||||
|
||||
// TODO: Send token to AuthManager that will compare it to stored hash using Argon2
|
||||
_logger.LogDebug($"Received token: {Convert.ToBase64String(data[..position].Span)}");
|
||||
|
||||
await _fingerprintManager.VerifyToken(data[..position].ToArray());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user