This repository was archived by the owner on Dec 2, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathUserController.cs
More file actions
467 lines (404 loc) · 16.2 KB
/
UserController.cs
File metadata and controls
467 lines (404 loc) · 16.2 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
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
using Commons;
using Commons.Collections;
using Commons.Enums;
using Commons.Filters;
using Isopoh.Cryptography.Argon2;
using Isopoh.Cryptography.SecureArray;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using MongoService;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Net;
using System.Net.Http;
using System.Net.Mail;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using WebAPI.Attributes;
using WebAPI.Models;
using static Commons.AppSettings;
namespace WebAPI.Controllers
{
/// <summary>
/// CRUD Controller for User resource
/// </summary>
[ApiVersion("1")]
[Route("user")]
[ApiController]
public class UserController : Controller
{
private readonly ILogger<UserController> _logger;
private IConfiguration _configuration;
private UserCollection _userCollection = new UserCollection();
private readonly string _recaptchaBaseURL = "https://www.google.com/recaptcha/api/siteverify";
private static readonly RandomNumberGenerator _rng = RandomNumberGenerator.Create();
public UserController(ILogger<UserController> logger, IConfiguration configuration)
{
_logger = logger;
_configuration = configuration;
}
/// <summary>
/// Retrieves a specific User by user id
/// </summary>
/// <param name="id">The User id</param>
[AllowAnonymous]
[EnableCors("CorsEveryone")]
[HttpGet("{id}"), MapToApiVersion("1")]
public APIResponse GetOne(long id)
{
try
{
User user = this._userCollection.Get(id);
if (user == null)
{
throw new APIException(HttpStatusCode.NotFound,
"User not found",
$"User with id {id} does not exists");
}
user.HideConfindentialValues();
return APIManager.SuccessResponse("User found", user);
}
catch (APIException ex)
{
return APIManager.ErrorResponse(ex);
}
catch (Exception ex)
{
this._logger.LogError(ex.Message);
return APIManager.ErrorResponse();
}
}
/// <summary>
/// Retrieves a list of User
/// </summary>
/// <param name="filter">The User filter</param>
[AllowAnonymous]
[EnableCors("CorsEveryone")]
[HttpGet, MapToApiVersion("1")]
public APIResponse GetMore([FromQuery] UserFilter filter)
{
try
{
Paging<User> result = this._userCollection.GetList<UserFilter>(filter);
if (result.LastPage == 0)
{
throw new APIException(HttpStatusCode.NotFound,
"Zero users found",
new List<User>());
}
if (filter.page > result.LastPage)
{
throw new APIException(HttpStatusCode.NotFound,
"Page out of range",
$"Last page number available is {result.LastPage}");
}
foreach(User user in result.Documents)
{
user.HideConfindentialValues();
}
return APIManager.SuccessResponse($"Page {result.CurrentPage} contains {result.Documents.Count} users. Last page number is {result.LastPage} for a total of {result.Count} users", result);
}
catch (APIException ex)
{
return APIManager.ErrorResponse(ex);
}
catch (Exception ex)
{
this._logger.LogError(ex.Message);
return APIManager.ErrorResponse();
}
}
/// <summary>
/// /// Create a new User
/// </summary>
/// <param name="g_recaptcha_response">The CAPTCHA status token</param>
/// <param name="model">The User model</param>
/// <returns></returns>
[AllowAnonymous]
#if DEBUG
[EnableCors("CorsEveryone")]
#else
[EnableCors("CorsInternal")]
#endif
[HttpPut, MapToApiVersion("1")]
[ApiExplorerSettings(IgnoreApi = true)]
public async Task<APIResponse> Create(string g_recaptcha_response, [FromBody] User model)
{
try
{
HttpClient httpClient = new HttpClient();
string recaptchaUrl = $"{_recaptchaBaseURL}?secret={_configuration.GetValue<string>("recaptcha_secret")}&response={g_recaptcha_response}&remoteip={HttpUtility.UrlEncode(Request.HttpContext.Connection.RemoteIpAddress.ToString())}";
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, recaptchaUrl);
HttpResponseMessage response = await httpClient.SendAsync(request);
GRecaptchaResponse result = JsonConvert.DeserializeObject<GRecaptchaResponse>(await response.Content.ReadAsStringAsync());
if (!result.Success)
{
throw new APIException(System.Net.HttpStatusCode.Unauthorized,
"Invalid CAPTCHA",
"CAPTCHA result is not valid");
}
if (string.IsNullOrEmpty(model.Username))
{
throw new APIException(HttpStatusCode.BadRequest,
"Missing username",
"Please provide a valid username");
}
if (string.IsNullOrEmpty(model.Email))
{
throw new APIException(HttpStatusCode.BadRequest,
"Missing email",
"Please provide a valid email");
}
if (string.IsNullOrEmpty(model.Password))
{
throw new APIException(HttpStatusCode.BadRequest,
"Missing password",
"Please provide a valid password");
}
if(!new EmailAddressAttribute().IsValid(model.Email))
{
throw new APIException(HttpStatusCode.UnprocessableEntity,
"Email not valid",
"Please provide a valid email");
}
List<User> collisions = new List<User>();
collisions.AddRange(this._userCollection.GetList(new UserFilter()
{
username = model.Username
}).Documents);
if(collisions.Count > 0)
{
throw new APIException(HttpStatusCode.Conflict,
"Duplicated username",
"The username provided is already used by another user");
}
collisions.AddRange(this._userCollection.GetList(new UserFilter()
{
email = model.Email
}).Documents);
if (collisions.Count > 0)
{
throw new APIException(HttpStatusCode.Conflict,
"Duplicated email",
"The email provided is already used by another user");
}
collisions = null;
byte[] passwordBytes = Encoding.UTF8.GetBytes(model.Password);
byte[] salt = new byte[16];
_rng.GetBytes(salt);
Argon2Config argonConfig = new Argon2Config()
{
Type = Argon2Type.DataIndependentAddressing,
Version = Argon2Version.Nineteen,
TimeCost = 10,
MemoryCost = 32768,
Lanes = 5,
Threads = Environment.ProcessorCount,
Password = passwordBytes,
Salt = salt,
HashLength = 20
};
Argon2 argon = new Argon2(argonConfig);
using (SecureArray<byte> hash = argon.Hash())
{
model.PasswordHash = argonConfig.EncodeString(hash.Buffer);
}
model.Password = null;
model.Role = UserRoleEnum.BASIC;
model.EmailVerified = false;
this._userCollection.Add(ref model);
SmtpClient smtp = new SmtpClient(_configuration.GetValue<string>("smtp_host"), _configuration.GetValue<int>("smtp_port"))
{
Credentials = new NetworkCredential(_configuration.GetValue<string>("smtp_username"), _configuration.GetValue<string>("smtp_password")),
EnableSsl = true
};
MailMessage mail = new MailMessage()
{
From = new MailAddress(_configuration.GetValue<string>("smtp_address")),
Subject = "Welcome to AniAPI",
Body = $"Welcome <b>{model.Username}</b>,<br/>click on the link below to confirm your account and start using <a href=\"https://aniapi.com\" target=\"_blank\">AniAPI</a>.<br/><a href=\"https://api.aniapi.com/v1/auth/{model.Id}\" target=\"_blank\">Complete registration</a>",
IsBodyHtml = true
};
mail.To.Add(model.Email);
smtp.Send(mail);
return APIManager.SuccessResponse("User created", model);
}
catch(APIException ex)
{
return APIManager.ErrorResponse(ex);
}
catch(Exception ex)
{
this._logger.LogError(ex.Message);
return APIManager.ErrorResponse();
}
}
/// <summary>
/// Update an existing User
/// </summary>
/// <param name="model">The User model</param>
[Attributes.Authorize]
#if DEBUG
[EnableCors("CorsEveryone")]
#else
[EnableCors("CorsInternal")]
#endif
[HttpPost, MapToApiVersion("1")]
public APIResponse Update([FromBody] User model)
{
try
{
User authenticatedUser = (User)HttpContext.Items["user"];
if(authenticatedUser.Role == UserRoleEnum.BASIC && authenticatedUser.Id != model.Id)
{
throw new APIException(HttpStatusCode.Forbidden,
"Forbidden",
"You have no access rights to edit this user");
}
if(!this._userCollection.Exists(ref model, false))
{
throw new APIException(HttpStatusCode.NotFound,
"User not found",
$"User with id {model.Id} does not exists");
}
User user = this._userCollection.Get(model.Id);
if (!string.IsNullOrEmpty(model.Password))
{
byte[] passwordBytes = Encoding.UTF8.GetBytes(model.Password);
byte[] salt = new byte[16];
_rng.GetBytes(salt);
Argon2Config argonConfig = new Argon2Config()
{
Type = Argon2Type.DataIndependentAddressing,
Version = Argon2Version.Nineteen,
TimeCost = 10,
MemoryCost = 32768,
Lanes = 5,
Threads = Environment.ProcessorCount,
Password = passwordBytes,
Salt = salt,
HashLength = 20
};
Argon2 argon = new Argon2(argonConfig);
using (SecureArray<byte> hash = argon.Hash())
{
user.PasswordHash = argonConfig.EncodeString(hash.Buffer);
}
model.Password = null;
}
user.Gender = model.Gender;
if(model.Localization != null)
{
user.Localization = model.Localization;
}
if(model.AnilistId != null)
{
user.AnilistId = model.AnilistId;
if (string.IsNullOrEmpty(model.AnilistToken))
{
throw new APIException(HttpStatusCode.BadRequest,
"Bad request",
"'anilist_id' field must come with 'anilist_token' field");
}
user.AnilistToken = model.AnilistToken;
if (string.IsNullOrEmpty(user.AvatarTracker))
{
user.AvatarTracker = "anilist";
}
}
if(model.MyAnimeListId != null)
{
user.MyAnimeListId = model.MyAnimeListId;
if (string.IsNullOrEmpty(model.MyAnimeListToken))
{
throw new APIException(HttpStatusCode.BadRequest,
"Bad request",
"'mal_id' field must come with 'mal_token' field");
}
user.MyAnimeListToken = model.MyAnimeListToken;
if (string.IsNullOrEmpty(user.AvatarTracker))
{
user.AvatarTracker = "mal";
}
}
if(model.AvatarTracker != null)
{
user.AvatarTracker = model.AvatarTracker;
}
this._userCollection.Edit(ref user);
user.Email = null;
user.EmailVerified = null;
user.PasswordHash = null;
user.LastLoginDate = null;
user.Token = null;
user.CalcDerivedFields();
user.AnilistId = null;
user.AnilistToken = null;
user.MyAnimeListId = null;
user.MyAnimeListToken = null;
return APIManager.SuccessResponse("User updated", user);
}
catch (APIException ex)
{
return APIManager.ErrorResponse(ex);
}
catch (Exception ex)
{
this._logger.LogError(ex.Message);
return APIManager.ErrorResponse();
}
}
/// <summary>
/// Delete an existing User by id
/// </summary>
/// <param name="id">The User id</param>
[Attributes.Authorize]
#if DEBUG
[EnableCors("CorsEveryone")]
#else
[EnableCors("CorsInternal")]
#endif
[HttpDelete("{id}"), MapToApiVersion("1")]
public APIResponse Delete(long id)
{
try
{
User authenticatedUser = (User)HttpContext.Items["user"];
if (authenticatedUser.Role == UserRoleEnum.BASIC && authenticatedUser.Id != id)
{
throw new APIException(HttpStatusCode.Forbidden,
"Forbidden",
"You have no access rights to edit this user");
}
User user = new User()
{
Id = id
};
if (!this._userCollection.Exists(ref user, false))
{
throw new APIException(HttpStatusCode.NotFound,
"User not found",
$"User with id {user.Id} does not exists");
}
this._userCollection.Delete(user.Id);
return APIManager.SuccessResponse("User deleted");
}
catch (APIException ex)
{
return APIManager.ErrorResponse(ex);
}
catch (Exception ex)
{
this._logger.LogError(ex.Message);
return APIManager.ErrorResponse();
}
}
}
}