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

Commit b73d66e

Browse files
committed
[force-code-for-refresh-token-new] Add forceCodeForRefreshToken to allow android to receive a refresh token on every sign in
1 parent 94e80fc commit b73d66e

File tree

10 files changed

+46
-10
lines changed

10 files changed

+46
-10
lines changed

packages/google_sign_in/google_sign_in/android/src/main/java/io/flutter/plugins/googlesignin/GoogleSignInPlugin.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,8 @@ public void onMethodCall(MethodCall call, Result result) {
137137
List<String> requestedScopes = call.argument("scopes");
138138
String hostedDomain = call.argument("hostedDomain");
139139
String clientId = call.argument("clientId");
140-
delegate.init(result, signInOption, requestedScopes, hostedDomain, clientId);
140+
boolean forceCodeForRefreshToken = call.argument("forceCodeForRefreshToken");
141+
delegate.init(result, signInOption, requestedScopes, hostedDomain, clientId, forceCodeForRefreshToken);
141142
break;
142143

143144
case METHOD_SIGN_IN_SILENTLY:
@@ -193,7 +194,8 @@ public void init(
193194
String signInOption,
194195
List<String> requestedScopes,
195196
String hostedDomain,
196-
String clientId);
197+
String clientId,
198+
boolean forceCodeForRefreshToken);
197199

