This repository has been archived on 2023-05-27. You can view files and clone it, but cannot push or open issues or pull requests.
BirdsiteLIVE/src/BirdsiteLive.Domain/Factories/MagicKeyFactory.cs
Sam Therapy e2ec4a5857
Some checks failed
continuous-integration/drone Build is failing
format things, I think
also prepare pipelines and stuff

Signed-off-by: Sam Therapy <sam@samtherapy.net>
2022-11-15 17:25:54 +01:00

41 lines
945 B
C#

using System.IO;
using BirdsiteLive.Cryptography;
namespace BirdsiteLive.Domain.Factories
{
public interface IMagicKeyFactory
{
MagicKey GetMagicKey();
}
public class MagicKeyFactory : IMagicKeyFactory
{
private const string Path = "key.json";
private static MagicKey _magicKey;
#region Ctor
public MagicKeyFactory() { }
#endregion
public MagicKey GetMagicKey()
{
//Cached key
if (_magicKey != null)
return _magicKey;
//Generate key if needed
if (!File.Exists(Path))
{
var key = MagicKey.Generate();
File.WriteAllText(Path, key.PrivateKey);
}
//Load and return key
var serializedKey = File.ReadAllText(Path);
_magicKey = new MagicKey(serializedKey);
return _magicKey;
}
}
}