Generate token with random prefix
Added getitem function for specific item - that decrypt is not invoked for previous run of the server - and that server restart always triggers a client update
This commit is contained in:
@ -1,13 +1,17 @@
|
||||
namespace MinecraftDiscordBot;
|
||||
|
||||
public class TimeoutTokenProvider : ITokenProvider {
|
||||
public TimeoutTokenProvider(int timeoutSeconds, ICipher? cipher = null) {
|
||||
public TimeoutTokenProvider(int instanceId, int timeoutSeconds, ICipher? cipher = null) {
|
||||
InstancePrefix = Convert.ToHexString(BitConverter.GetBytes(instanceId));
|
||||
_timeout = timeoutSeconds;
|
||||
_cipher = cipher ?? new AesCipher();
|
||||
}
|
||||
private readonly ICipher _cipher;
|
||||
private readonly int _timeout;
|
||||
public string InstancePrefix { get; }
|
||||
public bool VerifyToken(string token) {
|
||||
if (!token.StartsWith(InstancePrefix)) return false;
|
||||
token = token[InstancePrefix.Length..];
|
||||
byte[] data;
|
||||
try {
|
||||
data = _cipher.Decrypt(Convert.FromHexString(token));
|
||||
@ -21,7 +25,7 @@ public class TimeoutTokenProvider : ITokenProvider {
|
||||
public string GenerateToken() {
|
||||
var time = BitConverter.GetBytes(DateTime.UtcNow.ToBinary());
|
||||
var key = Guid.NewGuid().ToByteArray();
|
||||
var token = Convert.ToHexString(_cipher.Encrypt(time.Concat(key).ToArray()));
|
||||
var token = InstancePrefix + Convert.ToHexString(_cipher.Encrypt(time.Concat(key).ToArray()));
|
||||
return token;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user