2023-09-17 21:41:31 +02:00
|
|
|
using System;
|
|
|
|
using System.Linq;
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
using Microsoft.AspNetCore.Mvc.Filters;
|
2023-10-09 01:58:53 +02:00
|
|
|
using SharpRSS.API.Contracts.Models;
|
|
|
|
using SharpRSS.API.Data;
|
2023-10-09 21:05:38 +02:00
|
|
|
using SharpRSS.API.Models;
|
2023-09-17 21:41:31 +02:00
|
|
|
using ToolQit;
|
2023-10-09 01:58:53 +02:00
|
|
|
using ToolQit.Extensions;
|
2023-09-17 21:41:31 +02:00
|
|
|
using ToolQit.Logging;
|
|
|
|
|
|
|
|
namespace SharpRSS.API.Auth
|
|
|
|
{
|
|
|
|
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
|
|
|
|
public class SessionAuthorizeAttribute : Attribute, IAuthorizationFilter
|
|
|
|
{
|
2023-10-09 01:58:53 +02:00
|
|
|
public SessionAuthorizeAttribute(bool admin = true)
|
2023-09-17 21:41:31 +02:00
|
|
|
{
|
2023-10-09 01:58:53 +02:00
|
|
|
_admin = admin;
|
2023-09-17 21:41:31 +02:00
|
|
|
_log = LogManager.CreateLogger(typeof(SessionAuthorizeAttribute));
|
|
|
|
}
|
2023-10-09 01:58:53 +02:00
|
|
|
|
2023-09-17 21:41:31 +02:00
|
|
|
private readonly ILog _log;
|
2023-10-09 01:58:53 +02:00
|
|
|
private readonly bool _admin;
|
2023-10-09 21:05:38 +02:00
|
|
|
public void OnAuthorization(AuthorizationFilterContext context)
|
2023-09-17 21:41:31 +02:00
|
|
|
{
|
|
|
|
if (context.ActionDescriptor.EndpointMetadata.Any(obj => obj.GetType() == typeof(AllowAnonymousAttribute)))
|
2023-10-09 01:58:53 +02:00
|
|
|
return;
|
|
|
|
var authService = context.HttpContext.RequestServices.GetService(typeof(AuthService)) as AuthService;
|
|
|
|
if (authService == null)
|
2023-09-17 21:41:31 +02:00
|
|
|
{
|
2023-10-09 01:58:53 +02:00
|
|
|
context.Result = new UnauthorizedObjectResult(new Result("Failed to initialize service!", ResultStatus.InternalFail));
|
2023-09-17 21:41:31 +02:00
|
|
|
return;
|
|
|
|
}
|
2023-09-21 20:51:16 +02:00
|
|
|
|
2023-10-09 01:58:53 +02:00
|
|
|
if (context.HttpContext.Request.Headers.TryGetValue("SRSS-Session", out var val))
|
2023-09-21 20:51:16 +02:00
|
|
|
{
|
2023-10-09 01:58:53 +02:00
|
|
|
string? headerVal = val.ToString();
|
|
|
|
if (headerVal == null || headerVal.IsNullEmptyWhiteSpace())
|
|
|
|
{
|
|
|
|
context.Result = new UnauthorizedObjectResult(new Result("Invalid session ID"));
|
|
|
|
return;
|
|
|
|
}
|
2023-10-09 21:05:38 +02:00
|
|
|
var authSetResult = authService.ValidateSession(headerVal).Result;
|
|
|
|
if (!authSetResult.Success || authSetResult.Value == null)
|
2023-10-09 01:58:53 +02:00
|
|
|
{
|
|
|
|
context.Result = new UnauthorizedResult();
|
|
|
|
return;
|
|
|
|
}
|
2023-10-09 21:05:38 +02:00
|
|
|
if (authSetResult.Value.Session is { Expired: true })
|
2023-10-09 01:58:53 +02:00
|
|
|
{
|
|
|
|
context.Result = new UnauthorizedObjectResult(new Result("Session expired", ResultStatus.Failed));
|
|
|
|
return;
|
|
|
|
}
|
2023-10-09 21:05:38 +02:00
|
|
|
if (!authSetResult.Value.User.Active)
|
|
|
|
{
|
|
|
|
context.Result = new UnauthorizedObjectResult(new Result(
|
|
|
|
"User is not active, contact your administrator to enable this account!", ResultStatus.Failed));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
authSetResult.Value.AdminRequired = _admin;
|
|
|
|
context.RouteData.Values.Add(nameof(AuthorizationSet), authSetResult.Value);
|
2023-09-21 20:51:16 +02:00
|
|
|
return;
|
|
|
|
}
|
2023-09-17 21:41:31 +02:00
|
|
|
context.Result = new UnauthorizedResult();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|