-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
69 lines (61 loc) · 2.3 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
using System.Text.Json;
using System.Text.Json.Serialization;
using Digdir.BDB.Dialogporten.ServiceProvider.Auth;
using Digdir.BDB.Dialogporten.ServiceProvider.Clients;
using Digdir.BDB.Dialogporten.ServiceProvider.Services;
using Refit;
var builder = WebApplication.CreateBuilder(args);
builder.Configuration.AddUserSecrets<Program>();
builder.Services.AddControllers();
builder.Services
.AddEndpointsApiExplorer()
.AddSwaggerGen()
.AddIdportenAuthentication(builder.Configuration)
.AddDialogTokenAuthentication()
.AddTransient<TokenGeneratorMessageHandler>()
.AddTransient<ConsoleLoggingMessageHandler>()
.AddSingleton<ITokenGenerator, TokenGenerator>()
.AddSingleton<IBackgroundTaskQueue, BackgroundTaskQueue>()
.AddHostedService<EdDsaSecurityKeysCacheService>()
.AddHostedService<QueuedHostedService>()
.AddCors(options =>
{
options.AddPolicy("AllowedOriginsPolicy", builder =>
{
// This is to ease development (ie. various locahost ports)
// In a production setting, this should be restricted to https://af.altinn.no
builder.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod()
.SetPreflightMaxAge(TimeSpan.FromMinutes(10));
});
})
.AddRefitClient<IDialogporten>(_ => new RefitSettings
{
ContentSerializer = new SystemTextJsonContentSerializer(new JsonSerializerOptions
{
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
PropertyNameCaseInsensitive = true,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
Converters = {
new ObjectToInferredTypesConverter(),
new JsonStringEnumConverter(JsonNamingPolicy.CamelCase)
}
})
})
.ConfigureHttpClient(configuration =>
{
configuration.BaseAddress = new Uri(builder.Configuration["Dialogporten:BaseUrl"]!);
})
.AddHttpMessageHandler<ConsoleLoggingMessageHandler>()
.ConfigurePrimaryHttpMessageHandler<TokenGeneratorMessageHandler>();
var app = builder.Build();
app.UseSwagger();
app.UseSwaggerUI();
app.UseHttpsRedirection();
app.UseRouting();
app.UseCors("AllowedOriginsPolicy");
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
await app.RunAsync();