Foxchat.NET/Foxchat.Identity/Database/Models/Token.cs

27 lines
768 B
C#

using System.Security.Cryptography;
using Foxchat.Core.Utils;
using NodaTime;
namespace Foxchat.Identity.Database.Models;
public class Token : BaseModel
{
public byte[] Hash { get; set; } = null!;
public string[] Scopes { get; set; } = [];
public Instant Expires { get; set; }
// Tokens can be granted directly to applications with `client_credentials`
public Ulid? AccountId { get; set; }
public Account? Account { get; set; }
public Ulid ApplicationId { get; set; }
public Application Application { get; set; } = null!;
public static (string, byte[]) Generate()
{
var token = CryptoUtils.RandomToken(48);
var hash = SHA512.HashData(Convert.FromBase64String(token));
return (token, hash);
}
}