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

[google_sign_in] Add serverClientId option to return serverAuthCode #879

Closed
wants to merge 2 commits into from
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
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ Sarthak Verma <sarthak@artiosys.com>
Mike Diarmid <mike@invertase.io>
Invertase <oss@invertase.io>
Elliot Hesp <elliot@invertase.io>
Twin Sun, LLC <google-contrib@twinsunsolutions.com>
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 Down Expand Up @@ -246,6 +255,10 @@ public void init(
if (!Strings.isNullOrEmpty(hostedDomain)) {
optionsBuilder.setHostedDomain(hostedDomain);
}
if (!Strings.isNullOrEmpty(serverClientId)) {
optionsBuilder.requestServerAuthCode(serverClientId, true);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we need to expose this API, let's also expose the boolean parameter forceCodeForRefreshToken to the public API and make it false by default.

optionsBuilder.requestIdToken(serverClientId);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this required? Why do we not have the same on iOS? What happens if this block also calls requestIdToken?

        if (clientIdIdentifier != 0) {
          optionsBuilder.requestIdToken(registrar.context().getString(clientIdIdentifier));
        }

}

this.requestedScopes = requestedScopes;
signInClient = GoogleSignIn.getClient(registrar.context(), optionsBuilder.build());
Expand Down Expand Up @@ -361,6 +374,7 @@ private void onSignInAccount(GoogleSignInAccount account) {
response.put("id", account.getId());
response.put("idToken", account.getIdToken());
response.put("displayName", account.getDisplayName());
response.put("serverAuthCode", account.getServerAuthCode());
if (account.getPhotoUrl() != null) {
response.put("photoUrl", account.getPhotoUrl().toString());
}
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,9 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result
[GIDSignIn sharedInstance].clientID = plist[kClientIdKey];
[GIDSignIn sharedInstance].scopes = call.arguments[@"scopes"];
[GIDSignIn sharedInstance].hostedDomain = call.arguments[@"hostedDomain"];
if (![call.arguments[@"serverClientId"] isEqual:[NSNull null]]) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:

Suggested change
if (![call.arguments[@"serverClientId"] isEqual:[NSNull null]]) {
if ([call.arguments[@"serverClientId"] isKindOfClass:[NSString class]]) {

[GIDSignIn sharedInstance].serverClientID = call.arguments[@"serverClientId"];
}
result(nil);
} else {
result([FlutterError errorWithCode:@"missing-config"
Expand Down Expand Up @@ -173,6 +176,7 @@ - (void)signIn:(GIDSignIn *)signIn
@"email" : user.profile.email ?: [NSNull null],
@"id" : user.userID ?: [NSNull null],
@"photoUrl" : [photoUrl absoluteString] ?: [NSNull null],
@"serverAuthCode" : user.serverAuthCode ?: [NSNull null]
}
error:nil];
}
Expand Down
16 changes: 12 additions & 4 deletions packages/google_sign_in/lib/google_sign_in.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ class GoogleSignInAccount implements GoogleIdentity {
email = data['email'],
id = data['id'],
photoUrl = data['photoUrl'],
_idToken = data['idToken'] {
_idToken = data['idToken'],
serverAuthCode = data['serverAuthCode'] {
assert(id != null);
}

Expand All @@ -62,6 +63,7 @@ class GoogleSignInAccount implements GoogleIdentity {

final String _idToken;
final GoogleSignIn _googleSignIn;
final String serverAuthCode;

/// Retrieve [GoogleSignInAuthentication] for this account.
///
Expand Down Expand Up @@ -163,14 +165,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(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs dart doc for serverClientId as well.

{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 +212,8 @@ class GoogleSignIn {
/// Domain to restrict sign-in to.
final String hostedDomain;

final String serverClientId;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs dart doc


StreamController<GoogleSignInAccount> _currentUserController =
StreamController<GoogleSignInAccount>.broadcast();

Expand Down Expand Up @@ -246,6 +253,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