-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathProgram.cs
More file actions
137 lines (109 loc) · 4.18 KB
/
Program.cs
File metadata and controls
137 lines (109 loc) · 4.18 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
using API.Authentication;
using API.Middlewares;
using Infrastructure;
using Infrastructure.Middlewares;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.Identity.Web;
using OpenApiContact = NSwag.OpenApiContact;
using OpenApiInfo = NSwag.OpenApiInfo;
using OpenApiSecurityScheme = NSwag.OpenApiSecurityScheme;
var builder = WebApplication.CreateBuilder(args);
builder.UseDmsSerilog();
// Add service defaults & Aspire components.
builder.AddServiceDefaults();
builder.Services.AddRequestTimeouts();
builder.Services.AddOutputCache();
builder.Services.AddOpenApiDocument(options =>
{
options.PostProcess = document =>
{
document.Info = new OpenApiInfo()
{
Version = "v1",
Title = "DMS Api",
Description = "The DMS API",
Contact = new OpenApiContact()
{
Name = "CFO ICT",
Email = "cfo-ict@justice.gov.uk"
}
};
};
options.AddSecurity("ApiKey", new OpenApiSecurityScheme()
{
Type = NSwag.OpenApiSecuritySchemeType.ApiKey,
Name = "X-API-KEY",
In = NSwag.OpenApiSecurityApiKeyLocation.Header,
Description = "API Key needed to access endpoints"
});
options.OperationProcessors.Add(new AddApiKeyHeaderOperationProcessor());
});
// Add services to the container.
builder.Services.AddProblemDetails();
builder.Services.AddMicrosoftIdentityWebApiAuthentication(builder.Configuration);
// The below configures a policy scheme called "Smart" that can handle both JWT Bearer and (legacy) API Key authentication.
builder.Services.AddAuthentication(options =>
{
options.DefaultScheme = "Smart";
options.DefaultChallengeScheme = "Smart";
}).AddScheme<AuthenticationSchemeOptions, ApiKeyAuthenticationHandler>(LegacyApiKeyDefaults.AuthenticationScheme, _ => { })
.AddPolicyScheme("Smart", $"Supports {LegacyApiKeyDefaults.AuthenticationScheme} and {JwtBearerDefaults.AuthenticationScheme} schemes", options =>
{
options.ForwardDefaultSelector = context =>
{
var authHeader = context.Request.Headers.Authorization.FirstOrDefault();
if (!string.IsNullOrEmpty(authHeader) && authHeader.StartsWith("Bearer ", StringComparison.OrdinalIgnoreCase))
{
return JwtBearerDefaults.AuthenticationScheme;
}
return LegacyApiKeyDefaults.AuthenticationScheme;
};
});
builder.Services.AddAuthorization(options =>
{
options.AddPolicy("read", policy => policy.RequireScope("dms.read"));
options.AddPolicy("write", policy => policy.RequireScope("dms.write"));
options.AddPolicy("visualisation-read", policy =>
policy.RequireAuthenticatedUser()
.RequireScope("visualiser.read")
.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme));
options.AddPolicy("visualisation-write", policy =>
policy.RequireAuthenticatedUser()
.RequireScope("visualiser.write")
.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme));
options.FallbackPolicy = options.DefaultPolicy;
});
builder.Services.AddHttpContextAccessor();
builder.Services.AddScoped<ICurrentUserService, CurrentUserService>();
builder
.AddDatabaseServices()
.AddApplicationServices();
var app = builder.Build();
app.UseStaticFiles(new StaticFileOptions()
{
FileProvider = new Microsoft.Extensions.FileProviders.PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "static")),
RequestPath = "/static"
});
// Configure the HTTP request pipeline.
app.UseExceptionHandler();
if (builder.Configuration["IsDevelopment"] is not null && builder.Configuration.GetValue<bool>("IsDevelopment"))
{
app.RegisterDevelopmentEndpoints();
app.UseOpenApi();
app.UseSwaggerUi(options =>
{
options.CustomStylesheetPath = "/static/dms-swagger.css";
});
}
app.UseAuthentication();
app.UseAuthorization();
app.UseRequestTimeouts();
app.UseOutputCache();
app.RegisterClusteringEndpoints()
.RegisterDeliusEndpoints()
.RegisterOfflocEndpoints()
.RegisterSearchEndpoints()
.RegisterReferenceEndpoints()
.RegisterVisualisationEndpoints();
app.MapDefaultEndpoints();
app.Run();