mirror of
https://github.com/jonasroussel/dart_jsonwebtoken.git
synced 2025-07-15 02:59:09 +08:00
feat: Reduce dependencies
* Use `pointycastle` for HMAC calculation instead of `crypto` * Implement custom deep list equality instead of using the `collection` package
This commit is contained in:
@ -1,7 +1,6 @@
|
|||||||
import 'dart:math';
|
import 'dart:math';
|
||||||
import 'dart:typed_data';
|
import 'dart:typed_data';
|
||||||
|
|
||||||
import 'package:crypto/crypto.dart';
|
|
||||||
import 'package:ed25519_edwards/ed25519_edwards.dart' as ed;
|
import 'package:ed25519_edwards/ed25519_edwards.dart' as ed;
|
||||||
import 'package:pointycastle/pointycastle.dart' as pc;
|
import 'package:pointycastle/pointycastle.dart' as pc;
|
||||||
|
|
||||||
@ -148,12 +147,10 @@ class HMACAlgorithm extends JWTAlgorithm {
|
|||||||
|
|
||||||
final keyBytes = decodeHMACSecret(secretKey.key, secretKey.isBase64Encoded);
|
final keyBytes = decodeHMACSecret(secretKey.key, secretKey.isBase64Encoded);
|
||||||
|
|
||||||
final hmac = Hmac(
|
final hmac = pc.Mac('${_getHash(name)}/HMAC');
|
||||||
_getHash(name),
|
hmac.init(pc.KeyParameter(keyBytes));
|
||||||
keyBytes,
|
|
||||||
);
|
|
||||||
|
|
||||||
return Uint8List.fromList(hmac.convert(body).bytes);
|
return Uint8List.fromList(hmac.process(body));
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
@ -171,14 +168,14 @@ class HMACAlgorithm extends JWTAlgorithm {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
Hash _getHash(String name) {
|
String _getHash(String name) {
|
||||||
switch (name) {
|
switch (name) {
|
||||||
case 'HS256':
|
case 'HS256':
|
||||||
return sha256;
|
return 'SHA-256';
|
||||||
case 'HS384':
|
case 'HS384':
|
||||||
return sha384;
|
return 'SHA-384';
|
||||||
case 'HS512':
|
case 'HS512':
|
||||||
return sha512;
|
return 'SHA-512';
|
||||||
default:
|
default:
|
||||||
throw ArgumentError.value(name, 'name', 'unknown hash name');
|
throw ArgumentError.value(name, 'name', 'unknown hash name');
|
||||||
}
|
}
|
||||||
|
@ -159,3 +159,13 @@ ECDSAAlgorithm? ecCurveToAlgorithm(String curveName) {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool isListEquals<T>(List<T>? a, List<T>? b) {
|
||||||
|
if (identical(a, b)) return true;
|
||||||
|
if (a == null || b == null) return false;
|
||||||
|
if (a.length != b.length) return false;
|
||||||
|
for (var i = 0; i < a.length; i++) {
|
||||||
|
if (a[i] != b[i]) return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
@ -2,8 +2,6 @@ import 'dart:collection';
|
|||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
import 'dart:typed_data';
|
import 'dart:typed_data';
|
||||||
|
|
||||||
import 'package:collection/collection.dart';
|
|
||||||
|
|
||||||
import 'algorithms.dart';
|
import 'algorithms.dart';
|
||||||
import 'exceptions.dart';
|
import 'exceptions.dart';
|
||||||
import 'helpers.dart';
|
import 'helpers.dart';
|
||||||
@ -112,7 +110,7 @@ class JWT {
|
|||||||
if (payload['aud'] is String && payload['aud'] != audience.first) {
|
if (payload['aud'] is String && payload['aud'] != audience.first) {
|
||||||
throw JWTInvalidException('invalid audience');
|
throw JWTInvalidException('invalid audience');
|
||||||
} else if (payload['aud'] is List &&
|
} else if (payload['aud'] is List &&
|
||||||
!ListEquality().equals(payload['aud'], audience)) {
|
!isListEquals(payload['aud'], audience)) {
|
||||||
throw JWTInvalidException('invalid audience');
|
throw JWTInvalidException('invalid audience');
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -13,10 +13,8 @@ false_secrets:
|
|||||||
- /README.md
|
- /README.md
|
||||||
|
|
||||||
dependencies:
|
dependencies:
|
||||||
crypto: ^3.0.6
|
|
||||||
pointycastle: ^4.0.0
|
pointycastle: ^4.0.0
|
||||||
convert: ^3.1.2
|
convert: ^3.1.2
|
||||||
collection: ^1.17.1
|
|
||||||
ed25519_edwards: ^0.3.1
|
ed25519_edwards: ^0.3.1
|
||||||
clock: ^1.1.2
|
clock: ^1.1.2
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user