-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Finish implementing the token verification using the updated dart_jsonwebtoken library
- Loading branch information
Showing
4 changed files
with
169 additions
and
8 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
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
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,118 @@ | ||
import 'dart:convert'; | ||
|
||
import 'package:asn1lib/asn1lib.dart'; | ||
import 'package:dart_firebase_admin/src/utils/jwt.dart'; | ||
import 'package:dart_jsonwebtoken/dart_jsonwebtoken.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
void main() { | ||
group('PublicKeySignatureVerifier', () { | ||
final privateKey = RSAPrivateKey(''' | ||
-----BEGIN PRIVATE KEY----- | ||
MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQC7VJTUt9Us8cKj | ||
MzEfYyjiWA4R4/M2bS1GB4t7NXp98C3SC6dVMvDuictGeurT8jNbvJZHtCSuYEvu | ||
NMoSfm76oqFvAp8Gy0iz5sxjZmSnXyCdPEovGhLa0VzMaQ8s+CLOyS56YyCFGeJZ | ||
qgtzJ6GR3eqoYSW9b9UMvkBpZODSctWSNGj3P7jRFDO5VoTwCQAWbFnOjDfH5Ulg | ||
p2PKSQnSJP3AJLQNFNe7br1XbrhV//eO+t51mIpGSDCUv3E0DDFcWDTH9cXDTTlR | ||
ZVEiR2B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
2bzbsYEfYx3EoEVgMEpPhoarQnYPukrJO4gwE2o5Te6T5mJSZGlQJQj9q4ZB2Dfz | ||
et6INsK0oG8XVGXSpQvQh3RUYekCZQkBBFcpqWpbIEsCgYAnM3DQf3FJoSnXaMhr | ||
VBIovic5l0xFkEHskAjFTevO86Fsz1C2aSeRKSqGFoOQ0tmJzBEs1R6KqnHInicD | ||
TQrKhArgLXX4v3CddjfTRJkFWDbE/CkvKZNOrcf1nhaGCPspRJj2KUkj1Fhl9Cnc | ||
dn/RsYEONbwQSjIfMPkvxF+8HQ== | ||
-----END PRIVATE KEY----- | ||
'''); | ||
final keyFetcher = _TestKeyFetcher(); | ||
final payload = { | ||
'a': '1', | ||
'iat': DateTime.now().millisecondsSinceEpoch ~/ 1000, | ||
}; | ||
|
||
test('valid kid should pass', () async { | ||
final jwt = JWT( | ||
payload, | ||
header: {'kid': 'key1'}, | ||
); | ||
final token = jwt.sign( | ||
privateKey, | ||
algorithm: JWTAlgorithm.RS256, | ||
); | ||
await PublicKeySignatureVerifier(keyFetcher).verify(token); | ||
}); | ||
test('no kid should throw', () async { | ||
final jwt = JWT(payload); | ||
final token = jwt.sign( | ||
privateKey, | ||
algorithm: JWTAlgorithm.RS256, | ||
); | ||
await expectLater( | ||
PublicKeySignatureVerifier(keyFetcher).verify(token), | ||
throwsA(isA<JwtError>()), | ||
); | ||
}); | ||
test('invalid kid should throw', () async { | ||
final jwt = JWT( | ||
payload, | ||
header: {'kid': 'key2'}, | ||
); | ||
final token = jwt.sign( | ||
privateKey, | ||
algorithm: JWTAlgorithm.RS256, | ||
); | ||
await expectLater( | ||
PublicKeySignatureVerifier(keyFetcher).verify(token), | ||
throwsA(isA<JwtError>()), | ||
); | ||
}); | ||
}); | ||
} | ||
|
||
class _TestKeyFetcher implements KeyFetcher { | ||
@override | ||
Future<Map<String, String>> fetchPublicKeys() { | ||
return Future.value({ | ||
'key1': _publicKeyAsCert(''' | ||
-----BEGIN PUBLIC KEY----- | ||
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAu1SU1LfVLPHCozMxH2Mo | ||
4lgOEePzNm0tRgeLezV6ffAt0gunVTLw7onLRnrq0/IzW7yWR7QkrmBL7jTKEn5u | ||
+qKhbwKfBstIs+bMY2Zkp18gnTxKLxoS2tFczGkPLPgizskuemMghRniWaoLcyeh | ||
kd3qqGElvW/VDL5AaWTg0nLVkjRo9z+40RQzuVaE8AkAFmxZzow3x+VJYKdjykkJ | ||
0iT9wCS0DRTXu269V264Vf/3jvredZiKRkgwlL9xNAwxXFg0x/XFw005UWVRIkdg | ||
cKWTjpBP2dPwVZ4WWC+9aGVd+Gyn1o0CLelf4rEjGoXbAAEgAqeGUxrcIlbjXfbc | ||
mwIDAQAB | ||
-----END PUBLIC KEY----- | ||
'''), | ||
}); | ||
} | ||
} | ||
|
||
String _publicKeyAsCert(String publicKey) { | ||
final lines = LineSplitter.split(publicKey) | ||
.map((line) => line.trim()) | ||
.where((line) => line.isNotEmpty) | ||
.toList(); | ||
final base64 = lines.sublist(1, lines.length - 1).join(); | ||
final bytes = base64Decode(base64); | ||
final certSequence = ASN1Sequence(); | ||
final root = ASN1Sequence()..add(certSequence); | ||
for (var i = 0; i < 5; i++) { | ||
certSequence.add(ASN1Integer.fromInt(0)); | ||
} | ||
certSequence.add(ASN1Object.fromBytes(bytes)); | ||
return '${lines.first}\n${base64Encode(root.encodedBytes)}\n${lines.last}'; | ||
} |