Skip to content

[google_sign_in_web] Fixes force unwrap on values that can be null #4374

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

Merged
merged 1 commit into from
Jul 6, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions packages/google_sign_in/google_sign_in_web/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.12.0+3

* Fixes null cast error on accounts without picture or name details.

## 0.12.0+2

* Adds compatibility with `http` 1.0.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ final CredentialResponse goodCredential =
'credential': goodJwtToken,
});

/// A CredentialResponse wrapping a known good JWT Token as its `credential`.
final CredentialResponse minimalCredential =
jsifyAs<CredentialResponse>(<String, Object?>{
'credential': minimalJwtToken,
});

/// A JWT token with predefined values.
///
/// 'email': '[email protected]',
Expand All @@ -38,6 +44,22 @@ const String goodJwtToken =
const String goodPayload =
'eyJlbWFpbCI6ImFkdWx0bWFuQGV4YW1wbGUuY29tIiwic3ViIjoiMTIzNDU2IiwibmFtZSI6IlZpbmNlbnQgQWR1bHRtYW4iLCJwaWN0dXJlIjoiaHR0cHM6Ly90aGlzcGVyc29uZG9lc25vdGV4aXN0LmNvbS9pbWFnZT94PS5qcGcifQ';

/// A JWT token with minimal set of predefined values.
///
/// 'email': '[email protected]',
/// 'sub': '123456'
///
/// Signed with HS256 and the private key: 'symmetric-encryption-is-weak'
const String minimalJwtToken =
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.$minimalPayload.UTAe7dpdtFIMwsOqkZkjyjqyHnho5xHCcQylUFmOutM';
Comment on lines +53 to +54
Copy link
Member

Choose a reason for hiding this comment

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

Thanks for adding a test!

I'm glad you managed to create one of these, it's not a trivial process!


/// The payload of a JWT token that contains only non-nullable values.
///
/// "email": "[email protected]",
/// "sub": "123456"
const String minimalPayload =
'eyJlbWFpbCI6ImFkdWx0bWFuQGV4YW1wbGUuY29tIiwic3ViIjoiMTIzNDU2In0';

// More encrypted JWT Tokens may be created on https://jwt.io.
//
// First, decode the `goodJwtToken` above, modify to your heart's
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,17 @@ void main() {
expect(data.idToken, goodJwtToken);
});

testWidgets('happy case (minimal)', (_) async {
final GoogleSignInUserData data =
gisResponsesToUserData(minimalCredential)!;

expect(data.displayName, isNull);
expect(data.id, '123456');
expect(data.email, '[email protected]');
expect(data.photoUrl, isNull);
expect(data.idToken, minimalJwtToken);
});

testWidgets('null response -> null', (_) async {
expect(gisResponsesToUserData(null), isNull);
});
Expand Down Expand Up @@ -90,6 +101,14 @@ void main() {
));
});

testWidgets('happy case (minimal) -> data', (_) async {
final Map<String, Object?>? data = getJwtTokenPayload(minimalJwtToken);

expect(data, isNotNull);
expect(data, containsPair('email', '[email protected]'));
expect(data, containsPair('sub', '123456'));
});

testWidgets('null Token -> null', (_) async {
final Map<String, Object?>? data = getJwtTokenPayload(null);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ GoogleSignInUserData? gisResponsesToUserData(
return GoogleSignInUserData(
email: payload['email']! as String,
id: payload['sub']! as String,
displayName: payload['name']! as String,
photoUrl: payload['picture']! as String,
displayName: payload['name'] as String?,
photoUrl: payload['picture'] as String?,
Comment on lines -75 to +76
Copy link
Member

Choose a reason for hiding this comment

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

This makes total sense. Even the documentation of GoogleSignInUserData has the following comment on top of displayName and photoUrl:

/// Not guaranteed to be present for all users, even when configured.

idToken: credentialResponse.credential,
);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/google_sign_in/google_sign_in_web/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description: Flutter plugin for Google Sign-In, a secure authentication system
for signing in with a Google account on Android, iOS and Web.
repository: https://github.com/flutter/packages/tree/main/packages/google_sign_in/google_sign_in_web
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+google_sign_in%22
version: 0.12.0+2
version: 0.12.0+3

environment:
sdk: ">=2.18.0 <4.0.0"
Expand Down