From 881fe9af609ec2b430a0241f6031067386f4c42b Mon Sep 17 00:00:00 2001 From: jonasroussel Date: Sun, 5 Nov 2023 14:50:46 +0100 Subject: [PATCH] fixing issue with custom headers --- CHANGELOG.md | 4 ++++ lib/src/jwt.dart | 9 +++++---- pubspec.yaml | 2 +- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b86f7b0..41d8049 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 2.12.1 + +- Fixing issue with custom headers (https://github.com/jonasroussel/dart_jsonwebtoken/issues/48) + ## 2.12.0 - **NEW:** tests have been added diff --git a/lib/src/jwt.dart b/lib/src/jwt.dart index afc3df8..b23f2e8 100644 --- a/lib/src/jwt.dart +++ b/lib/src/jwt.dart @@ -261,9 +261,6 @@ class JWT { bool noIssueAt = false, }) { try { - header ??= {}; - header!.addAll({'alg': algorithm.name, 'typ': 'JWT'}); - if (payload is Map || payload is Map) { try { payload = Map.from(payload); @@ -287,7 +284,11 @@ class JWT { } } - final b64Header = base64Unpadded(jsonBase64.encode(header)); + final tokenHeader = Map.from(header ?? {}); + tokenHeader.putIfAbsent('alg', () => algorithm.name); + tokenHeader.putIfAbsent('typ', () => 'JWT'); + + final b64Header = base64Unpadded(jsonBase64.encode(tokenHeader)); String b64Payload; try { diff --git a/pubspec.yaml b/pubspec.yaml index 3a26a27..d609838 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: dart_jsonwebtoken description: A dart implementation of the famous javascript library 'jsonwebtoken' (JWT). -version: 2.12.0 +version: 2.12.1 repository: https://github.com/jonasroussel/dart_jsonwebtoken homepage: https://github.com/jonasroussel/dart_jsonwebtoken#readme