-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathProgram.cs
More file actions
186 lines (156 loc) · 6.33 KB
/
Program.cs
File metadata and controls
186 lines (156 loc) · 6.33 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
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using Microsoft.FluentUI.AspNetCore.Components;
using NoteBookmark.AIServices;
using NoteBookmark.BlazorApp;
using NoteBookmark.BlazorApp.Components;
var builder = WebApplication.CreateBuilder(args);
builder.AddServiceDefaults();
builder.AddAzureTableClient("nb-tables");
// Add HTTP client for API calls
builder.Services.AddHttpClient<PostNoteClient>(client =>
{
client.BaseAddress = new Uri("https+http://api");
});
// Register server-side AI settings provider (direct database access, unmasked)
builder.Services.AddScoped<AISettingsProvider>();
// Register AI services with settings provider that reads directly from database
builder.Services.AddTransient<SummaryService>(sp =>
{
var logger = sp.GetRequiredService<ILogger<SummaryService>>();
var settingsProvider = sp.GetRequiredService<AISettingsProvider>();
// Settings provider that fetches directly from database (server-side, unmasked)
Func<Task<(string ApiKey, string BaseUrl, string ModelName)>> provider = async () =>
{
var settings = await settingsProvider.GetAISettingsAsync();
return (
settings.ApiKey,
settings.BaseUrl,
settings.ModelName
);
};
return new SummaryService(logger, provider);
});
builder.Services.AddHttpClient(nameof(ResearchService));
builder.Services.AddTransient<ResearchService>(sp =>
{
var logger = sp.GetRequiredService<ILogger<ResearchService>>();
var settingsProvider = sp.GetRequiredService<AISettingsProvider>();
var httpClientFactory = sp.GetRequiredService<IHttpClientFactory>();
var client = httpClientFactory.CreateClient(nameof(ResearchService));
// Settings provider that fetches directly from database (server-side, unmasked)
Func<Task<(string ApiKey, string BaseUrl, string ModelName)>> provider = async () =>
{
var settings = await settingsProvider.GetAISettingsAsync();
return (
settings.ApiKey,
settings.BaseUrl,
settings.ModelName
);
};
return new ResearchService(client, logger, provider);
});
// Add authentication
builder.Services.AddAuthentication(options =>
{
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
})
.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme)
.AddOpenIdConnect(OpenIdConnectDefaults.AuthenticationScheme, options =>
{
var authority = builder.Configuration["Keycloak:Authority"];
options.Authority = authority;
options.ClientId = builder.Configuration["Keycloak:ClientId"];
options.ClientSecret = builder.Configuration["Keycloak:ClientSecret"];
options.ResponseType = "code";
options.SaveTokens = true;
options.GetClaimsFromUserInfoEndpoint = true;
// Allow overriding RequireHttpsMetadata via configuration for development/docker scenarios.
// If not explicitly configured, relax the requirement when running in a container against HTTP Keycloak.
var requireHttpsConfigured = builder.Configuration.GetValue<bool?>("Keycloak:RequireHttpsMetadata");
var isRunningInContainer = string.Equals(
System.Environment.GetEnvironmentVariable("DOTNET_RUNNING_IN_CONTAINER"),
"true",
StringComparison.OrdinalIgnoreCase);
if (requireHttpsConfigured.HasValue)
{
options.RequireHttpsMetadata = requireHttpsConfigured.Value;
}
else
{
var defaultRequireHttps = !builder.Environment.IsDevelopment();
if (isRunningInContainer &&
!string.IsNullOrEmpty(authority) &&
authority.StartsWith("http://", StringComparison.OrdinalIgnoreCase))
{
defaultRequireHttps = false;
}
options.RequireHttpsMetadata = defaultRequireHttps;
}
options.Scope.Clear();
options.Scope.Add("openid");
options.Scope.Add("profile");
options.Scope.Add("email");
options.TokenValidationParameters = new()
{
NameClaimType = "preferred_username",
RoleClaimType = "roles"
};
// Configure logout to properly pass id_token_hint to Keycloak
options.Events = new OpenIdConnectEvents
{
OnRedirectToIdentityProviderForSignOut = async context =>
{
// Get the id_token from saved tokens
var idToken = await context.HttpContext.GetTokenAsync("id_token");
if (!string.IsNullOrEmpty(idToken))
{
context.ProtocolMessage.IdTokenHint = idToken;
}
}
};
});
builder.Services.AddAuthorization();
builder.Services.AddCascadingAuthenticationState();
builder.Services.AddHttpContextAccessor();
// Add services to the container.
builder.Services.AddRazorComponents()
.AddInteractiveServerComponents();
builder.Services.AddFluentUIComponents();
var app = builder.Build();
app.MapDefaultEndpoints();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error", createScopeForErrors: true);
// 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.UseAntiforgery();
app.UseAuthentication();
app.UseAuthorization();
app.MapRazorComponents<App>()
.AddInteractiveServerRenderMode();
// Authentication endpoints
app.MapGet("/authentication/login", async (HttpContext context, string? returnUrl) =>
{
var authProperties = new AuthenticationProperties
{
RedirectUri = returnUrl ?? "/"
};
await context.ChallengeAsync(OpenIdConnectDefaults.AuthenticationScheme, authProperties);
});
app.MapGet("/authentication/logout", async (HttpContext context) =>
{
var authProperties = new AuthenticationProperties
{
RedirectUri = "/"
};
await context.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
await context.SignOutAsync(OpenIdConnectDefaults.AuthenticationScheme, authProperties);
});
app.Run();