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

[google_sign_in] Fix issue obtaining serverAuthCode on Android and add forceCodeForRefreshToken parameter // Update #3829

Closed
wants to merge 6 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
2 changes: 2 additions & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,5 @@ Anton Borries <mail@antonborri.es>
Alex Li <google@alexv525.com>
Rahul Raj <64.rahulraj@gmail.com>
Daniel Roek <daniel.roek@gmail.com>
Twin Sun, LLC <google-contrib@twinsunsolutions.com>
Andrew Cardinot <andrewcardinot@gmail.com>
4 changes: 4 additions & 0 deletions packages/google_sign_in/google_sign_in/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 5.0.3

* Add `force_code_for_refresh_token` option for Android, returns the right serverAuthCode

## 5.0.2

* Fix flutter/flutter#48602 iOS flow shows account selection, if user is signed in to Google on the device.
Expand Down
7 changes: 6 additions & 1 deletion packages/google_sign_in/google_sign_in/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

A Flutter plugin for [Google Sign In](https://developers.google.com/identity/).

*Note*: This plugin is still under development, and some APIs might not be available yet. [Feedback](https://github.com/flutter/flutter/issues) and [Pull Requests](https://github.com/flutter/plugins/pulls) are most welcome!
_Note_: This plugin is still under development, and some APIs might not be available yet. [Feedback](https://github.com/flutter/flutter/issues) and [Pull Requests](https://github.com/flutter/plugins/pulls) are most welcome!

## Android integration

Expand All @@ -20,6 +20,8 @@ enable the [Google People API](https://developers.google.com/people/).

Make sure you've filled out all required fields in the console for [OAuth consent screen](https://console.developers.google.com/apis/credentials/consent). Otherwise, you may encounter `APIException` errors.

If you need to force a server auth code to include a refresh token when exchanged for an access token, set `force_code_for_refresh_token` to true as in `google_sign_in/example/android/app/src/main/res/values/bools.xml`.

## iOS integration

1. [First register your application](https://developers.google.com/mobile/add?platform=ios).
Expand Down Expand Up @@ -63,9 +65,11 @@ plugin could be an option.
## Usage

### Import the package

To use this plugin, follow the [plugin installation instructions](https://pub.dev/packages/google_sign_in#pub-pkg-tab-installing).

### Use the plugin

Add the following import to your Dart code:

```dart
Expand All @@ -82,6 +86,7 @@ GoogleSignIn _googleSignIn = GoogleSignIn(
],
);
```

[Full list of available scopes](https://developers.google.com/identity/protocols/googlescopes).

You can now use the `GoogleSignIn` class to authenticate in your Dart code, e.g.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,18 @@ public void init(
optionsBuilder.requestServerAuthCode(clientId);
} else if (clientIdIdentifier != 0) {
optionsBuilder.requestIdToken(context.getString(clientIdIdentifier));
optionsBuilder.requestServerAuthCode(context.getString(clientIdIdentifier));

boolean forceCodeForRefreshToken = false;
int forceCodeForRefreshTokenIdentifier =
context
.getResources()
.getIdentifier("force_code_for_refresh_token", "bool", context.getPackageName());
if (forceCodeForRefreshTokenIdentifier != 0) {
forceCodeForRefreshToken =
context.getResources().getBoolean(forceCodeForRefreshTokenIdentifier);
}
optionsBuilder.requestServerAuthCode(
context.getString(clientIdIdentifier), forceCodeForRefreshToken);
}
for (String scope : requestedScopes) {
optionsBuilder.requestScopes(new Scope(scope));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<bool name="force_code_for_refresh_token">false</bool>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,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 @@ -70,6 +71,7 @@ class GoogleSignInAccount implements GoogleIdentity {

final String? _idToken;
final GoogleSignIn _googleSignIn;
final String? _serverAuthCode;

/// Retrieve [GoogleSignInAuthentication] for this account.
///
Expand Down Expand Up @@ -97,6 +99,9 @@ class GoogleSignInAccount implements GoogleIdentity {
if (response.idToken == null) {
response.idToken = _idToken;
}
if (response.serverAuthCode == null) {
response.serverAuthCode = _serverAuthCode;
}
return GoogleSignInAuthentication._(response);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/google_sign_in/google_sign_in/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: google_sign_in
description: Flutter plugin for Google Sign-In, a secure authentication system
for signing in with a Google account on Android and iOS.
homepage: https://github.com/flutter/plugins/tree/master/packages/google_sign_in/google_sign_in
version: 5.0.2
version: 5.0.3

flutter:
plugin:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,12 @@ class GoogleSignInUserData {
this.displayName,
this.photoUrl,
this.idToken,
this.serverAuthCode
});

/// Used to perform server-side actions with Google API
String? serverAuthCode;

/// The display name of the signed in user.
///
/// Not guaranteed to be present for all users, even when configured.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ GoogleSignInUserData? getUserDataFromMap(Map<String, dynamic>? data) {
id: data['id']!,
displayName: data['displayName'],
photoUrl: data['photoUrl'],
idToken: data['idToken']);
idToken: data['idToken'],
serverAuthCode: data['serverAuthCode']);
}

/// Converts token data coming from native code into the proper platform interface type.
Expand Down