SocialPub/PrivaPub/Middleware/SocialPubConfigurations.cs

217 lines
7.5 KiB
C#
Raw Normal View History

2023-02-18 08:52:17 +01:00
using Microsoft.AspNetCore.Authentication.JwtBearer;
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;
using NSign.Providers;
using NSign;
using NSign.Signatures;
using NSign.Client;
using System.Text;
using Microsoft.OpenApi.Models;
2023-02-19 00:43:43 +01:00
using PrivaPub.Services.ClientToServer.Public;
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>();
}
2023-02-19 00:43:43 +01:00
public static IServiceCollection PrivaPubHTTPSignature(this IServiceCollection service, IConfiguration configuration)
2023-02-18 08:52:17 +01:00
{
//HTTP CLIENT
service.Configure<AddDigestOptions>(options => options.WithHash(AddDigestOptions.Hash.Sha256))
.ConfigureMessageSigningOptions(options =>
{
2023-02-19 00:43:43 +01:00
options.SignatureName = "PrivaPub";
2023-02-18 08:52:17 +01:00
options
.WithMandatoryComponent(SignatureComponent.Path)
.WithMandatoryComponent(SignatureComponent.RequestTarget)
.SetParameters = signatureParams => signatureParams.WithKeyId("keyId");
})
.Services.Configure<SignatureVerificationOptions>(options =>
{
})
.AddHttpClient<ActivityPubClient>(nameof(ActivityPubClient))
.ConfigureHttpClient(httpClient =>
{
httpClient.DefaultRequestHeaders.Accept.Add(new("application/ld+json"));
})
.AddDigestAndSigningHandlers()
//.AddSignatureVerifiationHandler()
.Services
.AddSingleton<ISigner>(new HmacSha256SignatureProvider(Encoding.UTF8.GetBytes(configuration["AppConfiguration:Jwt:Key"])));
//MESSAGE RESPONSE
return service;
//.Configure<RequestSignatureVerificationOptions>(options =>
//{
// options.SignaturesToVerify.Add("sample");
// options.RequiredSignatureComponents.Add(SignatureComponent.Path);
// options.RequiredSignatureComponents.Add(SignatureComponent.Method);
// options.RequiredSignatureComponents.Add(SignatureComponent.Digest);
//})
//.AddSignatureVerification(serviceProvider =>
//{
// var memoryCache = serviceProvider.GetRequiredService<IMemoryCache>();
// //var httpContextAccessor = serviceProvider.GetRequiredService<IHttpContextAccessor>();
// //httpContextAccessor.HttpContext.Request.
2023-02-19 00:43:43 +01:00
// var cert = memoryCache.GetOrCreate("PrivaPub", (cacheEntry) => Extensions.Extensions.GetX509Certificate2("PrivaPubCert"));
2023-02-18 08:52:17 +01:00
// return new RsaPkcs15Sha256SignatureProvider(cert, "anon");
//})
//.ConfigureMessageSigningOptions(options =>
//{
// options.WithMandatoryComponent(SignatureComponent.Path)
// .WithMandatoryComponent(SignatureComponent.Method)
// .WithMandatoryComponent(SignatureComponent.Digest)
// .WithOptionalComponent(new HttpHeaderDictionaryStructuredComponent(NSign.Constants.Headers.Signature, "sample", bindRequest: true));
// options.SignatureName = "resp";
// options.SetParameters = (sigParams) =>
// {
// sigParams.WithCreatedNow();
// };
//})
//.ValidateOnStart()
//.Services
//.AddHttpClient("ActivityPub", (serviceProvider, client) =>
//{
// client.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("NSignSample", "0.1-beta"));
//}).Services;
//.AddSingleton<ISigner>(new RsaPssSha512SignatureProvider(
// new X509Certificate2(@"path\to\certificate.pfx", "PasswordForPfx"),
// "my-cert"));
}
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>()
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 =>
{
c.AddSecurityDefinition("Bearer", new()
{
In = ParameterLocation.Header,
Description = "Please enter a valid token",
Name = "Authorization",
Type = SecuritySchemeType.Http,
BearerFormat = "JWT",
Scheme = "Bearer"
});
c.AddSecurityRequirement(new()
{
{
new()
{
Reference = new()
{
Type = ReferenceType.SecurityScheme,
Id = "Bearer"
}
},
new string[]{}
}
});
})
.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();
});
});
}
}
}