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

Commit 7e55a7c

Browse files
authored
[google_sign_in] Add forceCodeForRefreshToken parameter platform implementations (#6130)
1 parent c08e447 commit 7e55a7c

File tree

12 files changed

+150
-16
lines changed

12 files changed

+150
-16
lines changed

packages/google_sign_in/google_sign_in_android/AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,4 @@ Aleksandr Yurkovskiy <sanekyy@gmail.com>
6464
Anton Borries <mail@antonborri.es>
6565
Alex Li <google@alexv525.com>
6666
Rahul Raj <64.rahulraj@gmail.com>
67+
Twin Sun, LLC <google-contrib@twinsunsolutions.com>

packages/google_sign_in/google_sign_in_android/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 6.1.0
2+
3+
* Adds override for `GoogleSignIn.initWithParams` to handle new `forceCodeForRefreshToken` parameter.
4+
15
## 6.0.1
26

37
* Updates gradle version to 7.2.1 on Android.

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,15 @@ public void onMethodCall(MethodCall call, Result result) {
140140
String hostedDomain = call.argument("hostedDomain");
141141
String clientId = call.argument("clientId");
142142
String serverClientId = call.argument("serverClientId");
143+
boolean forceCodeForRefreshToken = call.argument("forceCodeForRefreshToken");
143144
delegate.init(
144-
result, signInOption, requestedScopes, hostedDomain, clientId, serverClientId);
145+
result,
146+
signInOption,
147+
requestedScopes,
148+
hostedDomain,
149+
clientId,
150+
serverClientId,
151+
forceCodeForRefreshToken);
145152
break;
146153

147154
case METHOD_SIGN_IN_SILENTLY:
@@ -198,7 +205,8 @@ public void init(
198205
List<String> requestedScopes,
199206
String hostedDomain,
200207
String clientId,
201-
String serverClientId);
208+
String serverClientId,
209+
boolean forceCodeForRefreshToken);
202210

