mysimulation/server/FSO.Server.Common/JsonWebToken/JWTokenFactory.cs
Tony Bark 22191ce648 Removed NioTSO client and server
- NioTSO client isn't needed because we're using RayLib
- Added FreeSO's API server to handle most backend operations
2024-05-01 02:55:43 -04:00

48 lines
1.4 KiB
C#
Executable file

using FSO.Server.Common;
using Newtonsoft.Json;
using System.Collections.Generic;
namespace FSO.Server.Servers.Api.JsonWebToken
{
public class JWTInstance
{
public string Token;
public int ExpiresIn;
}
public class JWTFactory
{
private JWTConfiguration Config;
public JWTFactory(JWTConfiguration config)
{
this.Config = config;
}
public JWTUser DecodeToken(string token)
{
var payload = JWT.JsonWebToken.Decode(token, Config.Key, true);
Dictionary<string, string> payloadParsed = JsonConvert.DeserializeObject<Dictionary<string, string>>(payload);
return Newtonsoft.Json.JsonConvert.DeserializeObject<JWTUser>(payloadParsed["data"]);
}
public JWTInstance CreateToken(JWTUser data)
{
var tokenData = Newtonsoft.Json.JsonConvert.SerializeObject(data);
return CreateToken(tokenData, Config.TokenDuration);
}
private JWTInstance CreateToken(string data, int expiresIn)
{
var expires = Epoch.Now + expiresIn;
var payload = new Dictionary<string, object>()
{
{ "exp", expires },
{ "data", data }
};
var token = JWT.JsonWebToken.Encode(payload, Config.Key, JWT.JwtHashAlgorithm.HS384);
return new JWTInstance { Token = token, ExpiresIn = expiresIn };
}
}
}