cloutier--bird.makeup/src/BirdsiteLive.Domain/CryptoService.cs

53 lines
1.7 KiB
C#
Raw Normal View History

using System;
using System.Text;
using BirdsiteLive.Domain.Factories;
2020-06-06 00:14:42 -04:00
namespace BirdsiteLive.Domain
{
public interface ICryptoService
{
string GetUserPem(string id);
2020-06-28 23:42:23 -04:00
string SignAndGetSignatureHeader(DateTime date, string actor, string host, string inbox = null);
2020-06-06 00:14:42 -04:00
}
public class CryptoService : ICryptoService
{
private readonly IMagicKeyFactory _magicKeyFactory;
#region Ctor
public CryptoService(IMagicKeyFactory magicKeyFactory)
{
_magicKeyFactory = magicKeyFactory;
}
#endregion
public string GetUserPem(string id)
{
return _magicKeyFactory.GetMagicKey().AsPEM;
}
/// <summary>
///
/// </summary>
/// <param name="data"></param>
/// <param name="actor">in the form of https://domain.io/actor</param>
/// <param name="host">in the form of domain.io</param>
/// <returns></returns>
2020-06-28 23:42:23 -04:00
public string SignAndGetSignatureHeader(DateTime date, string actor, string targethost, string inbox = null)
{
2020-06-28 23:42:23 -04:00
var usedInbox = "/inbox";
if (!string.IsNullOrWhiteSpace(inbox))
usedInbox = inbox;
var httpDate = date.ToString("r");
2020-06-28 23:42:23 -04:00
var signedString = $"(request-target): post {usedInbox}\nhost: {targethost}\ndate: {httpDate}";
var signedStringBytes = Encoding.UTF8.GetBytes(signedString);
var signature = _magicKeyFactory.GetMagicKey().Sign(signedStringBytes);
var sig64 = Convert.ToBase64String(signature);
var header = "keyId=\"" + actor + "\",headers=\"(request-target) host date\",signature=\"" + sig64 + "\"";
return header;
}
2020-06-06 00:14:42 -04:00
}
}