203211
/**
204212
* Returns the account information for the user who is signed in to this app. If no user is
@@ -326,7 +334,8 @@ public void init(
326334
List<String> requestedScopes,
327335
String hostedDomain,
328336
String clientId,
329-
String serverClientId) {
337+
String serverClientId,
338+
boolean forceCodeForRefreshToken) {
330339
try {
331340
GoogleSignInOptions.Builder optionsBuilder;
332341

@@ -374,7 +383,7 @@ public void init(
374383
}
375384
if (!Strings.isNullOrEmpty(serverClientId)) {
376385
optionsBuilder.requestIdToken(serverClientId);
377-
optionsBuilder.requestServerAuthCode(serverClientId);
386+
optionsBuilder.requestServerAuthCode(serverClientId, forceCodeForRefreshToken);
378387
}
379388
for (String scope : requestedScopes) {
380389
optionsBuilder.requestScopes(new Scope(scope));

packages/google_sign_in/google_sign_in_android/android/src/test/java/io/flutter/plugins/googlesignin/GoogleSignInTest.java

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,48 @@ public void init_IgnoresClientIdIfServerClientIdIsProvided() {
239239
initAndAssertServerClientId(methodCall, serverClientId);
240240
}
241241

242+
@Test
243+
public void init_PassesForceCodeForRefreshTokenFalseWithServerClientIdParameter() {
244+
MethodCall methodCall = buildInitMethodCall("fakeClientId", "fakeServerClientId", false);
245+
246+
initAndAssertForceCodeForRefreshToken(methodCall, false);
247+
}
248+
249+
@Test
250+
public void init_PassesForceCodeForRefreshTokenTrueWithServerClientIdParameter() {
251+
MethodCall methodCall = buildInitMethodCall("fakeClientId", "fakeServerClientId", true);
252+
253+
initAndAssertForceCodeForRefreshToken(methodCall, true);
254+
}
255+
256+
@Test
257+
public void init_PassesForceCodeForRefreshTokenFalseWithServerClientIdFromResources() {
258+
final String packageName = "fakePackageName";
259+
final String serverClientId = "fakeServerClientId";
260+
final int resourceId = 1;
261+
MethodCall methodCall = buildInitMethodCall(null, null, false);
262+
when(mockContext.getPackageName()).thenReturn(packageName);
263+
when(mockResources.getIdentifier("default_web_client_id", "string", packageName))
264+
.thenReturn(resourceId);
265+
when(mockContext.getString(resourceId)).thenReturn(serverClientId);
266+
267+
initAndAssertForceCodeForRefreshToken(methodCall, false);
268+
}
269+
270+
@Test
271+
public void init_PassesForceCodeForRefreshTokenTrueWithServerClientIdFromResources() {
272+
final String packageName = "fakePackageName";
273+
final String serverClientId = "fakeServerClientId";
274+
final int resourceId = 1;
275+
MethodCall methodCall = buildInitMethodCall(null, null, true);
276+
when(mockContext.getPackageName()).thenReturn(packageName);
277+
when(mockResources.getIdentifier("default_web_client_id", "string", packageName))
278+
.thenReturn(resourceId);
279+
when(mockContext.getString(resourceId)).thenReturn(serverClientId);
280+
281+
initAndAssertForceCodeForRefreshToken(methodCall, true);
282+
}
283+
242284
public void initAndAssertServerClientId(MethodCall methodCall, String serverClientId) {
243285
ArgumentCaptor<GoogleSignInOptions> optionsCaptor =
244286
ArgumentCaptor.forClass(GoogleSignInOptions.class);
@@ -249,13 +291,39 @@ public void initAndAssertServerClientId(MethodCall methodCall, String serverClie
249291
Assert.assertEquals(serverClientId, optionsCaptor.getValue().getServerClientId());
250292
}
251293

294+
public void initAndAssertForceCodeForRefreshToken(
295+
MethodCall methodCall, boolean forceCodeForRefreshToken) {
296+
ArgumentCaptor<GoogleSignInOptions> optionsCaptor =
297+
ArgumentCaptor.forClass(GoogleSignInOptions.class);
298+
when(mockGoogleSignIn.getClient(any(Context.class), optionsCaptor.capture()))
299+
.thenReturn(mockClient);
300+
plugin.onMethodCall(methodCall, result);
301+
verify(result).success(null);
302+
Assert.assertEquals(
303+
forceCodeForRefreshToken, optionsCaptor.getValue().isForceCodeForRefreshToken());
304+
}
305+
252306
private static MethodCall buildInitMethodCall(String clientId, String serverClientId) {
253307
return buildInitMethodCall(
254-
"SignInOption.standard", Collections.<String>emptyList(), clientId, serverClientId);
308+
"SignInOption.standard", Collections.<String>emptyList(), clientId, serverClientId, false);
309+
}
310+
311+
private static MethodCall buildInitMethodCall(
312+
String clientId, String serverClientId, boolean forceCodeForRefreshToken) {
313+
return buildInitMethodCall(
314+
"SignInOption.standard",
315+
Collections.<String>emptyList(),
316+
clientId,
317+
serverClientId,
318+
forceCodeForRefreshToken);
255319
}
256320

257321
private static MethodCall buildInitMethodCall(
258-
String signInOption, List<String> scopes, String clientId, String serverClientId) {
322+
String signInOption,
323+
List<String> scopes,
324+
String clientId,
325+
String serverClientId,
326+
boolean forceCodeForRefreshToken) {
259327
HashMap<String, Object> arguments = new HashMap<>();
260328
arguments.put("signInOption", signInOption);
261329
arguments.put("scopes", scopes);
@@ -265,6 +333,7 @@ private static MethodCall buildInitMethodCall(
265333
if (serverClientId != null) {
266334
arguments.put("serverClientId", serverClientId);
267335
}
336+
arguments.put("forceCodeForRefreshToken", forceCodeForRefreshToken);
268337
return new MethodCall("init", arguments);
269338
}
270339
}

packages/google_sign_in/google_sign_in_android/lib/google_sign_in_android.dart

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,22 @@ class GoogleSignInAndroid extends GoogleSignInPlatform {
3030
String? hostedDomain,
3131
String? clientId,
3232
}) {
33+
return initWithParams(SignInInitParameters(
34+
signInOption: signInOption,
35+
scopes: scopes,
36+
hostedDomain: hostedDomain,
37+
clientId: clientId,
38+
));
39+
}
40+
41+
@override
42+
Future<void> initWithParams(SignInInitParameters params) {
3343
return channel.invokeMethod<void>('init', <String, dynamic>{
34-
'signInOption': signInOption.toString(),
35-
'scopes': scopes,
36-
'hostedDomain': hostedDomain,
37-
'clientId': clientId,
44+
'signInOption': params.signInOption.toString(),
45+
'scopes': params.scopes,
46+
'hostedDomain': params.hostedDomain,
47+
'clientId': params.clientId,
48+
'forceCodeForRefreshToken': params.forceCodeForRefreshToken,
3849
});
3950
}
4051

packages/google_sign_in/google_sign_in_android/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: google_sign_in_android
22
description: Android implementation of the google_sign_in plugin.
33
repository: https://github.com/flutter/plugins/tree/main/packages/google_sign_in/google_sign_in_android
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+google_sign_in%22
5-
version: 6.0.1
5+
version: 6.1.0
66

77
environment:
88
sdk: ">=2.14.0 <3.0.0"

packages/google_sign_in/google_sign_in_android/test/google_sign_in_android_test.dart

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,21 @@ void main() {
111111
'scopes': <String>['two', 'scopes'],
112112
'signInOption': 'SignInOption.games',
113113
'clientId': 'fakeClientId',
114+
'forceCodeForRefreshToken': false,
115+
}),
116+
() {
117+
googleSignIn.initWithParams(const SignInInitParameters(
118+
hostedDomain: 'example.com',
119+
scopes: <String>['two', 'scopes'],
120+
signInOption: SignInOption.games,
121+
clientId: 'fakeClientId',
122+
forceCodeForRefreshToken: true));
123+
}: isMethodCall('init', arguments: <String, dynamic>{
124+
'hostedDomain': 'example.com',
125+
'scopes': <String>['two', 'scopes'],
126+
'signInOption': 'SignInOption.games',
127+
'clientId': 'fakeClientId',
128+
'forceCodeForRefreshToken': true,
114129
}),
115130
() {
116131
googleSignIn.getTokens(

packages/google_sign_in/google_sign_in_ios/AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,4 @@ Aleksandr Yurkovskiy <sanekyy@gmail.com>
6464
Anton Borries <mail@antonborri.es>
6565
Alex Li <google@alexv525.com>
6666
Rahul Raj <64.rahulraj@gmail.com>
67+
Twin Sun, LLC <google-contrib@twinsunsolutions.com>

packages/google_sign_in/google_sign_in_ios/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 5.5.0
2+
3+
* Adds override for `GoogleSignInPlatform.initWithParams`.
4+
15
## 5.4.0
26

37
* Adds support for `serverClientId` configuration option.

packages/google_sign_in/google_sign_in_ios/lib/google_sign_in_ios.dart

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,25 @@ class GoogleSignInIOS extends GoogleSignInPlatform {
3030
String? hostedDomain,
3131
String? clientId,
3232
}) {
33-
if (signInOption == SignInOption.games) {
33+
return initWithParams(SignInInitParameters(
34+
signInOption: signInOption,
35+
scopes: scopes,
36+
hostedDomain: hostedDomain,
37+
clientId: clientId,
38+
));
39+
}
40+
41+
@override
42+
Future<void> initWithParams(SignInInitParameters params) {
43+
if (params.signInOption == SignInOption.games) {
3444
throw PlatformException(
3545
code: 'unsupported-options',
3646
message: 'Games sign in is not supported on iOS');
3747
}
3848
return channel.invokeMethod<void>('init', <String, dynamic>{
39-
'scopes': scopes,
40-
'hostedDomain': hostedDomain,
41-
'clientId': clientId,
49+
'scopes': params.scopes,
50+
'hostedDomain': params.hostedDomain,
51+
'clientId': params.clientId,
4252
});
4353
}
4454

packages/google_sign_in/google_sign_in_ios/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: google_sign_in_ios
22
description: iOS implementation of the google_sign_in plugin.
33
repository: https://github.com/flutter/plugins/tree/main/packages/google_sign_in/google_sign_in_ios
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+google_sign_in%22
5-
version: 5.4.0
5+
version: 5.5.0
66

77
environment:
88
sdk: ">=2.14.0 <3.0.0"

packages/google_sign_in/google_sign_in_ios/test/google_sign_in_ios_test.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,16 @@ void main() {
125125
'scopes': <String>['two', 'scopes'],
126126
'clientId': 'fakeClientId',
127127
}),
128+
() {
129+
googleSignIn.initWithParams(const SignInInitParameters(
130+
hostedDomain: 'example.com',
131+
scopes: <String>['two', 'scopes'],
132+
clientId: 'fakeClientId'));
133+
}: isMethodCall('init', arguments: <String, dynamic>{
134+
'hostedDomain': 'example.com',
135+
'scopes': <String>['two', 'scopes'],
136+
'clientId': 'fakeClientId',
137+
}),
128138
() {
129139
googleSignIn.getTokens(
130140
email: 'example@example.com', shouldRecoverAuth: false);

0 commit comments

Comments
 (0)