@capgo/capacitor-social-login 8.7.5 → 8.7.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +171 -76
- package/android/src/main/java/ee/forgr/capacitor/social/login/GoogleAuthorizationDecision.java +80 -0
- package/android/src/main/java/ee/forgr/capacitor/social/login/GoogleProvider.java +251 -75
- package/android/src/main/java/ee/forgr/capacitor/social/login/SocialLoginPlugin.java +3 -2
- package/dist/docs.json +3 -7
- package/dist/esm/definitions.d.ts +37 -6
- package/dist/esm/definitions.js.map +1 -1
- package/ios/Sources/SocialLoginPlugin/SocialLoginOpenURLHandler.swift +174 -0
- package/ios/Sources/SocialLoginPlugin/SocialLoginPlugin.swift +28 -1
- package/ios/Tests/SocialLoginPluginTests/SocialLoginOpenURLHandlerTests.swift +89 -0
- package/ios/Tests/SocialLoginPluginTests/SocialLoginPluginTests.swift +0 -10
- package/package.json +1 -1
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
package ee.forgr.capacitor.social.login;
|
|
2
2
|
|
|
3
|
+
import android.accounts.Account;
|
|
3
4
|
import android.app.Activity;
|
|
4
5
|
import android.app.PendingIntent;
|
|
5
6
|
import android.content.Context;
|
|
@@ -46,10 +47,13 @@ import java.util.HashSet;
|
|
|
46
47
|
import java.util.List;
|
|
47
48
|
import java.util.Objects;
|
|
48
49
|
import java.util.Set;
|
|
50
|
+
import java.util.concurrent.ExecutionException;
|
|
49
51
|
import java.util.concurrent.Executor;
|
|
50
52
|
import java.util.concurrent.ExecutorService;
|
|
51
53
|
import java.util.concurrent.Executors;
|
|
52
54
|
import java.util.concurrent.TimeUnit;
|
|
55
|
+
import java.util.concurrent.TimeoutException;
|
|
56
|
+
import java.util.concurrent.atomic.AtomicBoolean;
|
|
53
57
|
import org.json.JSONArray;
|
|
54
58
|
import org.json.JSONException;
|
|
55
59
|
import org.json.JSONObject;
|
|
@@ -85,7 +89,33 @@ public class GoogleProvider implements SocialProvider {
|
|
|
85
89
|
private String idToken = null;
|
|
86
90
|
private String accessToken = null;
|
|
87
91
|
private GoogleProviderLoginType mode = GoogleProviderLoginType.ONLINE;
|
|
92
|
+
private boolean skipAuthOnlyAuthorizationAfterSignIn = false;
|
|
88
93
|
private String hostedDomain = null;
|
|
94
|
+
private Integer pendingAuthorizationSlotIndex = null;
|
|
95
|
+
|
|
96
|
+
private static final class AuthorizationWaitContext {
|
|
97
|
+
|
|
98
|
+
private final AtomicBoolean finished = new AtomicBoolean(false);
|
|
99
|
+
private final AtomicBoolean consentUiLaunched = new AtomicBoolean(false);
|
|
100
|
+
private final int[] slotIndex = new int[] { -1 };
|
|
101
|
+
private CallbackToFutureAdapter.Completer<AuthorizationResult> slotCompleter = null;
|
|
102
|
+
|
|
103
|
+
private void markFinished() {
|
|
104
|
+
finished.set(true);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
private boolean isFinished() {
|
|
108
|
+
return finished.get();
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
private void markConsentUiLaunched() {
|
|
112
|
+
consentUiLaunched.set(true);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
private boolean isConsentUiLaunched() {
|
|
116
|
+
return consentUiLaunched.get();
|
|
117
|
+
}
|
|
118
|
+
}
|
|
89
119
|
|
|
90
120
|
private static String maskClientId(String clientId) {
|
|
91
121
|
if (clientId == null || clientId.isEmpty()) {
|
|
@@ -171,9 +201,19 @@ public class GoogleProvider implements SocialProvider {
|
|
|
171
201
|
}
|
|
172
202
|
|
|
173
203
|
public void initialize(String clientId, GoogleProviderLoginType mode, String hostedDomain) {
|
|
204
|
+
initialize(clientId, mode, hostedDomain, false);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
public void initialize(
|
|
208
|
+
String clientId,
|
|
209
|
+
GoogleProviderLoginType mode,
|
|
210
|
+
String hostedDomain,
|
|
211
|
+
boolean skipAuthOnlyAuthorizationAfterSignIn
|
|
212
|
+
) {
|
|
174
213
|
this.credentialManager = CredentialManager.create(activity);
|
|
175
214
|
this.clientId = clientId;
|
|
176
215
|
this.mode = mode;
|
|
216
|
+
this.skipAuthOnlyAuthorizationAfterSignIn = skipAuthOnlyAuthorizationAfterSignIn;
|
|
177
217
|
this.hostedDomain = hostedDomain;
|
|
178
218
|
logGoogleCloudDiagnostics("initialize");
|
|
179
219
|
|
|
@@ -186,8 +226,16 @@ public class GoogleProvider implements SocialProvider {
|
|
|
186
226
|
try {
|
|
187
227
|
JSONObject object = new JSONObject(data);
|
|
188
228
|
GoogleProvider.this.idToken = object.optString("idToken", null);
|
|
189
|
-
|
|
190
|
-
|
|
229
|
+
if (!object.has("accessToken") || object.isNull("accessToken")) {
|
|
230
|
+
GoogleProvider.this.accessToken = null;
|
|
231
|
+
} else {
|
|
232
|
+
String restoredAccessToken = object.optString("accessToken", null);
|
|
233
|
+
if (restoredAccessToken == null || restoredAccessToken.isEmpty() || "null".equals(restoredAccessToken)) {
|
|
234
|
+
GoogleProvider.this.accessToken = null;
|
|
235
|
+
} else {
|
|
236
|
+
GoogleProvider.this.accessToken = restoredAccessToken;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
191
239
|
JSONArray storedScopes = object.optJSONArray("scopes");
|
|
192
240
|
if (storedScopes != null && storedScopes.length() > 0) {
|
|
193
241
|
List<String> scopesList = new ArrayList<>(storedScopes.length());
|
|
@@ -216,31 +264,6 @@ public class GoogleProvider implements SocialProvider {
|
|
|
216
264
|
}
|
|
217
265
|
}
|
|
218
266
|
|
|
219
|
-
/**
|
|
220
|
-
* Authentication-only (OIDC) scopes that Credential Manager already covers via the ID token.
|
|
221
|
-
* AuthorizationClient is only needed when requesting additional Google API scopes.
|
|
222
|
-
*/
|
|
223
|
-
private static boolean isAuthenticationOnlyScopes(String[] scopes) {
|
|
224
|
-
if (scopes == null || scopes.length == 0) {
|
|
225
|
-
return true;
|
|
226
|
-
}
|
|
227
|
-
for (String scope : scopes) {
|
|
228
|
-
if (scope == null || scope.isEmpty()) {
|
|
229
|
-
continue;
|
|
230
|
-
}
|
|
231
|
-
boolean isDefault =
|
|
232
|
-
scope.equals("openid") ||
|
|
233
|
-
scope.equals("https://www.googleapis.com/auth/userinfo.email") ||
|
|
234
|
-
scope.equals("https://www.googleapis.com/auth/userinfo.profile") ||
|
|
235
|
-
scope.equals("email") ||
|
|
236
|
-
scope.equals("profile");
|
|
237
|
-
if (!isDefault) {
|
|
238
|
-
return false;
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
return true;
|
|
242
|
-
}
|
|
243
|
-
|
|
244
267
|
private void resolveOnlineLogin(
|
|
245
268
|
PluginCall call,
|
|
246
269
|
JSObject response,
|
|
@@ -579,7 +602,56 @@ public class GoogleProvider implements SocialProvider {
|
|
|
579
602
|
.apply();
|
|
580
603
|
}
|
|
581
604
|
|
|
582
|
-
private
|
|
605
|
+
private void clearAuthorizationSlot(int index, CallbackToFutureAdapter.Completer<AuthorizationResult> expectedCompleter) {
|
|
606
|
+
if (index < 0) {
|
|
607
|
+
return;
|
|
608
|
+
}
|
|
609
|
+
synchronized (this) {
|
|
610
|
+
if (index < futuresList.size()) {
|
|
611
|
+
CallbackToFutureAdapter.Completer<AuthorizationResult> current = futuresList.get(index);
|
|
612
|
+
if (expectedCompleter != null && current != expectedCompleter) {
|
|
613
|
+
return;
|
|
614
|
+
}
|
|
615
|
+
futuresList.set(index, null);
|
|
616
|
+
}
|
|
617
|
+
if (Objects.equals(pendingAuthorizationSlotIndex, index)) {
|
|
618
|
+
pendingAuthorizationSlotIndex = null;
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
private void finishWaitAndClearSlot(AuthorizationWaitContext waitContext) {
|
|
624
|
+
synchronized (this) {
|
|
625
|
+
waitContext.markFinished();
|
|
626
|
+
clearAuthorizationSlot(waitContext.slotIndex[0], waitContext.slotCompleter);
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
private AuthorizationResult awaitAuthorizationResult(ListenableFuture<AuthorizationResult> future, AuthorizationWaitContext waitContext)
|
|
631
|
+
throws ExecutionException, InterruptedException, TimeoutException {
|
|
632
|
+
long preLaunchDeadlineNanos = System.nanoTime() + TimeUnit.SECONDS.toNanos(GoogleAuthorizationDecision.AUTHORIZATION_WAIT_SECONDS);
|
|
633
|
+
while (true) {
|
|
634
|
+
if (waitContext.isConsentUiLaunched()) {
|
|
635
|
+
return future.get(GoogleAuthorizationDecision.AUTHORIZATION_CONSENT_WAIT_SECONDS, TimeUnit.SECONDS);
|
|
636
|
+
}
|
|
637
|
+
long remainingNanos = preLaunchDeadlineNanos - System.nanoTime();
|
|
638
|
+
if (remainingNanos <= 0) {
|
|
639
|
+
throw new TimeoutException(GoogleAuthorizationDecision.AUTHORIZATION_WAIT_TIMEOUT_MESSAGE);
|
|
640
|
+
}
|
|
641
|
+
long pollNanos = Math.min(remainingNanos, TimeUnit.SECONDS.toNanos(1));
|
|
642
|
+
try {
|
|
643
|
+
return future.get(pollNanos, TimeUnit.NANOSECONDS);
|
|
644
|
+
} catch (TimeoutException ignored) {
|
|
645
|
+
// Poll until authorize completes, consent UI launches, or pre-launch deadline.
|
|
646
|
+
}
|
|
647
|
+
}
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
private ListenableFuture<AuthorizationResult> getAuthorizationResult(
|
|
651
|
+
Boolean forceRefreshToken,
|
|
652
|
+
String googleAccountId,
|
|
653
|
+
AuthorizationWaitContext waitContext
|
|
654
|
+
) {
|
|
583
655
|
// Account account = new Account(credential.getId(), "com.google");
|
|
584
656
|
// String scopesString = "oauth2:" + TextUtils.join(" ", this.scopes);
|
|
585
657
|
// String token = GoogleAuthUtil.getToken(
|
|
@@ -605,7 +677,9 @@ public class GoogleProvider implements SocialProvider {
|
|
|
605
677
|
scopes.add(new Scope(scope));
|
|
606
678
|
}
|
|
607
679
|
AuthorizationRequest.Builder authorizationRequestBuilder = AuthorizationRequest.builder().setRequestedScopes(scopes);
|
|
608
|
-
|
|
680
|
+
if (googleAccountId != null && !googleAccountId.isEmpty()) {
|
|
681
|
+
authorizationRequestBuilder.setAccount(new Account(googleAccountId, "com.google"));
|
|
682
|
+
}
|
|
609
683
|
|
|
610
684
|
if (GoogleProvider.this.mode == GoogleProviderLoginType.OFFLINE) {
|
|
611
685
|
authorizationRequestBuilder = authorizationRequestBuilder.requestOfflineAccess(this.clientId, forceRefreshToken);
|
|
@@ -616,6 +690,9 @@ public class GoogleProvider implements SocialProvider {
|
|
|
616
690
|
Identity.getAuthorizationClient(context)
|
|
617
691
|
.authorize(authorizationRequest)
|
|
618
692
|
.addOnSuccessListener((authorizationResult) -> {
|
|
693
|
+
if (waitContext.isFinished()) {
|
|
694
|
+
return;
|
|
695
|
+
}
|
|
619
696
|
if (authorizationResult.hasResolution()) {
|
|
620
697
|
// Access needs to be granted by the user
|
|
621
698
|
PendingIntent pendingIntent = authorizationResult.getPendingIntent();
|
|
@@ -625,38 +702,68 @@ public class GoogleProvider implements SocialProvider {
|
|
|
625
702
|
return;
|
|
626
703
|
}
|
|
627
704
|
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
break;
|
|
634
|
-
}
|
|
635
|
-
}
|
|
636
|
-
|
|
637
|
-
if (fututeIndex == -1) {
|
|
638
|
-
completer.setException(new RuntimeException("Cannot find index for future"));
|
|
639
|
-
Log.e(LOG_TAG, "Cannot find index for future. Too many login requests??");
|
|
705
|
+
if (!GoogleAuthorizationDecision.canRouteAuthorizationResolution(activity)) {
|
|
706
|
+
Log.e(LOG_TAG, "AuthorizationClient returned a resolution, but MainActivity cannot route onActivityResult");
|
|
707
|
+
completer.setException(
|
|
708
|
+
new RuntimeException(GoogleAuthorizationDecision.UNROUTABLE_AUTHORIZATION_RESOLUTION_MESSAGE)
|
|
709
|
+
);
|
|
640
710
|
return;
|
|
641
711
|
}
|
|
642
712
|
|
|
643
|
-
|
|
713
|
+
synchronized (GoogleProvider.this) {
|
|
714
|
+
if (waitContext.isFinished()) {
|
|
715
|
+
return;
|
|
716
|
+
}
|
|
644
717
|
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
718
|
+
int fututeIndex = -1;
|
|
719
|
+
for (int i = 0; i < futuresList.size(); i++) {
|
|
720
|
+
if (futuresList.get(i) == null) {
|
|
721
|
+
fututeIndex = i;
|
|
722
|
+
break;
|
|
723
|
+
}
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
if (fututeIndex == -1) {
|
|
727
|
+
completer.setException(new RuntimeException("Cannot find index for future"));
|
|
728
|
+
Log.e(LOG_TAG, "Cannot find index for future. Too many login requests??");
|
|
729
|
+
return;
|
|
730
|
+
}
|
|
731
|
+
|
|
732
|
+
futuresList.set(fututeIndex, completer);
|
|
733
|
+
waitContext.slotIndex[0] = fututeIndex;
|
|
734
|
+
waitContext.slotCompleter = completer;
|
|
735
|
+
pendingAuthorizationSlotIndex = fututeIndex;
|
|
736
|
+
|
|
737
|
+
if (waitContext.isFinished()) {
|
|
738
|
+
clearAuthorizationSlot(fututeIndex, completer);
|
|
739
|
+
waitContext.slotIndex[0] = -1;
|
|
740
|
+
waitContext.slotCompleter = null;
|
|
741
|
+
return;
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
try {
|
|
745
|
+
activity.startIntentSenderForResult(
|
|
746
|
+
pendingIntent.getIntentSender(),
|
|
747
|
+
GoogleProvider.REQUEST_AUTHORIZE_GOOGLE_MIN + fututeIndex,
|
|
748
|
+
null,
|
|
749
|
+
0,
|
|
750
|
+
0,
|
|
751
|
+
0,
|
|
752
|
+
null
|
|
753
|
+
);
|
|
754
|
+
waitContext.markConsentUiLaunched();
|
|
755
|
+
} catch (IntentSender.SendIntentException e) {
|
|
756
|
+
Log.e(LOG_TAG, "Couldn't start Authorization UI: " + e.getLocalizedMessage());
|
|
757
|
+
clearAuthorizationSlot(fututeIndex, completer);
|
|
758
|
+
waitContext.slotIndex[0] = -1;
|
|
759
|
+
waitContext.slotCompleter = null;
|
|
760
|
+
completer.setException(e);
|
|
761
|
+
}
|
|
658
762
|
}
|
|
659
763
|
} else {
|
|
764
|
+
if (waitContext.isFinished()) {
|
|
765
|
+
return;
|
|
766
|
+
}
|
|
660
767
|
// Access already granted, continue with user action.
|
|
661
768
|
// Online mode: a null access token is allowed (ID-token auth still succeeds).
|
|
662
769
|
// Offline mode still requires serverAuthCode.
|
|
@@ -673,6 +780,9 @@ public class GoogleProvider implements SocialProvider {
|
|
|
673
780
|
}
|
|
674
781
|
})
|
|
675
782
|
.addOnFailureListener((e) -> {
|
|
783
|
+
if (waitContext.isFinished()) {
|
|
784
|
+
return;
|
|
785
|
+
}
|
|
676
786
|
completer.setException(new RuntimeException("Failed to authorize"));
|
|
677
787
|
Log.e(LOG_TAG, "Failed to authorize", e);
|
|
678
788
|
});
|
|
@@ -703,8 +813,25 @@ public class GoogleProvider implements SocialProvider {
|
|
|
703
813
|
|
|
704
814
|
GoogleIdTokenCredential googleIdTokenCredential = GoogleIdTokenCredential.createFrom(credential.getData());
|
|
705
815
|
String idToken = googleIdTokenCredential.getIdToken();
|
|
816
|
+
String googleAccountId = googleIdTokenCredential.getId();
|
|
817
|
+
|
|
818
|
+
if (
|
|
819
|
+
GoogleAuthorizationDecision.shouldSkipAuthorizationAfterCredentialSignIn(
|
|
820
|
+
GoogleProvider.this.mode,
|
|
821
|
+
GoogleProvider.this.scopes,
|
|
822
|
+
GoogleProvider.this.skipAuthOnlyAuthorizationAfterSignIn
|
|
823
|
+
)
|
|
824
|
+
) {
|
|
825
|
+
resolveOnlineLogin(call, response, resultObj, user, idToken, null);
|
|
826
|
+
return;
|
|
827
|
+
}
|
|
706
828
|
|
|
707
|
-
|
|
829
|
+
AuthorizationWaitContext authorizationWaitContext = new AuthorizationWaitContext();
|
|
830
|
+
ListenableFuture<AuthorizationResult> future = getAuthorizationResult(
|
|
831
|
+
forceRefreshToken,
|
|
832
|
+
googleAccountId,
|
|
833
|
+
authorizationWaitContext
|
|
834
|
+
);
|
|
708
835
|
|
|
709
836
|
// Use ExecutorService to retrieve the access token
|
|
710
837
|
ExecutorService executor = Executors.newSingleThreadExecutor();
|
|
@@ -714,7 +841,7 @@ public class GoogleProvider implements SocialProvider {
|
|
|
714
841
|
@Override
|
|
715
842
|
public void run() {
|
|
716
843
|
try {
|
|
717
|
-
AuthorizationResult authResult = future
|
|
844
|
+
AuthorizationResult authResult = awaitAuthorizationResult(future, authorizationWaitContext);
|
|
718
845
|
if (GoogleProvider.this.mode == GoogleProviderLoginType.ONLINE) {
|
|
719
846
|
// accessToken may be null: Credential Manager auth still succeeded.
|
|
720
847
|
resolveOnlineLogin(call, response, resultObj, user, idToken, authResult.getAccessToken());
|
|
@@ -728,13 +855,24 @@ public class GoogleProvider implements SocialProvider {
|
|
|
728
855
|
call.reject("Failed to get serverAuthCode");
|
|
729
856
|
}
|
|
730
857
|
}
|
|
858
|
+
} catch (TimeoutException e) {
|
|
859
|
+
finishWaitAndClearSlot(authorizationWaitContext);
|
|
860
|
+
String timeoutMessage = authorizationWaitContext.isConsentUiLaunched()
|
|
861
|
+
? GoogleAuthorizationDecision.AUTHORIZATION_CONSENT_WAIT_TIMEOUT_MESSAGE
|
|
862
|
+
: GoogleAuthorizationDecision.AUTHORIZATION_WAIT_TIMEOUT_MESSAGE;
|
|
863
|
+
call.reject(timeoutMessage);
|
|
731
864
|
} catch (Exception e) {
|
|
865
|
+
finishWaitAndClearSlot(authorizationWaitContext);
|
|
866
|
+
if (GoogleAuthorizationDecision.isUnroutableAuthorizationResolutionFailure(e)) {
|
|
867
|
+
call.reject(GoogleAuthorizationDecision.UNROUTABLE_AUTHORIZATION_RESOLUTION_MESSAGE);
|
|
868
|
+
return;
|
|
869
|
+
}
|
|
732
870
|
// AuthorizationClient failed after Credential Manager returned a valid ID token.
|
|
733
871
|
// For authentication-only online scopes, resolve with idToken + profile.
|
|
734
872
|
if (
|
|
735
873
|
GoogleProvider.this.mode == GoogleProviderLoginType.ONLINE &&
|
|
736
874
|
idToken != null &&
|
|
737
|
-
isAuthenticationOnlyScopes(GoogleProvider.this.scopes)
|
|
875
|
+
GoogleAuthorizationDecision.isAuthenticationOnlyScopes(GoogleProvider.this.scopes)
|
|
738
876
|
) {
|
|
739
877
|
Log.w(
|
|
740
878
|
LOG_TAG,
|
|
@@ -765,21 +903,28 @@ public class GoogleProvider implements SocialProvider {
|
|
|
765
903
|
|
|
766
904
|
public void handleAuthorizationIntent(int requestCode, Intent data) {
|
|
767
905
|
int futureIndex = requestCode - GoogleProvider.REQUEST_AUTHORIZE_GOOGLE_MIN;
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
906
|
+
CallbackToFutureAdapter.Completer<AuthorizationResult> future;
|
|
907
|
+
synchronized (this) {
|
|
908
|
+
if (futureIndex < 0 || futureIndex >= futuresList.size()) {
|
|
909
|
+
Log.e(
|
|
910
|
+
LOG_TAG,
|
|
911
|
+
String.format(
|
|
912
|
+
"Invalid future index. REQUEST_AUTHORIZE_GOOGLE_MIN: %d, requestCode: %d, futures list length: %d, futureIndex: %d",
|
|
913
|
+
REQUEST_AUTHORIZE_GOOGLE_MIN,
|
|
914
|
+
requestCode,
|
|
915
|
+
futuresList.size(),
|
|
916
|
+
futureIndex
|
|
917
|
+
)
|
|
918
|
+
);
|
|
919
|
+
return;
|
|
920
|
+
}
|
|
781
921
|
|
|
782
|
-
|
|
922
|
+
future = futuresList.get(futureIndex);
|
|
923
|
+
if (future == null) {
|
|
924
|
+
Log.w(LOG_TAG, "Authorization result arrived after the login request ended");
|
|
925
|
+
return;
|
|
926
|
+
}
|
|
927
|
+
}
|
|
783
928
|
|
|
784
929
|
try {
|
|
785
930
|
AuthorizationResult authorizationResult = Identity.getAuthorizationClient(this.activity).getAuthorizationResultFromIntent(data);
|
|
@@ -787,6 +932,8 @@ public class GoogleProvider implements SocialProvider {
|
|
|
787
932
|
} catch (ApiException e) {
|
|
788
933
|
Log.e(LOG_TAG, "Cannot get getAuthorizationResultFromIntent", e);
|
|
789
934
|
future.setException(new RuntimeException("Cannot get getAuthorizationResultFromIntent"));
|
|
935
|
+
} finally {
|
|
936
|
+
clearAuthorizationSlot(futureIndex, future);
|
|
790
937
|
}
|
|
791
938
|
}
|
|
792
939
|
|
|
@@ -1368,7 +1515,25 @@ public class GoogleProvider implements SocialProvider {
|
|
|
1368
1515
|
);
|
|
1369
1516
|
String newIdToken = googleIdTokenCredential.getIdToken();
|
|
1370
1517
|
|
|
1371
|
-
|
|
1518
|
+
if (
|
|
1519
|
+
GoogleAuthorizationDecision.shouldSkipAuthorizationAfterCredentialSignIn(
|
|
1520
|
+
GoogleProvider.this.mode,
|
|
1521
|
+
GoogleProvider.this.scopes,
|
|
1522
|
+
GoogleProvider.this.skipAuthOnlyAuthorizationAfterSignIn
|
|
1523
|
+
)
|
|
1524
|
+
) {
|
|
1525
|
+
persistState(newIdToken, null, GoogleProvider.this.scopes);
|
|
1526
|
+
call.resolve();
|
|
1527
|
+
return;
|
|
1528
|
+
}
|
|
1529
|
+
|
|
1530
|
+
String googleAccountId = googleIdTokenCredential.getId();
|
|
1531
|
+
AuthorizationWaitContext authorizationWaitContext = new AuthorizationWaitContext();
|
|
1532
|
+
ListenableFuture<AuthorizationResult> future = getAuthorizationResult(
|
|
1533
|
+
false,
|
|
1534
|
+
googleAccountId,
|
|
1535
|
+
authorizationWaitContext
|
|
1536
|
+
);
|
|
1372
1537
|
|
|
1373
1538
|
ExecutorService authExecutor = Executors.newSingleThreadExecutor();
|
|
1374
1539
|
authExecutor.execute(
|
|
@@ -1376,7 +1541,7 @@ public class GoogleProvider implements SocialProvider {
|
|
|
1376
1541
|
@Override
|
|
1377
1542
|
public void run() {
|
|
1378
1543
|
try {
|
|
1379
|
-
AuthorizationResult authResult = future
|
|
1544
|
+
AuthorizationResult authResult = awaitAuthorizationResult(future, authorizationWaitContext);
|
|
1380
1545
|
String newAccessToken = authResult.getAccessToken();
|
|
1381
1546
|
// Persist ID token even when access token is unavailable.
|
|
1382
1547
|
persistState(
|
|
@@ -1385,8 +1550,19 @@ public class GoogleProvider implements SocialProvider {
|
|
|
1385
1550
|
GoogleProvider.this.scopes
|
|
1386
1551
|
);
|
|
1387
1552
|
call.resolve();
|
|
1553
|
+
} catch (TimeoutException e) {
|
|
1554
|
+
finishWaitAndClearSlot(authorizationWaitContext);
|
|
1555
|
+
String timeoutMessage = authorizationWaitContext.isConsentUiLaunched()
|
|
1556
|
+
? GoogleAuthorizationDecision.AUTHORIZATION_CONSENT_WAIT_TIMEOUT_MESSAGE
|
|
1557
|
+
: GoogleAuthorizationDecision.AUTHORIZATION_WAIT_TIMEOUT_MESSAGE;
|
|
1558
|
+
call.reject(timeoutMessage);
|
|
1388
1559
|
} catch (Exception e) {
|
|
1389
|
-
|
|
1560
|
+
finishWaitAndClearSlot(authorizationWaitContext);
|
|
1561
|
+
if (GoogleAuthorizationDecision.isUnroutableAuthorizationResolutionFailure(e)) {
|
|
1562
|
+
call.reject(GoogleAuthorizationDecision.UNROUTABLE_AUTHORIZATION_RESOLUTION_MESSAGE);
|
|
1563
|
+
return;
|
|
1564
|
+
}
|
|
1565
|
+
if (GoogleAuthorizationDecision.isAuthenticationOnlyScopes(GoogleProvider.this.scopes)) {
|
|
1390
1566
|
try {
|
|
1391
1567
|
// Authorization failed; still keep the refreshed ID token.
|
|
1392
1568
|
persistState(newIdToken, null, GoogleProvider.this.scopes);
|
|
@@ -29,7 +29,7 @@ import org.json.JSONTokener;
|
|
|
29
29
|
@CapacitorPlugin(name = "SocialLogin")
|
|
30
30
|
public class SocialLoginPlugin extends Plugin {
|
|
31
31
|
|
|
32
|
-
private final String pluginVersion = "8.7.
|
|
32
|
+
private final String pluginVersion = "8.7.7";
|
|
33
33
|
|
|
34
34
|
public static String LOG_TAG = "CapgoSocialLogin";
|
|
35
35
|
private static final String USER_CANCELLED_CODE = "USER_CANCELLED";
|
|
@@ -222,7 +222,8 @@ public class SocialLoginPlugin extends Plugin {
|
|
|
222
222
|
call.reject("google.mode != (online || offline)");
|
|
223
223
|
return;
|
|
224
224
|
}
|
|
225
|
-
|
|
225
|
+
boolean skipAuthOnlyAuthorizationAfterSignIn = google.getBoolean("skipAuthOnlyAuthorizationAfterSignIn", false);
|
|
226
|
+
googleProvider.initialize(googleClientId, mode, hostedDomain, skipAuthOnlyAuthorizationAfterSignIn);
|
|
226
227
|
this.socialProviderHashMap.put("google", googleProvider);
|
|
227
228
|
}
|
|
228
229
|
|
package/dist/docs.json
CHANGED
|
@@ -146,16 +146,12 @@
|
|
|
146
146
|
],
|
|
147
147
|
"returns": "Promise<void>",
|
|
148
148
|
"tags": [
|
|
149
|
-
{
|
|
150
|
-
"name": "description",
|
|
151
|
-
"text": "refresh the access token\n\n**Google Offline Mode Limitation:**\nThis method is NOT supported when Google is initialized with `mode: 'offline'`.\nOffline mode only returns `serverAuthCode` for backend token exchange, so token refresh must happen on your backend.\nThe plugin logs and rejects with a message explaining that you should send `serverAuthCode` to your backend,\nrefresh the Google tokens there, or switch to `mode: 'online'` for client-side refresh.\n\n**Google Web Limitation:**\nOn Web, Google `refresh()` is not implemented, even when using `mode: 'online'`.\nCall `login()` again on Web to obtain a fresh token instead."
|
|
152
|
-
},
|
|
153
149
|
{
|
|
154
150
|
"name": "throws",
|
|
155
151
|
"text": "Error if Google provider is in offline mode, or on Web where Google `refresh()` is not implemented"
|
|
156
152
|
}
|
|
157
153
|
],
|
|
158
|
-
"docs": "Refresh the access token",
|
|
154
|
+
"docs": "Refresh the access token for the logged-in provider.\n\n**Google Android refresh behavior:**\nOn Android, when the saved ID token is still valid and either no access token is stored or Google access\ntoken validation succeeds within the timeout, `refresh()` resolves without updating either token. That\nearly return applies regardless of `skipAuthOnlyAuthorizationAfterSignIn`.\nIf validation fails or times out, `refresh()` continues even when a token remains stored; with\n`skipAuthOnlyAuthorizationAfterSignIn: true` and default OIDC scopes, a successful credential refresh\nthen clears that access token without obtaining a new one.\n\n**Google Offline Mode Limitation:**\nThis method is NOT supported when Google is initialized with `mode: 'offline'`.\nOffline mode only returns `serverAuthCode` for backend token exchange, so token refresh must happen on your backend.\nThe plugin logs and rejects with a message explaining that you should send `serverAuthCode` to your backend,\nrefresh the Google tokens there, or switch to `mode: 'online'` for client-side refresh.\n\n**Google Android `skipAuthOnlyAuthorizationAfterSignIn`:**\nWhen initialize sets `google.skipAuthOnlyAuthorizationAfterSignIn: true` and only default OIDC scopes are\nrequested, a refresh that runs past the early return skips `AuthorizationClient.authorize()`, updates the\nsaved `idToken`, and clears any saved access token; no new access token is obtained. Enabling the option\ndoes not clear a stored access token while the early return applies (validation succeeded or no token\nstored). With default `false`, refresh still skips authorize on the early return when no access token is\nstored but the ID token is valid.\n\n**Google Web Limitation:**\nOn Web, Google `refresh()` is not implemented, even when using `mode: 'online'`.\nCall `login()` again on Web to obtain a fresh token instead.",
|
|
159
155
|
"complexTypes": [
|
|
160
156
|
"LoginOptions"
|
|
161
157
|
],
|
|
@@ -437,7 +433,7 @@
|
|
|
437
433
|
"tags": [],
|
|
438
434
|
"docs": "",
|
|
439
435
|
"complexTypes": [],
|
|
440
|
-
"type": "{ iOSClientId?: string | undefined; iOSServerClientId?: string | undefined; webClientId?: string | undefined; mode?: 'online' | 'offline' | undefined; hostedDomain?: string | undefined; redirectUrl?: string | undefined; } | undefined"
|
|
436
|
+
"type": "{ iOSClientId?: string | undefined; iOSServerClientId?: string | undefined; webClientId?: string | undefined; mode?: 'online' | 'offline' | undefined; hostedDomain?: string | undefined; redirectUrl?: string | undefined; skipAuthOnlyAuthorizationAfterSignIn?: boolean | undefined; } | undefined"
|
|
441
437
|
},
|
|
442
438
|
{
|
|
443
439
|
"name": "apple",
|
|
@@ -1046,7 +1042,7 @@
|
|
|
1046
1042
|
{
|
|
1047
1043
|
"name": "accessToken",
|
|
1048
1044
|
"tags": [],
|
|
1049
|
-
"docs": "OAuth access token for Google APIs.\n\nMay be `null` on Android when
|
|
1045
|
+
"docs": "OAuth access token for Google APIs.\n\nMay be `null` on Android when AuthorizationClient does not return an access token, or when\n`skipAuthOnlyAuthorizationAfterSignIn` is enabled during initialize (opt-in). Use `idToken`\nfor user authentication; request additional Google API scopes when an access token is required.",
|
|
1050
1046
|
"complexTypes": [
|
|
1051
1047
|
"AccessToken"
|
|
1052
1048
|
],
|
|
@@ -428,6 +428,23 @@ export interface InitializeOptions {
|
|
|
428
428
|
* Google Redirect URL, should be your backend url that is configured in your google app
|
|
429
429
|
*/
|
|
430
430
|
redirectUrl?: string;
|
|
431
|
+
/**
|
|
432
|
+
* Android only. When `true`, skip `AuthorizationClient.authorize()` after Credential Manager sign-in
|
|
433
|
+
* when `mode` is `online` and only default OIDC scopes are requested. Use this on stock `BridgeActivity`
|
|
434
|
+
* apps that hit a hang during authorize; login may return `accessToken: null` while still returning
|
|
435
|
+
* `idToken` and profile. When refresh runs past its early return (saved ID token expired or missing, or
|
|
436
|
+
* access token validation failed or timed out), authentication-only scopes skip authorize, update the saved
|
|
437
|
+
* `idToken`, and clear any saved access token.
|
|
438
|
+
* Enabling this after a session still has valid saved tokens does not clear a stored access token when
|
|
439
|
+
* refresh's early return applies: saved ID token valid and either no access token stored or validation
|
|
440
|
+
* succeeds. Default `false` keeps prior behavior: authorize runs on login and refresh when needed, and
|
|
441
|
+
* `accessToken` is returned when Google provides one. With default `false`, refresh still returns without
|
|
442
|
+
* authorize when saved tokens are already valid, including when no access token is stored.
|
|
443
|
+
*
|
|
444
|
+
* @default false
|
|
445
|
+
* @since 8.8.0
|
|
446
|
+
*/
|
|
447
|
+
skipAuthOnlyAuthorizationAfterSignIn?: boolean;
|
|
431
448
|
};
|
|
432
449
|
apple?: {
|
|
433
450
|
/**
|
|
@@ -797,10 +814,9 @@ export interface GoogleLoginResponseOnline {
|
|
|
797
814
|
/**
|
|
798
815
|
* OAuth access token for Google APIs.
|
|
799
816
|
*
|
|
800
|
-
* May be `null` on Android when
|
|
801
|
-
*
|
|
802
|
-
*
|
|
803
|
-
* request additional Google API scopes when an access token is required.
|
|
817
|
+
* May be `null` on Android when AuthorizationClient does not return an access token, or when
|
|
818
|
+
* `skipAuthOnlyAuthorizationAfterSignIn` is enabled during initialize (opt-in). Use `idToken`
|
|
819
|
+
* for user authentication; request additional Google API scopes when an access token is required.
|
|
804
820
|
*/
|
|
805
821
|
accessToken: AccessToken | null;
|
|
806
822
|
/**
|
|
@@ -1363,8 +1379,15 @@ export interface SocialLoginPlugin {
|
|
|
1363
1379
|
*/
|
|
1364
1380
|
getAuthorizationCode(options: AuthorizationCodeOptions): Promise<AuthorizationCode>;
|
|
1365
1381
|
/**
|
|
1366
|
-
* Refresh the access token
|
|
1367
|
-
*
|
|
1382
|
+
* Refresh the access token for the logged-in provider.
|
|
1383
|
+
*
|
|
1384
|
+
* **Google Android refresh behavior:**
|
|
1385
|
+
* On Android, when the saved ID token is still valid and either no access token is stored or Google access
|
|
1386
|
+
* token validation succeeds within the timeout, `refresh()` resolves without updating either token. That
|
|
1387
|
+
* early return applies regardless of `skipAuthOnlyAuthorizationAfterSignIn`.
|
|
1388
|
+
* If validation fails or times out, `refresh()` continues even when a token remains stored; with
|
|
1389
|
+
* `skipAuthOnlyAuthorizationAfterSignIn: true` and default OIDC scopes, a successful credential refresh
|
|
1390
|
+
* then clears that access token without obtaining a new one.
|
|
1368
1391
|
*
|
|
1369
1392
|
* **Google Offline Mode Limitation:**
|
|
1370
1393
|
* This method is NOT supported when Google is initialized with `mode: 'offline'`.
|
|
@@ -1372,6 +1395,14 @@ export interface SocialLoginPlugin {
|
|
|
1372
1395
|
* The plugin logs and rejects with a message explaining that you should send `serverAuthCode` to your backend,
|
|
1373
1396
|
* refresh the Google tokens there, or switch to `mode: 'online'` for client-side refresh.
|
|
1374
1397
|
*
|
|
1398
|
+
* **Google Android `skipAuthOnlyAuthorizationAfterSignIn`:**
|
|
1399
|
+
* When initialize sets `google.skipAuthOnlyAuthorizationAfterSignIn: true` and only default OIDC scopes are
|
|
1400
|
+
* requested, a refresh that runs past the early return skips `AuthorizationClient.authorize()`, updates the
|
|
1401
|
+
* saved `idToken`, and clears any saved access token; no new access token is obtained. Enabling the option
|
|
1402
|
+
* does not clear a stored access token while the early return applies (validation succeeded or no token
|
|
1403
|
+
* stored). With default `false`, refresh still skips authorize on the early return when no access token is
|
|
1404
|
+
* stored but the ID token is valid.
|
|
1405
|
+
*
|
|
1375
1406
|
* **Google Web Limitation:**
|
|
1376
1407
|
* On Web, Google `refresh()` is not implemented, even when using `mode: 'online'`.
|
|
1377
1408
|
* Call `login()` again on Web to obtain a fresh token instead.
|