Skip to content
Closed
Show file tree
Hide file tree
Changes from 5 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
25 changes: 14 additions & 11 deletions packages/dart_firebase_admin/lib/src/auth/token_verifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,7 @@ class FirebaseTokenVerifier {
isEmulator: isEmulator,
);

final decodedIdToken = DecodedIdToken.fromMap(decoded.payload);
decodedIdToken.uid = decodedIdToken.sub;
return decodedIdToken;
return DecodedIdToken.fromMap(decoded.payload);
}

Future<DecodedToken> _decodeAndVerify(
Expand Down Expand Up @@ -249,6 +247,17 @@ class TokenProvider {
required this.tenant,
});

@internal
factory TokenProvider.fromMap(Map<dynamic, dynamic> map) {
return TokenProvider(
identities: map['identities']! as Map<String, Object?>,
Copy link
Copy Markdown
Contributor

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?

signInProvider: map['sign_in_provider']! as String,
signInSecondFactor: map['sign_in_second_factor'] as String?,
secondFactorIdentifier: map['second_factor_identifier'] as String?,
tenant: map['tenant'] as String?,
);
}

/// Provider-specific identity details corresponding
/// to the provider used to sign in the user.
Map<String, Object?> identities;
Expand Down Expand Up @@ -313,19 +322,13 @@ class DecodedIdToken {
email: map['email'] as String?,
emailVerified: map['email_verified'] as bool?,
exp: map['exp']! as int,
firebase: TokenProvider(
identities: Map.from(map['firebase']! as Map),
signInProvider: map['sign_in_provider']! as String,
signInSecondFactor: map['sign_in_second_factor'] as String?,
secondFactorIdentifier: map['second_factor_identifier'] as String?,
tenant: map['tenant'] as String?,
),
firebase: TokenProvider.fromMap(map['firebase']! as Map),
iat: map['iat']! as int,
iss: map['iss']! as String,
phoneNumber: map['phone_number'] as String?,
picture: map['picture'] as String?,
sub: map['sub']! as String,
uid: map['uid']! as String,
uid: map['sub']! as String,
);
}

Expand Down
33 changes: 20 additions & 13 deletions packages/dart_firebase_admin/lib/src/utils/jwt.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Copy link
Copy Markdown
Contributor

@labrom labrom Nov 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would recommend maybe keeping the verifyJwtSignature function call, and modifying the function to only catch JWTExpiredException, since the handling of JWTExpiredException is the same in both verifiers.

token,
SecretKey(''),
);
} on JWTInvalidException catch (e) {
// Emulator tokens have "alg": "none"
if (e.message == 'unknown algorithm') return;
if (e.message == 'invalid signature') return;
rethrow;
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
);
}
}

Expand Down
52 changes: 52 additions & 0 deletions packages/dart_firebase_admin/test/auth/token_verifier_test.dart
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',
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The doc says that uid isn't actually present in the token, and this field is just a convenience that takes its value from sub. How about removing this line then?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The 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');
});
});
}