using System.Security.Cryptography;
using System.Text;
using System.Text.Json;
using BigRegister.Api.Zgw;
namespace BigRegister.Tests;
///
/// The ZGW JWT is hand-signed (no library), so it needs a check that it's actually a valid
/// HS256 JWS with the claims OpenZaak requires. Decodes the minted token and re-verifies the
/// signature with the shared secret.
///
public class ZgwTokenProviderTests
{
private static readonly ZgwOptions Options = new()
{
ClientId = "big-register",
Secret = "super-secret-signing-key",
UserId = "u-123",
UserRepresentation = "Dr. Test",
};
[Fact]
public void Mints_a_three_part_jwt_with_the_required_claims()
{
var token = new ZgwTokenProvider(Options).Mint();
var parts = token.Split('.');
Assert.Equal(3, parts.Length);
var header = JsonSerializer.Deserialize(Decode(parts[0]));
Assert.Equal("HS256", header.GetProperty("alg").GetString());
Assert.Equal("JWT", header.GetProperty("typ").GetString());
var payload = JsonSerializer.Deserialize(Decode(parts[1]));
Assert.Equal("big-register", payload.GetProperty("iss").GetString());
Assert.Equal("big-register", payload.GetProperty("client_id").GetString());
Assert.Equal("u-123", payload.GetProperty("user_id").GetString());
Assert.Equal("Dr. Test", payload.GetProperty("user_representation").GetString());
// iat is a recent unix second
var iat = payload.GetProperty("iat").GetInt64();
Assert.InRange(iat, DateTimeOffset.UtcNow.ToUnixTimeSeconds() - 5, DateTimeOffset.UtcNow.ToUnixTimeSeconds() + 5);
}
[Fact]
public void Signature_verifies_with_the_shared_secret()
{
var token = new ZgwTokenProvider(Options).Mint();
var parts = token.Split('.');
using var hmac = new HMACSHA256(Encoding.UTF8.GetBytes(Options.Secret));
var expected = Base64Url(hmac.ComputeHash(Encoding.UTF8.GetBytes($"{parts[0]}.{parts[1]}")));
Assert.Equal(expected, parts[2]);
}
private static string Decode(string b64Url)
{
var s = b64Url.Replace('-', '+').Replace('_', '/');
s = s.PadRight(s.Length + (4 - s.Length % 4) % 4, '=');
return Encoding.UTF8.GetString(Convert.FromBase64String(s));
}
private static string Base64Url(byte[] bytes) =>
Convert.ToBase64String(bytes).TrimEnd('=').Replace('+', '-').Replace('/', '_');
}