2024-05-28 15:29:18 +02:00
|
|
|
using System.Security.Cryptography;
|
|
|
|
using Foxnouns.Backend.Utils;
|
|
|
|
|
|
|
|
namespace Foxnouns.Backend.Database.Models;
|
|
|
|
|
|
|
|
public class Application : BaseModel
|
|
|
|
{
|
|
|
|
public required string ClientId { get; init; }
|
|
|
|
public required string ClientSecret { get; init; }
|
|
|
|
public required string Name { get; init; }
|
|
|
|
public required string[] Scopes { get; init; }
|
|
|
|
public required string[] RedirectUris { get; set; }
|
|
|
|
|
|
|
|
public static Application Create(ISnowflakeGenerator snowflakeGenerator, string name, string[] scopes,
|
|
|
|
string[] redirectUrls)
|
|
|
|
{
|
|
|
|
var clientId = RandomNumberGenerator.GetHexString(32, true);
|
2024-07-08 19:03:04 +02:00
|
|
|
var clientSecret = AuthUtils.RandomToken();
|
2024-05-28 15:29:18 +02:00
|
|
|
|
2024-07-08 19:03:04 +02:00
|
|
|
if (scopes.Except(AuthUtils.ApplicationScopes).Any())
|
2024-05-28 15:29:18 +02:00
|
|
|
{
|
|
|
|
throw new ArgumentException("Invalid scopes passed to Application.Create", nameof(scopes));
|
|
|
|
}
|
|
|
|
|
2024-07-08 19:03:04 +02:00
|
|
|
if (redirectUrls.Any(s => !AuthUtils.ValidateRedirectUri(s)))
|
2024-05-28 15:29:18 +02:00
|
|
|
{
|
|
|
|
throw new ArgumentException("Invalid redirect URLs passed to Application.Create", nameof(redirectUrls));
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Application
|
|
|
|
{
|
|
|
|
Id = snowflakeGenerator.GenerateSnowflake(),
|
|
|
|
ClientId = clientId,
|
|
|
|
ClientSecret = clientSecret,
|
|
|
|
Name = name,
|
|
|
|
Scopes = scopes,
|
|
|
|
RedirectUris = redirectUrls
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|