Skip to content

Commit ab9cd47

Browse files
committed
v3.3.1
1 parent 64c54b2 commit ab9cd47

File tree

5 files changed

+22
-139
lines changed

5 files changed

+22
-139
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1+
## 3.3.1
2+
3+
- Rollback to a `dynamic` `JWT.payload`
4+
15
## 3.3.0
26

7+
- **RETRACTED**
38
- Increase `JWT.payload` strictness (https://github.com/jonasroussel/dart_jsonwebtoken/issues/67)
49

510
## 3.2.0

lib/src/jwt.dart

Lines changed: 16 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@ class JWT {
2020
/// value is a timestamp (number of seconds since epoch) in UTC if
2121
/// [issueAtUtc] is true, it is compared to the value of the 'iat' claim.
2222
/// Verification fails if the 'iat' claim is before [issueAt].
23-
///
24-
/// If the embedded `payload` is not a JSON map (but rather just a plain string),
25-
/// none of the verifications are executed. In that case only the signature is verified.
2623
static JWT verify(
2724
String token,
2825
JWTKey key, {
@@ -64,11 +61,10 @@ class JWT {
6461
throw JWTInvalidException('invalid signature');
6562
}
6663

67-
Object payload;
64+
dynamic payload;
6865

6966
try {
70-
payload =
71-
jsonBase64.decode(base64Padded(parts[1])) as Map<String, dynamic>;
67+
payload = jsonBase64.decode(base64Padded(parts[1]));
7268
} catch (ex) {
7369
payload = utf8.decode(base64Url.decode(base64Padded(parts[1])));
7470
}
@@ -205,16 +201,18 @@ class JWT {
205201
///
206202
/// This also sets [JWT.audience], [JWT.subject], [JWT.issuer], and
207203
/// [JWT.jwtId] even though they are not verified. Use with caution.
208-
///
209-
/// This methods only supports map payloads. For `String` payloads use `verify`.
210204
static JWT decode(String token) {
211205
try {
212206
final parts = token.split('.');
213-
var header =
214-
jsonBase64.decode(base64Padded(parts[0])) as Map<String, dynamic>;
207+
var header = jsonBase64.decode(base64Padded(parts[0]));
215208

216-
final payload =
217-
(jsonBase64.decode(base64Padded(parts[1])) as Map<String, dynamic>);
209+
dynamic payload;
210+
211+
try {
212+
payload = jsonBase64.decode(base64Padded(parts[1]));
213+
} catch (ex) {
214+
payload = utf8.decode(base64Url.decode(base64Padded(parts[1])));
215+
}
218216

219217
final audience = _parseAud(payload['aud']);
220218
final issuer = payload['iss']?.toString();
@@ -223,7 +221,7 @@ class JWT {
223221

224222
return JWT(
225223
payload,
226-
header: header,
224+
header: header is! Map<String, dynamic> ? null : header,
227225
audience: audience,
228226
issuer: issuer,
229227
subject: subject,
@@ -249,36 +247,16 @@ class JWT {
249247

250248
/// JSON Web Token
251249
JWT(
252-
Object payload, {
250+
this.payload, {
253251
this.audience,
254252
this.subject,
255253
this.issuer,
256254
this.jwtId,
257255
this.header,
258-
}) {
259-
this.payload = payload;
260-
}
261-
262-
late Object _payload;
256+
});
263257

264-
/// The token's payload, either as a `Map<String, dynamic>` or plain `String`
265-
/// (in case it was not a JSON-encoded map).
266-
///
267-
/// If it's a map, it has all claims, containing the utilized registered claims
268-
/// as well custom ones added.
269-
Object get payload => _payload;
270-
271-
void set payload(Object value) {
272-
if (value is String) {
273-
_payload = value;
274-
} else if (value is Map) {
275-
_payload = Map<String, dynamic>.from(value);
276-
} else {
277-
throw Exception(
278-
'Unexpected `payload` type `${value.runtimeType}`, must be either `String` or `Map<String, *>`',
279-
);
280-
}
281-
}
258+
/// Custom claims
259+
dynamic payload;
282260

283261
/// Audience claim
284262
Audience? audience;
@@ -310,8 +288,7 @@ class JWT {
310288
bool noIssueAt = false,
311289
}) {
312290
try {
313-
var payload = this.payload;
314-
if (payload is Map<String, dynamic>) {
291+
if (payload is Map<String, dynamic> || payload is Map<dynamic, dynamic>) {
315292
try {
316293
payload = Map<String, dynamic>.from(payload);
317294

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: dart_jsonwebtoken
22
description: An easy to use JSON Web Token (JWT) implementation in Dart with all algorithms supported.
3-
version: 3.3.0
3+
version: 3.3.1
44
repository: https://github.com/jonasroussel/dart_jsonwebtoken
55
homepage: https://github.com/jonasroussel/dart_jsonwebtoken#readme
66

test/create_test.dart

Lines changed: 0 additions & 86 deletions
This file was deleted.

test/header_test.dart

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -147,18 +147,5 @@ void main() {
147147
});
148148
});
149149
});
150-
151-
group('invalid header', () {
152-
test('invalid (non map) header should fail to decode', () {
153-
final token =
154-
'W10' + // base64 for `[]`, which can JSON decode but is not valid
155-
'.eyJmb28iOiJiYXIifQ' +
156-
'.'; // signature is not checked here
157-
158-
final jwt = JWT.tryDecode(token);
159-
160-
expect(jwt, isNull);
161-
});
162-
});
163150
});
164151
}

0 commit comments

Comments
 (0)