Skip to content

Commit 74925d9

Browse files
authored
[gsi_web] Do not initialize CodeClient if scopes are empty. (flutter#5537)
When initializing a `CodeClient`, it is mandatory to pass at least one `scope` (or the JS initialization will crash). This PR ensures that the `CodeClient` is not created unless `initialScopes.isNotEmpty`, and lets the user know when attempting to use said client that it hasn't been initialized properly. ## Issues * Fixes: flutter#139382 * Fixes: flutter#62474 * Closes: flutter/cocoon#3304 (makes this PR unneeded) ## Tests * Tested locally with `cocoon`.
1 parent 2c8512a commit 74925d9

File tree

3 files changed

+24
-11
lines changed

3 files changed

+24
-11
lines changed

packages/google_sign_in/google_sign_in_web/CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
## 0.12.2
1+
## 0.12.2+1
2+
3+
* Re-publishes `0.12.2` with a small fix to the CodeClient initialization.
4+
5+
## 0.12.2 (withdrawn)
26

37
* Adds server auth code retrieval to google_sign_in_web.
48
* Adds `web_only` library to access web-only methods more easily.

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

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,15 @@ class GisSdkClient {
5252
onError: _onTokenError,
5353
);
5454

55-
_codeClient = _initializeCodeClient(
56-
clientId,
57-
hostedDomain: hostedDomain,
58-
onResponse: _onCodeResponse,
59-
onError: _onCodeError,
60-
scopes: initialScopes,
61-
);
55+
if (initialScopes.isNotEmpty) {
56+
_codeClient = _initializeCodeClient(
57+
clientId,
58+
hostedDomain: hostedDomain,
59+
onResponse: _onCodeResponse,
60+
onError: _onCodeError,
61+
scopes: initialScopes,
62+
);
63+
}
6264
}
6365

6466
void _logIfEnabled(String message, [List<Object?>? more]) {
@@ -291,7 +293,13 @@ class GisSdkClient {
291293
/// Requests a server auth code per:
292294
/// https://developers.google.com/identity/oauth2/web/guides/use-code-model#initialize_a_code_client
293295
Future<String?> requestServerAuthCode() async {
294-
_codeClient.requestCode();
296+
// TODO(dit): Enable granular authorization, https://github.com/flutter/flutter/issues/139406
297+
assert(_codeClient != null,
298+
'CodeClient not initialized correctly. Ensure the `scopes` list passed to `init()` or `initWithParams()` is not empty!');
299+
if (_codeClient == null) {
300+
return null;
301+
}
302+
_codeClient!.requestCode();
295303
final CodeResponse response = await _codeResponses.stream.first;
296304
return response.code;
297305
}
@@ -461,7 +469,8 @@ class GisSdkClient {
461469

462470
// The Google Identity Services client for oauth requests.
463471
late TokenClient _tokenClient;
464-
late CodeClient _codeClient;
472+
// CodeClient will not be created if `initialScopes` is empty.
473+
CodeClient? _codeClient;
465474

466475
// Streams of credential and token responses.
467476
late StreamController<CredentialResponse> _credentialResponses;

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.2
6+
version: 0.12.2+1
77

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

0 commit comments

Comments
 (0)