PalacePal/Pal.Client/Net/JwtClaims.cs

78 lines
2.7 KiB
C#
Raw Permalink Normal View History

2022-12-11 14:22:41 +00:00
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.Json;
2023-03-26 13:47:18 +00:00
using System.Text.Json.Serialization;
2022-12-11 14:22:41 +00:00
2023-03-30 20:01:43 +00:00
namespace Pal.Client.Net;
2022-12-11 14:22:41 +00:00
2023-03-30 20:01:43 +00:00
internal sealed class JwtClaims
{
[JsonPropertyName("nameid")]
public Guid NameId { get; set; }
2022-12-11 14:22:41 +00:00
2023-03-30 20:01:43 +00:00
[JsonPropertyName("role")]
[JsonConverter(typeof(JwtRoleConverter))]
public List<string> Roles { get; set; } = new();
2022-12-11 14:22:41 +00:00
2023-03-30 20:01:43 +00:00
[JsonPropertyName("nbf")]
[JsonConverter(typeof(JwtDateConverter))]
public DateTimeOffset NotBefore { get; set; }
2022-12-11 14:22:41 +00:00
2023-03-30 20:01:43 +00:00
[JsonPropertyName("exp")]
[JsonConverter(typeof(JwtDateConverter))]
public DateTimeOffset ExpiresAt { get; set; }
2022-12-11 14:22:41 +00:00
2023-03-30 20:01:43 +00:00
public static JwtClaims FromAuthToken(string authToken)
{
if (string.IsNullOrEmpty(authToken))
throw new ArgumentException("Server sent no auth token", nameof(authToken));
string[] parts = authToken.Split('.');
if (parts.Length != 3)
throw new ArgumentException("Unsupported token type", nameof(authToken));
// fix padding manually
string payload = parts[1].Replace(",", "=").Replace("-", "+").Replace("/", "_");
if (payload.Length % 4 == 2)
payload += "==";
else if (payload.Length % 4 == 3)
payload += "=";
string content = Encoding.UTF8.GetString(Convert.FromBase64String(payload));
return JsonSerializer.Deserialize<JwtClaims>(content) ?? throw new InvalidOperationException("token deserialization returned null");
2022-12-11 14:22:41 +00:00
}
2023-03-30 20:01:43 +00:00
}
2022-12-11 14:22:41 +00:00
2023-03-30 20:01:43 +00:00
internal sealed class JwtRoleConverter : JsonConverter<List<string>>
{
public override List<string> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
2022-12-11 14:22:41 +00:00
{
2023-03-30 20:01:43 +00:00
if (reader.TokenType == JsonTokenType.String)
return new List<string> { reader.GetString() ?? throw new JsonException("no value present") };
else if (reader.TokenType == JsonTokenType.StartArray)
2022-12-11 14:22:41 +00:00
{
2023-03-30 20:01:43 +00:00
List<string> result = new();
while (reader.Read())
2022-12-11 14:22:41 +00:00
{
2023-03-30 20:01:43 +00:00
if (reader.TokenType == JsonTokenType.EndArray)
2022-12-11 14:22:41 +00:00
{
2023-03-30 20:01:43 +00:00
result.Sort();
return result;
2022-12-11 14:22:41 +00:00
}
2023-03-30 20:01:43 +00:00
if (reader.TokenType != JsonTokenType.String)
throw new JsonException("string expected");
2022-12-11 14:22:41 +00:00
2023-03-30 20:01:43 +00:00
result.Add(reader.GetString() ?? throw new JsonException("no value present"));
}
2022-12-11 14:22:41 +00:00
2023-03-30 20:01:43 +00:00
throw new JsonException("read to end of document");
2022-12-11 14:22:41 +00:00
}
2023-03-30 20:01:43 +00:00
else
throw new JsonException("bad token type");
2022-12-11 14:22:41 +00:00
}
2023-03-30 20:01:43 +00:00
public override void Write(Utf8JsonWriter writer, List<string> value, JsonSerializerOptions options) => throw new NotImplementedException();
2022-12-11 14:22:41 +00:00
}