-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
207 additions
and
177 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import 'dart:typed_data'; | ||
|
||
import 'package:web5/src/crypto.dart'; | ||
import 'package:web5/src/jws/jws_header.dart'; | ||
|
||
import 'package:web5/src/dids.dart'; | ||
|
||
class DecodedJws { | ||
final JwsHeader header; | ||
final Uint8List payload; | ||
final Uint8List signature; | ||
final List<String> parts; | ||
|
||
static final _didResolver = | ||
DidResolver(methodResolvers: [DidJwk.resolver, DidDht.resolver]); | ||
|
||
DecodedJws({ | ||
required this.header, | ||
required this.payload, | ||
required this.signature, | ||
required this.parts, | ||
}); | ||
|
||
Future<void> verify() async { | ||
if (header.kid == null || header.alg == null) { | ||
throw Exception( | ||
'Malformed JWS. expected header to contain kid and alg.', | ||
); | ||
} | ||
|
||
final dereferenceResult = await _didResolver.dereference(header.kid!); | ||
if (dereferenceResult.hasError()) { | ||
throw Exception( | ||
'Verification failed. Failed to dereference kid. Error: ${dereferenceResult.dereferencingMetadata.error}', | ||
); | ||
} | ||
|
||
final didResource = dereferenceResult.contentStream; | ||
if (didResource == null) { | ||
throw Exception( | ||
'Verification failed. Expected header kid to dereference a verification method', | ||
); | ||
} | ||
|
||
if (didResource is! DidVerificationMethod) { | ||
throw Exception( | ||
'Verification failed. Expected header kid to dereference a verification method', | ||
); | ||
} | ||
|
||
final publicKeyJwk = didResource.publicKeyJwk; | ||
final dsaName = | ||
DsaName.findByAlias(algorithm: header.alg, curve: publicKeyJwk!.crv); | ||
|
||
if (dsaName == null) { | ||
throw Exception('${header.alg}:${publicKeyJwk.crv} not supported.'); | ||
} | ||
|
||
await DsaAlgorithms.verify( | ||
algName: dsaName, | ||
publicKey: publicKeyJwk, | ||
payload: payload, | ||
signature: signature, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import 'dart:convert'; | ||
import 'dart:typed_data'; | ||
|
||
import 'package:web5/src/jws/decoded_jws.dart'; | ||
import 'package:web5/web5.dart'; | ||
|
||
class DecodedJwt { | ||
final JwtHeader header; | ||
final JwtClaims claims; | ||
final Uint8List signature; | ||
final List<String> parts; | ||
|
||
DecodedJwt({ | ||
required this.header, | ||
required this.claims, | ||
required this.signature, | ||
required this.parts, | ||
}); | ||
|
||
Future<void> verify() async { | ||
final decodedJws = DecodedJws( | ||
header: header, | ||
payload: Base64Codec.urlSafe().decoder.convertNoPadding(parts[1]), | ||
signature: signature, | ||
parts: parts, | ||
); | ||
|
||
await decodedJws.verify(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.