-
Notifications
You must be signed in to change notification settings - Fork 4
/
Startup.cs
77 lines (66 loc) · 2.4 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
namespace IO.Curity.OAuthAgent
{
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using IO.Curity.OAuthAgent.Exceptions;
public class Startup
{
private readonly OAuthAgentConfiguration configuration;
public Startup(OAuthAgentConfiguration configuration) {
this.configuration = configuration;
}
/*
* The OAuth agent is a simple REST API
*/
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseRouting();
if (this.configuration.CorsEnabled)
{
app.UseCors();
}
this.ConfigureMiddleware(app);
app.UseEndpoints(endpoints => {
endpoints.MapControllers();
});
}
/*
* CORS must be enabled if the OAuth agent is deployed to a different domain to the web origin, or disabled for same site deployments
*/
public void ConfigureServices(IServiceCollection services)
{
if (this.configuration.CorsEnabled)
{
services.AddCors(options => {
options.AddDefaultPolicy(
policy =>
{
policy.WithOrigins(this.configuration.TrustedWebOrigins)
.AllowAnyHeader()
.AllowAnyMethod();
policy.AllowCredentials();
});
});
}
services.AddControllers();
this.ConfigureDependencies(services);
}
private void ConfigureMiddleware(IApplicationBuilder app)
{
app.UseMiddleware<UnhandledExceptionMiddleware>();
}
/*
* Dependencies to implement the OAuth agent are stateless so can be created as singletons
*/
public void ConfigureDependencies(IServiceCollection services)
{
services.AddSingleton<LoginHandler>();
services.AddSingleton<CookieManager>();
services.AddSingleton<AuthorizationServerClient>();
services.AddSingleton<IdTokenValidator>();
services.AddSingleton<RequestValidator>();
services.AddSingleton<ErrorLogger>();
}
}
}