From 124df6bad3880aace5d9515f0d8e768ee10d4e2f Mon Sep 17 00:00:00 2001 From: Moe Jangda Date: Sun, 12 May 2024 21:08:16 -0500 Subject: [PATCH] allow custom `typ` to be provided --- packages/web5/lib/src/jwt/jwt.dart | 3 ++- packages/web5/test/jwt/jwt_test.dart | 19 +++++++++++++++++-- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/packages/web5/lib/src/jwt/jwt.dart b/packages/web5/lib/src/jwt/jwt.dart index 58a1e95..aaf6ceb 100644 --- a/packages/web5/lib/src/jwt/jwt.dart +++ b/packages/web5/lib/src/jwt/jwt.dart @@ -36,8 +36,9 @@ class Jwt { static Future sign({ required BearerDid did, required JwtClaims payload, + String? type = 'JWT', }) async { - final header = JwtHeader(typ: 'JWT'); + final header = JwtHeader(typ: type); final payloadBytes = json.toBytes(payload.toJson()); return Jws.sign(did: did, payload: payloadBytes, header: header); diff --git a/packages/web5/test/jwt/jwt_test.dart b/packages/web5/test/jwt/jwt_test.dart index 95ed241..2ed66ac 100644 --- a/packages/web5/test/jwt/jwt_test.dart +++ b/packages/web5/test/jwt/jwt_test.dart @@ -6,14 +6,29 @@ void main() { test('should decode signed JWT', () async { final did = await DidJwk.create(); - final signedJwt = - await Jwt.sign(did: did, payload: JwtClaims(iss: did.uri)); + final signedJwt = await Jwt.sign( + did: did, + payload: JwtClaims(iss: did.uri), + ); final parsedJwt = Jwt.decode(signedJwt); expect(parsedJwt.header.kid, contains('${did.uri}#')); expect(parsedJwt.claims.iss, equals(did.uri)); }); + test('should allow for custom typ', () async { + final did = await DidJwk.create(); + + final signedJwt = await Jwt.sign( + did: did, + payload: JwtClaims(iss: did.uri), + type: 'custom', + ); + + final parsedJwt = Jwt.decode(signedJwt); + expect(parsedJwt.header.typ, equals('custom')); + }); + test('should verify JWT signed by did:jwk', () async { final did = await DidJwk.create();