using Microsoft.AspNetCore.Authorization;

namespace CloudBuilder.Core.Authorization
{
    public class AuthorizationHandler : IAuthorizationHandler, IAuthorize
    {
        /// <summary>
        /// 身份认证Authorize
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public virtual Task HandleAsync(AuthorizationHandlerContext context)
        {
            if (context.HasFailed)
            {
                return Task.CompletedTask;
            }

            // 判断身份认证是否通过
            var isAuthenticated = context.User.Identity.IsAuthenticated;
            if (isAuthenticated)
            {
                context.Succeed(context.PendingRequirements.FirstOrDefault()!);
                return Task.CompletedTask;
            }

            context.Fail();

            return Task.CompletedTask;
        }
    }
}
