2024-05-19 17:20:45 +02:00
|
|
|
using System.Security.Cryptography;
|
|
|
|
using Foxchat.Core.Utils;
|
|
|
|
using NodaTime;
|
|
|
|
|
2024-05-11 15:26:47 +02:00
|
|
|
namespace Foxchat.Identity.Database.Models;
|
|
|
|
|
2024-05-19 17:20:45 +02:00
|
|
|
public class Token : BaseModel
|
2024-05-11 15:26:47 +02:00
|
|
|
{
|
2024-05-19 17:20:45 +02:00
|
|
|
public byte[] Hash { get; set; } = null!;
|
|
|
|
public string[] Scopes { get; set; } = [];
|
|
|
|
public Instant Expires { get; set; }
|
|
|
|
|
2024-05-20 17:00:21 +02:00
|
|
|
// Tokens can be granted directly to applications with `client_credentials`
|
|
|
|
public Ulid? AccountId { get; set; }
|
|
|
|
public Account? Account { get; set; }
|
2024-05-19 17:20:45 +02:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2024-05-20 20:37:22 +02:00
|
|
|
|
|
|
|
public static (string, Token) Create(Account? account, Application application, string[] scopes, Instant expires)
|
|
|
|
{
|
|
|
|
var (token, hash) = Generate();
|
|
|
|
return (token, new()
|
|
|
|
{
|
|
|
|
Hash = hash,
|
|
|
|
Scopes = scopes,
|
|
|
|
Expires = expires,
|
|
|
|
Account = account,
|
|
|
|
Application = application,
|
|
|
|
});
|
|
|
|
}
|
2024-05-19 17:20:45 +02:00
|
|
|
}
|