-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
52 lines (43 loc) · 1.88 KB
/
Program.cs
File metadata and controls
52 lines (43 loc) · 1.88 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
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using InterviewCopilotServer.Interfaces;
using InterviewCopilotServer.Services;
using Microsoft.Extensions.Caching.Memory;
using InterviewCopilotServer.Middleware;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(options =>
{
options.Authority = "https://login.microsoftonline.com/consumers/v2.0"; // Replace with your tenant ID
options.Audience = builder.Configuration.GetValue<string>("AAD_CLIENT_ID") ?? Environment.GetEnvironmentVariable("AAD_CLIENT_ID");
});
string aadClientId = builder.Configuration.GetValue<string>("AAD_CLIENT_ID") ?? Environment.GetEnvironmentVariable("AAD_CLIENT_ID");
builder.Services.AddAuthorization(options =>
{
options.AddPolicy("ApiScope", policy =>
{
policy.RequireAuthenticatedUser();
policy.RequireClaim("scope", $"api://{aadClientId}/profile");
});
});
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddSingleton<IOpenAIService, OpenAIService>();
builder.Services.AddSingleton<ISecretService, SecretService>();
builder.Services.AddMemoryCache();
builder.Services.AddCors();
builder.Configuration.AddEnvironmentVariables().AddUserSecrets<StartupBase>();
var app = builder.Build();
app.UseSwagger();
app.UseSwaggerUI();
app.UseMiddleware<ApiKeyRateLimiterMiddleware>(new MemoryCache(new MemoryCacheOptions()), TimeSpan.FromMinutes(1));
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();