Files
SPMega/backend/SpMega.Backend/Program.cs
T
Dmitrii 9238069d0e
Java CI / build (push) Successful in 48s
.NET+Docker CI/CD / Unit and Integration tests (push) Successful in 34s
Release CI / build-and-upload-mod (push) Successful in 7s
Release CI / build-and-upload-mod (release) Successful in 8s
.NET+Docker CI/CD / Push Docker image to ghcr.io (push) Failing after 27s
Refactor JWT configuration handling in Program.cs and TokenService.cs
Signed-off-by: Dmitrii <computer@yawaflua.tech>

Took 23 minutes
2026-06-29 05:00:55 +03:00

98 lines
3.2 KiB
C#

using System.Security.Cryptography;
using System.Text;
using Microsoft.AspNetCore.Authorization;
using Microsoft.EntityFrameworkCore;
using MongoDB.Driver;
using SpMega.Backend.Authetication;
using SpMega.Backend.Persistent.Database;
using SpMega.Backend.Services;
namespace SpMega.Backend;
public class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
builder.Configuration
.AddJsonFile("appsettings.json", true)
.AddJsonFile("appsettings.Development.json", true)
.AddEnvironmentVariables();
var conf = builder.Configuration;
var encryptionKey = conf["Encryption:Key"] ?? "a-default-fallback-only-for-dev-key-change-this!";
EncryptionHelper.Initialize(encryptionKey);
_ = conf["JWT:Secret"] ?? conf["JWT__Secret"] ?? throw new InvalidOperationException("JWT__Secret is not configured.");
builder.Services.AddAuthorization();
builder.Services.AddLogging(logging =>
{
logging.AddConsole();
logging.AddDebug();
});
builder.Services
.AddScoped<TokenService>()
.AddRazorPages();
builder.Services
.AddHttpLogging(logging =>
{
logging.LoggingFields = Microsoft.AspNetCore.HttpLogging.HttpLoggingFields.All;
})
.AddRouting()
.AddControllers();
builder.Services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtAuthenticationSchemeOptions.DefaultScheme;
options.DefaultChallengeScheme = JwtAuthenticationSchemeOptions.DefaultScheme;
})
.AddScheme<JwtAuthenticationSchemeOptions, JwtAuthHandler>(
JwtAuthenticationSchemeOptions.DefaultScheme,
options => { });
builder.Services.AddAuthorizationBuilder()
.SetDefaultPolicy(new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build());
var serviceCollection = new ServiceCollection();
var mongoClient =
new MongoClient(builder.Configuration.GetValue<string>("Mongo") ?? "mongodb://curiosity:27018");
serviceCollection.AddSingleton<IMongoClient>(mongoClient);
builder.Services.AddDbContext<AppDbContext>((_, options) =>
{
options.UseMongoDB(mongoClient, "spmega");
});
var app = builder.Build();
app.UseAuthentication();
app.UseAuthorization();
app.UseWhen(ctx => ctx.Request.Path.StartsWithSegments("/api"), appBuilder =>
{
appBuilder.UseHttpLogging();
});
app.MapControllers();
app.MapRazorPages();
app.Run();
}
public static string GenerateRandomString(int length)
{
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
var result = new StringBuilder(length);
for (var i = 0; i < length; i++)
{
var index = RandomNumberGenerator.GetInt32(chars.Length);
result.Append(chars[index]);
}
return result.ToString();
}
}