-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathRegistrationService.cs
More file actions
52 lines (41 loc) · 1.51 KB
/
RegistrationService.cs
File metadata and controls
52 lines (41 loc) · 1.51 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 System.Security.Cryptography;
using PocketDDD.Server.DB;
using PocketDDD.Server.Model.DBModel;
using PocketDDD.Shared.API.RequestDTOs;
using PocketDDD.Shared.API.ResponseDTOs;
namespace PocketDDD.Server.Services;
public class RegistrationService(PocketDDDContext dbContext, EventDataService eventDataService)
{
public async Task<LoginResultDTO> Register(RegisterDTO dto)
{
var currentEventDetailId = await eventDataService.FetchCurrentEventDetailId();
var user = new User
{
EventDetailId = currentEventDetailId,
Name = dto.Name,
Token = GenerateBearerToken(),
EventScore = 1
};
dbContext.Users.Add(user);
await dbContext.SaveChangesAsync();
var dtoResponse = new LoginResultDTO(user.Name, user.Token);
return dtoResponse;
}
private string GenerateBearerToken()
{
var characterArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789".Distinct().ToArray();
const int length = 40;
var token = new char[length];
using (var cryptRNG = RandomNumberGenerator.Create())
{
byte[] tokenBuffer = new byte[length * 8];
cryptRNG.GetBytes(tokenBuffer);
for (int i = 0; i < length; i++)
{
ulong value = BitConverter.ToUInt64(tokenBuffer, i * 8);
token[i] = characterArray[value % (uint)characterArray.Length];
}
}
return new string(token);
}
}