Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement detached JWS verification #54

Merged
merged 1 commit into from
Mar 27, 2024
Merged
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
40 changes: 31 additions & 9 deletions packages/web5/lib/src/jws/jws.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@ import 'package:web5/src/jws/decoded_jws.dart';
import 'package:web5/src/jws/jws_header.dart';

class Jws {
static DecodedJws decode(String jws) {
/// Decodes a compact JWS per [RFC 7515](https://tools.ietf.org/html/rfc7515)
/// and returns a [DecodedJws] object.
///
/// ### Note
/// `detachedPayload` is optional and should be provided if the payload
/// is detached. More information on detached payloads can be found
/// [here](https://tools.ietf.org/html/rfc7515#section-7.2).
static DecodedJws decode(String jws, {Uint8List? detachedPayload}) {
final parts = jws.split('.');

if (parts.length != 3) {
Expand All @@ -27,12 +34,17 @@ class Jws {
}

final Uint8List payload;
try {
payload = Base64Url.decode(parts[1]);
} on Exception {
throw Exception(
'Malformed JWT. failed to decode claims',
);
if (detachedPayload == null) {
try {
payload = Base64Url.decode(parts[1]);
} on Exception {
throw Exception(
'Malformed JWT. failed to decode claims',
);
}
} else {
payload = detachedPayload;
parts[1] = Base64Url.encode(detachedPayload);
}

final Uint8List signature;
Expand Down Expand Up @@ -92,9 +104,19 @@ class Jws {
}
}

static Future<DecodedJws> verify(String jws) async {
/// Verifies a compact JWS per [RFC 7515](https://tools.ietf.org/html/rfc7515)
/// and returns a [DecodedJws] object. Throws [Exception] if verification fails.
///
/// ### Note
/// `detachedPayload` is optional and should be provided if the payload
/// is detached. More information on detached payloads can be found
/// [here](https://tools.ietf.org/html/rfc7515#section-7.2).
static Future<DecodedJws> verify(
String jws, {
Uint8List? detachedPayload,
}) async {
try {
final decodedJws = decode(jws);
final decodedJws = decode(jws, detachedPayload: detachedPayload);
await decodedJws.verify();
return decodedJws;
} on Exception catch (e) {
Expand Down
21 changes: 21 additions & 0 deletions packages/web5/test/jws/jws_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import 'dart:typed_data';

import 'package:web5/web5.dart';
import 'package:test/test.dart';

void main() {
group('Jws', () {
test('should successfuly sign & verify detached compact jws', () async {
final did = await DidJwk.create();
final payload = Uint8List.fromList('hello'.codeUnits);
final compactJws =
await Jws.sign(did: did, payload: payload, detachedPayload: true);

final parts = compactJws.split('.');
expect(parts.length, equals(3));
expect(parts[1], equals(''));

expect(Jws.verify(compactJws, detachedPayload: payload), completes);
});
});
}
Loading