This commit is contained in:
Jonas Roussel
2022-12-11 11:12:26 +01:00
parent 343afbdc92
commit 02494a1805
4 changed files with 67 additions and 2 deletions

View File

@ -1,3 +1,7 @@
## 2.6.1
- Adding a `try` version of `decode`, `verify` and `sign`, that simply returns `null` instead of throwing errors
## 2.6.0
- Adding a `JWT.decode` method to simply decode a token without checking its signature

View File

@ -1,6 +1,6 @@
MIT License
Copyright (c) 2022 Jonas Roussel
Copyright (c) 2023 Jonas Roussel
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@ -147,6 +147,37 @@ class JWT {
}
}
/// Exactly like `verify`, just return null instead of throwing errors.
static JWT? tryVerify(
String token,
JWTKey key, {
bool checkHeaderType = true,
bool checkExpiresIn = true,
bool checkNotBefore = true,
Duration? issueAt,
Audience? audience,
String? subject,
String? issuer,
String? jwtId,
}) {
try {
return verify(
token,
key,
checkHeaderType: checkHeaderType,
checkExpiresIn: checkExpiresIn,
checkNotBefore: checkNotBefore,
issueAt: issueAt,
audience: audience,
subject: subject,
issuer: issuer,
jwtId: jwtId,
);
} catch (ex) {
return null;
}
}
/// Decode a token without checking its signature
static JWT decode(String token) {
try {
@ -178,6 +209,15 @@ class JWT {
}
}
/// Exactly like `decode`, just return `null` instead of throwing errors.
static JWT? tryDecode(String token) {
try {
return decode(token);
} catch (ex) {
return null;
}
}
/// JSON Web Token
JWT(
this.payload, {
@ -282,6 +322,27 @@ class JWT {
}
}
/// Exactly like `sign`, just return `null` instead of throwing errors.
String? trySign(
JWTKey key, {
JWTAlgorithm algorithm = JWTAlgorithm.HS256,
Duration? expiresIn,
Duration? notBefore,
bool noIssueAt = false,
}) {
try {
return sign(
key,
algorithm: algorithm,
expiresIn: expiresIn,
notBefore: notBefore,
noIssueAt: noIssueAt,
);
} catch (ex) {
return null;
}
}
static Audience? _parseAud(dynamic val) {
if (val is String) {
return Audience.one(val);

View File

@ -1,6 +1,6 @@
name: dart_jsonwebtoken
description: A dart implementation of the famous javascript library 'jsonwebtoken' (JWT).
version: 2.6.0
version: 2.6.1
repository: https://github.com/jonasroussel/dart_jsonwebtoken
homepage: https://github.com/jonasroussel/dart_jsonwebtoken#readme