using System; using Luticate2.Auth.Attributes.EntityAccessors; using Luticate2.Auth.Business; using Luticate2.Auth.DataAccess; using Luticate2.Auth.Dbo.Basic; using Luticate2.Auth.Dbo.Users; using Luticate2.Auth.Interfaces.Groups; using Luticate2.Auth.Interfaces.Permissions; using Luticate2.Auth.Interfaces.Tokens; using Luticate2.Auth.Interfaces.Users; using Luticate2.Auth.Middlewares; using Luticate2.Utils.Controllers; using Luticate2.Utils.Dbo.Basic; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; namespace Luticate2.Auth.Controllers { public static class LuAuthExtensions { public const string RoutePrefix = "luticate"; public const string LuticateItemsLoggedUser = "loggedUser"; public const string TokenCookieName = "luticate2-token"; public static IServiceCollection AddLuticateAuth(this IServiceCollection services, Action utilsOptionsDelegate, Action optionsAction, Action authOptionsDelegate) { services.AddLuticateUtils(utilsOptionsDelegate); services.TryAddSingleton(); services.TryAddScoped(); services.TryAddScoped(); services.TryAddScoped(); services.TryAddTransient(); services.TryAddTransient(); services.TryAddTransient(); services.TryAddTransient(); services.TryAddTransient(); services.TryAddTransient(); services.TryAddTransient(); services.TryAddTransient(); services.TryAddTransient(); services.AddDbContext(options => { options.UseInternalServiceProvider(new ServiceCollection() .AddEntityFrameworkNpgsqlLuticate() .AddEntityFrameworkNpgsql() .BuildServiceProvider()); optionsAction.Invoke(options); }, ServiceLifetime.Transient); services.Configure(authOptionsDelegate); return services; } public static IMvcBuilder AddLuticateAuth(this IMvcBuilder builder) { builder.AddLuticateUtils(); builder.Services.Configure( options => { options.Filters.Add(typeof(LuLoggedUserMiddleware)); options.Filters.Add(typeof(LuPermissionMiddleware)); }); return builder; } public static IApplicationBuilder UseLuticateAuth(this IApplicationBuilder app) { app.UseLuticateUtils(); return app; } public static LuUsersFullDbo GetLuLoggedUser(this HttpContext context) { return context.GetLuItems()[LuticateItemsLoggedUser] as LuUsersFullDbo; } public static void SetLuLoggedUser(this HttpContext context, LuUsersFullDbo user) { context.GetLuItems()[LuticateItemsLoggedUser] = user; } public static string GetLuUserToken(this HttpContext context) { var token = context.Request.Cookies[TokenCookieName]; return string.IsNullOrWhiteSpace(token) ? null : token; } } }