CodeLiturgy.Dashboard/CodeLiturgy.Views/StartupExtensions.cs

185 lines
6.5 KiB
C#
Raw Normal View History

2022-11-26 01:35:47 +03:00
using System.Reflection;
2022-09-29 02:37:24 +03:00
using System.Text;
using BlueWest.Cryptography;
2022-11-18 03:15:53 +03:00
using CodeLiturgy.Data.Application.Users;
using CodeLiturgy.Data.Auth;
using CodeLiturgy.Data.Auth.Context.Users;
2022-10-30 19:48:24 +03:00
using CodeLiturgy.Domain;
2022-09-29 02:37:24 +03:00
using BlueWest.WebApi.Context.Users;
2022-11-26 01:35:47 +03:00
using CodeLiturgy.Startup.Application;
2022-10-30 19:48:24 +03:00
using CodeLiturgy.Views.Utils;
2022-09-29 02:37:24 +03:00
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Identity;
2022-11-13 19:53:18 +03:00
using Microsoft.EntityFrameworkCore;
2022-11-26 01:35:47 +03:00
using Microsoft.OpenApi.Models;
2022-09-29 02:37:24 +03:00
2022-10-30 19:48:24 +03:00
namespace CodeLiturgy.Views;
2022-09-29 02:37:24 +03:00
public static class StartupExtensions
{
2022-11-26 01:35:47 +03:00
public static IServiceCollection ConfigureSwagger(this IServiceCollection serviceCollection)
{
return serviceCollection
.AddSwaggerGen(options =>
{
options.SchemaFilter<SwaggerEnumSchemaFilter>();
options.SwaggerDoc("v1", new OpenApiInfo
{
Title = "CodeLiturgy.Views.App",
Version = "v1"
});
// Set the comments path for the Swagger JSON and UI.
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
options.IncludeXmlComments(xmlPath);
options.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
{
Description =
"JWT Authorization header using the Bearer scheme (Example: 'Bearer 12345abcdef')",
Name = "Authorization",
In = ParameterLocation.Header,
Type = SecuritySchemeType.ApiKey,
Scheme = "Bearer"
});
options.AddSecurityRequirement(new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "Bearer"
}
},
Array.Empty<string>()
}
});
});
}
2022-09-29 02:37:24 +03:00
2022-11-18 03:15:53 +03:00
private static string GetDbConnectionString(this IConfiguration configurationRoot, string key)
2022-09-29 02:37:24 +03:00
{
var startupMode = configurationRoot["mode"];
2022-10-27 20:13:02 +03:00
2022-11-18 03:15:53 +03:00
if (!string.IsNullOrEmpty(startupMode))
2022-09-29 02:37:24 +03:00
{
2022-11-18 03:15:53 +03:00
var config = configurationRoot.GetSection($"ConnectionString:{startupMode}")[key];
2022-09-29 02:37:24 +03:00
return config;
}
2022-11-18 03:15:53 +03:00
return String.Empty;
2022-10-27 20:13:02 +03:00
}
2022-09-29 02:37:24 +03:00
2022-10-27 20:13:02 +03:00
internal static IServiceCollection AddAuthServerServices(this IServiceCollection services,
IConfiguration configuration, IWebHostEnvironment environment)
{
services.AddSession(options =>
{
options.Cookie.Domain = SessionConstants.CookieDomain;
options.Cookie.HttpOnly = true;
options.IdleTimeout = TimeSpan.FromHours(8);
});
services
.AddScoped<UserRepository>()
.AddScoped<IUserManager, ApplicationUserManager>()
.AddScoped<IAuthManager, AuthManager>()
.AddScoped<IHasher, Hasher>();
// Database Context and Swagger
// Register the ConfigurationBuilder instance of AuthSettings
var authSettings = configuration.GetSection(nameof(AuthSettings));
services.Configure<AuthSettings>(authSettings);
2022-09-29 02:37:24 +03:00
2022-10-27 20:13:02 +03:00
services.AddAuthentication(options =>
2022-09-29 02:37:24 +03:00
{
2022-10-27 20:13:02 +03:00
options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
2022-11-18 03:33:03 +03:00
options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
2022-10-27 20:13:02 +03:00
options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
2022-11-18 03:33:03 +03:00
options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
2022-10-27 20:13:02 +03:00
})
.AddCookie(options =>
2022-09-29 02:37:24 +03:00
{
2022-10-27 20:13:02 +03:00
options.LoginPath = Routes.AuthLoginRoute;
options.LogoutPath = Routes.AuthLogoutRoute;
2022-09-29 02:37:24 +03:00
});
2022-11-18 03:33:03 +03:00
2022-09-29 02:37:24 +03:00
2022-10-27 20:13:02 +03:00
// api user claim policy
services.AddAuthorization(options =>
{
options.AddPolicy(SessionConstants.ApiNamePolicy,
2022-10-30 19:48:24 +03:00
policy => policy.RequireClaim(Constants.JwtClaimIdentifiers.Rol,
Constants.JwtClaims.ApiAccess));
2022-11-17 00:17:44 +03:00
options.AddPolicy(SessionConstants.CookieNamePolicy, policy =>
{
policy.RequireClaim(Constants.CookieClaims.CookieAccess);
});
2022-10-27 20:13:02 +03:00
});
// add identity
var identityBuilder = services.AddIdentityCore<ApplicationUser>(o =>
2022-09-29 02:37:24 +03:00
{
o.User.RequireUniqueEmail = true;
2022-10-27 20:13:02 +03:00
2022-09-29 02:37:24 +03:00
// configure identity options
o.Password.RequireDigit = false;
o.Password.RequireLowercase = false;
o.Password.RequireUppercase = false;
o.Password.RequireNonAlphanumeric = false;
o.Password.RequiredLength = 6;
})
2022-10-27 20:13:02 +03:00
.AddUserManager<ApplicationUserManager>()
.AddUserStore<UserRepository>();
identityBuilder =
new IdentityBuilder(identityBuilder.UserType, typeof(ApplicationRole), identityBuilder.Services);
identityBuilder
.AddEntityFrameworkStores<ApplicationUserDbContext>()
.AddDefaultTokenProviders();
return services;
}
2022-11-13 19:53:18 +03:00
private static DbContextOptionsBuilder UsePsqlConfiguration(this DbContextOptionsBuilder builder, IConfiguration configuration)
2022-10-27 20:13:02 +03:00
{
2022-11-18 03:15:53 +03:00
var connString = configuration.GetDbConnectionString("db");
2022-11-13 19:53:18 +03:00
builder.UseNpgsql(connString);
return builder;
2022-10-27 20:13:02 +03:00
}
2022-09-29 02:37:24 +03:00
2022-11-13 14:27:48 +03:00
2022-10-27 20:13:02 +03:00
/// <summary>
/// Setup database Contexts
/// </summary>
/// <param name="serviceCollection"></param>
/// <param name="configuration"></param>
/// <param name="environment"></param>
/// <returns></returns>
2022-11-13 14:27:48 +03:00
public static IServiceCollection PreparePostgresqlDatabasePool(this IServiceCollection serviceCollection,
2022-10-27 20:13:02 +03:00
IConfiguration configuration, IWebHostEnvironment environment)
{
2022-11-13 19:53:18 +03:00
return serviceCollection
2022-11-17 00:17:44 +03:00
.AddDbContextPool<SiteDbContext>(options => options.UsePsqlConfiguration(configuration))
2022-11-13 14:27:48 +03:00
.AddDbContextPool<ApplicationUserDbContext>(options =>
2022-11-13 19:53:18 +03:00
options.UsePsqlConfiguration(configuration));
2022-10-27 20:13:02 +03:00
}
2022-11-13 14:27:48 +03:00
2022-09-29 02:37:24 +03:00
}