2021-11-17 10:23:49 -05:00
// Copyright Epic Games, Inc. All Rights Reserved.
2022-06-20 07:40:23 -04:00
using System.Collections.Generic ;
2021-11-17 08:36:23 -05:00
using Microsoft.AspNetCore.Mvc ;
using System.ComponentModel.DataAnnotations ;
using System.Threading.Tasks ;
using Microsoft.AspNetCore.Authorization ;
2022-01-31 13:38:05 -05:00
using EpicGames.Horde.Storage ;
2022-06-20 07:40:23 -04:00
using Jupiter.Common ;
using Microsoft.Extensions.Options ;
2021-11-17 08:36:23 -05:00
2022-10-12 06:36:30 -04:00
namespace Jupiter.Controllers
2021-11-17 08:36:23 -05:00
{
2023-07-27 11:20:47 -04:00
[ApiController]
[Route("api/v1/auth")]
[Authorize]
public class AuthController : ControllerBase
{
private readonly IRequestHelper _requestHelper ;
private readonly INamespacePolicyResolver _namespacePolicyResolver ;
private readonly IOptionsMonitor < AuthSettings > _authSettings ;
2021-11-17 08:36:23 -05:00
2023-07-27 11:20:47 -04:00
public AuthController ( IRequestHelper requestHelper , INamespacePolicyResolver namespacePolicyResolver , IOptionsMonitor < AuthSettings > authSettings )
{
_requestHelper = requestHelper ;
_namespacePolicyResolver = namespacePolicyResolver ;
_authSettings = authSettings ;
}
2021-11-17 08:36:23 -05:00
2023-07-27 11:20:47 -04:00
[HttpGet("{ns}")]
2023-08-10 08:22:50 -04:00
public async Task < IActionResult > VerifyAsync (
2023-07-27 11:20:47 -04:00
[FromRoute] [ Required ] NamespaceId ns
)
{
2023-08-10 23:09:40 -04:00
ActionResult ? result = await _requestHelper . HasAccessToNamespaceAsync ( User , Request , ns , new [ ] { JupiterAclAction . ReadObject } ) ;
2023-07-27 11:20:47 -04:00
if ( result ! = null )
{
return result ;
}
2021-11-17 08:36:23 -05:00
2023-07-27 11:20:47 -04:00
return Ok ( ) ;
}
2022-06-20 07:40:23 -04:00
2023-07-27 11:20:47 -04:00
[HttpGet("{ns}/actions")]
public IActionResult Actions (
[FromRoute] [ Required ] NamespaceId ns
)
{
NamespacePolicy policy = _namespacePolicyResolver . GetPoliciesForNs ( ns ) ;
2022-06-20 07:40:23 -04:00
2023-07-27 11:20:47 -04:00
List < JupiterAclAction > allowedActions = new List < JupiterAclAction > ( ) ;
foreach ( AclEntry acl in policy . Acls )
{
allowedActions . AddRange ( acl . Resolve ( User ) ) ;
}
2022-06-20 07:40:23 -04:00
2023-07-27 11:20:47 -04:00
// the root and namespace acls are combined, namespace acls can not override what we define in the root
foreach ( AclEntry acl in _authSettings . CurrentValue . Acls )
{
allowedActions . AddRange ( acl . Resolve ( User ) ) ;
}
2022-06-20 07:40:23 -04:00
2023-07-27 11:20:47 -04:00
return Ok ( new JsonResult ( new { Actions = allowedActions } ) ) ;
}
}
2022-06-20 07:40:23 -04:00
2023-07-27 11:20:47 -04:00
public class ActionsResult
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "CA2227:Collection properties should be read only", Justification = "Used by serialization")]
public List < JupiterAclAction > Actions { get ; set ; } = new List < JupiterAclAction > ( ) ;
}
2021-11-17 08:36:23 -05:00
}