-
Notifications
You must be signed in to change notification settings - Fork 42
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
Fix Issues with verifyIdToken #58
base: main
Are you sure you want to change the base?
Changes from 5 commits
1981f22
f2d9a30
b0c844c
8cab3de
c37a01f
a6d1436
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,12 +9,15 @@ class EmulatorSignatureVerifier implements SignatureVerifier { | |
@override | ||
Future<void> verify(String token) async { | ||
// Signature checks skipped for emulator; no need to fetch public keys. | ||
|
||
try { | ||
verifyJwtSignature( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would recommend maybe keeping the |
||
token, | ||
SecretKey(''), | ||
); | ||
} on JWTInvalidException catch (e) { | ||
// Emulator tokens have "alg": "none" | ||
if (e.message == 'unknown algorithm') return; | ||
if (e.message == 'invalid signature') return; | ||
rethrow; | ||
} | ||
|
@@ -122,11 +125,23 @@ class PublicKeySignatureVerifier implements SignatureVerifier { | |
'no-matching-kid-error', | ||
); | ||
} | ||
verifyJwtSignature( | ||
token, | ||
RSAPublicKey.cert(publicKey), | ||
issueAt: Duration.zero, // Any past date should be valid | ||
); | ||
|
||
try { | ||
verifyJwtSignature( | ||
token, | ||
RSAPublicKey.cert(publicKey), | ||
issueAt: Duration.zero, // Any past date should be valid | ||
); | ||
} catch (e, stackTrace) { | ||
Error.throwWithStackTrace( | ||
JwtError( | ||
JwtErrorCode.invalidSignature, | ||
'Error while verifying signature of Firebase ID token: $e', | ||
), | ||
stackTrace, | ||
); | ||
} | ||
|
||
// At this point most JWTException's should have been caught in | ||
// verifyJwtSignature, but we could still get some from JWT.decode above | ||
} on JWTException catch (e) { | ||
|
@@ -169,14 +184,6 @@ void verifyJwtSignature( | |
), | ||
stackTrace, | ||
); | ||
} catch (e, stackTrace) { | ||
Error.throwWithStackTrace( | ||
JwtError( | ||
JwtErrorCode.invalidSignature, | ||
'Error while verifying signature of Firebase ID token: $e', | ||
), | ||
stackTrace, | ||
); | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import 'package:dart_firebase_admin/src/auth.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
void main() { | ||
group('DecodedIdToken', () { | ||
test('.fromMap', () async { | ||
final idToken = DecodedIdToken.fromMap( | ||
{ | ||
'aud': 'mock-aud', | ||
'auth_time': 1, | ||
'email': 'mock-email', | ||
'email_verified': true, | ||
'exp': 1, | ||
'firebase': { | ||
'identities': { | ||
'email': 'mock-email', | ||
}, | ||
'sign_in_provider': 'mock-sign-in-provider', | ||
'sign_in_second_factor': 'mock-sign-in-second-factor', | ||
'second_factor_identifier': 'mock-second-factor-identifier', | ||
'tenant': 'mock-tenant', | ||
}, | ||
'iat': 1, | ||
'iss': 'mock-iss', | ||
'phone_number': 'mock-phone-number', | ||
'picture': 'mock-picture', | ||
'sub': 'mock-sub', | ||
'uid': 'mock-sub', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The doc says that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch |
||
}, | ||
); | ||
expect(idToken.aud, 'mock-aud'); | ||
expect(idToken.authTime, DateTime.fromMillisecondsSinceEpoch(1000)); | ||
expect(idToken.email, 'mock-email'); | ||
expect(idToken.emailVerified, true); | ||
expect(idToken.exp, 1); | ||
expect(idToken.firebase.identities, {'email': 'mock-email'}); | ||
expect(idToken.firebase.signInProvider, 'mock-sign-in-provider'); | ||
expect(idToken.firebase.signInSecondFactor, 'mock-sign-in-second-factor'); | ||
expect( | ||
idToken.firebase.secondFactorIdentifier, | ||
'mock-second-factor-identifier', | ||
); | ||
expect(idToken.firebase.tenant, 'mock-tenant'); | ||
expect(idToken.iat, 1); | ||
expect(idToken.iss, 'mock-iss'); | ||
expect(idToken.phoneNumber, 'mock-phone-number'); | ||
expect(idToken.picture, 'mock-picture'); | ||
expect(idToken.sub, 'mock-sub'); | ||
expect(idToken.uid, 'mock-sub'); | ||
}); | ||
}); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since behavior changed here, would you mind writing a test?