2024-05-20 10:46:31 -05:00
using API.Authentication ;
using API.Authentication.Interfaces ;
2024-07-09 18:03:05 -05:00
using API.Hashing ;
using API.Hashing.Interfaces ;
2024-04-22 16:54:28 -05:00
using API.Services ;
2024-07-09 18:03:42 -05:00
using API.Services.Interfaces ;
2024-04-16 18:38:11 -05:00
using DAL.Contexts ;
2024-07-09 18:03:42 -05:00
using DAL.Models ;
2024-07-12 18:05:54 -05:00
using Microsoft.AspNetCore.Authentication.Cookies ;
2024-04-16 18:38:11 -05:00
using Microsoft.EntityFrameworkCore ;
2024-04-22 18:04:14 -05:00
using Serilog ;
2024-04-16 18:38:11 -05:00
using System.Reflection ;
2024-07-09 18:03:42 -05:00
using ConfigurationManager = Microsoft . Extensions . Configuration . ConfigurationManager ;
2024-04-16 18:38:11 -05:00
using InvalidOperationException = System . InvalidOperationException ;
namespace API
{
2024-08-19 16:08:35 -05:00
internal static class Program
2024-04-16 18:38:11 -05:00
{
2024-08-19 16:08:35 -05:00
public static IServiceCollection AddLazyResolution ( this IServiceCollection services )
{
return services . AddTransient (
typeof ( Lazy < > ) ,
typeof ( LazilyResolved < > ) ) ;
}
private class LazilyResolved < T > : Lazy < T >
{
public LazilyResolved ( IServiceProvider serviceProvider )
: base ( serviceProvider . GetRequiredService < T > )
{
}
}
2024-04-16 18:38:11 -05:00
public static void Main ( string [ ] args )
{
WebApplicationBuilder builder = WebApplication . CreateBuilder ( args ) ;
ConfigurationManager configManager = builder . Configuration ;
builder . Services . AddControllers ( ) ;
builder . Services . AddEndpointsApiExplorer ( ) ;
builder . Services . AddSwaggerGen ( options = >
{
string xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml" ;
string xmlPath = Path . Combine ( AppContext . BaseDirectory , xmlFile ) ;
options . IncludeXmlComments ( xmlPath , true ) ;
} ) ;
2024-04-22 18:04:14 -05:00
builder . Host . UseSerilog ( ( context , configuration ) = > configuration . ReadFrom . Configuration ( context . Configuration ) ) ;
2024-04-22 16:54:28 -05:00
builder . Services . AddDbContext < SASGContext > ( options = > { options . UseMySQL ( builder . Configuration [ "connectionString" ] ? ? throw new InvalidOperationException ( "Connection String is null" ) ) ; } ) ;
builder . Services . AddTransient < ColorService > ( ) ;
2024-04-22 18:04:14 -05:00
builder . Services . AddTransient < EventService > ( ) ;
builder . Services . AddTransient < GrantService > ( ) ;
builder . Services . AddTransient < ImageService > ( ) ;
builder . Services . AddTransient < PermissionService > ( ) ;
builder . Services . AddTransient < SavedEventService > ( ) ;
2024-08-31 18:38:07 -05:00
builder . Services . AddTransient < SignupService > ( ) ;
2024-07-12 17:27:01 -05:00
builder . Services . AddTransient < UserService > ( options = >
{
ILogger < UserService > logger = options . GetRequiredService < ILogger < UserService > > ( ) ;
SASGContext context = options . GetRequiredService < SASGContext > ( ) ;
2024-07-15 13:23:58 -05:00
IUserAuthentication authentication = options . GetRequiredService < IUserAuthentication > ( ) ;
2024-07-12 17:27:01 -05:00
PermissionService permissionService = options . GetRequiredService < PermissionService > ( ) ;
ulong defaultUserPermission = UInt64 . Parse ( builder . Configuration [ "defaultUserPermission" ] ? ? throw new InvalidOperationException ( "defaultUserPermission is null" ) ) ;
return new UserService ( logger , context , authentication , permissionService , defaultUserPermission ) ;
} ) ;
2024-04-16 18:38:11 -05:00
2024-05-20 10:46:31 -05:00
builder . Services . AddTransient < IColorAuthentication , ColorAuthentication > ( ) ;
2024-07-15 13:23:58 -05:00
builder . Services . AddTransient < IEventAuthentication , EventAuthentication > ( ) ;
builder . Services . AddTransient < IGrantAuthentication , GrantAuthentication > ( ) ;
builder . Services . AddTransient < IImageAuthentication , ImageAuthentication > ( ) ;
builder . Services . AddTransient < IColorAuthentication , ColorAuthentication > ( ) ;
2024-08-19 16:08:35 -05:00
builder . Services . AddTransient < IPermissionAuthentication , PermissionAuthentication > ( ) ;
builder . Services . AddTransient < ISavedEventAuthentication , SavedEventAuthentication > ( ) ;
builder . Services . AddTransient < IUserAuthentication , UserAuthentication > ( ) ;
2024-08-31 18:38:07 -05:00
builder . Services . AddTransient < ISignupAuthentication , SignupAuthentication > ( ) ;
2024-05-20 10:46:31 -05:00
2024-07-09 18:03:05 -05:00
builder . Services . AddTransient < IHashingFactory , HashingFactory > ( ) ;
builder . Services . AddTransient < IHashingAlgorithm , Pbkdf2 > ( ) ;
2024-08-19 16:08:35 -05:00
builder . Services . AddTransient < IGrantManager , GrantManager > ( ) ;
2024-07-09 18:03:42 -05:00
builder . Services . AddTransient < IUserManager , UserManager > ( options = >
{
UserService userService = options . GetRequiredService < UserService > ( ) ;
IHashingFactory hashingFactory = options . GetRequiredService < IHashingFactory > ( ) ;
ILogger < UserManager > logger = options . GetRequiredService < ILogger < UserManager > > ( ) ;
HashingType hashingType ;
if ( ! Enum . TryParse ( builder . Configuration [ "preferredHashingType" ] , out hashingType ) )
throw new InvalidOperationException ( $"preferredHashingType not one of {String.Join(" , ", Enum.GetNames(typeof(HashingType)))}" ) ;
return new UserManager ( userService , hashingFactory , logger , hashingType ) ;
} ) ;
2024-07-12 18:05:54 -05:00
2024-08-31 20:59:42 -05:00
builder . Services . AddAuthentication ( CookieAuthenticationDefaults . AuthenticationScheme ) . AddCookie ( options = >
{
options . Events . OnRedirectToAccessDenied = context = >
{
context . Response . StatusCode = 403 ;
return Task . CompletedTask ;
} ;
} ) ;
2024-08-31 18:38:07 -05:00
// builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie(options =>
// {
// options.Cookie.SameSite = SameSiteMode.None;
// options.Cookie.SecurePolicy = CookieSecurePolicy.Always;
// });
2024-08-30 00:25:46 -05:00
2024-08-19 16:08:35 -05:00
builder . Services . AddLazyResolution ( ) ;
2024-07-12 18:05:54 -05:00
2024-04-16 18:38:11 -05:00
WebApplication app = builder . Build ( ) ;
if ( app . Environment . IsDevelopment ( ) )
{
app . UseSwagger ( ) ;
app . UseSwaggerUI ( ) ;
}
app . UseCookiePolicy ( new CookiePolicyOptions
{
2024-08-31 18:38:07 -05:00
MinimumSameSitePolicy = SameSiteMode . Strict
2024-04-16 18:38:11 -05:00
} ) ;
2024-07-12 18:05:54 -05:00
app . UseAuthorization ( ) ;
2024-08-30 00:25:46 -05:00
app . UseCors ( builder = > {
builder . AllowAnyOrigin ( ) . AllowAnyMethod ( ) . AllowAnyHeader ( ) ;
} ) ;
2024-07-12 18:05:54 -05:00
2024-04-16 18:38:11 -05:00
app . UseHttpsRedirection ( ) ;
app . MapControllers ( ) ;
app . Run ( ) ;
}
}
2024-04-22 16:54:28 -05:00
}