-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(jwt): upgrade analysis_options (#2715)
- Loading branch information
Showing
13 changed files
with
127 additions
and
41 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 |
---|---|---|
@@ -1 +1,3 @@ | ||
include: package:very_good_analysis/analysis_options.5.1.0.yaml | ||
include: package:very_good_analysis/analysis_options.7.0.0.yaml | ||
analyzer: | ||
exclude: ["example/**"] |
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 |
---|---|---|
@@ -1,4 +1,3 @@ | ||
// ignore_for_file: avoid_print | ||
import 'package:jwt/jwt.dart' as jwt; | ||
|
||
Future<void> main() async { | ||
|
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
packages/jwt/lib/src/models/public_key_store/jwk_key_store.g.dart
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,4 +1,3 @@ | ||
// ignore_for_file: prefer_const_constructors | ||
import 'dart:io'; | ||
|
||
import 'package:clock/clock.dart'; | ||
|
@@ -10,10 +9,14 @@ import 'package:test/test.dart'; | |
void main() { | ||
const token = // cspell: disable-next-line | ||
'''eyJhbGciOiJSUzI1NiIsImtpZCI6ImMxMGM5MGJhNGMzNjYzNTE2ZTA3MDdkMGU5YTg5NDgxMDYyODUxNTgiLCJ0eXAiOiJKV1QifQ.eyJpc3MiOiJodHRwczovL3NlY3VyZXRva2VuLmdvb2dsZS5jb20vbXktYXBwIiwiYXVkIjoibXktYXBwIiwiYXV0aF90aW1lIjoxNjQzNjg0MjY2LCJ1c2VyX2lkIjoiRzR1MzdXdk90dmVWR0pRb1pCWGpxcHVWazZWMiIsInN1YiI6Ikc0dTM3V3ZPdHZlVkdKUW9aQlhqcXB1Vms2VjIiLCJpYXQiOjE2NDM2ODQyNjYsImV4cCI6MTY0MzY4Nzg2NiwiZW1haWwiOiJ0ZXN0QGdtYWlsLmNvbSIsImVtYWlsX3ZlcmlmaWVkIjp0cnVlLCJmaXJlYmFzZSI6eyJpZGVudGl0aWVzIjp7ImVtYWlsIjpbInRlc3RAZ21haWwuY29tIl19LCJzaWduX2luX3Byb3ZpZGVyIjoicGFzc3dvcmQifX0.bUWnX_XmR1d9EmeFeYSsK_CHU1u9NPIHgyaQueZ6urYOtxvuL_QodjPl0c9CBJwctwPnxVyRmkeNCw0oF9xBgph0NApLL4FIG6vpDPZfW9txZBYr8xIvaqvmD0diACENAQdjRT2XmyEdQ2-U7SsTonybHmLoU9FMQTjAgw4NCALQvExfB6rtQ9GDsOBt1xoBkB3Vo7a5OmugZ1aHXF69b8As6137-Dggf5qx5R3oLRFovICMMesQziE3vGi-WKcbQxSeiD-9a6ShPAhk41XiyjFGDEOtUCQo63uwQnMw3g0KVtC6bzIyFq-E91vhxumxXzxPYC-kg7iUYiSZy7Y-Aw'''; | ||
const tokenInvalidPayload = // cspell: disable-next-line | ||
'''eyJhbGciOiJSUzI1NiIsImtpZCI6ImMxMGM5MGJhNGMzNjYzNTE2ZTA3MDdkMGU5YTg5NDgxMDYyODUxNTgiLCJ0eXAiOiJKV1QifQ.invalid.bUWnX_XmR1d9EmeFeYSsK_CHU1u9NPIHgyaQueZ6urYOtxvuL_QodjPl0c9CBJwctwPnxVyRmkeNCw0oF9xBgph0NApLL4FIG6vpDPZfW9txZBYr8xIvaqvmD0diACENAQdjRT2XmyEdQ2-U7SsTonybHmLoU9FMQTjAgw4NCALQvExfB6rtQ9GDsOBt1xoBkB3Vo7a5OmugZ1aHXF69b8As6137-Dggf5qx5R3oLRFovICMMesQziE3vGi-WKcbQxSeiD-9a6ShPAhk41XiyjFGDEOtUCQo63uwQnMw3g0KVtC6bzIyFq-E91vhxumxXzxPYC-kg7iUYiSZy7Y-Aw'''; | ||
const tokenNoAuthTime = // cspell: disable-next-line | ||
'''eyJhbGciOiJSUzI1NiIsImtpZCI6ImMxMGM5MGJhNGMzNjYzNTE2ZTA3MDdkMGU5YTg5NDgxMDYyODUxNTgiLCJ0eXAiOiJKV1QifQ.eyJpc3MiOiJodHRwczovL3NlY3VyZXRva2VuLmdvb2dsZS5jb20vbXktYXBwIiwiYXVkIjoibXktYXBwIiwidXNlcl9pZCI6Ikc0dTM3V3ZPdHZlVkdKUW9aQlhqcXB1Vms2VjIiLCJzdWIiOiJHNHUzN1d2T3R2ZVZHSlFvWkJYanFwdVZrNlYyIiwiaWF0IjoxNjQzN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ttf36aDkVR97Rd50aMp-0yzk8do_4AUPFi9nEhKu0Ye8ox_9hdTBttJYxoOq0NbH2zz0JHSnSHqoTvCleVhoGqg8YghzH0NiqncPfDzi-IcRfy2K8CrOoXuqXaj3YWrbrzNWAYV46eFmvI2TmPO55AyFOjhvLpW-uf96ceOPjueZm8o5K2DZym86BAhSdShknONV2O7b2vW34TXf3UJdISs5p9z6Si4JOWGjbVPY45CO16ODdDxyUGUM2-IVQloB7bg0nMqQbIjKDoO5g9d1nguR2Z-YBiv0BX1MDWDlrBsMAzzRVaFlf6YQS1LKxu2tnMdCrBnK_Wvqt5sQ'''; | ||
const tokenInvalidSignature = // cspell: disable-next-line | ||
'''eyJhbGciOiJSUzI1NiIsImtpZCI6ImMxMGM5MGJhNGMzNjYzNTE2ZTA3MDdkMGU5YTg5NDgxMDYyODUxNTgiLCJ0eXAiOiJKV1QifQ.eyJpc3MiOiJodHRwczovL3NlY3VyZXRva2VuLmdvb2dsZS5jb20vbXktYXBwIiwiYXVkIjoibXktYXBwIiwiYXV0aF90aW1lIjoxNjQzNjg0MjY2LCJ1c2VyX2lkIjoiRzR1MzdXdk90dmVWR0pRb1pCWGpxcHVWazZWMiIsInN1YiI6Ikc0dTM3V3ZPdHZlVkdKUW9aQlhqcXB1Vms2VjIiLCJpYXQiOjE2NDM2ODQyNjYsImV4cCI6MTY0MzY4Nzg2NiwiZW1haWwiOiJ0ZXN0QGdtYWlsLmNvbSIsImVtYWlsX3ZlcmlmaWVkIjp0cnVlLCJmaXJlYmFzZSI6eyJpZGVudGl0aWVzIjp7ImVtYWlsIjpbInRlc3RAZ21haWwuY29tIl19LCJzaWduX2luX3Byb3ZpZGVyIjoicGFzc3dvcmQifX0.invalid-signature'''; | ||
const keyValuePublicKeysUrl = | ||
'https://www.googleapis.com/robot/v1/metadata/x509/[email protected]'; | ||
const jwkPublicKeysUrl = | ||
|
@@ -24,7 +27,7 @@ void main() { | |
File(p.join('test', 'fixtures', 'key_value_key_store.json')) | ||
.readAsStringSync(); | ||
final expiresAt = DateTime.fromMillisecondsSinceEpoch(1643687866 * 1000); | ||
final validTime = expiresAt.subtract(Duration(minutes: 15)); | ||
final validTime = expiresAt.subtract(const Duration(minutes: 15)); | ||
|
||
late String keyStoreResponseBody; | ||
|
||
|
@@ -85,6 +88,45 @@ void main() { | |
); | ||
}); | ||
|
||
test('throws a JwtVerificationFailure if payload is not valid', () async { | ||
await expectLater( | ||
() => verify( | ||
tokenInvalidPayload, | ||
audience: {audience}, | ||
issuer: issuer, | ||
publicKeysUrl: keyValuePublicKeysUrl, | ||
), | ||
throwsA( | ||
isA<JwtVerificationFailure>().having( | ||
(e) => e.reason, | ||
'reason', | ||
'JWT payload is malformed.', | ||
), | ||
), | ||
); | ||
}); | ||
|
||
test('throws a JwtVerificationFailure if signature is not valid', | ||
() async { | ||
await withClock(Clock.fixed(validTime), () async { | ||
await expectLater( | ||
() => verify( | ||
tokenInvalidSignature, | ||
audience: {audience}, | ||
issuer: issuer, | ||
publicKeysUrl: keyValuePublicKeysUrl, | ||
), | ||
throwsA( | ||
isA<JwtVerificationFailure>().having( | ||
(e) => e.reason, | ||
'reason', | ||
'JWT signature is malformed.', | ||
), | ||
), | ||
); | ||
}); | ||
}); | ||
|
||
test('throws exception if jwt has no matching public key id', () async { | ||
await withClock(Clock.fixed(validTime), () async { | ||
await expectLater( | ||
|
@@ -105,8 +147,9 @@ void main() { | |
}); | ||
}); | ||
|
||
test('throws exception if invalid keys are provided by the publicKeysUrl', | ||
() async { | ||
test( | ||
'throws exception if invalid keys are provided ' | ||
'by the publicKeysUrl (KeyValueKeyStore)', () async { | ||
getOverride = (Uri uri) async { | ||
return Response( | ||
'{"123": 456}', | ||
|
@@ -134,6 +177,36 @@ void main() { | |
}); | ||
}); | ||
|
||
test( | ||
'throws exception if invalid keys are provided ' | ||
'by the publicKeysUrl (JwkKeyStore)', () async { | ||
getOverride = (Uri uri) async { | ||
return Response( | ||
'{"keys": 456}', | ||
HttpStatus.ok, | ||
headers: {'cache-control': 'max-age=3600'}, | ||
); | ||
}; | ||
|
||
await withClock(Clock.fixed(validTime), () async { | ||
await expectLater( | ||
() => verify( | ||
tokenWithNoMatchingKid, | ||
audience: {audience}, | ||
issuer: issuer, | ||
publicKeysUrl: keyValuePublicKeysUrl, | ||
), | ||
throwsA( | ||
isA<JwtVerificationFailure>().having( | ||
(e) => e.reason, | ||
'reason', | ||
'''Invalid public keys returned by https://www.googleapis.com/robot/v1/metadata/x509/[email protected].''', | ||
), | ||
), | ||
); | ||
}); | ||
}); | ||
|
||
test('can verify an invalid audience', () async { | ||
await withClock(Clock.fixed(validTime), () async { | ||
try { | ||
|
@@ -144,7 +217,7 @@ void main() { | |
publicKeysUrl: keyValuePublicKeysUrl, | ||
); | ||
fail('should throw'); | ||
} catch (error) { | ||
} on Exception catch (error) { | ||
expect( | ||
error, | ||
isA<JwtVerificationFailure>().having( | ||
|
@@ -167,7 +240,7 @@ void main() { | |
publicKeysUrl: keyValuePublicKeysUrl, | ||
); | ||
fail('should throw'); | ||
} catch (error) { | ||
} on Exception catch (error) { | ||
expect( | ||
error, | ||
isA<JwtVerificationFailure>().having( | ||
|
@@ -269,7 +342,7 @@ void main() { | |
group('JwtVerificationFailure', () { | ||
test('toString is correct', () { | ||
const reason = 'reason'; | ||
final failure = JwtVerificationFailure(reason); | ||
const failure = JwtVerificationFailure(reason); | ||
expect(failure.toString(), equals('JwtVerificationFailure: $reason')); | ||
}); | ||
}); | ||
|