-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
177 lines (146 loc) · 5 KB
/
Program.cs
File metadata and controls
177 lines (146 loc) · 5 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
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using Scalar.AspNetCore;
using sparkly_server.Enum;
using sparkly_server.Infrastructure;
using sparkly_server.Services.Auth;
using sparkly_server.Services.Auth.provider;
using sparkly_server.Services.Auth.service;
using sparkly_server.Services.Posts.repo;
using sparkly_server.Services.Posts.service;
using sparkly_server.Services.Projects;
using sparkly_server.Services.Projects.repo;
using sparkly_server.Services.Projects.service;
using sparkly_server.Services.Users;
using sparkly_server.Services.Users.CurrentUser;
using sparkly_server.Services.Users.repo;
using sparkly_server.Services.Users.service;
using System.Text;
var builder = WebApplication.CreateBuilder(args);
// JWT configuration
var jwtKey = builder.Configuration["SPARKLY_JWT_KEY"]
?? Environment.GetEnvironmentVariable("SPARKLY_JWT_KEY");
if (string.IsNullOrWhiteSpace(jwtKey))
{
if (builder.Environment.IsDevelopment() || builder.Environment.IsEnvironment("Testing"))
{
jwtKey = "this-is-very-long-test-jwt-key-123456";
}
else
{
throw new Exception("JWT key missing");
}
}
var jwtIssuer = builder.Configuration["SPARKLY_JWT_ISSUER"]
?? Environment.GetEnvironmentVariable("SPARKLY_JWT_ISSUER")
?? "sparkly";
var jwtAudience = builder.Configuration["SPARKLY_JWT_AUDIENCE"]
?? Environment.GetEnvironmentVariable("SPARKLY_JWT_AUDIENCE")
?? "sparkly-api";
// Common services
builder.Services.AddHttpContextAccessor();
builder.Services.AddAuthorization(options =>
{
options.AddPolicy("AdminOnly", policy =>
policy.RequireRole(Roles.Admin));
});
// Domain / app services
builder.Services.AddScoped<IUserRepository, UserRepository>();
builder.Services.AddScoped<IUserService, UserService>();
builder.Services.AddScoped<IJwtProvider, JwtProvider>();
builder.Services.AddScoped<IAuthService, AuthService>();
builder.Services.AddScoped<ICurrentUser, CurrentUser>();
builder.Services.AddScoped<IProjectRepository, ProjectRepository>();
builder.Services.AddScoped<IProjectService, ProjectService>();
builder.Services.AddScoped<IPostRepository, PostRepository>();
builder.Services.AddScoped<IPostService, PostService>();
// Database
if (builder.Environment.IsEnvironment("Testing"))
{
builder.Services.AddDbContext<AppDbContext>(options =>
{
options.UseInMemoryDatabase("sparkly-tests");
});
}
else
{
var connectionString = builder.Configuration.GetConnectionString("Default")
?? Environment.GetEnvironmentVariable("ConnectionStrings__Default")
?? throw new Exception("Connection string 'Default' not found.");
builder.Services.AddDbContext<AppDbContext>(options =>
{
options.UseNpgsql(connectionString);
});
}
builder.Services.AddControllers();
// OpenAPI / Scalar
builder.Services.AddOpenApi();
// CORS
builder.Services.AddCors(options =>
{
options.AddPolicy("FrontendDev", policy =>
{
policy
.WithOrigins("http://localhost:4200")
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials();
});
});
// Authentication
builder.Services
.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
var keyBytes = Encoding.UTF8.GetBytes(jwtKey);
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = false,
ValidateAudience = false,
ValidateLifetime = true,
IssuerSigningKey = new SymmetricSecurityKey(keyBytes),
ValidateIssuerSigningKey = true,
ClockSkew = TimeSpan.FromMinutes(1),
};
options.Events = new JwtBearerEvents
{
OnAuthenticationFailed = context =>
{
Console.WriteLine("[JWT] Authentication failed:");
Console.WriteLine(context.Exception.ToString());
return Task.CompletedTask;
},
OnChallenge = context =>
{
Console.WriteLine("[JWT] Challenge fired:");
Console.WriteLine($"Error: {context.Error}, Desc: {context.ErrorDescription}");
return Task.CompletedTask;
}
};
});
var app = builder.Build();
// Pipeline
if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
app.MapScalarApiReference();
}
// Run migrations only outside Testing
if (!app.Environment.IsEnvironment("Testing"))
{
using var scope = app.Services.CreateScope();
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
db.Database.Migrate();
}
if (!app.Environment.IsEnvironment("Testing"))
{
app.UseHttpsRedirection();
}
app.UseAuthentication();
app.UseAuthorization();
app.UseCors("FrontendDev");
app.MapControllers();
app.MapGet("/healthz", () => Results.Ok(new { status = "ok" }));
app.Run();
public partial class Program;