mirror of
https://github.com/jonasroussel/dart_jsonwebtoken.git
synced 2025-08-06 13:51:08 +08:00
54 lines
1.5 KiB
Dart
54 lines
1.5 KiB
Dart
import 'package:clock/clock.dart';
|
|
import 'package:dart_jsonwebtoken/dart_jsonwebtoken.dart';
|
|
import 'package:fake_async/fake_async.dart';
|
|
import 'package:test/test.dart';
|
|
|
|
final hsKey = SecretKey('secret passphrase');
|
|
|
|
void main() {
|
|
group("JWT Header", () {
|
|
//--------------------//
|
|
// Expiration (exp) //
|
|
//--------------------//
|
|
group('exp', () {
|
|
test('should be expired', () {
|
|
final duration = Duration(hours: 1);
|
|
final token = JWT({'foo': 'bar'}).sign(hsKey, expiresIn: duration);
|
|
|
|
fakeAsync((async) {
|
|
async.elapse(duration);
|
|
expect(
|
|
() => JWT.verify(token, hsKey),
|
|
throwsA(isA<JWTExpiredException>()),
|
|
);
|
|
});
|
|
});
|
|
|
|
test('should be still valid', () {
|
|
final iat = DateTime(2023);
|
|
final exp = DateTime(2023).add(Duration(hours: 1));
|
|
|
|
withClock(
|
|
Clock.fixed(DateTime(2023)),
|
|
() {
|
|
final duration = Duration(hours: 1);
|
|
final token = JWT({'foo': 'bar'}).sign(hsKey, expiresIn: duration);
|
|
|
|
fakeAsync((async) {
|
|
async.elapse(Duration(minutes: 30));
|
|
expect(
|
|
JWT.verify(token, hsKey).payload,
|
|
equals({
|
|
'foo': 'bar',
|
|
'iat': iat.millisecondsSinceEpoch ~/ 1000,
|
|
'exp': exp.millisecondsSinceEpoch ~/ 1000,
|
|
}),
|
|
);
|
|
});
|
|
},
|
|
);
|
|
});
|
|
});
|
|
});
|
|
}
|