198200
/**
199201
* Returns the account information for the user who is signed in to this app. If no user is
@@ -318,7 +320,8 @@ public void init(
318320
String signInOption,
319321
List<String> requestedScopes,
320322
String hostedDomain,
321-
String clientId) {
323+
String clientId,
324+
boolean forceCodeForRefreshToken) {
322325
try {
323326
GoogleSignInOptions.Builder optionsBuilder;
324327

@@ -345,10 +348,10 @@ public void init(
345348
.getIdentifier("default_web_client_id", "string", context.getPackageName());
346349
if (!Strings.isNullOrEmpty(clientId)) {
347350
optionsBuilder.requestIdToken(clientId);
348-
optionsBuilder.requestServerAuthCode(clientId);
351+
optionsBuilder.requestServerAuthCode(clientId, forceCodeForRefreshToken);
349352
} else if (clientIdIdentifier != 0) {
350353
optionsBuilder.requestIdToken(context.getString(clientIdIdentifier));
351-
optionsBuilder.requestServerAuthCode(context.getString(clientIdIdentifier));
354+
optionsBuilder.requestServerAuthCode(context.getString(clientIdIdentifier), forceCodeForRefreshToken);
352355
}
353356
for (String scope : requestedScopes) {
354357
optionsBuilder.requestScopes(new Scope(scope));

packages/google_sign_in/google_sign_in/lib/google_sign_in.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ class GoogleSignIn {
180180
this.scopes = const <String>[],
181181
this.hostedDomain,
182182
this.clientId,
183+
this.forceCodeForRefreshToken = false,
183184
});
184185

185186
/// Factory for creating default sign in user experience.
@@ -228,6 +229,9 @@ class GoogleSignIn {
228229
/// Client ID being used to connect to google sign-in. Only supported on web.
229230
final String? clientId;
230231

232+
/// Force the authorization code to be valid for a refresh token every time. Only needed on Android.
233+
final bool forceCodeForRefreshToken;
234+
231235
StreamController<GoogleSignInAccount?> _currentUserController =
232236
StreamController<GoogleSignInAccount?>.broadcast();
233237

@@ -262,6 +266,7 @@ class GoogleSignIn {
262266
scopes: scopes,
263267
hostedDomain: hostedDomain,
264268
clientId: clientId,
269+
forceCodeForRefreshToken: forceCodeForRefreshToken,
265270
)..catchError((dynamic _) {
266271
// Invalidate initialization if it errors out.
267272
_initialization = null;

packages/google_sign_in/google_sign_in/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ flutter:
2323
dependencies:
2424
flutter:
2525
sdk: flutter
26-
google_sign_in_platform_interface: ^2.1.0
26+
google_sign_in_platform_interface: ^2.2.0
2727
google_sign_in_web: ^0.10.0
2828

2929
dev_dependencies:

packages/google_sign_in/google_sign_in/test/google_sign_in_test.dart

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,27 @@ void main() {
9696
'scopes': <String>[],
9797
'hostedDomain': null,
9898
'clientId': fakeClientId,
99+
'forceCodeForRefreshToken': false,
100+
}),
101+
isMethodCall('signIn', arguments: null),
102+
],
103+
);
104+
});
105+
106+
test('forceCodeForRefreshToken sent with init method call', () async {
107+
final fakeClientId = 'fakeClientId';
108+
googleSignIn = GoogleSignIn(clientId: fakeClientId, forceCodeForRefreshToken: true);
109+
await googleSignIn.signIn();
110+
expect(googleSignIn.currentUser, isNotNull);
111+
expect(
112+
log,
113+
<Matcher>[
114+
isMethodCall('init', arguments: <String, dynamic>{
115+
'signInOption': 'SignInOption.standard',
116+
'scopes': <String>[],
117+
'hostedDomain': null,
118+
'clientId': fakeClientId,
119+
'forceCodeForRefreshToken': true,
99120
}),
100121
isMethodCall('signIn', arguments: null),
101122
],
@@ -430,5 +451,6 @@ Matcher _isSignInMethodCall({String signInOption = 'SignInOption.standard'}) {
430451
'scopes': <String>[],
431452
'hostedDomain': null,
432453
'clientId': null,
454+
'forceCodeForRefreshToken': false
433455
});
434456
}

packages/google_sign_in/google_sign_in_platform_interface/lib/google_sign_in_platform_interface.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ abstract class GoogleSignInPlatform {
8585
SignInOption signInOption = SignInOption.standard,
8686
String? hostedDomain,
8787
String? clientId,
88+
bool forceCodeForRefreshToken = false,
8889
}) async {
8990
throw UnimplementedError('init() has not been implemented.');
9091
}

packages/google_sign_in/google_sign_in_platform_interface/lib/src/method_channel_google_sign_in.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,14 @@ class MethodChannelGoogleSignIn extends GoogleSignInPlatform {
2525
SignInOption signInOption = SignInOption.standard,
2626
String? hostedDomain,
2727
String? clientId,
28+
bool forceCodeForRefreshToken = false,
2829
}) {
2930
return channel.invokeMethod<void>('init', <String, dynamic>{
3031
'signInOption': signInOption.toString(),
3132
'scopes': scopes,
3233
'hostedDomain': hostedDomain,
3334
'clientId': clientId,
35+
'forceCodeForRefreshToken': forceCodeForRefreshToken,
3436
});
3537
}
3638

packages/google_sign_in/google_sign_in_platform_interface/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ repository: https://github.com/flutter/plugins/tree/master/packages/google_sign_
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+google_sign_in%22
55
# NOTE: We strongly prefer non-breaking changes, even at the expense of a
66
# less-clean API. See https://flutter.dev/go/platform-interface-breaking-changes
7-
version: 2.1.1
7+
version: 2.2.0
88

99
environment:
1010
sdk: ">=2.12.0 <3.0.0"

packages/google_sign_in/google_sign_in_platform_interface/test/method_channel_google_sign_in_test.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,14 @@ void main() {
102102
hostedDomain: 'example.com',
103103
scopes: <String>['two', 'scopes'],
104104
signInOption: SignInOption.games,
105-
clientId: 'fakeClientId');
105+
clientId: 'fakeClientId',
106+
forceCodeForRefreshToken: true);
106107
}: isMethodCall('init', arguments: <String, dynamic>{
107108
'hostedDomain': 'example.com',
108109
'scopes': <String>['two', 'scopes'],
109110
'signInOption': 'SignInOption.games',
110111
'clientId': 'fakeClientId',
112+
'forceCodeForRefreshToken': true,
111113
}),
112114
() {
113115
googleSignIn.getTokens(

packages/google_sign_in/google_sign_in_web/lib/google_sign_in_web.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ class GoogleSignInPlugin extends GoogleSignInPlatform {
7171
SignInOption signInOption = SignInOption.standard,
7272
String? hostedDomain,
7373
String? clientId,
74+
bool forceCodeForRefreshToken = false,
7475
}) async {
7576
final String? appClientId = clientId ?? _autoDetectedClientId;
7677
assert(

packages/google_sign_in/google_sign_in_web/pubspec.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ description: Flutter plugin for Google Sign-In, a secure authentication system
33
for signing in with a Google account on Android, iOS and Web.
44
repository: https://github.com/flutter/plugins/tree/master/packages/google_sign_in/google_sign_in_web
55
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+google_sign_in%22
6-
version: 0.10.0+4
6+
version: 0.10.1+4
77

88
environment:
99
sdk: ">=2.12.0 <3.0.0"
@@ -22,7 +22,7 @@ dependencies:
2222
sdk: flutter
2323
flutter_web_plugins:
2424
sdk: flutter
25-
google_sign_in_platform_interface: ^2.0.0
25+
google_sign_in_platform_interface: ^2.2.0
2626
js: ^0.6.3
2727
pedantic: ^1.10.0
2828

0 commit comments

Comments
 (0)