Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit d07de93

Browse files
authored
[google_sign_in] Support Dart-based configuration and serverClientId (#5250)
1 parent 569268f commit d07de93

File tree

5 files changed

+110
-19
lines changed

5 files changed

+110
-19
lines changed

packages/google_sign_in/google_sign_in/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 5.4.0
2+
3+
* Adds support for configuring `serverClientId` through `GoogleSignIn` constructor.
4+
* Adds support for Dart-based configuration as alternative to `GoogleService-Info.plist` for iOS.
5+
16
## 5.3.3
27

38
* Updates references to the obsolete master branch.

packages/google_sign_in/google_sign_in/README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,22 @@ This plugin requires iOS 9.0 or higher.
6565
<!-- End of the Google Sign-in Section -->
6666
```
6767

68+
As an alternative to adding `GoogleService-Info.plist` to your Xcode project, you can instead
69+
configure your app in Dart code. In this case, skip steps 3-6 and pass `clientId` and
70+
`serverClientId` to the `GoogleSignIn` constructor:
71+
72+
```dart
73+
GoogleSignIn _googleSignIn = GoogleSignIn(
74+
...
75+
// The OAuth client id of your app. This is required.
76+
clientId: ...,
77+
// If you need to authenticate to a backend server, specify its OAuth client. This is optional.
78+
serverClientId: ...,
79+
);
80+
```
81+
82+
Note that step 7 is still required.
83+
6884
#### iOS additional requirement
6985

7086
Note that according to

packages/google_sign_in/google_sign_in/lib/google_sign_in.dart

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import 'src/common.dart';
1212

1313
export 'package:google_sign_in_platform_interface/google_sign_in_platform_interface.dart'
1414
show SignInOption;
15+
1516
export 'src/common.dart';
1617
export 'widgets.dart';
1718

@@ -183,6 +184,7 @@ class GoogleSignIn {
183184
this.scopes = const <String>[],
184185
this.hostedDomain,
185186
this.clientId,
187+
this.serverClientId,
186188
});
187189

188190
/// Factory for creating default sign in user experience.
@@ -228,9 +230,29 @@ class GoogleSignIn {
228230
/// Domain to restrict sign-in to.
229231
final String? hostedDomain;
230232

231-
/// Client ID being used to connect to google sign-in. Only supported on web.
233+
/// Client ID being used to connect to google sign-in.
234+
///
235+
/// This option is not supported on all platforms (e.g. Android). It is
236+
/// optional if file-based configuration is used.
237+
///
238+
/// The value specified here has precedence over a value from a configuration
239+
/// file.
232240
final String? clientId;
233241

242+
/// Client ID of the backend server to which the app needs to authenticate
243+
/// itself.
244+
///
245+
/// Optional and not supported on all platforms (e.g. web). By default, it
246+
/// is initialized from a configuration file if available.
247+
///
248+
/// The value specified here has precedence over a value from a configuration
249+
/// file.
250+
///
251+
/// [GoogleSignInAuthentication.idToken] and
252+
/// [GoogleSignInAccount.serverAuthCode] will be specific to the backend
253+
/// server.
254+
final String? serverClientId;
255+
234256
final StreamController<GoogleSignInAccount?> _currentUserController =
235257
StreamController<GoogleSignInAccount?>.broadcast();
236258

@@ -260,15 +282,18 @@ class GoogleSignIn {
260282
}
261283

262284
Future<void> _ensureInitialized() {
263-
return _initialization ??= GoogleSignInPlatform.instance.init(
285+
return _initialization ??=
286+
GoogleSignInPlatform.instance.initWithParams(SignInInitParameters(
264287
signInOption: signInOption,
265288
scopes: scopes,
266289
hostedDomain: hostedDomain,
267290
clientId: clientId,
268-
)..catchError((dynamic _) {
269-
// Invalidate initialization if it errors out.
270-
_initialization = null;
271-
});
291+
serverClientId: serverClientId,
292+
))
293+
..catchError((dynamic _) {
294+
// Invalidate initialization if it errors out.
295+
_initialization = null;
296+
});
272297
}
273298

274299
/// The most recently scheduled method call.

packages/google_sign_in/google_sign_in/pubspec.yaml

Lines changed: 4 additions & 4 deletions
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 and iOS.
44
repository: https://github.com/flutter/plugins/tree/main/packages/google_sign_in/google_sign_in
55
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+google_sign_in%22
6-
version: 5.3.3
6+
version: 5.4.0
77

88

99
environment:
@@ -23,9 +23,9 @@ flutter:
2323
dependencies:
2424
flutter:
2525
sdk: flutter
26-
google_sign_in_android: ^5.2.5
27-
google_sign_in_ios: ^5.2.5
28-
google_sign_in_platform_interface: ^2.1.0
26+
google_sign_in_android: ^6.0.0
27+
google_sign_in_ios: ^5.4.0
28+
google_sign_in_platform_interface: ^2.2.0
2929
google_sign_in_web: ^0.10.0
3030

3131
dev_dependencies:

packages/google_sign_in/google_sign_in/test/google_sign_in_test.dart

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import 'package:google_sign_in/google_sign_in.dart';
99
import 'package:google_sign_in_platform_interface/google_sign_in_platform_interface.dart';
1010
import 'package:mockito/annotations.dart';
1111
import 'package:mockito/mockito.dart';
12+
1213
import 'google_sign_in_test.mocks.dart';
1314

1415
/// Verify that [GoogleSignInAccount] can be mocked even though it's unused
@@ -58,7 +59,7 @@ void main() {
5859
verify(mockPlatform.signIn());
5960
});
6061

61-
test('signIn prioritize clientId parameter when available', () async {
62+
test('clientId parameter is forwarded to implementation', () async {
6263
const String fakeClientId = 'fakeClientId';
6364
final GoogleSignIn googleSignIn = GoogleSignIn(clientId: fakeClientId);
6465

@@ -68,6 +69,17 @@ void main() {
6869
verify(mockPlatform.signIn());
6970
});
7071

72+
test('serverClientId parameter is forwarded to implementation', () async {
73+
const String fakeServerClientId = 'fakeServerClientId';
74+
final GoogleSignIn googleSignIn =
75+
GoogleSignIn(serverClientId: fakeServerClientId);
76+
77+
await googleSignIn.signIn();
78+
79+
_verifyInit(mockPlatform, serverClientId: fakeServerClientId);
80+
verify(mockPlatform.signIn());
81+
});
82+
7183
test('signOut', () async {
7284
final GoogleSignIn googleSignIn = GoogleSignIn();
7385

@@ -240,10 +252,12 @@ void main() {
240252
test('can sign in after init failed before', () async {
241253
final GoogleSignIn googleSignIn = GoogleSignIn();
242254

243-
when(mockPlatform.init()).thenThrow(Exception('First init fails'));
255+
when(mockPlatform.initWithParams(any))
256+
.thenThrow(Exception('First init fails'));
244257
expect(googleSignIn.signIn(), throwsA(isInstanceOf<Exception>()));
245258

246-
when(mockPlatform.init()).thenAnswer((Invocation _) async {});
259+
when(mockPlatform.initWithParams(any))
260+
.thenAnswer((Invocation _) async {});
247261
expect(await googleSignIn.signIn(), isNotNull);
248262
});
249263

@@ -334,13 +348,44 @@ void main() {
334348

335349
void _verifyInit(
336350
MockGoogleSignInPlatform mockSignIn, {
351+
List<String> scopes = const <String>[],
337352
SignInOption signInOption = SignInOption.standard,
353+
String? hostedDomain,
338354
String? clientId,
355+
String? serverClientId,
356+
bool forceCodeForRefreshToken = false,
339357
}) {
340-
verify(mockSignIn.init(
341-
signInOption: signInOption,
342-
scopes: <String>[],
343-
hostedDomain: null,
344-
clientId: clientId,
345-
));
358+
verify(mockSignIn.initWithParams(argThat(
359+
isA<SignInInitParameters>()
360+
.having(
361+
(SignInInitParameters p) => p.scopes,
362+
'scopes',
363+
scopes,
364+
)
365+
.having(
366+
(SignInInitParameters p) => p.signInOption,
367+
'signInOption',
368+
signInOption,
369+
)
370+
.having(
371+
(SignInInitParameters p) => p.hostedDomain,
372+
'hostedDomain',
373+
hostedDomain,
374+
)
375+
.having(
376+
(SignInInitParameters p) => p.clientId,
377+
'clientId',
378+
clientId,
379+
)
380+
.having(
381+
(SignInInitParameters p) => p.serverClientId,
382+
'serverClientId',
383+
serverClientId,
384+
)
385+
.having(
386+
(SignInInitParameters p) => p.forceCodeForRefreshToken,
387+
'forceCodeForRefreshToken',
388+
forceCodeForRefreshToken,
389+
),
390+
)));
346391
}

0 commit comments

Comments
 (0)