-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
309 lines (269 loc) · 11.5 KB
/
Copy pathProgram.cs
File metadata and controls
309 lines (269 loc) · 11.5 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
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/* matts
* "Matthew's ATS" - Portfolio Project
* Copyright (C) 2023 Matthew E. Kehrer <matthew@kehrer.dev>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
**/
using Azure.Identity;
using FluentValidation;
using MapsterMapper;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Microsoft.IdentityModel.Tokens;
using Microsoft.Extensions.Azure;
using Neo4j.Driver;
using matts.Configuration;
using matts.Models;
using matts.Models.Db;
using matts.Interfaces;
using matts.Services;
using matts.Daos;
using matts.Repositories;
using matts.Constants;
using matts.Middleware;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Identity;
var builder = WebApplication.CreateBuilder(args);
var credential = new DefaultAzureCredential();
// Setup Azure App Config Service
bool useAzureAppConfig = builder.Configuration.GetSection("AzurePlatform").GetValue<bool?>("UseAzureAppConfiguration") ?? false;
if (useAzureAppConfig)
{
builder.Configuration.AddAzureAppConfiguration(
options =>
{
string? connectionString = builder.Configuration.GetConnectionString("AzureAppConfiguration");
string? primaryServiceUrl = builder.Configuration.GetSection("AzurePlatform")
.GetSection("AzureAppConfiguration")
.GetValue<string?>("PrimaryServiceUrl");
if (connectionString != null)
{
options = options.Connect(connectionString);
}
else if (Uri.IsWellFormedUriString(primaryServiceUrl, UriKind.Absolute))
{
options = options.Connect(new Uri(primaryServiceUrl, UriKind.Absolute), credential);
}
else
{
throw new ApplicationException("MISSING REQUIRED CONFIG VALUES: Azure App Coonfiguration ConnString OR Azure App Configuration URL");
}
options.Select("MattsSPA:*", builder.Environment.EnvironmentName)
.TrimKeyPrefix("MattsSPA:")
.ConfigureKeyVault(kv => kv.SetCredential(credential));
}
);
builder.Services.AddAzureAppConfiguration();
}
// Setup other Azure services
bool useAzureBlobService = builder.Configuration.GetSection("AzurePlatform").GetValue<bool?>("UseAzureBlobService") ?? false;
builder.Services.AddAzureClients(clients =>
{
if (useAzureBlobService)
{
var blobConfigs = builder.Configuration.GetSection("AzurePlatform")
.GetSection("AzureBlobConfigurations")
.Get<AzureBlobConfiguration[]>()
?? throw new ApplicationException("MISSING REQUIRED CONFIG SECTION: AzureBlobConfigurations");
// Create blob clients and register their configs as well
int i = 0;
foreach (var config in blobConfigs)
{
if (config.ServiceName == null)
{
throw new ApplicationException("MISSING REQUIRED CONFIG VALUE: AzureBlobConfiguration[i]:ServiceName");
}
string? connectionString = builder.Configuration.GetConnectionString(config.ServiceName);
if (connectionString != null)
{
clients.AddBlobServiceClient(connectionString).WithName(config.ServiceName);
}
else if (config.PrimaryServiceUrl != null)
{
clients.AddBlobServiceClient(config.PrimaryServiceUrl).WithName(config.ServiceName).WithCredential(credential);
}
else
{
throw new ApplicationException("MISSING REQUIRED CONFIG VALUES: Azure Blob ConnString OR Azure Blob URL");
}
builder.Services.Configure<AzureBlobConfiguration>(
config.ServiceName,
builder.Configuration.GetSection($"AzurePlatform:AzureBlobConfigurations:{i++}")
);
}
}
});
// Setup the remaining services
builder.Services.AddAuthorization(options =>
{
options.AddPolicy("Employers", policy =>
policy.RequireRole(UserRoleConstants.USER_ROLE_EMPLOYER));
options.AddPolicy("LoggedInUsers", policy =>
policy.RequireAuthenticatedUser());
});
builder.Services.AddAuthentication("Bearer").AddJwtBearer(o =>
{
o.TokenValidationParameters = new TokenValidationParameters
{
IssuerValidator = (string issuer, SecurityToken securityToken, TokenValidationParameters validationParameters) =>
{
var usersJwtIssuer = builder.Configuration["Authentication:Schemes:Bearer:ValidIssuer"];
var appJwtIssuer = builder.Configuration["Jwt:Issuer"];
if ((usersJwtIssuer != null && issuer == usersJwtIssuer) || (appJwtIssuer != null && issuer == appJwtIssuer))
{
return issuer;
}
throw new SecurityTokenInvalidIssuerException($"Invalid Issuer: {issuer}");
},
IssuerSigningKeyResolver = (string token, SecurityToken securityToken, string kid, TokenValidationParameters validationParameters) =>
{
var logger = builder.Logging.Services.BuildServiceProvider().GetRequiredService<ILogger<Program>>();
List<SecurityKey> keys = new();
if (builder.Environment.IsDevelopment())
{
// Add key for dotnet-user-jwts
var userJwtKey = builder.Configuration["Authentication:Schemes:Bearer:SigningKeys:0:Value"];
if (userJwtKey != null)
{
keys.Add(new SymmetricSecurityKey(Convert.FromBase64String(userJwtKey)));
}
else
{
logger.LogError("The dotnet-user-jwts Jwt Signing Key is missing from Config!");
}
}
// Add key for the app
var appJwtKey = builder.Configuration["Jwt:SigningKey"];
if (appJwtKey != null)
{
keys.Add(new SymmetricSecurityKey(Convert.FromBase64String(appJwtKey)));
}
else
{
logger.LogError("The application Jwt Signing Key is missing from Config!");
}
return keys;
},
ValidAudiences = builder.Configuration.GetSection("Authentication:Schemes:Bearer:ValidAudiences").Get<string[]>(),
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true
};
});
// CONFIGURATION FOR IOPTIONS
builder.Services.Configure<AzurePlatformConfiguration>(builder.Configuration.GetSection("AzurePlatform"));
builder.Services.Configure<Neo4JConfiguration>(builder.Configuration.GetSection("Neo4J"));
builder.Services.Configure<JwtConfiguration>(builder.Configuration.GetSection("Jwt"));
builder.Services.Configure<ClientAppConfiguration>(builder.Configuration.GetSection("ClientApp"));
// CONFIGURATION FOR IOPTIONSMONITOR / IOPTIONSSNAPSHOT
{
// Oauth Config
var oauthConfigs = builder.Configuration.GetSection("Oauth")
.GetSection("OauthConfigurations")
.Get<OauthConfig[]>()
?? throw new ApplicationException("MISSING REQUIRED CONFIG SECTION: OauthConfigurations");
int i = 0;
foreach (var config in oauthConfigs)
{
if (config.GetType().GetProperties().Any(info => info.GetValue(config) is null))
{
throw new ApplicationException($"MISSING REQUIRED CONFIG VALUE WITHIN: OauthConfigurations[{i}]");
}
builder.Services.Configure<OauthConfig>(
config.ServiceName,
builder.Configuration.GetSection($"Oauth:OauthConfigurations:{i++}")
);
}
}
// CSRF/XSRF Protection Setup
builder.Services.AddAntiforgery(options =>
{
options.Cookie = new CookieBuilder
{
Name = "ASP-XSRF-TOKEN",
IsEssential = true,
SecurePolicy = CookieSecurePolicy.Always
};
options.HeaderName = "X-Xsrf-Token";
});
builder.Services.AddControllersWithViews(options =>
{
options.Filters.Add(new AutoValidateAntiforgeryTokenAttribute());
});
// SINGLETONS
/*** Neo4J ***/
builder.Services.AddSingleton<IDriver>(implementationFactory: provider =>
{
var settings = (provider.GetRequiredService<IOptions<Neo4JConfiguration>>()?.Value)
?? throw new ApplicationException("MISSING REQUIRED CONFIG VALUES: Neo4J Driver");
return GraphDatabase.Driver(settings.ConnectionURL, AuthTokens.Basic(settings.User, settings.Password));
});
/*** CSP Policy ***/
builder.Services.AddSingleton<CSP>(implementationFactory: provider =>
{
var policy = CSP.DefaultPolicy.Clone();
policy.ConnectSrc = $"{CSP.Self} matthews-ats-funcs-staging.azurewebsites.net";
// The hash is the sha256 hash of the inline script within ClientApp/src/index.html
policy.ScriptSrc = $"{policy.ScriptSrc} 'sha256-Vywrtc+OVj0nO9NJ0FmjzNfROZD38W0ll9q3KcU4Huk='";
policy.Sandbox = $"{policy.Sandbox} allow-downloads allow-popups allow-popups-to-escape-sandbox";
policy.BaseUri = string.Empty;
return policy;
});
/*** LinkedIn OAuth Service ***/
builder.Services.AddSingleton<ILinkedinOAuthService, LinkedinOAuthService>();
// SCOPED
builder.Services.AddScoped<IValidator<User>, UserValidator>();
builder.Services.AddScoped<IValidator<UserRegistration>, UserRegistrationValidator>();
builder.Services.AddScoped<IValidator<ApplyToJob>, ApplyToJobValidator>();
builder.Services.AddScoped<IValidator<Job>, JobValidator>();
builder.Services.AddScoped(typeof(IDataAccessObject<JobDb>), typeof(JobDao));
builder.Services.AddScoped(typeof(IDataAccessObject<ApplicantDb>), typeof(ApplicantDao));
builder.Services.AddScoped(typeof(IDataAccessObject<User>), typeof(UserDao));
builder.Services.AddScoped(typeof(IDataAccessObject<EmployerDb>), typeof(EmployerDao));
builder.Services.AddScoped<IApplicantRepository, ApplicantRepository>();
builder.Services.AddScoped<IEmployerRepository, EmployerRepository>();
builder.Services.AddScoped<IJobRepository, JobRepository>();
builder.Services.AddScoped<IUserRepository, UserRepository>();
builder.Services.AddScoped<IJobService, JobService>();
builder.Services.AddScoped<IUserService, UserService>();
// TRANSIENT
builder.Services.AddTransient<IMapper, Mapper>();
// HTTP CLIENTS
builder.Services.AddHttpClient("linkedin_client");
var app = builder.Build();
if (useAzureAppConfig)
{
app.UseAzureAppConfiguration();
}
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseMiddleware<AntiforgeryMiddleware>(); // MUST go after auth middleware
app.UseAuthorization();
app.UseMiddleware<ContentSecurityPolicyMiddleware>();
app.UseWebSockets();
app.MapControllerRoute(
name: "default",
pattern: "{controller}/{action=Index}/{id?}");
app.MapFallbackToFile("index.html");
app.Run();
public partial class Program { }