This commit is contained in:
Jonas Roussel
2021-04-13 18:45:19 +02:00
parent 97950a5d9c
commit 2fcea79c95
4 changed files with 84 additions and 50 deletions

View File

@ -1,3 +1,8 @@
## 2.1.0
- When an undefined error occur `JWTUndefinedError` is thrown containing the original error in `error` property (https://github.com/jonasroussel/jsonwebtoken/issues/9)
- **BREAKING CHANGE**: `jwt.verify` no longer support `throwUndefinedErrors` parameter
## 2.0.1
- Fixing `JWT.sign` to include `iat` & other attributes when payload is an empty Map

View File

@ -12,19 +12,41 @@ class JWTError extends Error {
/// An error thrown when jwt is invalid
class JWTInvalidError extends JWTError {
JWTInvalidError(String message) : super(message);
@override
String toString() => 'JWTInvalidError: $message';
}
/// An error thrown when jwt is expired
class JWTExpiredError extends JWTError {
JWTExpiredError() : super('jwt expired');
@override
String toString() => 'JWTExpiredError: $message';
}
/// An error thrown when jwt is not active
class JWTNotActiveError extends JWTError {
JWTNotActiveError() : super('jwt not active');
@override
String toString() => 'JWTNotActiveError: $message';
}
/// An error thrown when parsing failed
class JWTParseError extends JWTError {
JWTParseError(String message) : super(message);
@override
String toString() => 'JWTParseError: $message';
}
/// An error thrown by default
class JWTUndefinedError extends JWTError {
JWTUndefinedError(this.error) : super(error.toString());
final Error error;
@override
String toString() => 'JWTUndefinedError: $message';
}

View File

@ -20,7 +20,6 @@ class JWT {
bool checkHeaderType = true,
bool checkExpiresIn = true,
bool checkNotBefore = true,
bool throwUndefinedErrors = false,
Duration? issueAt,
String? audience,
String? subject,
@ -129,10 +128,10 @@ class JWT {
return JWT(payload);
}
} catch (ex) {
if (throwUndefinedErrors) {
rethrow;
if (ex is Error) {
throw JWTUndefinedError(ex);
} else {
throw JWTInvalidError('invalid token');
rethrow;
}
}
}
@ -175,6 +174,7 @@ class JWT {
Duration? notBefore,
bool noIssueAt = false,
}) {
try {
final header = {'alg': algorithm.name, 'typ': 'JWT'};
if (payload is Map<String, dynamic> || payload is Map<dynamic, dynamic>) {
@ -226,5 +226,12 @@ class JWT {
);
return body + '.' + signature;
} catch (ex) {
if (ex is Error) {
throw JWTUndefinedError(ex);
} else {
rethrow;
}
}
}
}

View File

@ -1,6 +1,6 @@
name: dart_jsonwebtoken
description: A dart implementation of the famous javascript library 'jsonwebtoken' (JWT).
version: 2.0.1
version: 2.1.0
repository: https://github.com/jonasroussel/jsonwebtoken
homepage: https://github.com/jonasroussel/jsonwebtoken#readme
@ -8,9 +8,9 @@ environment:
sdk: '>=2.12.0 <3.0.0'
dependencies:
crypto: ^3.0.0
crypto: ^3.0.1
pointycastle: ^3.0.1
ed25519_edwards: ^0.1.0
dev_dependencies:
pedantic: ^1.9.2
pedantic: ^1.11.0