last ...
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using IdentityModel;
|
||||
using System.Threading.Tasks;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
|
||||
namespace xIdentityService.Authorization
|
||||
{
|
||||
public class XApiKeyScopeRequirement : IAuthorizationRequirement
|
||||
{
|
||||
public IReadOnlyCollection<string> Scopes { get; }
|
||||
|
||||
public XApiKeyScopeRequirement(IEnumerable<string> scopes)
|
||||
{
|
||||
Scopes = [.. scopes ?? []];
|
||||
}
|
||||
}
|
||||
|
||||
public class XApiKeyScopeHandler : AuthorizationHandler<XApiKeyScopeRequirement>
|
||||
{
|
||||
protected override Task HandleRequirementAsync(
|
||||
AuthorizationHandlerContext context,
|
||||
XApiKeyScopeRequirement requirement
|
||||
)
|
||||
{
|
||||
//
|
||||
if (!requirement.Scopes.Any())
|
||||
{
|
||||
//
|
||||
context.Succeed(requirement);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
//
|
||||
var userScopes = new HashSet<string>(
|
||||
context.User
|
||||
.FindAll(JwtClaimTypes.Scope)
|
||||
.SelectMany(c => c.Value.Split([' '], StringSplitOptions.RemoveEmptyEntries)),
|
||||
StringComparer.OrdinalIgnoreCase
|
||||
);
|
||||
|
||||
//
|
||||
if (requirement.Scopes.All(s => userScopes.Contains(s)))
|
||||
{
|
||||
context.Succeed(requirement);
|
||||
}
|
||||
|
||||
//
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user