From 2e818c5813b9696f4e289f6f2a86ac05fb049311 Mon Sep 17 00:00:00 2001 From: Jarrod Sinclair Date: Wed, 10 Apr 2019 23:46:08 +1000 Subject: [PATCH 1/2] Adding serverClientID optional argument to GoogleSignIn, support for both iOS and Android --- .../googlesignin/GoogleSignInPlugin.java | 31 +++++++++---------- packages/google_sign_in/example/lib/main.dart | 2 +- .../ios/Classes/GoogleSignInPlugin.m | 4 +++ .../google_sign_in/lib/google_sign_in.dart | 13 ++++++-- .../test/google_sign_in_test.dart | 13 ++++++++ 5 files changed, 43 insertions(+), 20 deletions(-) diff --git a/packages/google_sign_in/android/src/main/java/io/flutter/plugins/googlesignin/GoogleSignInPlugin.java b/packages/google_sign_in/android/src/main/java/io/flutter/plugins/googlesignin/GoogleSignInPlugin.java index d2fc260c7b1c..92a9c39d8982 100755 --- a/packages/google_sign_in/android/src/main/java/io/flutter/plugins/googlesignin/GoogleSignInPlugin.java +++ b/packages/google_sign_in/android/src/main/java/io/flutter/plugins/googlesignin/GoogleSignInPlugin.java @@ -66,7 +66,8 @@ public void onMethodCall(MethodCall call, Result result) { String signInOption = call.argument("signInOption"); List requestedScopes = call.argument("scopes"); String hostedDomain = call.argument("hostedDomain"); - delegate.init(result, signInOption, requestedScopes, hostedDomain); + String serverClientID = call.argument("serverClientID"); + delegate.init(result, signInOption, requestedScopes, hostedDomain, serverClientID); break; case METHOD_SIGN_IN_SILENTLY: @@ -113,7 +114,11 @@ public void onMethodCall(MethodCall call, Result result) { public interface IDelegate { /** Initializes this delegate so that it is ready to perform other operations. */ public void init( - Result result, String signInOption, List requestedScopes, String hostedDomain); + Result result, + String signInOption, + List requestedScopes, + String hostedDomain, + String serverClientID); /** * Returns the account information for the user who is signed in to this app. If no user is @@ -210,7 +215,11 @@ private void checkAndSetPendingOperation(String method, Result result, Object da */ @Override public void init( - Result result, String signInOption, List requestedScopes, String hostedDomain) { + Result result, + String signInOption, + List requestedScopes, + String hostedDomain, + String serverClientID) { try { GoogleSignInOptions.Builder optionsBuilder; @@ -227,25 +236,15 @@ public void init( throw new IllegalStateException("Unknown signInOption"); } - // Only requests a clientId if google-services.json was present and parsed - // by the google-services Gradle script. - // TODO(jackson): Perhaps we should provide a mechanism to override this - // behavior. - int clientIdIdentifier = - registrar - .context() - .getResources() - .getIdentifier( - "default_web_client_id", "string", registrar.context().getPackageName()); - if (clientIdIdentifier != 0) { - optionsBuilder.requestIdToken(registrar.context().getString(clientIdIdentifier)); - } for (String scope : requestedScopes) { optionsBuilder.requestScopes(new Scope(scope)); } if (!Strings.isNullOrEmpty(hostedDomain)) { optionsBuilder.setHostedDomain(hostedDomain); } + if (!Strings.isNullOrEmpty(serverClientID)) { + optionsBuilder.requestIdToken(serverClientID); + } this.requestedScopes = requestedScopes; signInClient = GoogleSignIn.getClient(registrar.context(), optionsBuilder.build()); diff --git a/packages/google_sign_in/example/lib/main.dart b/packages/google_sign_in/example/lib/main.dart index 42a81b80841c..08b1e0d073d9 100755 --- a/packages/google_sign_in/example/lib/main.dart +++ b/packages/google_sign_in/example/lib/main.dart @@ -54,7 +54,7 @@ class SignInDemoState extends State { }); final http.Response response = await http.get( 'https://people.googleapis.com/v1/people/me/connections' - '?requestMask.includeField=person.names', + '?requestMask.includeField=person.names', headers: await _currentUser.authHeaders, ); if (response.statusCode != 200) { diff --git a/packages/google_sign_in/ios/Classes/GoogleSignInPlugin.m b/packages/google_sign_in/ios/Classes/GoogleSignInPlugin.m index 483bc5c6e81c..6ff98ca6f744 100644 --- a/packages/google_sign_in/ios/Classes/GoogleSignInPlugin.m +++ b/packages/google_sign_in/ios/Classes/GoogleSignInPlugin.m @@ -75,6 +75,10 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result [GIDSignIn sharedInstance].clientID = plist[kClientIdKey]; [GIDSignIn sharedInstance].scopes = call.arguments[@"scopes"]; [GIDSignIn sharedInstance].hostedDomain = call.arguments[@"hostedDomain"]; + NSString *serverClientID = call.arguments[@"serverClientID"]; + if (serverClientID != (id)[NSNull null] && serverClientID.length > 0) { + [GIDSignIn sharedInstance].serverClientID = serverClientID; + } result(nil); } else { result([FlutterError errorWithCode:@"missing-config" diff --git a/packages/google_sign_in/lib/google_sign_in.dart b/packages/google_sign_in/lib/google_sign_in.dart index 234eb5102da2..a644be9f8b06 100755 --- a/packages/google_sign_in/lib/google_sign_in.dart +++ b/packages/google_sign_in/lib/google_sign_in.dart @@ -163,14 +163,17 @@ class GoogleSignIn { /// The [hostedDomain] argument specifies a hosted domain restriction. By /// setting this, sign in will be restricted to accounts of the user in the /// specified domain. By default, the list of accounts will not be restricted. - GoogleSignIn({this.signInOption, this.scopes, this.hostedDomain}); + GoogleSignIn( + {this.signInOption, this.scopes, this.hostedDomain, this.serverClientID}); /// Factory for creating default sign in user experience. - factory GoogleSignIn.standard({List scopes, String hostedDomain}) { + factory GoogleSignIn.standard( + {List scopes, String hostedDomain, String serverClientID}) { return GoogleSignIn( signInOption: SignInOption.standard, scopes: scopes, - hostedDomain: hostedDomain); + hostedDomain: hostedDomain, + serverClientID: serverClientID); } /// Factory for creating sign in suitable for games. This option must not be @@ -207,6 +210,9 @@ class GoogleSignIn { /// Domain to restrict sign-in to. final String hostedDomain; + /// The client ID of the home web server. + final String serverClientID; + StreamController _currentUserController = StreamController.broadcast(); @@ -246,6 +252,7 @@ class GoogleSignIn { 'signInOption': (signInOption ?? SignInOption.standard).toString(), 'scopes': scopes ?? [], 'hostedDomain': hostedDomain, + 'serverClientID': serverClientID, }) ..catchError((dynamic _) { // Invalidate initialization if it errored out. diff --git a/packages/google_sign_in/test/google_sign_in_test.dart b/packages/google_sign_in/test/google_sign_in_test.dart index b0ba5e82d3e2..e0c754e33462 100755 --- a/packages/google_sign_in/test/google_sign_in_test.dart +++ b/packages/google_sign_in/test/google_sign_in_test.dart @@ -59,6 +59,7 @@ void main() { 'signInOption': 'SignInOption.standard', 'scopes': [], 'hostedDomain': null, + 'serverClientID': null, }), isMethodCall('signInSilently', arguments: null), ], @@ -75,6 +76,7 @@ void main() { 'signInOption': 'SignInOption.standard', 'scopes': [], 'hostedDomain': null, + 'serverClientID': null, }), isMethodCall('signIn', arguments: null), ], @@ -89,6 +91,7 @@ void main() { 'signInOption': 'SignInOption.standard', 'scopes': [], 'hostedDomain': null, + 'serverClientID': null, }), isMethodCall('signOut', arguments: null), ]); @@ -104,6 +107,7 @@ void main() { 'signInOption': 'SignInOption.standard', 'scopes': [], 'hostedDomain': null, + 'serverClientID': null, }), isMethodCall('disconnect', arguments: null), ], @@ -121,6 +125,7 @@ void main() { 'signInOption': 'SignInOption.standard', 'scopes': [], 'hostedDomain': null, + 'serverClientID': null, }), isMethodCall('disconnect', arguments: null), ], @@ -135,6 +140,7 @@ void main() { 'signInOption': 'SignInOption.standard', 'scopes': [], 'hostedDomain': null, + 'serverClientID': null, }), isMethodCall('isSignedIn', arguments: null), ]); @@ -161,6 +167,7 @@ void main() { 'signInOption': 'SignInOption.standard', 'scopes': [], 'hostedDomain': null, + 'serverClientID': null, }), isMethodCall('signInSilently', arguments: null), ], @@ -178,6 +185,7 @@ void main() { 'signInOption': 'SignInOption.standard', 'scopes': [], 'hostedDomain': null, + 'serverClientID': null, }), isMethodCall('signInSilently', arguments: null), isMethodCall('signIn', arguments: null), @@ -200,6 +208,7 @@ void main() { 'signInOption': 'SignInOption.standard', 'scopes': [], 'hostedDomain': null, + 'serverClientID': null, }), isMethodCall('signInSilently', arguments: null), ], @@ -231,6 +240,7 @@ void main() { 'signInOption': 'SignInOption.standard', 'scopes': [], 'hostedDomain': null, + 'serverClientID': null, }), isMethodCall('signOut', arguments: null), isMethodCall('signOut', arguments: null), @@ -256,6 +266,7 @@ void main() { 'signInOption': 'SignInOption.standard', 'scopes': [], 'hostedDomain': null, + 'serverClientID': null, }), isMethodCall('signInSilently', arguments: null), isMethodCall('signOut', arguments: null), @@ -307,6 +318,7 @@ void main() { 'signInOption': 'SignInOption.standard', 'scopes': [], 'hostedDomain': null, + 'serverClientID': null, }), isMethodCall('signInSilently', arguments: null), ], @@ -326,6 +338,7 @@ void main() { 'signInOption': 'SignInOption.games', 'scopes': [], 'hostedDomain': null, + 'serverClientID': null, }), isMethodCall('signInSilently', arguments: null), ], From 2cd694db8b08b4f38200a3e58aec0f4a314144dd Mon Sep 17 00:00:00 2001 From: Jarrod Sinclair Date: Thu, 11 Apr 2019 10:22:30 +1000 Subject: [PATCH 2/2] Ignoring flutter_plugin_tools formatter, to make Cirrus CI test+format happy --- packages/google_sign_in/example/lib/main.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/google_sign_in/example/lib/main.dart b/packages/google_sign_in/example/lib/main.dart index 08b1e0d073d9..42a81b80841c 100755 --- a/packages/google_sign_in/example/lib/main.dart +++ b/packages/google_sign_in/example/lib/main.dart @@ -54,7 +54,7 @@ class SignInDemoState extends State { }); final http.Response response = await http.get( 'https://people.googleapis.com/v1/people/me/connections' - '?requestMask.includeField=person.names', + '?requestMask.includeField=person.names', headers: await _currentUser.authHeaders, ); if (response.statusCode != 200) {