Files
ClubPenguinServer/MareSynchronosServer/MareSynchronosShared/Utils/ServerTokenGenerator.cs
rootdarkarchon 42b15cb6b7 Add Server-Side Download Queue (#21)
* test add queueing to file service

* further adjustments to download queueing

* add check for whether the request is still in the queue to CheckQueue

* forcefully release slot if download didn't finish in 15s

* actually cancel the delay task

* add metrics and refactor some of the request queue service

* refactor pathing

* reuse httpclient

* add queue request dto to requestfile, enqueue users immediately if a slot is available

* change startup to include all controllers

* update server pathing

* update pathing, again

* several adjustments to auth, banning, jwt server tokens, renaming, authorization

* update api I guess

* adjust automated banning of charaident and reg

* generate jwt on servers for internal authentication

* remove mvcextensions

Co-authored-by: rootdarkarchon <root.darkarchon@outlook.com>
2023-01-11 12:22:22 +01:00

55 lines
1.8 KiB
C#

using MareSynchronosShared.Services;
using Microsoft.IdentityModel.Tokens;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text;
namespace MareSynchronosShared.Utils;
public class ServerTokenGenerator
{
private readonly IConfigurationService<MareConfigurationAuthBase> _configuration;
private Dictionary<string, string> _tokenDictionary { get; set; } = new(StringComparer.Ordinal);
public string Token
{
get
{
var currentJwt = _configuration.GetValue<string>(nameof(MareConfigurationAuthBase.Jwt));
if (_tokenDictionary.TryGetValue(currentJwt, out var token))
{
return token;
}
return GenerateToken();
}
}
public ServerTokenGenerator(IConfigurationService<MareConfigurationAuthBase> configuration)
{
_configuration = configuration;
}
private string GenerateToken()
{
var signingKey = _configuration.GetValue<string>(nameof(MareConfigurationAuthBase.Jwt));
var authSigningKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(signingKey));
var token = new SecurityTokenDescriptor()
{
Subject = new ClaimsIdentity(new List<Claim>()
{
new Claim(MareClaimTypes.Uid, _configuration.GetValue<string>(nameof(MareConfigurationBase.ShardName))),
new Claim(MareClaimTypes.Internal, true.ToString())
}),
SigningCredentials = new SigningCredentials(authSigningKey, SecurityAlgorithms.HmacSha256Signature)
};
var handler = new JwtSecurityTokenHandler();
var rawData = handler.CreateJwtSecurityToken(token).RawData;
_tokenDictionary[signingKey] = rawData;
return rawData;
}
}