2019-02-16 16:04:40 -05:00
|
|
|
|
using SharedLibraryCore.Helpers;
|
|
|
|
|
using SharedLibraryCore.Interfaces;
|
2019-02-15 23:19:59 -05:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
|
using System.Security.Cryptography;
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
|
|
namespace IW4MAdmin.Application.Misc
|
|
|
|
|
{
|
2022-01-26 11:32:16 -05:00
|
|
|
|
internal class TokenAuthentication : ITokenAuthentication
|
2019-02-15 23:19:59 -05:00
|
|
|
|
{
|
2022-06-16 11:07:03 -04:00
|
|
|
|
private readonly ConcurrentDictionary<int, TokenState> _tokens;
|
2022-01-26 11:32:16 -05:00
|
|
|
|
private readonly RandomNumberGenerator _random;
|
2022-06-15 20:37:34 -04:00
|
|
|
|
private static readonly TimeSpan TimeoutPeriod = new(0, 0, 120);
|
2022-01-26 11:32:16 -05:00
|
|
|
|
private const short TokenLength = 4;
|
2019-02-15 23:19:59 -05:00
|
|
|
|
|
|
|
|
|
public TokenAuthentication()
|
|
|
|
|
{
|
2022-06-16 11:07:03 -04:00
|
|
|
|
_tokens = new ConcurrentDictionary<int, TokenState>();
|
2022-01-26 11:32:16 -05:00
|
|
|
|
_random = RandomNumberGenerator.Create();
|
2019-02-15 23:19:59 -05:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-15 20:37:34 -04:00
|
|
|
|
public bool AuthorizeToken(ITokenIdentifier authInfo)
|
2019-02-15 23:19:59 -05:00
|
|
|
|
{
|
2022-06-16 11:07:03 -04:00
|
|
|
|
var authorizeSuccessful = _tokens.ContainsKey(authInfo.ClientId) &&
|
|
|
|
|
_tokens[authInfo.ClientId].Token == authInfo.Token;
|
2019-02-15 23:19:59 -05:00
|
|
|
|
|
|
|
|
|
if (authorizeSuccessful)
|
|
|
|
|
{
|
2022-06-16 11:07:03 -04:00
|
|
|
|
_tokens.TryRemove(authInfo.ClientId, out _);
|
2019-02-15 23:19:59 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return authorizeSuccessful;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-15 20:37:34 -04:00
|
|
|
|
public TokenState GenerateNextToken(ITokenIdentifier authInfo)
|
2019-02-15 23:19:59 -05:00
|
|
|
|
{
|
2022-01-26 11:32:16 -05:00
|
|
|
|
TokenState state;
|
2019-02-16 16:04:40 -05:00
|
|
|
|
|
2022-06-16 11:07:03 -04:00
|
|
|
|
if (_tokens.ContainsKey(authInfo.ClientId))
|
2019-02-15 23:19:59 -05:00
|
|
|
|
{
|
2022-06-16 11:07:03 -04:00
|
|
|
|
state = _tokens[authInfo.ClientId];
|
2019-02-15 23:19:59 -05:00
|
|
|
|
|
2022-06-15 20:37:34 -04:00
|
|
|
|
if (DateTime.Now - state.RequestTime > TimeoutPeriod)
|
2019-02-15 23:19:59 -05:00
|
|
|
|
{
|
2022-06-16 11:07:03 -04:00
|
|
|
|
_tokens.TryRemove(authInfo.ClientId, out _);
|
2019-02-15 23:19:59 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else
|
|
|
|
|
{
|
2019-02-16 16:04:40 -05:00
|
|
|
|
return state;
|
2019-02-15 23:19:59 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-26 11:32:16 -05:00
|
|
|
|
state = new TokenState
|
2019-02-15 23:19:59 -05:00
|
|
|
|
{
|
2019-02-16 16:04:40 -05:00
|
|
|
|
Token = _generateToken(),
|
2022-01-26 11:32:16 -05:00
|
|
|
|
TokenDuration = TimeoutPeriod
|
2019-02-15 23:19:59 -05:00
|
|
|
|
};
|
|
|
|
|
|
2022-06-16 11:07:03 -04:00
|
|
|
|
_tokens.TryAdd(authInfo.ClientId, state);
|
2019-02-16 16:04:40 -05:00
|
|
|
|
|
|
|
|
|
// perform some housekeeping so we don't have built up tokens if they're not ever used
|
|
|
|
|
foreach (var (key, value) in _tokens)
|
|
|
|
|
{
|
2022-06-16 11:07:03 -04:00
|
|
|
|
if (DateTime.Now - value.RequestTime > TimeoutPeriod)
|
2019-02-16 16:04:40 -05:00
|
|
|
|
{
|
2022-01-26 11:32:16 -05:00
|
|
|
|
_tokens.TryRemove(key, out _);
|
2019-02-16 16:04:40 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return state;
|
2019-02-15 23:19:59 -05:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-26 11:32:16 -05:00
|
|
|
|
private string _generateToken()
|
2019-02-15 23:19:59 -05:00
|
|
|
|
{
|
2022-01-26 11:32:16 -05:00
|
|
|
|
bool ValidCharacter(char c)
|
2019-02-15 23:19:59 -05:00
|
|
|
|
{
|
|
|
|
|
// this ensure that the characters are 0-9, A-Z, a-z
|
|
|
|
|
return (c > 47 && c < 58) || (c > 64 && c < 91) || (c > 96 && c < 123);
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-26 11:32:16 -05:00
|
|
|
|
var token = new StringBuilder();
|
2019-02-15 23:19:59 -05:00
|
|
|
|
|
2022-01-26 11:32:16 -05:00
|
|
|
|
var charSet = new byte[1];
|
|
|
|
|
while (token.Length < TokenLength)
|
2019-02-15 23:19:59 -05:00
|
|
|
|
{
|
|
|
|
|
_random.GetBytes(charSet);
|
|
|
|
|
|
2022-01-26 11:32:16 -05:00
|
|
|
|
if (ValidCharacter((char)charSet[0]))
|
2019-02-15 23:19:59 -05:00
|
|
|
|
{
|
|
|
|
|
token.Append((char)charSet[0]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_random.Dispose();
|
|
|
|
|
return token.ToString();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|