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; }
|
2024-09-14 16:37:52 +02:00
|
|
|
public required string[] RedirectUris { get; init; }
|
2024-05-28 15:29:18 +02:00
|
|
|
|
2024-10-02 00:28:07 +02:00
|
|
|
public static Application Create(
|
|
|
|
ISnowflakeGenerator snowflakeGenerator,
|
|
|
|
string name,
|
|
|
|
string[] scopes,
|
|
|
|
string[] redirectUrls
|
|
|
|
)
|
2024-05-28 15:29:18 +02:00
|
|
|
{
|
2024-12-08 15:07:25 +01:00
|
|
|
string clientId = RandomNumberGenerator.GetHexString(32, true);
|
|
|
|
string 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
|
|
|
{
|
2024-10-02 00:28:07 +02:00
|
|
|
throw new ArgumentException(
|
|
|
|
"Invalid scopes passed to Application.Create",
|
|
|
|
nameof(scopes)
|
|
|
|
);
|
2024-05-28 15:29:18 +02:00
|
|
|
}
|
|
|
|
|
2024-07-08 19:03:04 +02:00
|
|
|
if (redirectUrls.Any(s => !AuthUtils.ValidateRedirectUri(s)))
|
2024-05-28 15:29:18 +02:00
|
|
|
{
|
2024-10-02 00:28:07 +02:00
|
|
|
throw new ArgumentException(
|
|
|
|
"Invalid redirect URLs passed to Application.Create",
|
|
|
|
nameof(redirectUrls)
|
|
|
|
);
|
2024-05-28 15:29:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return new Application
|
|
|
|
{
|
|
|
|
Id = snowflakeGenerator.GenerateSnowflake(),
|
|
|
|
ClientId = clientId,
|
|
|
|
ClientSecret = clientSecret,
|
|
|
|
Name = name,
|
|
|
|
Scopes = scopes,
|
2024-10-02 00:28:07 +02:00
|
|
|
RedirectUris = redirectUrls,
|
2024-05-28 15:29:18 +02:00
|
|
|
};
|
|
|
|
}
|
2024-10-02 00:28:07 +02:00
|
|
|
}
|