|
| 1 | +namespace SpacetimeDB; |
| 2 | + |
| 3 | +using System; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.Linq; |
| 6 | +using System.Text.Json; |
| 7 | + |
| 8 | +public sealed class JwtClaims |
| 9 | +{ |
| 10 | + private readonly string _payload; |
| 11 | + private readonly Lazy<JsonDocument> _parsed; |
| 12 | + private readonly Lazy<List<string>> _audience; |
| 13 | + |
| 14 | + public Identity Identity { get; } |
| 15 | + |
| 16 | + /// <summary> |
| 17 | + /// Create a JwtClaims from a raw JWT payload (JSON claims) and its associated Identity. |
| 18 | + /// |
| 19 | + /// This only takes an Identity because the Blake3 hash package on nuget wraps rust code. |
| 20 | + /// We should not expose this constructor publicly, but it is needed for AuthCtx. |
| 21 | + /// </summary> |
| 22 | + internal JwtClaims(string jwt, Identity identity) |
| 23 | + { |
| 24 | + _payload = jwt ?? throw new ArgumentNullException(nameof(jwt)); |
| 25 | + _parsed = new Lazy<JsonDocument>(() => JsonDocument.Parse(_payload)); |
| 26 | + _audience = new Lazy<List<string>>(ExtractAudience); |
| 27 | + Identity = identity; |
| 28 | + } |
| 29 | + |
| 30 | + private JsonDocument Parsed => _parsed.Value; |
| 31 | + |
| 32 | + private JsonElement RootElement => Parsed.RootElement; |
| 33 | + |
| 34 | + public string Subject |
| 35 | + { |
| 36 | + get |
| 37 | + { |
| 38 | + if ( |
| 39 | + RootElement.TryGetProperty("sub", out var sub) |
| 40 | + && sub.ValueKind == JsonValueKind.String |
| 41 | + ) |
| 42 | + { |
| 43 | + return sub.GetString()!; |
| 44 | + } |
| 45 | + |
| 46 | + throw new InvalidOperationException("JWT missing or invalid 'sub' claim"); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + public string Issuer |
| 51 | + { |
| 52 | + get |
| 53 | + { |
| 54 | + if ( |
| 55 | + RootElement.TryGetProperty("iss", out var iss) |
| 56 | + && iss.ValueKind == JsonValueKind.String |
| 57 | + ) |
| 58 | + { |
| 59 | + return iss.GetString()!; |
| 60 | + } |
| 61 | + |
| 62 | + throw new InvalidOperationException("JWT missing or invalid 'iss' claim"); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + private List<string> ExtractAudience() |
| 67 | + { |
| 68 | + if (!RootElement.TryGetProperty("aud", out var aud)) |
| 69 | + { |
| 70 | + throw new InvalidOperationException("JWT missing 'aud' claim"); |
| 71 | + } |
| 72 | + |
| 73 | + return aud.ValueKind switch |
| 74 | + { |
| 75 | + JsonValueKind.String => new List<string> { aud.GetString()! }, |
| 76 | + JsonValueKind.Array => aud.EnumerateArray() |
| 77 | + .Where(e => e.ValueKind == JsonValueKind.String) |
| 78 | + .Select(e => e.GetString()!) |
| 79 | + .ToList(), |
| 80 | + _ => throw new InvalidOperationException("Unexpected type for 'aud' claim in JWT"), |
| 81 | + }; |
| 82 | + } |
| 83 | + |
| 84 | + public IReadOnlyList<string> Audience => _audience.Value; |
| 85 | + |
| 86 | + // TODO: Should this be exposed as a JsonDocument, since that it in the stdlib? |
| 87 | + public string RawPayload => _payload; |
| 88 | +} |
0 commit comments