init
This commit is contained in:
		
						commit
						f6629fbb33
					
				
					 32 changed files with 1608 additions and 0 deletions
				
			
		
							
								
								
									
										44
									
								
								Foxchat.Core/Database/IDatabaseContext.cs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										44
									
								
								Foxchat.Core/Database/IDatabaseContext.cs
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,44 @@
 | 
			
		|||
using System.Security.Cryptography;
 | 
			
		||||
using Microsoft.EntityFrameworkCore;
 | 
			
		||||
 | 
			
		||||
namespace Foxchat.Core.Database;
 | 
			
		||||
 | 
			
		||||
public abstract class IDatabaseContext : DbContext
 | 
			
		||||
{
 | 
			
		||||
    public virtual DbSet<Instance> Instance { get; set; }
 | 
			
		||||
 | 
			
		||||
    public async ValueTask<bool> InitializeInstanceAsync(CancellationToken ct = default)
 | 
			
		||||
    {
 | 
			
		||||
        var instance = await Instance.Where(i => i.Id == 1).FirstOrDefaultAsync(ct);
 | 
			
		||||
        if (instance != null) return false;
 | 
			
		||||
 | 
			
		||||
        var rsa = RSA.Create();
 | 
			
		||||
        var publicKey = rsa.ExportRSAPublicKeyPem();
 | 
			
		||||
        var privateKey = rsa.ExportRSAPrivateKeyPem();
 | 
			
		||||
 | 
			
		||||
        await Instance.AddAsync(new Instance
 | 
			
		||||
        {
 | 
			
		||||
            PublicKey = publicKey!,
 | 
			
		||||
            PrivateKey = privateKey!,
 | 
			
		||||
        }, ct);
 | 
			
		||||
 | 
			
		||||
        await SaveChangesAsync(ct);
 | 
			
		||||
        return true;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public async Task<Instance> GetInstanceAsync(CancellationToken ct = default)
 | 
			
		||||
    {
 | 
			
		||||
        var instance = await Instance.FirstOrDefaultAsync(ct)
 | 
			
		||||
            ?? throw new Exception("GetInstanceAsync called without Instance being initialized"); // TODO: replace this with specific exception type
 | 
			
		||||
        return instance;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public async Task<RSA> GetInstanceKeysAsync(CancellationToken ct = default)
 | 
			
		||||
    {
 | 
			
		||||
        var instance = await GetInstanceAsync(ct);
 | 
			
		||||
 | 
			
		||||
        var rsa = RSA.Create();
 | 
			
		||||
        rsa.ImportFromPem(instance.PrivateKey);
 | 
			
		||||
        return rsa;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										8
									
								
								Foxchat.Core/Database/Instance.cs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								Foxchat.Core/Database/Instance.cs
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,8 @@
 | 
			
		|||
namespace Foxchat.Core.Database;
 | 
			
		||||
 | 
			
		||||
public class Instance
 | 
			
		||||
{
 | 
			
		||||
    public int Id { get; init; }
 | 
			
		||||
    public string PublicKey { get; set; } = null!;
 | 
			
		||||
    public string PrivateKey { get; set; } = null!;
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue