-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
69 lines (67 loc) · 3.63 KB
/
Program.cs
File metadata and controls
69 lines (67 loc) · 3.63 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
using Finder.Web.Database;
using Finder.Web.Repositories;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.OAuth;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using System.Security.Claims;
namespace Finder.Web;
public static class Program {
public static void Main(string[] args) {
if (string.IsNullOrEmpty(Environment.GetEnvironmentVariable("DISCORD_CLIENT_ID"))) {
Console.WriteLine("Environment variable DISCORD_CLIENT_ID is not set.");
return;
}
if (string.IsNullOrEmpty(Environment.GetEnvironmentVariable("DISCORD_CLIENT_SECRET"))) {
Console.WriteLine("Environment variable DISCORD_CLIENT_SECRET is not set.");
return;
}
if (string.IsNullOrEmpty(Environment.GetEnvironmentVariable("DISCORD_BOT_TOKEN"))) {
Console.WriteLine("Environment variable DISCORD_BOT_TOKEN is not set.");
return;
}
if (string.IsNullOrEmpty(Environment.GetEnvironmentVariable("DB_CONNECTION_STRING"))) {
Console.WriteLine("Environment variable DB_CONNECTION_STRING is not set.");
return;
}
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddDbContext<ApplicationContext>(options => options.UseNpgsql(Environment.GetEnvironmentVariable("DB_CONNECTION_STRING")!), ServiceLifetime.Transient);
builder.Services.AddTransient(typeof(IRepository<>), typeof(Repository<>));
builder.Services.AddTransient<IUnitOfWork, UnitOfWork>();
builder.Services.AddAuthentication(options => options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(options => {
options.LoginPath = "/login";
options.ExpireTimeSpan = TimeSpan.FromDays(7);
}).AddDiscord(options => {
options.Scope.Add("identify");
options.Scope.Add("guilds");
options.Prompt = "none";
options.ClientId = Environment.GetEnvironmentVariable("DISCORD_CLIENT_ID")!;
options.ClientSecret = Environment.GetEnvironmentVariable("DISCORD_CLIENT_SECRET")!;
options.SaveTokens = true;
options.Events = new OAuthEvents {
OnCreatingTicket = context => {
List<string> ownerIds = Environment.GetEnvironmentVariable("BOT_OWNER_IDS")?.Split(',').ToList() ?? new List<string>();
if (!ulong.TryParse(context.Principal.FindFirstValue(ClaimTypes.NameIdentifier), out var userId)) return Task.CompletedTask;
if (ownerIds.IsNullOrEmpty() || !ownerIds.Contains(userId.ToString())) {
context.Identity.AddClaim(new Claim("IsBotOwner", "false"));
} else {
context.Identity.AddClaim(new Claim("IsBotOwner", "true"));
}
return Task.CompletedTask;
}
};
});
builder.Services.AddAuthorization(options => options.AddPolicy("IsBotOwner", policy => policy.RequireClaim("IsBotOwner", "true", "false")));
builder.Services.AddHttpClient();
builder.Services.AddControllersWithViews();
var app = builder.Build();
if (app.Environment.IsDevelopment()) app.UseDeveloperExceptionPage();
app.UseRouting();
app.UseStaticFiles();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllerRoute(name: "default", pattern: "{controller=Home}/{action=Index}/{id?}");
app.Run();
}
}