Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions lib/src/jwt.dart
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,8 @@ class JWT {
static JWT decode(String token) {
try {
final parts = token.split('.');
final header = jsonBase64.decode(base64Padded(parts[0]));
var header =
jsonBase64.decode(base64Padded(parts[0])) as Map<String, dynamic>;

final payload =
(jsonBase64.decode(base64Padded(parts[1])) as Map<String, dynamic>);
Expand All @@ -222,8 +223,8 @@ class JWT {

return JWT(
payload,
header: header is! Map<String, dynamic> ? null : header,
audience: audience,
header: header,
audience: audiance,
issuer: issuer,
subject: subject,
jwtId: jwtId,
Expand Down
13 changes: 13 additions & 0 deletions test/header_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -147,5 +147,18 @@ void main() {
});
});
});

group('invalid header', () {
test('invalid (non map) header should fail to decode', () {
final token =
'W10' + // base64 for `[]`, which can JSON decode but is not valid
'.eyJmb28iOiJiYXIifQ' +
'.'; // signature is not checked here

final jwt = JWT.tryDecode(token);

expect(jwt, isNull);
});
});
});
}
Loading