Skip to content

Commit 61df84d

Browse files
[google_sign_in_web] Fixes force unwrap on values that can be null (#4374)
During Google Sign-in, the code uses two force unwraps on values (name and picture) that can be not present in the response. This cause an unhandled error that blocks sign-in. Fixes flutter/flutter#130002 reported by me. The bug report describes how to get that error together with a screenshot of a given line. My PR fixes that and add additional test for the future.
1 parent c768b14 commit 61df84d

File tree

5 files changed

+48
-3
lines changed

5 files changed

+48
-3
lines changed

packages/google_sign_in/google_sign_in_web/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.12.0+3
2+
3+
* Fixes null cast error on accounts without picture or name details.
4+
15
## 0.12.0+2
26

37
* Adds compatibility with `http` 1.0.

packages/google_sign_in/google_sign_in_web/example/integration_test/src/jwt_examples.dart

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ final CredentialResponse goodCredential =
1818
'credential': goodJwtToken,
1919
});
2020

21+
/// A CredentialResponse wrapping a known good JWT Token as its `credential`.
22+
final CredentialResponse minimalCredential =
23+
jsifyAs<CredentialResponse>(<String, Object?>{
24+
'credential': minimalJwtToken,
25+
});
26+
2127
/// A JWT token with predefined values.
2228
///
2329
/// 'email': '[email protected]',
@@ -38,6 +44,22 @@ const String goodJwtToken =
3844
const String goodPayload =
3945
'eyJlbWFpbCI6ImFkdWx0bWFuQGV4YW1wbGUuY29tIiwic3ViIjoiMTIzNDU2IiwibmFtZSI6IlZpbmNlbnQgQWR1bHRtYW4iLCJwaWN0dXJlIjoiaHR0cHM6Ly90aGlzcGVyc29uZG9lc25vdGV4aXN0LmNvbS9pbWFnZT94PS5qcGcifQ';
4046

47+
/// A JWT token with minimal set of predefined values.
48+
///
49+
/// 'email': '[email protected]',
50+
/// 'sub': '123456'
51+
///
52+
/// Signed with HS256 and the private key: 'symmetric-encryption-is-weak'
53+
const String minimalJwtToken =
54+
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.$minimalPayload.UTAe7dpdtFIMwsOqkZkjyjqyHnho5xHCcQylUFmOutM';
55+
56+
/// The payload of a JWT token that contains only non-nullable values.
57+
///
58+
/// "email": "[email protected]",
59+
/// "sub": "123456"
60+
const String minimalPayload =
61+
'eyJlbWFpbCI6ImFkdWx0bWFuQGV4YW1wbGUuY29tIiwic3ViIjoiMTIzNDU2In0';
62+
4163
// More encrypted JWT Tokens may be created on https://jwt.io.
4264
//
4365
// First, decode the `goodJwtToken` above, modify to your heart's

packages/google_sign_in/google_sign_in_web/example/integration_test/utils_test.dart

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,17 @@ void main() {
5757
expect(data.idToken, goodJwtToken);
5858
});
5959

60+
testWidgets('happy case (minimal)', (_) async {
61+
final GoogleSignInUserData data =
62+
gisResponsesToUserData(minimalCredential)!;
63+
64+
expect(data.displayName, isNull);
65+
expect(data.id, '123456');
66+
expect(data.email, '[email protected]');
67+
expect(data.photoUrl, isNull);
68+
expect(data.idToken, minimalJwtToken);
69+
});
70+
6071
testWidgets('null response -> null', (_) async {
6172
expect(gisResponsesToUserData(null), isNull);
6273
});
@@ -90,6 +101,14 @@ void main() {
90101
));
91102
});
92103

104+
testWidgets('happy case (minimal) -> data', (_) async {
105+
final Map<String, Object?>? data = getJwtTokenPayload(minimalJwtToken);
106+
107+
expect(data, isNotNull);
108+
expect(data, containsPair('email', '[email protected]'));
109+
expect(data, containsPair('sub', '123456'));
110+
});
111+
93112
testWidgets('null Token -> null', (_) async {
94113
final Map<String, Object?>? data = getJwtTokenPayload(null);
95114

packages/google_sign_in/google_sign_in_web/lib/src/utils.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ GoogleSignInUserData? gisResponsesToUserData(
7272
return GoogleSignInUserData(
7373
email: payload['email']! as String,
7474
id: payload['sub']! as String,
75-
displayName: payload['name']! as String,
76-
photoUrl: payload['picture']! as String,
75+
displayName: payload['name'] as String?,
76+
photoUrl: payload['picture'] as String?,
7777
idToken: credentialResponse.credential,
7878
);
7979
}

packages/google_sign_in/google_sign_in_web/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ description: Flutter plugin for Google Sign-In, a secure authentication system
33
for signing in with a Google account on Android, iOS and Web.
44
repository: https://github.com/flutter/packages/tree/main/packages/google_sign_in/google_sign_in_web
55
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+google_sign_in%22
6-
version: 0.12.0+2
6+
version: 0.12.0+3
77

88
environment:
99
sdk: ">=2.18.0 <4.0.0"

0 commit comments

Comments
 (0)