From f097c0d2ee3a9475baa8c187a8ce10a79a1b3efa Mon Sep 17 00:00:00 2001 From: JHubi1 Date: Sun, 2 Feb 2025 09:31:23 +0100 Subject: [PATCH 1/2] Set parameters when using JWT.decode Set audience, issuer, subject and jwtId when using the JWT.decode function --- lib/src/jwt.dart | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lib/src/jwt.dart b/lib/src/jwt.dart index fbe4067..1769de2 100644 --- a/lib/src/jwt.dart +++ b/lib/src/jwt.dart @@ -188,6 +188,9 @@ class JWT { } /// Decode a token without checking its signature + /// + /// This also sets [JWT.audience], [JWT.subject], [JWT.issuer], and + /// [JWT.jwtId] even though they are not verified. Use with caution. static JWT decode(String token) { try { final parts = token.split('.'); @@ -207,6 +210,10 @@ class JWT { return JWT( payload, header: header, + audience: _parseAud(payload['aud']), + issuer: payload['iss']?.toString(), + subject: payload['sub']?.toString(), + jwtId: payload['jti']?.toString(), ); } } catch (ex, stackTrace) { From 025f48b34e21faebde381cd5884de28611c37ff4 Mon Sep 17 00:00:00 2001 From: Jonas Roussel Date: Mon, 3 Feb 2025 10:10:30 +0100 Subject: [PATCH 2/2] refactor: simplify decode method --- lib/src/jwt.dart | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/lib/src/jwt.dart b/lib/src/jwt.dart index 1769de2..cc48618 100644 --- a/lib/src/jwt.dart +++ b/lib/src/jwt.dart @@ -188,7 +188,7 @@ class JWT { } /// Decode a token without checking its signature - /// + /// /// This also sets [JWT.audience], [JWT.subject], [JWT.issuer], and /// [JWT.jwtId] even though they are not verified. Use with caution. static JWT decode(String token) { @@ -204,18 +204,19 @@ class JWT { payload = utf8.decode(base64.decode(base64Padded(parts[1]))); } - if (header == null || header is! Map) { - return JWT(payload); - } else { - return JWT( - payload, - header: header, - audience: _parseAud(payload['aud']), - issuer: payload['iss']?.toString(), - subject: payload['sub']?.toString(), - jwtId: payload['jti']?.toString(), - ); - } + final audiance = _parseAud(payload['aud']); + final issuer = payload['iss']?.toString(); + final subject = payload['sub']?.toString(); + final jwtId = payload['jti']?.toString(); + + return JWT( + payload, + header: header is! Map ? null : header, + audience: audiance, + issuer: issuer, + subject: subject, + jwtId: jwtId, + ); } catch (ex, stackTrace) { if (ex is Exception && ex is! JWTException) { throw JWTUndefinedException(ex, stackTrace);