185 lines
6.5 KiB
C#
185 lines
6.5 KiB
C#
using System.Reflection;
|
|
using System.Text;
|
|
using BlueWest.Cryptography;
|
|
using CodeLiturgy.Data.Application.Users;
|
|
using CodeLiturgy.Data.Auth;
|
|
using CodeLiturgy.Data.Auth.Context.Users;
|
|
using CodeLiturgy.Domain;
|
|
using BlueWest.WebApi.Context.Users;
|
|
using CodeLiturgy.Startup.Application;
|
|
using CodeLiturgy.Views.Utils;
|
|
using Microsoft.AspNetCore.Authentication.Cookies;
|
|
using Microsoft.AspNetCore.Identity;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.OpenApi.Models;
|
|
|
|
namespace CodeLiturgy.Views;
|
|
|
|
public static class StartupExtensions
|
|
{
|
|
|
|
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>()
|
|
}
|
|
});
|
|
|
|
});
|
|
}
|
|
|
|
private static string GetDbConnectionString(this IConfiguration configurationRoot, string key)
|
|
{
|
|
var startupMode = configurationRoot["mode"];
|
|
|
|
if (!string.IsNullOrEmpty(startupMode))
|
|
{
|
|
var config = configurationRoot.GetSection($"ConnectionString:{startupMode}")[key];
|
|
return config;
|
|
}
|
|
return String.Empty;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
services.AddAuthentication(options =>
|
|
{
|
|
options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
|
|
options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
|
|
options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
|
|
options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
|
|
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
|
|
})
|
|
.AddCookie(options =>
|
|
{
|
|
options.LoginPath = Routes.AuthLoginRoute;
|
|
options.LogoutPath = Routes.AuthLogoutRoute;
|
|
});
|
|
|
|
|
|
|
|
// api user claim policy
|
|
services.AddAuthorization(options =>
|
|
{
|
|
options.AddPolicy(SessionConstants.ApiNamePolicy,
|
|
policy => policy.RequireClaim(Constants.JwtClaimIdentifiers.Rol,
|
|
Constants.JwtClaims.ApiAccess));
|
|
options.AddPolicy(SessionConstants.CookieNamePolicy, policy =>
|
|
{
|
|
policy.RequireClaim(Constants.CookieClaims.CookieAccess);
|
|
});
|
|
});
|
|
|
|
// add identity
|
|
var identityBuilder = services.AddIdentityCore<ApplicationUser>(o =>
|
|
{
|
|
o.User.RequireUniqueEmail = true;
|
|
|
|
// configure identity options
|
|
o.Password.RequireDigit = false;
|
|
o.Password.RequireLowercase = false;
|
|
o.Password.RequireUppercase = false;
|
|
o.Password.RequireNonAlphanumeric = false;
|
|
o.Password.RequiredLength = 6;
|
|
})
|
|
.AddUserManager<ApplicationUserManager>()
|
|
.AddUserStore<UserRepository>();
|
|
|
|
identityBuilder =
|
|
new IdentityBuilder(identityBuilder.UserType, typeof(ApplicationRole), identityBuilder.Services);
|
|
identityBuilder
|
|
.AddEntityFrameworkStores<ApplicationUserDbContext>()
|
|
.AddDefaultTokenProviders();
|
|
|
|
return services;
|
|
}
|
|
|
|
|
|
private static DbContextOptionsBuilder UsePsqlConfiguration(this DbContextOptionsBuilder builder, IConfiguration configuration)
|
|
{
|
|
var connString = configuration.GetDbConnectionString("db");
|
|
builder.UseNpgsql(connString);
|
|
return builder;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Setup database Contexts
|
|
/// </summary>
|
|
/// <param name="serviceCollection"></param>
|
|
/// <param name="configuration"></param>
|
|
/// <param name="environment"></param>
|
|
/// <returns></returns>
|
|
public static IServiceCollection PreparePostgresqlDatabasePool(this IServiceCollection serviceCollection,
|
|
IConfiguration configuration, IWebHostEnvironment environment)
|
|
{
|
|
return serviceCollection
|
|
.AddDbContextPool<SiteDbContext>(options => options.UsePsqlConfiguration(configuration))
|
|
.AddDbContextPool<ApplicationUserDbContext>(options =>
|
|
options.UsePsqlConfiguration(configuration));
|
|
}
|
|
|
|
|
|
} |