2020-06-15 23:15:59 +00:00
|
|
|
using System.Security.Claims;
|
|
|
|
|
|
|
|
using PluralKit.Core;
|
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
namespace PluralKit.API;
|
|
|
|
|
|
|
|
public static class AuthExt
|
2020-06-15 23:15:59 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
public static SystemId CurrentSystem(this ClaimsPrincipal user)
|
2020-06-15 23:15:59 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
var claim = user.FindFirst(PKClaims.SystemId);
|
|
|
|
if (claim == null) throw new ArgumentException("User is unauthorized");
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
if (int.TryParse(claim.Value, out var id))
|
|
|
|
return new SystemId(id);
|
|
|
|
throw new ArgumentException("User has non-integer system ID claim");
|
|
|
|
}
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public static LookupContext ContextFor(this ClaimsPrincipal user, PKSystem system)
|
|
|
|
{
|
|
|
|
if (!user.Identity.IsAuthenticated) return LookupContext.API;
|
|
|
|
return system.Id == user.CurrentSystem() ? LookupContext.ByOwner : LookupContext.API;
|
|
|
|
}
|
2020-06-15 23:15:59 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public static LookupContext ContextFor(this ClaimsPrincipal user, PKMember member)
|
|
|
|
{
|
|
|
|
if (!user.Identity.IsAuthenticated) return LookupContext.API;
|
|
|
|
return member.System == user.CurrentSystem() ? LookupContext.ByOwner : LookupContext.API;
|
2020-06-15 23:15:59 +00:00
|
|
|
}
|
|
|
|
}
|