Add FingerprintManager

This commit is contained in:
2021-06-03 20:19:42 +02:00
parent 1c6b545e6b
commit adcde19346
14 changed files with 240 additions and 5 deletions

View File

@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
namespace Akari.Prototype.Server.Options
{
public class AkariOptions
{
public const string FilePath = "akari.json";
public const string SectionPath = "Akari";
[Required]
public string DataPath { get; set; }
}
}

View File

@@ -1,7 +1,9 @@
using Akari.Prototype.Server.Options;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.IO;
@@ -15,7 +17,17 @@ namespace Akari.Prototype.Server
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
var host = CreateHostBuilder(args).Build();
var services = host.Services;
var akariOptions = services.GetRequiredService<IOptions<AkariOptions>>().Value;
if (!Directory.Exists(akariOptions.DataPath))
{
Directory.CreateDirectory(akariOptions.DataPath);
}
host.Run();
}
// Additional configuration is required to successfully run gRPC on macOS.

View 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)
{
}
}
}

View 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);
}
}
}

View 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);
}
}

View 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);
}
}

View 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
{
}
}

View 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
{
}
}

View File

@@ -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());
}
}
}

View File

@@ -32,6 +32,10 @@ namespace Akari.Prototype.Server
.Bind(Configuration.GetSection(TcpProviderOptions.SectionPath))
.ValidateDataAnnotations();
services.AddSingleton<IFingerprintManager, FingerprintManager>();
services.AddSingleton<IAuthManager, AuthManager>();
services.AddSingleton<IKeyManager, KeyManager>();
services.AddHostedService<TcpProviderService>();
}

View File

@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
namespace Akari.Prototype.Server.Utils
{
public static class AkariPath
{
public const string BasePath = "Data";
public static string GetPath(string path) => Path.Combine(BasePath, path);
}
}

View File

@@ -11,7 +11,7 @@ namespace Akari.Prototype.Server.Utils
{
public IPAddressAttribute() : base("Must be a valid IP address")
{
}
public override bool IsValid(object value)

View File

@@ -0,0 +1,35 @@
using Isopoh.Cryptography.Argon2;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Threading.Tasks;
namespace Akari.Prototype.Server.Utils
{
public static class Security
{
public const int HashLength = 32;
public const int SaltLength = 12;
public static string Argon2idHash(byte[] password)
{
var salt = new byte[8];
RandomNumberGenerator.Fill(salt);
var config = new Argon2Config()
{
HashLength = 32,
Lanes = Environment.ProcessorCount / 2,
Threads = Environment.ProcessorCount / 2,
Password = password,
Salt = salt,
Type = Argon2Type.HybridAddressing,
Version = Argon2Version.Nineteen
};
return Argon2.Hash(config);
}
}
}

View File

@@ -0,0 +1,6 @@
{
"Akari":
{
"DataPath": "Data"
}
}