356 lines
9.6 KiB
C#
356 lines
9.6 KiB
C#
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Http.Extensions;
|
|
using Microsoft.Extensions.Logging;
|
|
using Raven.Client.Documents;
|
|
using Raven.Client.Documents.Session;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Security.Claims;
|
|
using System.Threading.Tasks;
|
|
using zero.Core.Database;
|
|
using zero.Core.Entities;
|
|
using zero.Core.Extensions;
|
|
using zero.Core.Handlers;
|
|
using zero.Core.Identity;
|
|
using zero.Core.Options;
|
|
|
|
namespace zero.Core.Api
|
|
{
|
|
public class ApplicationContext : IApplicationContext
|
|
{
|
|
/// <inheritdoc />
|
|
public IApplication App { get; protected set; }
|
|
|
|
/// <inheritdoc />
|
|
public string AppId { get; protected set; }
|
|
|
|
protected IZeroStore Store { get; private set; }
|
|
|
|
protected IZeroOptions Options { get; private set; }
|
|
|
|
protected ILogger<ApplicationContext> Logger { get; private set; }
|
|
|
|
protected IHandlerHolder Handler { get; private set; }
|
|
|
|
static IList<IApplication> Apps { get; set; }
|
|
|
|
|
|
|
|
public ApplicationContext(IZeroStore store, IZeroOptions options, ILogger<ApplicationContext> logger, IHandlerHolder handler = null)
|
|
{
|
|
Store = store;
|
|
Options = options;
|
|
Logger = logger;
|
|
Handler = handler;
|
|
}
|
|
|
|
|
|
/// <inheritdoc />
|
|
public async Task<IApplication> Resolve(HttpContext context, ClaimsPrincipal user)
|
|
{
|
|
if (context?.Request == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
IApplication app;
|
|
|
|
if (IsBackofficeRequest(context))
|
|
{
|
|
app = await ResolveFromUser(user);
|
|
}
|
|
else
|
|
{
|
|
app = await ResolveFromRequest(context);
|
|
}
|
|
|
|
if (app == null)
|
|
{
|
|
//Logger.LogWarning("Could not resolve application for host {host}", context.Request.Host);
|
|
IList<IApplication> apps = await GetApplications();
|
|
app = apps.FirstOrDefault();
|
|
}
|
|
|
|
App = app;
|
|
AppId = app?.Id;
|
|
|
|
return app;
|
|
}
|
|
|
|
|
|
/// <inheritdoc />
|
|
public async Task<bool> TrySwitchForUser(IBackofficeUser user, string appId)
|
|
{
|
|
if (user == null || appId.IsNullOrEmpty())
|
|
{
|
|
return false;
|
|
}
|
|
|
|
string[] allowedAppIds = GetAllowedAppIdsForUser(user);
|
|
|
|
bool isMainId = false; // appId.Equals(user.AppId, StringComparison.InvariantCultureIgnoreCase); // TODO appx fix
|
|
bool isAllowedId = allowedAppIds.Contains(appId, StringComparer.InvariantCultureIgnoreCase);
|
|
|
|
if (user.IsSuper || isMainId || isAllowedId)
|
|
{
|
|
user.CurrentAppId = appId;
|
|
|
|
//byte[] bytes = new byte[20];
|
|
//RandomNumberGenerator.Fill(bytes);
|
|
//user.SecurityStamp = Base32.ToBase32(bytes); // TODO update security stamp but Base32 is .net core internal
|
|
|
|
using IAsyncDocumentSession session = Store.OpenAsyncSession();
|
|
await session.StoreAsync(user);
|
|
await session.SaveChangesAsync();
|
|
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
|
|
/// <inheritdoc />
|
|
public async Task<IApplication> ResolveFromUser(ClaimsPrincipal user)
|
|
{
|
|
IBackofficeUser userEntity = await GetBackofficeUser(user);
|
|
return await ResolveFromUser(userEntity);
|
|
}
|
|
|
|
|
|
/// <inheritdoc />
|
|
public async Task<IApplication> ResolveFromUser(IBackofficeUser user)
|
|
{
|
|
if (user == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
string appId = null;
|
|
string[] allowedAppIds = GetAllowedAppIdsForUser(user);
|
|
|
|
if (!user.CurrentAppId.IsNullOrEmpty())
|
|
{
|
|
if (user.IsSuper || allowedAppIds.Contains(user.CurrentAppId, StringComparer.InvariantCultureIgnoreCase))
|
|
{
|
|
appId = user.CurrentAppId;
|
|
}
|
|
else
|
|
{
|
|
//appId = user.AppId; // TODO appx fix
|
|
}
|
|
}
|
|
else
|
|
{
|
|
//appId = user.AppId; // TODO appx fix
|
|
}
|
|
|
|
if (appId.IsNullOrEmpty())
|
|
{
|
|
throw new Exception($"User entity ${user.Id} needs a valid AppId");
|
|
}
|
|
|
|
using IAsyncDocumentSession session = Store.OpenAsyncSession();
|
|
return await session.LoadAsync<Application>(appId);
|
|
}
|
|
|
|
|
|
/// <inheritdoc />
|
|
public async Task<IApplication> ResolveFromRequest(HttpContext context)
|
|
{
|
|
return Handler.Get<IApplicationResolverHandler>()?.Resolve(context.Request, await GetApplications()) ?? await ResolveFromUri(context.Request.GetEncodedUrl());
|
|
}
|
|
|
|
|
|
/// <inheritdoc />
|
|
public async Task<IApplication> ResolveFromUri(string uriString)
|
|
{
|
|
return ResolveFromUriInternal(new Uri(uriString, UriKind.Absolute), await GetApplications());
|
|
}
|
|
|
|
|
|
/// <inheritdoc />
|
|
public async Task<IApplication> ResolveFromUri(Uri uri)
|
|
{
|
|
return ResolveFromUriInternal(uri, await GetApplications());
|
|
}
|
|
|
|
|
|
/// <inheritdoc />
|
|
public async Task<IApplicationContext> ForId(string appId)
|
|
{
|
|
using IAsyncDocumentSession session = Store.OpenAsyncSession();
|
|
IApplication app = await session.LoadAsync<Application>(appId);
|
|
|
|
return new ApplicationContext(Store, Options, Logger, Handler)
|
|
{
|
|
App = app,
|
|
AppId = appId
|
|
};
|
|
}
|
|
|
|
|
|
/// <inheritdoc />
|
|
public bool IsBackofficeRequest(HttpContext context)
|
|
{
|
|
string path = Options.BackofficePath.EnsureStartsWith('/').TrimEnd('/');
|
|
return context.Request.Path.ToString().StartsWith(path);
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Get matching application from an URI
|
|
/// </summary>
|
|
IApplication ResolveFromUriInternal(Uri uri, IList<IApplication> apps)
|
|
{
|
|
string[] protocols = new string[3] { "https://", "http://", "//" };
|
|
|
|
IApplication currentApp = null;
|
|
|
|
foreach (IApplication app in apps)
|
|
{
|
|
if (app.Domains?.Length < 1)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
foreach (Uri domain in app.Domains)
|
|
{
|
|
//string normalizedDomain = domain;
|
|
|
|
//if (!protocols.Any(protocol => domain.StartsWith(protocol, StringComparison.OrdinalIgnoreCase)))
|
|
//{
|
|
// normalizedDomain = "http://" + domain;
|
|
//}
|
|
|
|
//UriBuilder uriBuilder = new UriBuilder(normalizedDomain);
|
|
|
|
//if (!uriBuilder.Uri.IsAbsoluteUri)
|
|
//{
|
|
// continue;
|
|
//}
|
|
|
|
int compareResult = Uri.Compare(uri, domain, UriComponents.HostAndPort, UriFormat.SafeUnescaped, StringComparison.OrdinalIgnoreCase);
|
|
|
|
if (compareResult == 0)
|
|
{
|
|
currentApp = app;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
return currentApp;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Get all applications to choose from
|
|
/// </summary>
|
|
async Task<IList<IApplication>> GetApplications()
|
|
{
|
|
if (Apps != null)
|
|
{
|
|
return Apps;
|
|
}
|
|
using (IAsyncDocumentSession session = Store.OpenAsyncSession())
|
|
{
|
|
Apps = await session.Query<IApplication>().ToListAsync();
|
|
return Apps;
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Get backoffice user from claims principal
|
|
/// </summary>
|
|
async Task<IBackofficeUser> GetBackofficeUser(ClaimsPrincipal user)
|
|
{
|
|
string userId = user.FindFirstValue(Constants.Auth.Claims.UserId);
|
|
|
|
using IAsyncDocumentSession session = Store.OpenAsyncSession();
|
|
return await session.LoadAsync<IBackofficeUser>(userId);
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Get applications the user has access to
|
|
/// </summary>
|
|
string[] GetAllowedAppIdsForUser(IBackofficeUser user)
|
|
{
|
|
IEnumerable<Permission> permissions = user.Claims
|
|
.Where(claim => claim.Type == Constants.Auth.Claims.Permission && claim.Value.StartsWith(Permissions.Applications))
|
|
.Select(x => Permission.FromClaim(x.ToClaim(), Permissions.Applications));
|
|
|
|
string[] appIds = permissions.Where(x => x.IsTrue).Select(x => x.NormalizedKey).ToArray();
|
|
|
|
return appIds;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public interface IApplicationContext
|
|
{
|
|
/// <summary>
|
|
/// Currently loaded application
|
|
/// </summary>
|
|
IApplication App { get; }
|
|
|
|
/// <summary>
|
|
/// Current loaded application Id
|
|
/// </summary>
|
|
string AppId { get; }
|
|
|
|
/// <summary>
|
|
/// Resolves the current application from either the backoffice user (in case it is backoffice request)
|
|
/// or the domain (in case it is frontend request).
|
|
/// The resolved data is stored in the App + AppId properties.
|
|
/// </summary>
|
|
Task<IApplication> Resolve(HttpContext context, ClaimsPrincipal user);
|
|
|
|
/// <summary>
|
|
/// Try to switch the current application for a user
|
|
/// </summary>
|
|
Task<bool> TrySwitchForUser(IBackofficeUser user, string appId);
|
|
|
|
/// <summary>
|
|
/// Resolves the current application from the request path
|
|
/// </summary>
|
|
Task<IApplication> ResolveFromRequest(HttpContext context);
|
|
|
|
/// <summary>
|
|
/// Get matching application from an URI string
|
|
/// </summary>
|
|
Task<IApplication> ResolveFromUri(string uriString);
|
|
|
|
/// <summary>
|
|
/// Get matching application from an URI
|
|
/// </summary>
|
|
Task<IApplication> ResolveFromUri(Uri uri);
|
|
|
|
/// <summary>
|
|
/// Resolves the current application from the logged-in backoffice user.
|
|
/// This method won't return apps the user has no access to.
|
|
/// </summary>
|
|
Task<IApplication> ResolveFromUser(ClaimsPrincipal user);
|
|
|
|
/// <summary>
|
|
/// Resolves the current application from a user.
|
|
/// This method won't return apps the user has no access to.
|
|
/// </summary>
|
|
Task<IApplication> ResolveFromUser(IBackofficeUser user);
|
|
|
|
/// <summary>
|
|
/// Creates a new application context for the specified application.
|
|
/// </summary>
|
|
Task<IApplicationContext> ForId(string appId);
|
|
|
|
/// <summary>
|
|
/// Whether the current request is a backoffice request
|
|
/// </summary>
|
|
bool IsBackofficeRequest(HttpContext context);
|
|
}
|
|
}
|