Thanks to visit codestin.com
Credit goes to github.com

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

[google_sign_in] Adding serverClientID optional argument, support for both iOS and Android #1475

Closed
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ public void onMethodCall(MethodCall call, Result result) {
String signInOption = call.argument("signInOption");
List<String> 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:
Expand Down Expand Up @@ -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<String> requestedScopes, String hostedDomain);
Result result,
String signInOption,
List<String> requestedScopes,
String hostedDomain,
String serverClientID);

/**
* Returns the account information for the user who is signed in to this app. If no user is
Expand Down Expand Up @@ -210,7 +215,11 @@ private void checkAndSetPendingOperation(String method, Result result, Object da
*/
@Override
public void init(
Result result, String signInOption, List<String> requestedScopes, String hostedDomain) {
Result result,
String signInOption,
List<String> requestedScopes,
String hostedDomain,
String serverClientID) {
try {
GoogleSignInOptions.Builder optionsBuilder;

Expand All @@ -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());
Expand Down
4 changes: 4 additions & 0 deletions packages/google_sign_in/ios/Classes/GoogleSignInPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
13 changes: 10 additions & 3 deletions packages/google_sign_in/lib/google_sign_in.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> scopes, String hostedDomain}) {
factory GoogleSignIn.standard(
{List<String> 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
Expand Down Expand Up @@ -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<GoogleSignInAccount> _currentUserController =
StreamController<GoogleSignInAccount>.broadcast();

Expand Down Expand Up @@ -246,6 +252,7 @@ class GoogleSignIn {
'signInOption': (signInOption ?? SignInOption.standard).toString(),
'scopes': scopes ?? <String>[],
'hostedDomain': hostedDomain,
'serverClientID': serverClientID,
})
..catchError((dynamic _) {
// Invalidate initialization if it errored out.
Expand Down
13 changes: 13 additions & 0 deletions packages/google_sign_in/test/google_sign_in_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ void main() {
'signInOption': 'SignInOption.standard',
'scopes': <String>[],
'hostedDomain': null,
'serverClientID': null,
}),
isMethodCall('signInSilently', arguments: null),
],
Expand All @@ -75,6 +76,7 @@ void main() {
'signInOption': 'SignInOption.standard',
'scopes': <String>[],
'hostedDomain': null,
'serverClientID': null,
}),
isMethodCall('signIn', arguments: null),
],
Expand All @@ -89,6 +91,7 @@ void main() {
'signInOption': 'SignInOption.standard',
'scopes': <String>[],
'hostedDomain': null,
'serverClientID': null,
}),
isMethodCall('signOut', arguments: null),
]);
Expand All @@ -104,6 +107,7 @@ void main() {
'signInOption': 'SignInOption.standard',
'scopes': <String>[],
'hostedDomain': null,
'serverClientID': null,
}),
isMethodCall('disconnect', arguments: null),
],
Expand All @@ -121,6 +125,7 @@ void main() {
'signInOption': 'SignInOption.standard',
'scopes': <String>[],
'hostedDomain': null,
'serverClientID': null,
}),
isMethodCall('disconnect', arguments: null),
],
Expand All @@ -135,6 +140,7 @@ void main() {
'signInOption': 'SignInOption.standard',
'scopes': <String>[],
'hostedDomain': null,
'serverClientID': null,
}),
isMethodCall('isSignedIn', arguments: null),
]);
Expand All @@ -161,6 +167,7 @@ void main() {
'signInOption': 'SignInOption.standard',
'scopes': <String>[],
'hostedDomain': null,
'serverClientID': null,
}),
isMethodCall('signInSilently', arguments: null),
],
Expand All @@ -178,6 +185,7 @@ void main() {
'signInOption': 'SignInOption.standard',
'scopes': <String>[],
'hostedDomain': null,
'serverClientID': null,
}),
isMethodCall('signInSilently', arguments: null),
isMethodCall('signIn', arguments: null),
Expand All @@ -200,6 +208,7 @@ void main() {
'signInOption': 'SignInOption.standard',
'scopes': <String>[],
'hostedDomain': null,
'serverClientID': null,
}),
isMethodCall('signInSilently', arguments: null),
],
Expand Down Expand Up @@ -231,6 +240,7 @@ void main() {
'signInOption': 'SignInOption.standard',
'scopes': <String>[],
'hostedDomain': null,
'serverClientID': null,
}),
isMethodCall('signOut', arguments: null),
isMethodCall('signOut', arguments: null),
Expand All @@ -256,6 +266,7 @@ void main() {
'signInOption': 'SignInOption.standard',
'scopes': <String>[],
'hostedDomain': null,
'serverClientID': null,
}),
isMethodCall('signInSilently', arguments: null),
isMethodCall('signOut', arguments: null),
Expand Down Expand Up @@ -307,6 +318,7 @@ void main() {
'signInOption': 'SignInOption.standard',
'scopes': <String>[],
'hostedDomain': null,
'serverClientID': null,
}),
isMethodCall('signInSilently', arguments: null),
],
Expand All @@ -326,6 +338,7 @@ void main() {
'signInOption': 'SignInOption.games',
'scopes': <String>[],
'hostedDomain': null,
'serverClientID': null,
}),
isMethodCall('signInSilently', arguments: null),
],
Expand Down