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] Add ability to request serverAuthCode on Android #1704

Closed
wants to merge 8 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
4 changes: 4 additions & 0 deletions packages/google_sign_in/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 4.0.3

* Support to requesting serverAuthCode on Android

## 4.0.2

* Add missing template type parameter to `invokeMethod` calls.
Expand Down
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);
boolean requestAuthCode = call.argument("requestAuthCode");
delegate.init(result, signInOption, requestedScopes, hostedDomain, requestAuthCode);
break;

case METHOD_SIGN_IN_SILENTLY:
Expand Down Expand Up @@ -113,7 +114,7 @@ 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, boolean requestAuthCode);

/**
* Returns the account information for the user who is signed in to this app. If no user is
Expand Down Expand Up @@ -210,7 +211,7 @@ 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, boolean requestAuthCode) {
try {
GoogleSignInOptions.Builder optionsBuilder;

Expand All @@ -221,7 +222,8 @@ public void init(
break;
case DEFAULT_SIGN_IN:
optionsBuilder =
new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN).requestEmail();
new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail();
break;
default:
throw new IllegalStateException("Unknown signInOption");
Expand All @@ -238,8 +240,13 @@ public void init(
.getIdentifier(
"default_web_client_id", "string", registrar.context().getPackageName());
if (clientIdIdentifier != 0) {
optionsBuilder.requestIdToken(registrar.context().getString(clientIdIdentifier));
final String clientIdFromContext = registrar.context().getString(clientIdIdentifier);
optionsBuilder.requestIdToken(clientIdFromContext);
if (requestAuthCode) {
optionsBuilder.requestServerAuthCode(clientIdFromContext);
}
}

for (String scope : requestedScopes) {
optionsBuilder.requestScopes(new Scope(scope));
}
Expand Down Expand Up @@ -361,6 +368,7 @@ private void onSignInAccount(GoogleSignInAccount account) {
response.put("id", account.getId());
response.put("idToken", account.getIdToken());
response.put("displayName", account.getDisplayName());
response.put("authCode", account.getServerAuthCode());
if (account.getPhotoUrl() != null) {
response.put("photoUrl", account.getPhotoUrl().toString());
}
Expand Down
24 changes: 17 additions & 7 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'],
authCode = data['authCode'] {
assert(id != null);
}

Expand All @@ -60,6 +61,9 @@ class GoogleSignInAccount implements GoogleIdentity {
@override
final String photoUrl;

//TODO Always returns empty on IOS
final String authCode;

final String _idToken;
final GoogleSignIn _googleSignIn;

Expand Down Expand Up @@ -157,14 +161,16 @@ 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.requestAuthCode = false});

/// Factory for creating default sign in user experience.
factory GoogleSignIn.standard({List<String> scopes, String hostedDomain}) {
factory GoogleSignIn.standard({List<String> scopes, String hostedDomain, bool requestAuthCode}) {
return GoogleSignIn(
signInOption: SignInOption.standard,
scopes: scopes,
hostedDomain: hostedDomain);
hostedDomain: hostedDomain,
requestAuthCode: requestAuthCode,
);
}

/// Factory for creating sign in suitable for games. This option must not be
Expand Down Expand Up @@ -201,6 +207,9 @@ class GoogleSignIn {
/// Domain to restrict sign-in to.
final String hostedDomain;

/// Set if server-side login is required
final bool requestAuthCode;

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

Expand All @@ -213,9 +222,9 @@ class GoogleSignIn {

Future<GoogleSignInAccount> _callMethod(String method) async {
await _ensureInitialized();

final Map<String, dynamic> response =
await channel.invokeMapMethod<String, dynamic>(method);
final Map<String, dynamic> response =
await channel.invokeMapMethod<String, dynamic>(method);
return _setCurrentUser(response != null && response.isNotEmpty
? GoogleSignInAccount._(this, response)
: null);
Expand All @@ -235,6 +244,7 @@ class GoogleSignIn {
'signInOption': (signInOption ?? SignInOption.standard).toString(),
'scopes': scopes ?? <String>[],
'hostedDomain': hostedDomain,
'requestAuthCode' : requestAuthCode,
})
..catchError((dynamic _) {
// Invalidate initialization if it errored out.
Expand Down
2 changes: 1 addition & 1 deletion packages/google_sign_in/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description: Flutter plugin for Google Sign-In, a secure authentication system
for signing in with a Google account on Android and iOS.
author: Flutter Team <[email protected]>
homepage: https://github.com/flutter/plugins/tree/master/packages/google_sign_in
version: 4.0.2
version: 4.0.3

flutter:
plugin:
Expand Down