Skip to content
Open
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
43 changes: 43 additions & 0 deletions lib/src/jsonwebkey.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,47 @@ final class JsonWebKey {
this.k,
});

static void _verifyUseAndKeyOps(
String? use,
List<String>? keyOps,
Map<String, dynamic> json,
) {
if (use == null || keyOps == null) {
return; // nothing to validate
}

const encryptionOps = {
'encrypt',
'decrypt',
'wrapKey',
'unwrapKey',
};

const signingOps = {
'sign',
'verify',
};

Set<String>? allowedOps;
if (use == 'enc') {
allowedOps = encryptionOps;
} else if (use == 'sig') {
allowedOps = signingOps;
} else {
// Unknown "use" values are ignored (spec-compatible)
return;
}

for (final op in keyOps) {
if (!allowedOps.contains(op)) {
throw FormatException(
'JWK property "key_ops" conflicts with "use": "$use"',
json,
);
}
}
}

static JsonWebKey fromJson(Map<String, dynamic> json) {
const stringKeys = [
'kty',
Expand Down Expand Up @@ -95,6 +136,8 @@ final class JsonWebKey {
}
key_ops = (json['key_ops'] as List).map((e) => e as String).toList();
}
_verifyUseAndKeyOps(json['use'] as String?, key_ops, json);


if (json.containsKey('ext') && json['ext'] is! bool) {
throw FormatException('JWK entry "ext" must be boolean', json);
Expand Down