144 lines
3.6 KiB
C#
144 lines
3.6 KiB
C#
using Microsoft.AspNetCore.Authentication;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.Extensions.Logging;
|
|
using System.Security.Claims;
|
|
using System.Threading.Tasks;
|
|
using zero.Core.Api;
|
|
using zero.Core.Entities;
|
|
using zero.Core.Identity;
|
|
using zero.Core.Options;
|
|
using zero.Core.Routing;
|
|
|
|
namespace zero.Core
|
|
{
|
|
public class ZeroContext : IZeroContext
|
|
{
|
|
/// <inheritdoc />
|
|
public IApplication App { get; protected set; }
|
|
|
|
/// <inheritdoc />
|
|
public string AppId { get; protected set; }
|
|
|
|
/// <inheritdoc />
|
|
public ClaimsPrincipal BackofficeUser { get; protected set; }
|
|
|
|
/// <inheritdoc />
|
|
public ClaimsIdentity BackofficeIdentity { get; protected set; }
|
|
|
|
/// <inheritdoc />
|
|
public bool IsBackofficeRequest { get; protected set; }
|
|
|
|
/// <inheritdoc />
|
|
public IZeroOptions Options { get; protected set; }
|
|
|
|
/// <inheritdoc />
|
|
public IRoute Route { get; private set; }
|
|
|
|
/// <inheritdoc />
|
|
public IResolvedRoute ResolvedRoute { get; private set; }
|
|
|
|
|
|
protected IApplicationContext AppContext { get; private set; }
|
|
|
|
protected ILogger<ZeroContext> Logger { get; private set; }
|
|
|
|
|
|
private bool _resolved = false;
|
|
|
|
|
|
public ZeroContext(IZeroOptions options, IApplicationContext appContext, ILogger<ZeroContext> logger)
|
|
{
|
|
Options = options;
|
|
AppContext = appContext;
|
|
Logger = logger;
|
|
}
|
|
|
|
|
|
/// <inheritdoc />
|
|
public async virtual Task Resolve(HttpContext context)
|
|
{
|
|
if (_resolved || context?.Request is null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
_resolved = true;
|
|
|
|
AuthenticateResult authResult = await context.AuthenticateAsync(Constants.Auth.BackofficeScheme);
|
|
if (authResult?.Principal is not null)
|
|
{
|
|
BackofficeUser = authResult.Principal;
|
|
if (BackofficeUserIdentity.TryGet(authResult.Principal, out BackofficeUserIdentity identity))
|
|
{
|
|
BackofficeIdentity = identity;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
BackofficeUser = new ClaimsPrincipal();
|
|
}
|
|
|
|
App = await AppContext.Resolve(context, BackofficeUser);
|
|
AppId = App.Id;
|
|
|
|
IsBackofficeRequest = AppContext.IsBackofficeRequest(context);
|
|
|
|
if (IsBackofficeRequest is false && context.Request.RouteValues.TryGetValue("zero.route", out object route))
|
|
{
|
|
ResolvedRoute = (IResolvedRoute)route;
|
|
Route = ResolvedRoute.Route;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
public interface IZeroContext
|
|
{
|
|
/// <summary>
|
|
/// Currently loaded application
|
|
/// </summary>
|
|
IApplication App { get; }
|
|
|
|
/// <summary>
|
|
/// Current loaded application Id
|
|
/// </summary>
|
|
string AppId { get; }
|
|
|
|
/// <summary>
|
|
/// Resolved backoffice user principal
|
|
/// </summary>
|
|
ClaimsPrincipal BackofficeUser { get; }
|
|
|
|
/// <summary>
|
|
/// Resolved backoffice user identity
|
|
/// </summary>
|
|
ClaimsIdentity BackofficeIdentity { get; }
|
|
|
|
/// <summary>
|
|
/// Whether the current request is a backoffice request or not
|
|
/// </summary>
|
|
bool IsBackofficeRequest { get; }
|
|
|
|
/// <summary>
|
|
/// Global zero options
|
|
/// </summary>
|
|
IZeroOptions Options { get; }
|
|
|
|
/// <summary>
|
|
/// Matching (frontend) path route
|
|
/// </summary>
|
|
IRoute Route { get; }
|
|
|
|
/// <summary>
|
|
/// Matching (frontend) resolved route
|
|
/// </summary>
|
|
IResolvedRoute ResolvedRoute { get; }
|
|
|
|
/// <summary>
|
|
/// Resolves the current application (for backoffice + frontend requests) and
|
|
/// the currently active backoffice user, as users are not signed in with the default scheme and do therefore not populate HttpContext.User
|
|
/// </summary>
|
|
Task Resolve(HttpContext context);
|
|
}
|
|
}
|