test: Add tests for iat claim

This commit is contained in:
Jonas Roussel
2025-02-27 12:03:37 +01:00
parent eb9c722e17
commit 948c7284af

View File

@ -25,11 +25,11 @@ void main() {
}); });
test('should be still valid', () { test('should be still valid', () {
final iat = DateTime(2023); final iat = DateTime(2042);
final exp = DateTime(2023).add(Duration(hours: 1)); final exp = DateTime(2042).add(Duration(hours: 1));
withClock( withClock(
Clock.fixed(DateTime(2023)), Clock.fixed(DateTime(2042)),
() { () {
final duration = Duration(hours: 1); final duration = Duration(hours: 1);
final token = JWT({'foo': 'bar'}).sign(hsKey, expiresIn: duration); final token = JWT({'foo': 'bar'}).sign(hsKey, expiresIn: duration);
@ -75,11 +75,11 @@ void main() {
}); });
test('should be valid after nbf time', () { test('should be valid after nbf time', () {
final iat = DateTime(2023); final iat = DateTime(2042);
final nbf = iat.add(Duration(minutes: 30)); final nbf = iat.add(Duration(minutes: 30));
withClock( withClock(
Clock.fixed(DateTime(2023)), Clock.fixed(DateTime(2042)),
() { () {
final token = JWT({'foo': 'bar'}).sign( final token = JWT({'foo': 'bar'}).sign(
hsKey, hsKey,
@ -112,6 +112,40 @@ void main() {
contains('foo'), contains('foo'),
); );
}); });
//----------------------//
// Issued At (iat) //
//----------------------//
group('iat', () {
test('should have iat claim by default', () {
final iat = DateTime(2042);
withClock(Clock.fixed(iat), () {
final token = JWT({'foo': 'bar'}).sign(hsKey);
expect(
JWT.verify(token, hsKey).payload,
equals({
'foo': 'bar',
'iat': iat.millisecondsSinceEpoch ~/ 1000,
}),
);
});
});
test('should be valid when iat is in the future', () {
final futureIat = DateTime(2042).add(Duration(hours: 1));
final token = JWT({
'foo': 'bar',
'iat': futureIat.millisecondsSinceEpoch ~/ 1000
}).sign(hsKey);
expect(
JWT.verify(token, hsKey).payload,
contains('foo'),
);
});
});
}); });
}); });
} }