-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
228 lines (199 loc) · 7.59 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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
using System.Security.Claims;
using authority;
using authority.Schema;
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.EntityFrameworkCore;
using Microsoft.OpenApi.Models;
var builder = WebApplication.CreateBuilder(args);
Console.WriteLine($"Running app in {builder.Environment.EnvironmentName} mode");
var swaggerisAvailable = builder.Configuration.GetValue<bool>("Swagger_Enabled");
builder.Logging.ClearProviders();
builder.Logging.AddConsole();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(options =>
{
options.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
{
Name = "Authorization",
Type = SecuritySchemeType.ApiKey,
Scheme = "Bearer",
BearerFormat = "JWT",
In = ParameterLocation.Header,
Description = "JSON Web Token based security"
});
options.AddSecurityRequirement(new OpenApiSecurityRequirement{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "Bearer"
}
},
Array.Empty<string>()
}
});
});
if (builder.Environment.IsDevelopment())
builder.Services.AddDbContext<Db>(options =>
{
options.UseNpgsql(Db.GetDbConnetion(builder));
options.EnableSensitiveDataLogging();
});
else
builder.Services.AddDbContext<Db>(options => options.UseNpgsql(Db.GetDbConnetion(builder)));
builder.Services.AddSingleton<IPrivateRSAProvider>(
new PrivateRSAProvider(builder.Configuration.GetValue<string>("Private_Key") ?? ".private.pem"));
builder.Services.AddSingleton<IJWTTokenFactoryOptions>(new JWTTokenFactoryOptions(
TimeSpan.FromHours(Convert.ToInt32(builder.Configuration.GetValue<string>("Token_Expiration_Hours") ?? "24")),
TimeSpan.FromDays(Convert.ToInt32(builder.Configuration.GetValue<string>("Refresh_Expiration_Days") ?? "30")),
builder.Configuration.GetValue<string>("Authority") ?? "dnd-authority",
(builder.Configuration.GetValue<string>("Audiance") ?? "dnd-authority,dnd-api").Split(',')
));
builder.Services.AddSingleton<IJWTTokenFactory, JWTTokenFactory>();
builder.Services.AddScoped<IPasswordHasher>((service) =>
new PasswordHasher(builder.Configuration.GetValue<int>("Reset_Token_Expiration_Hours")));
builder.Services.AddScoped<IUserService, UserService>();
builder.Services.AddScoped<IMail>((service) => new Mail(
builder.Configuration.GetValue<string>("SMTP_Host") ?? "",
builder.Configuration.GetValue<int>("SMTP_Port"),
builder.Configuration.GetValue<bool>("SMTP_SSL"),
builder.Configuration.GetValue<string>("SMTP_Username") ?? "",
builder.Configuration.GetValue<string>("SMTP_Password") ?? "",
builder.Configuration.GetValue<string>("Email_From") ?? "",
builder.Configuration.GetValue<string>("Email_From_Name") ?? "",
builder.Configuration.GetValue<string>("Reset_Email_Text_Address") ?? "",
builder.Configuration.GetValue<string>("Reset_Email_Html_Address") ?? "",
builder.Configuration.GetValue<string>("Reset_Email_Link") ?? "",
service.GetService<ILogger>()!));
builder.AddJWTAuthentication();
builder.Services.AddAuthorization();
builder.Services.AddCors();
var app = builder.Build();
using (var scope = app.Services.CreateScope())
{
if (scope.ServiceProvider.GetService<Db>() is { } db &&
scope.ServiceProvider.GetService<IUserService>() is { } userService)
{
await db.Database.MigrateAsync();
await userService.SeedAdminsAsync(
[.. (builder.Configuration.GetValue<string>("Admin_User_Emails") ?? "[email protected],[email protected]").Split(',')],
[.. (builder.Configuration.GetValue<string>("Admin_User_Names") ?? "aaa,bbb").Split(',')],
[.. (builder.Configuration.GetValue<string>("Admin_User_Password") ?? "ab123,ab123").Split(',')]);
}
}
app.UseCors(builder =>
{
builder.AllowAnyHeader();
builder.AllowAnyMethod();
builder.AllowAnyOrigin();
});
app.UseAuthentication();
app.UseAuthorization();
if (swaggerisAvailable)
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.MapPost("/register", async Task<Results<BadRequest<string>, Ok<JWTToken>>> (RegisterInfo registerInfo,
IJWTTokenFactory tokenFactory, Db db, IUserService userService) =>
{
try
{
var user = await userService.SignUpAsync(new User
{
Email = registerInfo.Email,
Name = registerInfo.Name
}, registerInfo.Password);
user.LastLogIn = DateTime.UtcNow;
await db.SaveChangesAsync();
return TypedResults.Ok(tokenFactory.Sign(user));
}
catch { }
return TypedResults.BadRequest(registerInfo.Email);
})
.AllowAnonymous()
.WithTags("authority")
.WithDescription("Create new user for application")
.WithOpenApi();
app.MapPost("/refresh", Results<UnauthorizedHttpResult, Ok<JWTToken>>
(JWTToken token, IJWTTokenFactory tokenFactory, ILogger<Program> logger) =>
{
try
{
return TypedResults.Ok(tokenFactory.Refresh(token));
}
catch (Exception ex)
{
logger.LogWarning(ex.Message);
}
return TypedResults.Unauthorized();
})
.AllowAnonymous()
.WithTags("authority")
.WithDescription("Refreshing provided token")
.WithOpenApi();
app.MapPost("/login", async Task<Results<UnauthorizedHttpResult, Ok<JWTToken>>> (PasswordLogin login,
IJWTTokenFactory tokenFactory, IUserService userService) =>
{
try
{
return TypedResults.Ok(tokenFactory.Sign(await userService
.SignInAsync(login.Email, login.Password)));
}
catch { }
return TypedResults.Unauthorized();
})
.AllowAnonymous()
.WithTags("authority")
.WithDescription("Login using email and password")
.WithOpenApi();
app.MapPost("/resetPassword", async Task<Results<NotFound<string>, UnauthorizedHttpResult, Ok>>
(ResetPassword reset, IUserService userService, IMail mail) =>
{
try
{
var user = await userService.GetAsync(reset.Email);
if (string.IsNullOrEmpty(reset.Password) || string.IsNullOrEmpty(reset.ResetToken))
{
await userService.GenerateResetPasswordToken(user);
await mail.SendResetAsync(user);
return TypedResults.Ok();
}
if (user.ResetToken == reset.ResetToken && DateTime.UtcNow < user.ResetExpirationTime)
{
await userService.ChangePasswordAsync(user, reset.Password);
return TypedResults.Ok();
}
return TypedResults.Unauthorized();
}
catch { }
return TypedResults.NotFound(reset.Email);
})
.AllowAnonymous()
.WithTags("authority")
.WithDescription("Reset password / send reset password mail")
.WithOpenApi();
app.MapPost("/changePassword", async Task<Results<Ok, UnauthorizedHttpResult>>
(ChangePassword change, ClaimsPrincipal principal, IUserService userService) =>
{
try
{
var user = await userService.SignInAsync(principal.GetUser().Email, change.Password);
await userService.ChangePasswordAsync(user, change.NewPassword);
return TypedResults.Ok();
}
catch { }
return TypedResults.Unauthorized();
})
.RequireAuthorization(Authorization.User)
.WithTags("authority")
.WithDescription("Change password")
.WithOpenApi();
app.MapGet("/me", (ClaimsPrincipal principal) => new Profile(principal.GetUser()))
.RequireAuthorization(Authorization.User)
.WithTags("authority")
.WithDescription("Getting current user's info")
.WithOpenApi();
app.Run();