8000 feat: add optional JWT expiry enforcement by marco-a-affinidi · Pull Request #303 · affinidi/affinidi-ssi-dart · GitHub
[go: up one dir, main page]

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/src/credentials/jwt/jwt_dm_v1_suite.dart
Original file line number Diff line number Diff line change
Expand Up @@ -90,23 +90,29 @@
return JwtVcDataModelV1.fromJws(jws);
}

/// Verifies the signature and expiry of [input].
///
/// Set [allowMissingExpiry] to false when the relying party's trust policy
/// requires every credential to have an expiry.
@override
Future<bool> verifyIntegrity(JwtVcDataModelV1 input,
{DateTime Function() getNow = DateTime.now,
DidResolver? didResolver}) async {
DidResolver? didResolver,
bool allowMissingExpiry = true}) async {
final segments = input.serialized.split('.');

if (segments.length != 3) {
throw SsiException(
message: 'Invalid JWT',
code: SsiExceptionType.invalidVC.code,
);
}

var now = getNow();
final exp = input.jws.payload['exp'];
if (exp == null && !allowMissingExpiry) return false;
if (exp != null &&
now.isAfter(DateTime.fromMillisecondsSinceEpoch((exp as int) * 1000,

Check warning

Code scanning / AgenticSec

Security finding — see the Security tab for details Medium

Security finding — please refer to this repository's Security → Code scanning tab for the title and full details.
isUtc: true))) {
return false;
}
Expand Down
27 changes: 23 additions & 4 deletions test/credentials/jwt/jwt_exp_validation_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import 'package:test/test.dart';
import '../../test_utils.dart';

void main() {
group('JWT VC Data Model V1 - exp validation', () {
group('When verifying a JWT VC expiration', () {
final testSeed =
Uint8List.fromList(List.generate(32, (index) => index + 1));

Expand All @@ -18,8 +18,7 @@ void main() {
suite = JwtDm1Suite();
});

test('verifyIntegrity should return true when exp is not present',
() async {
test('it permits a missing exp by default', () async {
final credential = MutableVcDataModelV1.fromJson({
'@context': [dmV1ContextUrl],
'id': 'urn:uuid:test-credential-no-exp',
Expand All @@ -33,11 +32,31 @@ void main() {
final issuedCredential = await suite.issue(
unsignedData: VcDataModelV1.fromMutable(credential), signer: signer);

// Verify with current time - should be valid since no exp
final isValid = await suite.verifyIntegrity(issuedCredential);
expect(isValid, isTrue);
});

test('it rejects a missing exp when explicitly required', () async {
final credential = MutableVcDataModelV1.fromJson({
'@context': [dmV1ContextUrl],
'id': 'urn:uuid:test-credential-no-exp-opt-out',
'type': ['VerifiableCredential', 'TestCredential'],
'holder': {'id': signer.did},
'issuanceDate': '2023-01-01T12:00:00Z',
'credentialSubject': {'email': 'test@example.com'},
})
..issuer = MutableIssuer.uri(signer.did);

final issuedCredential = await suite.issue(
unsignedData: VcDataModelV1.fromMutable(credential), signer: signer);

final isValid = await suite.verifyIntegrity(
issuedCredential,
allowMissingExpiry: false,
);
expect(isValid, isFalse);
});

test('verifyIntegrity should return true when current time is before exp',
() async {
final credential = MutableVcDataModelV1.fromJson({
Expand Down
2 changes: 1 addition & 1 deletion test/verifiable_credential_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ void main() {
);

test(
'it passes integrity check',
'it passes integrity check when expiry is missing',
() async {
var actualIntegrity = await JwtDm1Suite()
.verifyIntegrity(verifiableCredential as JwtVcDataModelV1);
Expand Down
Loading
0