2026-10-01 08:05:56 +02:00
|
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
2023-02-18 08:52:17 +01:00
|
|
|
using Microsoft.AspNetCore.ResponseCompression;
|
|
|
|
|
|
2023-02-19 00:43:43 +01:00
|
|
|
using PrivaPub.ClientModels;
|
|
|
|
|
using PrivaPub.Extensions;
|
|
|
|
|
using PrivaPub.Models;
|
|
|
|
|
using PrivaPub.Services;
|
|
|
|
|
using PrivaPub.StaticServices;
|
2023-02-18 08:52:17 +01:00
|
|
|
|
|
|
|
|
using System.Text.Json.Serialization;
|
2026-10-01 08:05:56 +02:00
|
|
|
using Microsoft.OpenApi;
|
|
|
|
|
using PrivaPub.Services.ClientToServer.Private;
|
2023-02-19 00:43:43 +01:00
|
|
|
using PrivaPub.Services.ClientToServer.Public;
|
2026-10-01 08:05:56 +02:00
|
|
|
using PrivaPub.Services.Federation;
|
2023-02-18 08:52:17 +01:00
|
|
|
|
2023-02-19 00:43:43 +01:00
|
|
|
namespace PrivaPub.Middleware
|
2023-02-18 08:52:17 +01:00
|
|
|
{
|
2023-02-19 00:43:43 +01:00
|
|
|
public static class PrivaPubConfigurations
|
2023-02-18 08:52:17 +01:00
|
|
|
{
|
2023-02-19 00:43:43 +01:00
|
|
|
public static IServiceCollection PrivaPubAppSettingsConfiguration(this IServiceCollection service, IConfiguration configuration)
|
2023-02-18 08:52:17 +01:00
|
|
|
{
|
|
|
|
|
return service
|
|
|
|
|
.Configure<MongoSettings>(configuration.GetSection(nameof(MongoSettings)))
|
|
|
|
|
.Configure<AppConfiguration>(configuration.GetSection(nameof(AppConfiguration)));
|
|
|
|
|
}
|
2023-02-19 00:43:43 +01:00
|
|
|
public static IServiceCollection PrivaPubWorkersConfiguration(this IServiceCollection service)
|
2023-02-18 08:52:17 +01:00
|
|
|
{
|
|
|
|
|
return service;
|
|
|
|
|
//.AddHostedService<DiscussionsWorker>()
|
|
|
|
|
//.AddHostedService<GroupsCleanerWorker>()
|
|
|
|
|
//.AddHostedService<PoliciesCleanerWorker>();
|
|
|
|
|
}
|
2026-10-01 08:05:56 +02:00
|
|
|
public static IServiceCollection PrivaPubFederationConfiguration(this IServiceCollection service)
|
2023-02-18 08:52:17 +01:00
|
|
|
{
|
2026-10-01 08:05:56 +02:00
|
|
|
service.AddHttpClient(RemoteActorService.HttpClientName, client =>
|
|
|
|
|
{
|
|
|
|
|
client.Timeout = TimeSpan.FromSeconds(20);
|
|
|
|
|
client.DefaultRequestHeaders.UserAgent.ParseAdd($"PrivaPub/{BuildInfo.Ref}");
|
|
|
|
|
});
|
|
|
|
|
return service
|
|
|
|
|
.AddSingleton<ILocalActorService, LocalActorService>()
|
|
|
|
|
.AddSingleton<IRemoteActorService, RemoteActorService>()
|
|
|
|
|
.AddSingleton<IDeliveryService, DeliveryService>()
|
|
|
|
|
.AddTransient<IInboxService, InboxService>()
|
|
|
|
|
.AddHostedService<DeliveryWorker>();
|
2023-02-18 08:52:17 +01:00
|
|
|
}
|
2023-02-19 00:43:43 +01:00
|
|
|
public static IServiceCollection PrivaPubAuthServicesConfiguration(this IServiceCollection service, IConfiguration configuration)
|
2023-02-18 08:52:17 +01:00
|
|
|
{
|
|
|
|
|
return service
|
|
|
|
|
.AddAuthorization(options =>
|
|
|
|
|
{
|
|
|
|
|
options.AddPolicy(Policies.IsUser, Extensions.Extensions.IsUserPolicy());
|
|
|
|
|
options.AddPolicy(Policies.IsAdmin, Extensions.Extensions.IsAdminPolicy());
|
|
|
|
|
options.AddPolicy(Policies.IsModerator, Extensions.Extensions.IsModeratorPolicy());
|
|
|
|
|
})
|
|
|
|
|
.AddAuthentication(options =>
|
|
|
|
|
{
|
|
|
|
|
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
|
|
|
|
|
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
|
|
|
|
|
options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
|
|
|
|
|
})
|
2023-02-19 00:43:43 +01:00
|
|
|
.AddPrivaPubAuth(configuration)
|
2023-02-18 08:52:17 +01:00
|
|
|
.Services
|
|
|
|
|
.AddSingleton<AuthTokenManager>()
|
|
|
|
|
.AddSingleton<IPasswordHasher, PasswordHasher>();
|
|
|
|
|
}
|
2023-02-19 00:43:43 +01:00
|
|
|
public static IServiceCollection PrivaPubInternalizationConfiguration(this IServiceCollection service, IConfiguration configuration)
|
2023-02-18 08:52:17 +01:00
|
|
|
{
|
|
|
|
|
return service
|
|
|
|
|
.AddLocalization()
|
|
|
|
|
.AddSingleton<RequestLocalizationOptionsService>();
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-19 00:43:43 +01:00
|
|
|
public static IServiceCollection PrivaPubOptimizationConfiguration(this IServiceCollection service)
|
2023-02-18 08:52:17 +01:00
|
|
|
{
|
|
|
|
|
return service.AddResponseCompression(opts =>
|
|
|
|
|
{
|
|
|
|
|
opts.Providers.Add<BrotliCompressionProvider>();
|
|
|
|
|
opts.MimeTypes = ResponseCompressionDefaults.MimeTypes.Concat(new[] { "application/octet-stream" });
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-19 00:43:43 +01:00
|
|
|
public static IServiceCollection PrivaPubDataBaseConfiguration(this IServiceCollection service)
|
2023-02-18 08:52:17 +01:00
|
|
|
{
|
|
|
|
|
return service.AddSingleton<DbEntities>();
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-19 00:43:43 +01:00
|
|
|
public static IServiceCollection PrivaPubServicesConfiguration(this IServiceCollection service)
|
2023-02-18 08:52:17 +01:00
|
|
|
{
|
|
|
|
|
return service
|
|
|
|
|
.AddTransient<IDataService, DataService>()
|
|
|
|
|
.AddTransient<IRootUsersService, RootUsersService>()
|
2023-02-19 00:43:43 +01:00
|
|
|
.AddTransient<IPublicAvatarUsersService, PublicAvatarUsersService>()
|
2026-10-01 08:05:56 +02:00
|
|
|
.AddTransient<IPrivateAvatarUsersService, PrivateAvatarUsersService>()
|
|
|
|
|
.AddTransient<IGroupUsersService, GroupUsersService>()
|
|
|
|
|
.AddTransient<IPostsService, PostsService>()
|
2023-02-18 08:52:17 +01:00
|
|
|
.AddSingleton<AppConfigurationService>()
|
|
|
|
|
.AddHttpContextAccessor()
|
|
|
|
|
.AddMemoryCache()
|
|
|
|
|
.AddSingleton<IPasswordHasher, PasswordHasher>();
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-19 00:43:43 +01:00
|
|
|
public static IServiceCollection PrivaPubMiddlewareConfiguration(this IServiceCollection service)
|
2023-02-18 08:52:17 +01:00
|
|
|
{
|
|
|
|
|
return service
|
|
|
|
|
.AddEndpointsApiExplorer()
|
|
|
|
|
.AddSwaggerGen(c =>
|
|
|
|
|
{
|
2026-10-01 08:05:56 +02:00
|
|
|
c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
|
2023-02-18 08:52:17 +01:00
|
|
|
{
|
|
|
|
|
In = ParameterLocation.Header,
|
|
|
|
|
Description = "Please enter a valid token",
|
|
|
|
|
Name = "Authorization",
|
|
|
|
|
Type = SecuritySchemeType.Http,
|
|
|
|
|
BearerFormat = "JWT",
|
2026-10-01 08:05:56 +02:00
|
|
|
Scheme = "bearer"
|
2023-02-18 08:52:17 +01:00
|
|
|
});
|
2026-10-01 08:05:56 +02:00
|
|
|
c.AddSecurityRequirement(document => new OpenApiSecurityRequirement
|
2023-02-18 08:52:17 +01:00
|
|
|
{
|
2026-10-01 08:05:56 +02:00
|
|
|
[new OpenApiSecuritySchemeReference("Bearer", document)] = []
|
2023-02-18 08:52:17 +01:00
|
|
|
});
|
|
|
|
|
})
|
|
|
|
|
.AddControllers(options => { options.Filters.Add<OperationCancelledExceptionFilter>(); })
|
|
|
|
|
.AddJsonOptions(options =>
|
|
|
|
|
{
|
|
|
|
|
options.JsonSerializerOptions.IgnoreReadOnlyFields = false;
|
|
|
|
|
options.JsonSerializerOptions.IgnoreReadOnlyProperties = false;
|
|
|
|
|
options.JsonSerializerOptions.PropertyNameCaseInsensitive = true;
|
|
|
|
|
options.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
|
|
|
|
|
options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
|
|
|
|
|
}).Services;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-19 00:43:43 +01:00
|
|
|
public static IServiceCollection PrivaPubCORSConfiguration(this IServiceCollection service)
|
2023-02-18 08:52:17 +01:00
|
|
|
{
|
|
|
|
|
return service.AddCors(options =>
|
|
|
|
|
{
|
|
|
|
|
options.DefaultPolicyName = "DefaultCORS";
|
|
|
|
|
options.AddDefaultPolicy(configure =>
|
|
|
|
|
{
|
|
|
|
|
configure.AllowAnyMethod()
|
|
|
|
|
.AllowAnyHeader()
|
|
|
|
|
.AllowAnyOrigin()
|
|
|
|
|
.AllowAnyMethod()
|
|
|
|
|
.DisallowCredentials();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|