-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
129 lines (100 loc) · 4.01 KB
/
Copy pathProgram.cs
File metadata and controls
129 lines (100 loc) · 4.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
using DotNetEnv;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.EntityFrameworkCore;
using Ordis.Components;
// TEST-ONLY auth bypass gate. `--testing` is the ONLY way to enable it — there is
// deliberately no env-var/config fallback, so it can never be on in production.
// See TestingSupport.cs for the full explanation.
var testingMode = TestingAuth.IsEnabled(args);
// Strip the flag before it reaches the command-line configuration provider (which
// would otherwise reject a value-less switch), so it can only be read as a raw
// argument via TestingAuth.IsEnabled above.
var builder = WebApplication.CreateBuilder(
args.Where(a => a != TestingAuth.TestingFlag).ToArray());
if (testingMode)
{
Console.WriteLine(
"[TESTING] --testing flag set: auto-authenticating as auto-generated 3-digit " +
"test accounts. Do NOT use this in production.");
}
builder.Configuration.AddEnvironmentVariables();
Env.Load();
builder.Services.Configure<RpgConfig>(builder.Configuration.GetSection("RpgConfig"));
builder.Services.Configure<Dictionary<string, APIConfig>>(
builder.Configuration.GetSection("ApiConfig")
);
builder.Services.AddHttpClient();
builder.Services.AddSingleton<DiscordService>();
builder.Services.AddSingleton<LiveUpdateService>();
builder.Services.AddDbContextFactory<OrdisContext>((sp, opts) =>
{
opts.UseNpgsql(builder.Configuration.GetConnectionString("CharacterDb"));
});
builder.Services.AddScoped<PlayerCharacterService>();
builder.Services.AddScoped<CampaignService>();
builder.Services.AddScoped<BuffService>();
builder.Services.AddScoped<UserState>();
builder.Services.AddRazorComponents()
.AddInteractiveServerComponents();
/* ---------------- AUTH ---------------- */
var discordConfig = builder.Configuration.GetSection("Discord");
var clientId = discordConfig["ClientId"]!;
var clientSecret = discordConfig["ClientSecret"]!;
// Normal Discord OAuth in production. When --testing is set, this also registers
// the auto-authenticating test scheme and makes it the default (see TestingSupport.cs).
TestingAuth.ConfigureAuthentication(builder.Services, testingMode, clientId, clientSecret);
builder.Services.AddAuthorization(options =>
{
options.AddPolicy("HasDiscordId", p => p.RequireClaim("discord_id"));
});
/* ---------------- APP ---------------- */
builder.WebHost.ConfigureKestrel((context, options) =>
{
options.Configure(context.Configuration.GetSection("Kestrel"));
});
var app = builder.Build();
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error", createScopeForErrors: true);
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseAuthentication();
app.UseAuthorization();
app.UseAntiforgery();
app.MapStaticAssets();
/* login endpoint — preserves ReturnUrl */
app.MapGet("/login", async (HttpContext ctx) =>
{
var returnUrl = ctx.Request.Query["ReturnUrl"].ToString();
await ctx.ChallengeAsync("Discord", new AuthenticationProperties
{
RedirectUri = string.IsNullOrEmpty(returnUrl) ? "/" : returnUrl
});
});
app.MapGet("/logout", async (HttpContext ctx) =>
{
await ctx.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
ctx.Response.Redirect("/");
});
if (testingMode)
{
// TEST-ONLY: /testlogin lets browser tests switch between (or auto-create)
// 3-digit test accounts. Only mapped under --testing (see TestingSupport.cs).
TestingAuth.MapTestingEndpoints(app);
}
app.MapRazorComponents<App>()
.AddInteractiveServerRenderMode();
using (var scope = app.Services.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService<OrdisContext>();
db.Database.Migrate();
}
if (testingMode)
{
// TEST-ONLY: seed the default test accounts + a usable character for each so
// the roll panel is reachable for automated browser testing.
await TestingAuth.SeedTestDataAsync(app.Services);
}
app.Run();