-
Notifications
You must be signed in to change notification settings - Fork 0
/
Startup.cs
221 lines (189 loc) · 8.34 KB
/
Startup.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using ConfigurationScanner;
using DroHub.Areas.DHub.API;
using DroHub.Areas.DHub.Helpers;
using DroHub.Areas.DHub.Helpers.ResourceAuthorizationHandlers;
using DroHub.Areas.DHub.Models;
using DroHub.Areas.DHub.SignalRHubs;
using DroHub.Areas.Identity.Data;
using DroHub.Areas.Identity.Pages;
using DroHub.Areas.Identity.Services;
using DroHub.Data;
using DroHub.Helpers;
using DroHub.Helpers.AuthenticationHandler;
using DroHub.Helpers.Thrift;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
namespace DroHub
{
public class Startup
{
public const string STARTUP_COMPLETE_MAGIC = "Initialized successfully";
public Startup(IConfiguration configuration)
{
Configuration = configuration;
(Configuration as IConfigurationRoot)
.ThrowOnConfiguredForbiddenToken();
}
private IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services) {
services.AddRouting();
services.AddDbContext<DroHubContext>(options => {
options.UseMySql(Configuration.GetDroHubConnectionString());
});
services.Configure<ForwardedHeadersOptions>(options =>
{
foreach (var addr in Dns.GetHostEntry("server").AddressList) {
options.KnownProxies.Add(addr);
}
});
services.Configure<RepositoryOptions>(Configuration.GetSection("RepositoriesConfiguration"));
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
// options.MinimumSameSitePolicy = SameSiteMode.Unspecified;
services.ConfigureExternalCookie(o => {
o.Cookie.SameSite = SameSiteMode.None;
});
services.ConfigureApplicationCookie(o =>
{
o.Cookie.SameSite = SameSiteMode.None;
});
});
services.Configure<ApiBehaviorOptions>(o => {
o.InvalidModelStateResponseFactory = context => {
//We do not want to disturb other APIs
if (context.ActionDescriptor.RouteValues["action"] != "UploadMedia" ||
context.ActionDescriptor.RouteValues["controller"] != "AndroidApplication")
return new BadRequestResult();
var model_error =
context.ModelState.Values.SelectMany(v => v.Errors
.Select(b => b.ErrorMessage)).FirstOrDefault();
if (!string.IsNullOrEmpty(model_error)) {
return new JsonResult(new {
result = "nok",
error = model_error
});
}
return new BadRequestResult();
};
});
services.Configure<JanusServiceOptions>(Configuration.GetSection("JanusServiceOptions"));
services.AddHostedService<NotificationsHubPoller>();
services.AddHttpClient<JanusService>();
services.AddSingleton(provider => Configuration);
services.AddDataProtection()
.PersistKeysToFileSystem(new DirectoryInfo(
Configuration.GetValue<string>("KeysDirectory")));
services.AddSignalR();
services.AddAuthentication(RouteAuthenticationHandler.SchemeName)
.RouteAuthentication(IdentityConstants.ApplicationScheme, new List<AuthenticationSchemeRoute> {
new AuthenticationSchemeRoute {
SchemeName = TokenAuthenticationHandler.SchemeName,
StartPaths = new List<PathString>() {"/api/AndroidApplication"}
},
new AuthenticationSchemeRoute {
SchemeName = TelemetryAuthenticationHandler.SchemeName,
StartPaths = new List<PathString>() {"/ws"}
}
})
.AddTokenAuthentication()
.AddTelemetryAuthentication();
services.AddAuthorization();
services.AddSubscriptionAPI();
services.AddDeviceResourceAuthorization();
services.AddMediaObjectResourceAuthorization();
services.AddDroHubUserResourceAuthorization();
services.AddMailJetEmailSenderExtensions(Configuration);
services.AddDeviceAPI();
services.AddDeviceConnectionSessionAPI();
services.AddMediaObjectTagAPI();
services.AddWebSocketManager();
services
.AddMvc()
.AddRazorRuntimeCompilation()
.AddRazorPagesOptions(options => {
options.Conventions.AddAreaPageRoute("Identity", "/Account/Login", "/Account/");
});
services.AddScoped<IUserClaimsPrincipalFactory<DroHubUser>, DroHubClaimsIdentityFactory>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app,
IWebHostEnvironment env,
DroHubContext db_context,
SignInManager<DroHubUser> sign_in_manager,
ILogger<DroHubContext> logger)
{
if (Configuration.GetValue<bool>("AutoMigration")) {
db_context.Database.Migrate();
logger.LogWarning("Ran migrate database");
}
foreach(var u in db_context.Users.ToList()) {
var t = DroHubUser.refreshClaims(sign_in_manager, u);
t.Wait();
if (t.Result == IdentityResult.Failed()) {
logger.LogError("failed to refresh claims");
}
}
logger.LogWarning("Ran user claims migration");
{
var t = ProgramExtensions.InitializeAdminUserHelper
.createAdminUser(logger, sign_in_manager, db_context);
t.Wait();
}
{
var t = LocalStorageHelper.generateVideoPreviewForConnectionDir(
logger, true);
t.Wait();
logger.LogWarning("Generated video previews");
}
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else {
app.UseStatusCodePages();
}
app.UseForwardedHeaders(new ForwardedHeadersOptions
{
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
});
app.UseDefaultFiles();
app.UseStaticFiles();
app.UseRouting();
app.UseCookiePolicy(new CookiePolicyOptions
{
MinimumSameSitePolicy = SameSiteMode.None
});
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints => {
endpoints.MapHub<NotificationsHub>("/notificationshub");
endpoints.MapHub<TelemetryHub>("/telemetryhub");
endpoints
.MapControllerRoute("areas", "{area:exists}/{controller}/{action=Index}/{id?}")
.RequireAuthorization();
endpoints.MapControllerRoute("api", "api/{controller}/{action}");
endpoints.MapWebSocketManager("/ws");
endpoints.MapRazorPages();
});
logger.LogInformation(STARTUP_COMPLETE_MAGIC);
}
}
}