mirror of
https://github.com/simtactics/mysimulation.git
synced 2025-03-23 01:42:24 +00:00
- NioTSO client isn't needed because we're using RayLib - Added FreeSO's API server to handle most backend operations
48 lines
1.4 KiB
C#
Executable file
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 };
|
|
}
|
|
}
|
|
}
|