mirror of
https://github.com/whoswhip/Sharpbin.git
synced 2026-12-06 02:48:43 +00:00
feat: auth, compression, jwt, paste wip, roles, ratelimiting
This commit is contained in:
@@ -1,6 +1,11 @@
|
||||
using Microsoft.AspNetCore.RateLimiting;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using SharpbinV3.Data;
|
||||
using SharpbinV3.Services;
|
||||
using System.Threading.RateLimiting;
|
||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using System.Text;
|
||||
|
||||
namespace SharpbinV3
|
||||
{
|
||||
@@ -19,9 +24,49 @@ namespace SharpbinV3
|
||||
{
|
||||
opt.UseSqlite(builder.Configuration.GetConnectionString("DefaultConnection"));
|
||||
});
|
||||
builder.Services.AddSingleton<ICompressionService, CompressionService>();
|
||||
builder.Services.AddScoped<IUserService, UserService>();
|
||||
builder.Services.AddScoped<IAuthService, AuthService>();
|
||||
|
||||
builder.Services.AddAuthentication(options =>
|
||||
{
|
||||
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
|
||||
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
|
||||
options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
|
||||
})
|
||||
.AddJwtBearer(jwt =>
|
||||
{
|
||||
var key = Encoding.ASCII.GetBytes(builder.Configuration.GetSection("JwtConfig:Secret").Value ?? string.Empty);
|
||||
jwt.SaveToken = true;
|
||||
jwt.TokenValidationParameters = new TokenValidationParameters
|
||||
{
|
||||
ValidateIssuerSigningKey = true,
|
||||
IssuerSigningKey = new SymmetricSecurityKey(key),
|
||||
ValidateIssuer = true,
|
||||
ValidIssuer = builder.Configuration["JwtConfig:Issuer"],
|
||||
ValidateAudience = true,
|
||||
ValidAudience = builder.Configuration["JwtConfig:Audience"],
|
||||
RequireExpirationTime = true,
|
||||
ValidateLifetime = true
|
||||
};
|
||||
});
|
||||
|
||||
builder.Services.AddRateLimiter(options =>
|
||||
{
|
||||
options.AddSlidingWindowLimiter("Sliding", opt =>
|
||||
{
|
||||
opt.Window = TimeSpan.FromSeconds(10);
|
||||
opt.PermitLimit = 10;
|
||||
opt.QueueLimit = 2;
|
||||
opt.SegmentsPerWindow = 5;
|
||||
opt.QueueProcessingOrder = QueueProcessingOrder.OldestFirst;
|
||||
});
|
||||
|
||||
options.RejectionStatusCode = StatusCodes.Status429TooManyRequests;
|
||||
});
|
||||
|
||||
var app = builder.Build();
|
||||
app.UseRateLimiter();
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
if (app.Environment.IsDevelopment())
|
||||
@@ -31,6 +76,7 @@ namespace SharpbinV3
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
|
||||
app.UseAuthentication();
|
||||
app.UseAuthorization();
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user