1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using HarmonyLib;
|
---|
4 | using JetBrains.Annotations;
|
---|
5 |
|
---|
6 | namespace Webserver {
|
---|
7 | public static class UserRegistrationTokens {
|
---|
8 | private const float tokenExpirationMinutes = 3;
|
---|
9 |
|
---|
10 | public class RegistrationData {
|
---|
11 | public readonly string PlayerName;
|
---|
12 | public readonly DateTime ExpiryTime;
|
---|
13 | public readonly PlatformUserIdentifierAbs PlatformUserId;
|
---|
14 | public readonly PlatformUserIdentifierAbs CrossPlatformUserId;
|
---|
15 |
|
---|
16 | public RegistrationData (string _playerName, PlatformUserIdentifierAbs _platformUserId, PlatformUserIdentifierAbs _crossPlatformUserId) {
|
---|
17 | PlayerName = _playerName;
|
---|
18 | ExpiryTime = DateTime.Now + TimeSpan.FromMinutes (tokenExpirationMinutes);
|
---|
19 | PlatformUserId = _platformUserId;
|
---|
20 | CrossPlatformUserId = _crossPlatformUserId;
|
---|
21 | }
|
---|
22 | }
|
---|
23 |
|
---|
24 | public static bool TryValidate (string _token, out RegistrationData _data) {
|
---|
25 | return activeTokens.TryGetValue (_token, out _data) && _data.ExpiryTime > DateTime.Now;
|
---|
26 | }
|
---|
27 |
|
---|
28 | private static readonly Dictionary<string, RegistrationData> activeTokens = new Dictionary<string, RegistrationData> ();
|
---|
29 |
|
---|
30 | [HarmonyPatch(typeof(ConsoleCmdCreateWebUser))]
|
---|
31 | [HarmonyPatch("createToken")]
|
---|
32 | private class CommandPatch {
|
---|
33 | [UsedImplicitly]
|
---|
34 | private static bool Prefix (ref string __result, string _playerName, PlatformUserIdentifierAbs _platformUserId, PlatformUserIdentifierAbs _crossPlatformUserId) {
|
---|
35 | string token = Utils.GenerateGuid ();
|
---|
36 |
|
---|
37 | bool hasCrossplatformId = _crossPlatformUserId != null;
|
---|
38 | DateTime currentTime = DateTime.Now;
|
---|
39 |
|
---|
40 | // Check if the user already has a registration attempt running and if so delete the old one
|
---|
41 | // Also clear out any expired tokens
|
---|
42 | activeTokens.RemoveAll (_data => _data.ExpiryTime < currentTime || _platformUserId.Equals (_data.PlatformUserId) ||
|
---|
43 | (hasCrossplatformId && _crossPlatformUserId.Equals (_data.CrossPlatformUserId)));
|
---|
44 |
|
---|
45 | RegistrationData data = new RegistrationData (_playerName, _platformUserId, _crossPlatformUserId);
|
---|
46 | activeTokens [token] = data;
|
---|
47 |
|
---|
48 | __result = token;
|
---|
49 | return false;
|
---|
50 | }
|
---|
51 | }
|
---|
52 |
|
---|
53 | }
|
---|
54 | }
|
---|