mirror of
https://github.com/jonasroussel/dart_jsonwebtoken.git
synced 2025-07-31 07:53:27 +08:00
v1.5.0
This commit is contained in:
@ -2,9 +2,12 @@ class JWTError extends Error {
|
||||
JWTError(this.message);
|
||||
|
||||
final String message;
|
||||
|
||||
@override
|
||||
String toString() => 'JWTError: $message';
|
||||
}
|
||||
|
||||
/// An error thrown when toke is invalid
|
||||
/// An error thrown when token is invalid
|
||||
class JWTInvalidError extends JWTError {
|
||||
JWTInvalidError(String message) : super(message);
|
||||
}
|
||||
|
@ -165,26 +165,37 @@ class JWT {
|
||||
}) {
|
||||
final header = {'alg': algorithm.name, 'typ': 'JWT'};
|
||||
|
||||
if (payload is Map) {
|
||||
if (!noIssueAt) payload['iat'] = secondsSinceEpoch(DateTime.now());
|
||||
if (expiresIn != null) {
|
||||
payload['exp'] = secondsSinceEpoch(DateTime.now().add(expiresIn));
|
||||
}
|
||||
if (notBefore != null) {
|
||||
payload['nbf'] = secondsSinceEpoch(DateTime.now().add(notBefore));
|
||||
}
|
||||
if (audience != null) payload['aud'] = audience;
|
||||
if (subject != null) payload['sub'] = subject;
|
||||
if (issuer != null) payload['iss'] = issuer;
|
||||
if (jwtId != null) payload['jti'] = jwtId;
|
||||
if (payload is Map<String, dynamic>) {
|
||||
payload = Map<String, dynamic>.from(payload);
|
||||
|
||||
try {
|
||||
if (!noIssueAt) payload['iat'] = secondsSinceEpoch(DateTime.now());
|
||||
if (expiresIn != null) {
|
||||
payload['exp'] = secondsSinceEpoch(DateTime.now().add(expiresIn));
|
||||
}
|
||||
if (notBefore != null) {
|
||||
payload['nbf'] = secondsSinceEpoch(DateTime.now().add(notBefore));
|
||||
}
|
||||
if (audience != null) payload['aud'] = audience;
|
||||
if (subject != null) payload['sub'] = subject;
|
||||
if (issuer != null) payload['iss'] = issuer;
|
||||
if (jwtId != null) payload['jti'] = jwtId;
|
||||
} catch (ex) {}
|
||||
}
|
||||
|
||||
final b64Header = base64Unpadded(jsonBase64.encode(header));
|
||||
final b64Payload = base64Unpadded(
|
||||
payload is String
|
||||
? base64.encode(utf8.encode(payload))
|
||||
: jsonBase64.encode(payload),
|
||||
);
|
||||
|
||||
String b64Payload;
|
||||
try {
|
||||
b64Payload = base64Unpadded(
|
||||
payload is String
|
||||
? base64.encode(utf8.encode(payload))
|
||||
: jsonBase64.encode(payload),
|
||||
);
|
||||
} catch (ex) {
|
||||
throw JWTError(
|
||||
'invalid payload json format (Map keys must be String type)');
|
||||
}
|
||||
|
||||
final body = '${b64Header}.${b64Payload}';
|
||||
final signature = base64Unpadded(
|
||||
|
Reference in New Issue
Block a user