-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
157 lines (124 loc) · 4.88 KB
/
Program.cs
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Authentication;
using chaos.Models;
using chaos.Services;
using chaos;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Protocols.OpenIdConnect;
using System.Security.Cryptography;
using chaos.utils;
using Microsoft.IdentityModel.Tokens;
using chaos.AuthRepository;
using Swashbuckle.AspNetCore.SwaggerGen;
using Microsoft.Extensions.Options;
using System.Reflection;
using chaos.Swagger;
using chaos.Hubs;
using Microsoft.AspNetCore.SignalR;
using Microsoft.AspNetCore.Http.Connections;
using System.Security.Claims;
var builder = WebApplication.CreateBuilder(args);
DotNetEnv.Env.Load(); // load in all env variables before commencing build
var supabase_url = Environment.GetEnvironmentVariable("SUPABASE_URL");
var supabase_api_key = Environment.GetEnvironmentVariable("SUPABASE_API_KEY");
var supabaseOptions = new Supabase.SupabaseOptions
{
AutoConnectRealtime = true
};
// Add services to the container.
builder.Services.AddCors((options)=>{
options.AddPolicy("AllowAllOrigins", (builder)=>{
builder.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod();
});
});
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(o => {
o.Events = new JwtBearerEvents(){
OnMessageReceived = (context) =>
{
var path = context.HttpContext.Request.Path;
if(path.StartsWithSegments("/chat-socket"))
{
var accessToken = context.Request.Query["access_token"];
var bToken = context.Request.Headers.Authorization;
Console.WriteLine($"Access Token {accessToken} b:: {bToken}");
if(context.Request.Query.ContainsKey("access_token")){
context.Token = accessToken;
}
}
return Task.CompletedTask;
}
};
o.TokenValidationParameters = new TokenValidationParameters()
{
ValidateAudience = false,
ValidateIssuer = false,
ValidateLifetime = false,
RequireExpirationTime = false
};
var rsaKey = RSA.Create();
rsaKey.ImportRSAPrivateKey(AppEnvironment.APP_RSA_KEY_PAIR, out _);
o.Configuration = new OpenIdConnectConfiguration()
{
SigningKeys = {
new RsaSecurityKey(rsaKey)
}
};
o.MapInboundClaims = false;
});
builder.Services.AddAuthorization(options=>{
options.AddPolicy(IdentityData.FriendPolicyName, p => p.RequireClaim(IdentityData.FriendClaimName, "true"));
});
builder.Services.AddControllers();
builder.Services.AddDbContext<ChatContext>(options => options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection")));
builder.Services.AddAutoMapper(typeof(Program).Assembly);
builder.Services.AddScoped<IChannel, chaos.Services.Channel>();
builder.Services.AddScoped<IUser, chaos.Services.User>();
builder.Services.AddScoped<IOrganization, chaos.Services.Organization>();
builder.Services.AddScoped<IApps, chaos.Services.App>();
builder.Services.AddScoped<IAuthService, AuthServive>();
builder.Services.AddScoped<AppResourcesAccessFilter>();
builder.Services.AddSingleton<IMessageSink, MessageService>();
builder.Services.AddHostedService(sp => {
var sink = sp.GetService<IMessageSink>();
return (MessageService)sink!; // TODO: figure out how to fix this
});
builder.Services.AddSingleton<IUserIdProvider, ChatHubIDProvider>();
builder.Services.AddSignalR(o => {
o.AddFilter<ChatHubFilter>();
});
if(supabase_url != null && supabase_api_key != null){
builder.Services.AddSingleton<Supabase.Client>(new Supabase.Client(supabase_url, supabase_api_key, supabaseOptions));
}
builder.Services.AddScoped<IUpload, SupabaseUpload>();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(c =>
{
// Other configurations...
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
c.IncludeXmlComments(xmlPath);
c.OperationFilter<AddCustomHeaderParameter>();
});
builder.Services.AddTransient<IConfigureOptions<SwaggerGenOptions>, ConfigureSwaggerOptions>();
var app = builder.Build();
app.UseCors("AllowAllOrigins");
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
// app.UseStaticFiles();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
app.MapHub<ChatHub>("/chat-socket", o => {
o.Transports = HttpTransportType.WebSockets;
});
app.Run();