@capgo/capacitor-social-login 8.3.39 → 8.4.0
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 +39 -36
- package/android/src/main/java/ee/forgr/capacitor/social/login/GoogleProvider.java +195 -101
- package/android/src/main/java/ee/forgr/capacitor/social/login/OAuth2Provider.java +266 -35
- package/android/src/main/java/ee/forgr/capacitor/social/login/SocialLoginPlugin.java +30 -1
- package/dist/docs.json +17 -1
- package/dist/esm/definitions.d.ts +25 -0
- package/dist/esm/definitions.js.map +1 -1
- package/ios/Sources/SocialLoginPlugin/AppleProvider.swift +11 -31
- package/ios/Sources/SocialLoginPlugin/SocialLoginPlugin.swift +28 -41
- package/package.json +1 -1
|
@@ -7,6 +7,7 @@ import android.content.SharedPreferences;
|
|
|
7
7
|
import android.net.Uri;
|
|
8
8
|
import android.util.Base64;
|
|
9
9
|
import android.util.Log;
|
|
10
|
+
import androidx.browser.customtabs.CustomTabsIntent;
|
|
10
11
|
import com.getcapacitor.JSArray;
|
|
11
12
|
import com.getcapacitor.JSObject;
|
|
12
13
|
import com.getcapacitor.PluginCall;
|
|
@@ -42,6 +43,12 @@ public class OAuth2Provider implements SocialProvider {
|
|
|
42
43
|
private static final String USER_CANCELLED_CODE = "USER_CANCELLED";
|
|
43
44
|
private static final String PREFS_NAME = "CapgoOAuth2ProviderPrefs";
|
|
44
45
|
private static final String PREFS_KEY_PREFIX = "OAuth2Tokens_";
|
|
46
|
+
private static final String CUSTOM_TABS_PENDING_IN_PROGRESS = "OAuth2CustomTabsInProgress";
|
|
47
|
+
private static final String CUSTOM_TABS_PENDING_PROVIDER_ID = "OAuth2CustomTabsProviderId";
|
|
48
|
+
private static final String CUSTOM_TABS_PENDING_STATE = "OAuth2CustomTabsState";
|
|
49
|
+
private static final String CUSTOM_TABS_PENDING_CODE_VERIFIER = "OAuth2CustomTabsCodeVerifier";
|
|
50
|
+
private static final String CUSTOM_TABS_PENDING_REDIRECT = "OAuth2CustomTabsRedirect";
|
|
51
|
+
private static final String CUSTOM_TABS_PENDING_SCOPE = "OAuth2CustomTabsScope";
|
|
45
52
|
|
|
46
53
|
public interface ActivityLauncher {
|
|
47
54
|
void launchForResult(Intent intent, int requestCode);
|
|
@@ -56,6 +63,7 @@ public class OAuth2Provider implements SocialProvider {
|
|
|
56
63
|
|
|
57
64
|
private PluginCall pendingCall;
|
|
58
65
|
private OAuth2PendingState pendingState;
|
|
66
|
+
private boolean pendingUseCustomTabs;
|
|
59
67
|
private ActivityLauncher activityLauncher;
|
|
60
68
|
|
|
61
69
|
public void setActivityLauncher(ActivityLauncher launcher) {
|
|
@@ -90,6 +98,7 @@ public class OAuth2Provider implements SocialProvider {
|
|
|
90
98
|
final String logoutUrl;
|
|
91
99
|
final String postLogoutRedirectUrl;
|
|
92
100
|
final Map<String, String> additionalLogoutParameters;
|
|
101
|
+
final boolean androidUseCustomTabs;
|
|
93
102
|
final boolean logsEnabled;
|
|
94
103
|
|
|
95
104
|
OAuth2ProviderConfig(
|
|
@@ -111,6 +120,7 @@ public class OAuth2Provider implements SocialProvider {
|
|
|
111
120
|
String logoutUrl,
|
|
112
121
|
String postLogoutRedirectUrl,
|
|
113
122
|
Map<String, String> additionalLogoutParameters,
|
|
123
|
+
boolean androidUseCustomTabs,
|
|
114
124
|
boolean logsEnabled
|
|
115
125
|
) {
|
|
116
126
|
this.appId = appId;
|
|
@@ -131,6 +141,7 @@ public class OAuth2Provider implements SocialProvider {
|
|
|
131
141
|
this.logoutUrl = logoutUrl;
|
|
132
142
|
this.postLogoutRedirectUrl = postLogoutRedirectUrl;
|
|
133
143
|
this.additionalLogoutParameters = additionalLogoutParameters;
|
|
144
|
+
this.androidUseCustomTabs = androidUseCustomTabs;
|
|
134
145
|
this.logsEnabled = logsEnabled;
|
|
135
146
|
}
|
|
136
147
|
}
|
|
@@ -241,6 +252,7 @@ public class OAuth2Provider implements SocialProvider {
|
|
|
241
252
|
(config.logoutUrl != null && !config.logoutUrl.isEmpty()) ? config.logoutUrl : endSession,
|
|
242
253
|
config.postLogoutRedirectUrl,
|
|
243
254
|
config.additionalLogoutParameters,
|
|
255
|
+
config.androidUseCustomTabs,
|
|
244
256
|
config.logsEnabled
|
|
245
257
|
);
|
|
246
258
|
providers.put(providerId, resolved);
|
|
@@ -334,6 +346,7 @@ public class OAuth2Provider implements SocialProvider {
|
|
|
334
346
|
config.has("logoutUrl") ? config.optString("logoutUrl", null) : config.optString("endSessionEndpoint", null),
|
|
335
347
|
config.optString("postLogoutRedirectUrl", null),
|
|
336
348
|
additionalLogoutParameters,
|
|
349
|
+
config.optBoolean("androidUseCustomTabs", false),
|
|
337
350
|
config.optBoolean("logsEnabled", false)
|
|
338
351
|
);
|
|
339
352
|
|
|
@@ -486,22 +499,24 @@ public class OAuth2Provider implements SocialProvider {
|
|
|
486
499
|
Log.d(LOG_TAG, "Opening authorization URL: " + builder.build().toString());
|
|
487
500
|
}
|
|
488
501
|
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
activity.runOnUiThread(() -> {
|
|
494
|
-
if (activityLauncher != null) {
|
|
495
|
-
activityLauncher.launchForResult(intent, REQUEST_CODE);
|
|
496
|
-
} else {
|
|
497
|
-
Log.w(
|
|
498
|
-
LOG_TAG,
|
|
499
|
-
"activityLauncher is null — falling back to raw startActivityForResult. " +
|
|
500
|
-
"Activity result routing through Capacitor will not work."
|
|
501
|
-
);
|
|
502
|
-
activity.startActivityForResult(intent, REQUEST_CODE);
|
|
502
|
+
pendingUseCustomTabs = resolved.androidUseCustomTabs;
|
|
503
|
+
if (resolved.androidUseCustomTabs) {
|
|
504
|
+
if (resolved.logsEnabled) {
|
|
505
|
+
Log.d(LOG_TAG, "Using Custom Tabs for OAuth2 authorization");
|
|
503
506
|
}
|
|
504
|
-
|
|
507
|
+
final String authUrl = builder.build().toString();
|
|
508
|
+
activity.runOnUiThread(() -> {
|
|
509
|
+
if (!launchCustomTabs(authUrl)) {
|
|
510
|
+
// No Custom Tabs browser — fall back to embedded WebView
|
|
511
|
+
pendingUseCustomTabs = false;
|
|
512
|
+
clearPersistedCustomTabsState();
|
|
513
|
+
launchWebViewActivity(authUrl, finalRedirect);
|
|
514
|
+
}
|
|
515
|
+
});
|
|
516
|
+
return;
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
activity.runOnUiThread(() -> launchWebViewActivity(builder.build().toString(), finalRedirect));
|
|
505
520
|
}
|
|
506
521
|
|
|
507
522
|
@Override
|
|
@@ -684,45 +699,257 @@ public class OAuth2Provider implements SocialProvider {
|
|
|
684
699
|
return true;
|
|
685
700
|
}
|
|
686
701
|
|
|
687
|
-
|
|
702
|
+
return processCallbackData(data);
|
|
703
|
+
}
|
|
704
|
+
|
|
705
|
+
/**
|
|
706
|
+
* Handle OAuth redirect delivered via deep link / App Link after a Custom Tabs auth session.
|
|
707
|
+
* @return true if this URI was consumed as an OAuth2 Custom Tabs callback
|
|
708
|
+
*/
|
|
709
|
+
public boolean handleRedirectUri(Uri uri) {
|
|
710
|
+
if (uri == null) {
|
|
711
|
+
return false;
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
// After process death, restore PKCE/state so we can still complete the exchange.
|
|
715
|
+
if (pendingState == null) {
|
|
716
|
+
restorePersistedCustomTabsState();
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
if (pendingState == null || !pendingUseCustomTabs) {
|
|
720
|
+
return false;
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
if (!matchesRedirectUri(pendingState.redirectUri, uri)) {
|
|
724
|
+
return false;
|
|
725
|
+
}
|
|
726
|
+
|
|
727
|
+
Intent data = callbackUriToIntent(uri);
|
|
728
|
+
|
|
729
|
+
// Prevent handleOnResume from treating the return as cancellation while token exchange runs
|
|
730
|
+
pendingUseCustomTabs = false;
|
|
731
|
+
clearPersistedCustomTabsState();
|
|
732
|
+
return processCallbackData(data);
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
/**
|
|
736
|
+
* Called when the host activity resumes after Custom Tabs without receiving a redirect.
|
|
737
|
+
* Treat as user cancellation (same pattern as {@code openSecureWindow}).
|
|
738
|
+
*/
|
|
739
|
+
public void handleUserReturnedWithoutCallback() {
|
|
740
|
+
if (!pendingUseCustomTabs) {
|
|
741
|
+
return;
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
// Prefer consuming a redirect already attached to the activity intent (cold start / recreate)
|
|
745
|
+
Intent current = activity != null ? activity.getIntent() : null;
|
|
746
|
+
if (current != null && Intent.ACTION_VIEW.equals(current.getAction()) && current.getData() != null) {
|
|
747
|
+
if (handleRedirectUri(current.getData())) {
|
|
748
|
+
return;
|
|
749
|
+
}
|
|
750
|
+
}
|
|
751
|
+
|
|
752
|
+
if (pendingCall == null) {
|
|
753
|
+
clearPersistedCustomTabsState();
|
|
754
|
+
pendingUseCustomTabs = false;
|
|
755
|
+
pendingState = null;
|
|
756
|
+
return;
|
|
757
|
+
}
|
|
758
|
+
pendingCall.reject("User cancelled", USER_CANCELLED_CODE);
|
|
759
|
+
cleanupPending();
|
|
760
|
+
}
|
|
761
|
+
|
|
762
|
+
/**
|
|
763
|
+
* Restore any persisted Custom Tabs login state (e.g. after process death) and return whether
|
|
764
|
+
* a Custom Tabs session was in progress.
|
|
765
|
+
*/
|
|
766
|
+
public boolean restorePersistedCustomTabsState() {
|
|
767
|
+
SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
|
|
768
|
+
String providerId = prefs.getString(CUSTOM_TABS_PENDING_PROVIDER_ID, null);
|
|
769
|
+
String state = prefs.getString(CUSTOM_TABS_PENDING_STATE, null);
|
|
770
|
+
String codeVerifier = prefs.getString(CUSTOM_TABS_PENDING_CODE_VERIFIER, null);
|
|
771
|
+
String redirectUri = prefs.getString(CUSTOM_TABS_PENDING_REDIRECT, null);
|
|
772
|
+
String scope = prefs.getString(CUSTOM_TABS_PENDING_SCOPE, null);
|
|
773
|
+
boolean inProgress = prefs.getBoolean(CUSTOM_TABS_PENDING_IN_PROGRESS, false);
|
|
774
|
+
if (!inProgress || providerId == null || state == null || redirectUri == null) {
|
|
775
|
+
return false;
|
|
776
|
+
}
|
|
777
|
+
pendingState = new OAuth2PendingState(providerId, state, codeVerifier, redirectUri, scope != null ? scope : "");
|
|
778
|
+
pendingUseCustomTabs = true;
|
|
779
|
+
return true;
|
|
780
|
+
}
|
|
781
|
+
|
|
782
|
+
private boolean processCallbackData(Intent data) {
|
|
783
|
+
if (pendingState == null) {
|
|
784
|
+
return true;
|
|
785
|
+
}
|
|
786
|
+
|
|
787
|
+
String returnedState = data != null ? data.getStringExtra("state") : null;
|
|
688
788
|
if (returnedState == null || !returnedState.equals(pendingState.state)) {
|
|
689
|
-
pendingCall
|
|
789
|
+
if (pendingCall != null) {
|
|
790
|
+
pendingCall.reject("State mismatch during OAuth2 login");
|
|
791
|
+
}
|
|
690
792
|
cleanupPending();
|
|
691
793
|
return true;
|
|
692
794
|
}
|
|
693
795
|
|
|
694
|
-
String error = data.getStringExtra("error");
|
|
796
|
+
String error = data != null ? data.getStringExtra("error") : null;
|
|
695
797
|
if (error != null) {
|
|
696
798
|
String description = data.getStringExtra("error_description");
|
|
697
799
|
String message = description != null ? description : error;
|
|
698
|
-
if (
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
800
|
+
if (pendingCall != null) {
|
|
801
|
+
if (isUserDeniedRedirect(error, description)) {
|
|
802
|
+
pendingCall.reject(message, USER_CANCELLED_CODE);
|
|
803
|
+
} else {
|
|
804
|
+
pendingCall.reject(message);
|
|
805
|
+
}
|
|
702
806
|
}
|
|
703
807
|
cleanupPending();
|
|
704
808
|
return true;
|
|
705
809
|
}
|
|
706
810
|
|
|
707
811
|
// Check for code (authorization code flow)
|
|
708
|
-
String code = data.getStringExtra("code");
|
|
812
|
+
String code = data != null ? data.getStringExtra("code") : null;
|
|
709
813
|
if (code != null) {
|
|
814
|
+
if (pendingCall == null) {
|
|
815
|
+
// Process death: original JS call is gone — still exchange & store tokens.
|
|
816
|
+
Log.w(LOG_TAG, "Completing OAuth2 Custom Tabs callback without pending PluginCall (process death?)");
|
|
817
|
+
}
|
|
710
818
|
exchangeAuthorizationCode(code);
|
|
711
819
|
return true;
|
|
712
820
|
}
|
|
713
821
|
|
|
714
822
|
// Check for access_token (implicit flow)
|
|
715
|
-
String accessToken = data.getStringExtra("access_token");
|
|
823
|
+
String accessToken = data != null ? data.getStringExtra("access_token") : null;
|
|
716
824
|
if (accessToken != null) {
|
|
825
|
+
if (pendingCall == null) {
|
|
826
|
+
Log.w(LOG_TAG, "Completing OAuth2 Custom Tabs implicit callback without pending PluginCall (process death?)");
|
|
827
|
+
}
|
|
717
828
|
handleImplicitFlowResponse(data);
|
|
718
829
|
return true;
|
|
719
830
|
}
|
|
720
831
|
|
|
721
|
-
pendingCall
|
|
832
|
+
if (pendingCall != null) {
|
|
833
|
+
pendingCall.reject("No authorization code or access token in callback");
|
|
834
|
+
}
|
|
722
835
|
cleanupPending();
|
|
723
836
|
return true;
|
|
724
837
|
}
|
|
725
838
|
|
|
839
|
+
private void launchWebViewActivity(String authUrl, String redirectUrl) {
|
|
840
|
+
Intent intent = new Intent(activity, OAuth2LoginActivity.class);
|
|
841
|
+
intent.putExtra(OAuth2LoginActivity.EXTRA_AUTH_URL, authUrl);
|
|
842
|
+
intent.putExtra(OAuth2LoginActivity.EXTRA_REDIRECT_URL, redirectUrl);
|
|
843
|
+
|
|
844
|
+
if (activityLauncher != null) {
|
|
845
|
+
activityLauncher.launchForResult(intent, REQUEST_CODE);
|
|
846
|
+
} else {
|
|
847
|
+
Log.w(
|
|
848
|
+
LOG_TAG,
|
|
849
|
+
"activityLauncher is null — falling back to raw startActivityForResult. " +
|
|
850
|
+
"Activity result routing through Capacitor will not work."
|
|
851
|
+
);
|
|
852
|
+
activity.startActivityForResult(intent, REQUEST_CODE);
|
|
853
|
+
}
|
|
854
|
+
}
|
|
855
|
+
|
|
856
|
+
/**
|
|
857
|
+
* @return true if Custom Tabs launched successfully
|
|
858
|
+
*/
|
|
859
|
+
private boolean launchCustomTabs(String url) {
|
|
860
|
+
try {
|
|
861
|
+
persistCustomTabsState();
|
|
862
|
+
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
|
|
863
|
+
CustomTabsIntent customTabsIntent = builder.build();
|
|
864
|
+
customTabsIntent.intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
|
|
865
|
+
customTabsIntent.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
|
866
|
+
customTabsIntent.launchUrl(activity, Uri.parse(url));
|
|
867
|
+
return true;
|
|
868
|
+
} catch (android.content.ActivityNotFoundException e) {
|
|
869
|
+
Log.w(LOG_TAG, "Custom Tabs unavailable, falling back to WebView", e);
|
|
870
|
+
clearPersistedCustomTabsState();
|
|
871
|
+
return false;
|
|
872
|
+
}
|
|
873
|
+
}
|
|
874
|
+
|
|
875
|
+
private void persistCustomTabsState() {
|
|
876
|
+
if (pendingState == null) {
|
|
877
|
+
return;
|
|
878
|
+
}
|
|
879
|
+
context
|
|
880
|
+
.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
|
|
881
|
+
.edit()
|
|
882
|
+
.putBoolean(CUSTOM_TABS_PENDING_IN_PROGRESS, true)
|
|
883
|
+
.putString(CUSTOM_TABS_PENDING_PROVIDER_ID, pendingState.providerId)
|
|
884
|
+
.putString(CUSTOM_TABS_PENDING_STATE, pendingState.state)
|
|
885
|
+
.putString(CUSTOM_TABS_PENDING_CODE_VERIFIER, pendingState.codeVerifier)
|
|
886
|
+
.putString(CUSTOM_TABS_PENDING_REDIRECT, pendingState.redirectUri)
|
|
887
|
+
.putString(CUSTOM_TABS_PENDING_SCOPE, pendingState.scope)
|
|
888
|
+
.apply();
|
|
889
|
+
}
|
|
890
|
+
|
|
891
|
+
private void clearPersistedCustomTabsState() {
|
|
892
|
+
context
|
|
893
|
+
.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
|
|
894
|
+
.edit()
|
|
895
|
+
.remove(CUSTOM_TABS_PENDING_IN_PROGRESS)
|
|
896
|
+
.remove(CUSTOM_TABS_PENDING_PROVIDER_ID)
|
|
897
|
+
.remove(CUSTOM_TABS_PENDING_STATE)
|
|
898
|
+
.remove(CUSTOM_TABS_PENDING_CODE_VERIFIER)
|
|
899
|
+
.remove(CUSTOM_TABS_PENDING_REDIRECT)
|
|
900
|
+
.remove(CUSTOM_TABS_PENDING_SCOPE)
|
|
901
|
+
.apply();
|
|
902
|
+
}
|
|
903
|
+
|
|
904
|
+
static boolean matchesRedirectUri(String expectedRedirect, Uri actual) {
|
|
905
|
+
if (expectedRedirect == null || expectedRedirect.isEmpty() || actual == null) {
|
|
906
|
+
return false;
|
|
907
|
+
}
|
|
908
|
+
Uri expected = Uri.parse(expectedRedirect);
|
|
909
|
+
if (expected.getScheme() == null || actual.getScheme() == null) {
|
|
910
|
+
return false;
|
|
911
|
+
}
|
|
912
|
+
if (!expected.getScheme().equalsIgnoreCase(actual.getScheme())) {
|
|
913
|
+
return false;
|
|
914
|
+
}
|
|
915
|
+
String expectedAuthority = expected.getAuthority();
|
|
916
|
+
String actualAuthority = actual.getAuthority();
|
|
917
|
+
if (expectedAuthority == null ? actualAuthority != null : !expectedAuthority.equalsIgnoreCase(actualAuthority)) {
|
|
918
|
+
return false;
|
|
919
|
+
}
|
|
920
|
+
String expectedPath = normalizePath(expected.getPath());
|
|
921
|
+
String actualPath = normalizePath(actual.getPath());
|
|
922
|
+
return expectedPath.equals(actualPath);
|
|
923
|
+
}
|
|
924
|
+
|
|
925
|
+
private static String normalizePath(String path) {
|
|
926
|
+
if (path == null || path.isEmpty()) {
|
|
927
|
+
return "";
|
|
928
|
+
}
|
|
929
|
+
if (path.length() > 1 && path.endsWith("/")) {
|
|
930
|
+
return path.substring(0, path.length() - 1);
|
|
931
|
+
}
|
|
932
|
+
return path;
|
|
933
|
+
}
|
|
934
|
+
|
|
935
|
+
static Intent callbackUriToIntent(Uri uri) {
|
|
936
|
+
Intent data = new Intent();
|
|
937
|
+
data.putExtra("code", uri.getQueryParameter("code"));
|
|
938
|
+
data.putExtra("state", uri.getQueryParameter("state"));
|
|
939
|
+
data.putExtra("error", uri.getQueryParameter("error"));
|
|
940
|
+
data.putExtra("error_description", uri.getQueryParameter("error_description"));
|
|
941
|
+
|
|
942
|
+
// Use encoded fragment so values containing encoded '&' (%26) are not split incorrectly
|
|
943
|
+
String encodedFragment = uri.getEncodedFragment();
|
|
944
|
+
if (encodedFragment != null && !encodedFragment.isEmpty()) {
|
|
945
|
+
Uri fragmentUri = Uri.parse("https://callback.invalid/?" + encodedFragment);
|
|
946
|
+
for (String key : fragmentUri.getQueryParameterNames()) {
|
|
947
|
+
data.putExtra(key, fragmentUri.getQueryParameter(key));
|
|
948
|
+
}
|
|
949
|
+
}
|
|
950
|
+
return data;
|
|
951
|
+
}
|
|
952
|
+
|
|
726
953
|
private boolean isUserDeniedRedirect(String error, String description) {
|
|
727
954
|
if ("access_denied".equalsIgnoreCase(error)) {
|
|
728
955
|
return true;
|
|
@@ -795,7 +1022,9 @@ public class OAuth2Provider implements SocialProvider {
|
|
|
795
1022
|
OAuth2ProviderConfig config = getProvider(providerId);
|
|
796
1023
|
|
|
797
1024
|
if (config == null) {
|
|
798
|
-
pendingCall
|
|
1025
|
+
if (pendingCall != null) {
|
|
1026
|
+
pendingCall.reject("OAuth2 provider '" + providerId + "' not found");
|
|
1027
|
+
}
|
|
799
1028
|
cleanupPending();
|
|
800
1029
|
return;
|
|
801
1030
|
}
|
|
@@ -809,7 +1038,9 @@ public class OAuth2Provider implements SocialProvider {
|
|
|
809
1038
|
@Override
|
|
810
1039
|
public void onSuccess(OAuth2ProviderConfig resolved) {
|
|
811
1040
|
if (resolved.accessTokenEndpoint == null || resolved.accessTokenEndpoint.isEmpty()) {
|
|
812
|
-
pendingCall
|
|
1041
|
+
if (pendingCall != null) {
|
|
1042
|
+
pendingCall.reject("No accessTokenEndpoint configured for code exchange");
|
|
1043
|
+
}
|
|
813
1044
|
cleanupPending();
|
|
814
1045
|
return;
|
|
815
1046
|
}
|
|
@@ -818,7 +1049,9 @@ public class OAuth2Provider implements SocialProvider {
|
|
|
818
1049
|
|
|
819
1050
|
@Override
|
|
820
1051
|
public void onError(String message) {
|
|
821
|
-
pendingCall
|
|
1052
|
+
if (pendingCall != null) {
|
|
1053
|
+
pendingCall.reject(message);
|
|
1054
|
+
}
|
|
822
1055
|
cleanupPending();
|
|
823
1056
|
}
|
|
824
1057
|
}
|
|
@@ -830,7 +1063,7 @@ public class OAuth2Provider implements SocialProvider {
|
|
|
830
1063
|
}
|
|
831
1064
|
|
|
832
1065
|
private void exchangeAuthorizationCodeWithConfig(String code, OAuth2ProviderConfig config) {
|
|
833
|
-
if (pendingState == null
|
|
1066
|
+
if (pendingState == null) {
|
|
834
1067
|
cleanupPending();
|
|
835
1068
|
return;
|
|
836
1069
|
}
|
|
@@ -1001,10 +1234,6 @@ public class OAuth2Provider implements SocialProvider {
|
|
|
1001
1234
|
String fallbackRefreshToken,
|
|
1002
1235
|
boolean wrapResponse
|
|
1003
1236
|
) throws JSONException {
|
|
1004
|
-
if (call == null) {
|
|
1005
|
-
return;
|
|
1006
|
-
}
|
|
1007
|
-
|
|
1008
1237
|
final String accessToken = tokenPayload.getString("access_token");
|
|
1009
1238
|
final String tokenType = tokenPayload.optString("token_type", "bearer");
|
|
1010
1239
|
final int expiresIn = tokenPayload.optInt("expires_in", 3600);
|
|
@@ -1117,12 +1346,12 @@ public class OAuth2Provider implements SocialProvider {
|
|
|
1117
1346
|
PluginCall call,
|
|
1118
1347
|
boolean wrapResponse
|
|
1119
1348
|
) {
|
|
1349
|
+
persistTokens(providerId, accessToken, refreshToken, idToken, tokenType, expiresAt, scopes);
|
|
1350
|
+
|
|
1120
1351
|
if (call == null) {
|
|
1121
1352
|
return;
|
|
1122
1353
|
}
|
|
1123
1354
|
|
|
1124
|
-
persistTokens(providerId, accessToken, refreshToken, idToken, tokenType, expiresAt, scopes);
|
|
1125
|
-
|
|
1126
1355
|
JSObject accessTokenObject = new JSObject();
|
|
1127
1356
|
accessTokenObject.put("token", accessToken);
|
|
1128
1357
|
accessTokenObject.put("tokenType", tokenType);
|
|
@@ -1248,6 +1477,8 @@ public class OAuth2Provider implements SocialProvider {
|
|
|
1248
1477
|
private void cleanupPending() {
|
|
1249
1478
|
pendingCall = null;
|
|
1250
1479
|
pendingState = null;
|
|
1480
|
+
pendingUseCustomTabs = false;
|
|
1481
|
+
clearPersistedCustomTabsState();
|
|
1251
1482
|
}
|
|
1252
1483
|
|
|
1253
1484
|
private static Map<String, String> jsonObjectToMap(JSONObject json) throws JSONException {
|
|
@@ -24,13 +24,14 @@ import org.json.JSONObject;
|
|
|
24
24
|
@CapacitorPlugin(name = "SocialLogin")
|
|
25
25
|
public class SocialLoginPlugin extends Plugin {
|
|
26
26
|
|
|
27
|
-
private final String pluginVersion = "8.
|
|
27
|
+
private final String pluginVersion = "8.4.0";
|
|
28
28
|
|
|
29
29
|
public static String LOG_TAG = "CapgoSocialLogin";
|
|
30
30
|
public HashMap<String, SocialProvider> socialProviderHashMap = new HashMap<>();
|
|
31
31
|
|
|
32
32
|
private PluginCall openSecureWindowSavedCall;
|
|
33
33
|
private String openSecureWindowRedirectUri;
|
|
34
|
+
private Uri pendingOAuth2RedirectUri;
|
|
34
35
|
|
|
35
36
|
@PluginMethod
|
|
36
37
|
public void initialize(PluginCall call) {
|
|
@@ -183,6 +184,19 @@ public class SocialLoginPlugin extends Plugin {
|
|
|
183
184
|
Log.e(LOG_TAG, "OAuth2 activityLauncher fired but pendingCall is null, cannot route result");
|
|
184
185
|
}
|
|
185
186
|
});
|
|
187
|
+
// Replay a redirect that arrived before initialize (cold start / process death)
|
|
188
|
+
boolean handledRedirect = false;
|
|
189
|
+
if (pendingOAuth2RedirectUri != null) {
|
|
190
|
+
Uri buffered = pendingOAuth2RedirectUri;
|
|
191
|
+
pendingOAuth2RedirectUri = null;
|
|
192
|
+
handledRedirect = oauth2Provider.handleRedirectUri(buffered);
|
|
193
|
+
}
|
|
194
|
+
// If handleOnResume ran before initialize, persisted Custom Tabs state may still be
|
|
195
|
+
// present with no buffered redirect — restore then cancel/clear the orphaned session
|
|
196
|
+
// (or complete via activity.getIntent() if the redirect is attached there).
|
|
197
|
+
if (!handledRedirect && oauth2Provider.restorePersistedCustomTabsState()) {
|
|
198
|
+
oauth2Provider.handleUserReturnedWithoutCallback();
|
|
199
|
+
}
|
|
186
200
|
} catch (JSONException e) {
|
|
187
201
|
call.reject("Failed to initialize OAuth2 provider: " + e.getMessage());
|
|
188
202
|
return;
|
|
@@ -597,6 +611,11 @@ public class SocialLoginPlugin extends Plugin {
|
|
|
597
611
|
protected void handleOnResume() {
|
|
598
612
|
super.handleOnResume();
|
|
599
613
|
|
|
614
|
+
SocialProvider oauth2Provider = socialProviderHashMap.get("oauth2");
|
|
615
|
+
if (oauth2Provider instanceof OAuth2Provider) {
|
|
616
|
+
((OAuth2Provider) oauth2Provider).handleUserReturnedWithoutCallback();
|
|
617
|
+
}
|
|
618
|
+
|
|
600
619
|
// If we have a saved call and user returned without callback, reject
|
|
601
620
|
if (openSecureWindowSavedCall != null) {
|
|
602
621
|
openSecureWindowSavedCall.reject("OAuth cancelled or no callback received");
|
|
@@ -617,6 +636,16 @@ public class SocialLoginPlugin extends Plugin {
|
|
|
617
636
|
return;
|
|
618
637
|
}
|
|
619
638
|
|
|
639
|
+
SocialProvider oauth2Provider = socialProviderHashMap.get("oauth2");
|
|
640
|
+
if (oauth2Provider instanceof OAuth2Provider) {
|
|
641
|
+
if (((OAuth2Provider) oauth2Provider).handleRedirectUri(uri)) {
|
|
642
|
+
return;
|
|
643
|
+
}
|
|
644
|
+
} else {
|
|
645
|
+
// Buffer until SocialLogin.initialize() registers the oauth2 provider
|
|
646
|
+
pendingOAuth2RedirectUri = uri;
|
|
647
|
+
}
|
|
648
|
+
|
|
620
649
|
if (openSecureWindowRedirectUri == null) {
|
|
621
650
|
return;
|
|
622
651
|
}
|
package/dist/docs.json
CHANGED
|
@@ -678,6 +678,22 @@
|
|
|
678
678
|
"complexTypes": [],
|
|
679
679
|
"type": "boolean | undefined"
|
|
680
680
|
},
|
|
681
|
+
{
|
|
682
|
+
"name": "androidUseCustomTabs",
|
|
683
|
+
"tags": [
|
|
684
|
+
{
|
|
685
|
+
"text": "false",
|
|
686
|
+
"name": "default"
|
|
687
|
+
},
|
|
688
|
+
{
|
|
689
|
+
"text": "true",
|
|
690
|
+
"name": "example"
|
|
691
|
+
}
|
|
692
|
+
],
|
|
693
|
+
"docs": "Android-only: Use Chrome Custom Tabs (system browser) instead of an embedded WebView\nfor the authorization request.\n\nCustom Tabs follow RFC 8252 (OAuth 2.0 for Native Apps) and fix issues with brokered\nIdPs (Microsoft Entra Conditional Access / Authenticator, passkeys/WebAuthn, Google\n`disallowed_useragent`, SSO cookie sharing, password managers).\n\nRequires a custom-scheme or App Link `redirectUrl` with a matching intent filter in\nyour app's `AndroidManifest.xml` (same setup as `openSecureWindow()` / Apple on Android).\n\nDefaults to `false` to preserve the historical WebView flow.",
|
|
694
|
+
"complexTypes": [],
|
|
695
|
+
"type": "boolean | undefined"
|
|
696
|
+
},
|
|
681
697
|
{
|
|
682
698
|
"name": "logsEnabled",
|
|
683
699
|
"tags": [
|
|
@@ -830,7 +846,7 @@
|
|
|
830
846
|
{
|
|
831
847
|
"name": "accessToken",
|
|
832
848
|
"tags": [],
|
|
833
|
-
"docs": "",
|
|
849
|
+
"docs": "OAuth access token for Google APIs.\n\nMay be `null` on Android when Credential Manager authentication succeeds but\nAuthorizationClient does not return an access token (authentication-only /\nID-token login with default OIDC scopes). Use `idToken` for user authentication;\nrequest additional Google API scopes when an access token is required.",
|
|
834
850
|
"complexTypes": [
|
|
835
851
|
"AccessToken"
|
|
836
852
|
],
|
|
@@ -141,6 +141,23 @@ export interface OAuth2ProviderConfig {
|
|
|
141
141
|
* Alias for `iosPrefersEphemeralWebBrowserSession` (to match Capawesome OAuth naming).
|
|
142
142
|
*/
|
|
143
143
|
iosPrefersEphemeralSession?: boolean;
|
|
144
|
+
/**
|
|
145
|
+
* Android-only: Use Chrome Custom Tabs (system browser) instead of an embedded WebView
|
|
146
|
+
* for the authorization request.
|
|
147
|
+
*
|
|
148
|
+
* Custom Tabs follow RFC 8252 (OAuth 2.0 for Native Apps) and fix issues with brokered
|
|
149
|
+
* IdPs (Microsoft Entra Conditional Access / Authenticator, passkeys/WebAuthn, Google
|
|
150
|
+
* `disallowed_useragent`, SSO cookie sharing, password managers).
|
|
151
|
+
*
|
|
152
|
+
* Requires a custom-scheme or App Link `redirectUrl` with a matching intent filter in
|
|
153
|
+
* your app's `AndroidManifest.xml` (same setup as `openSecureWindow()` / Apple on Android).
|
|
154
|
+
*
|
|
155
|
+
* Defaults to `false` to preserve the historical WebView flow.
|
|
156
|
+
*
|
|
157
|
+
* @default false
|
|
158
|
+
* @example true
|
|
159
|
+
*/
|
|
160
|
+
androidUseCustomTabs?: boolean;
|
|
144
161
|
/**
|
|
145
162
|
* Enable debug logging
|
|
146
163
|
* @default false
|
|
@@ -572,6 +589,14 @@ export interface GoogleLoginOptions {
|
|
|
572
589
|
prompt?: 'none' | 'consent' | 'select_account' | 'consent select_account' | 'select_account consent';
|
|
573
590
|
}
|
|
574
591
|
export interface GoogleLoginResponseOnline {
|
|
592
|
+
/**
|
|
593
|
+
* OAuth access token for Google APIs.
|
|
594
|
+
*
|
|
595
|
+
* May be `null` on Android when Credential Manager authentication succeeds but
|
|
596
|
+
* AuthorizationClient does not return an access token (authentication-only /
|
|
597
|
+
* ID-token login with default OIDC scopes). Use `idToken` for user authentication;
|
|
598
|
+
* request additional Google API scopes when an access token is required.
|
|
599
|
+
*/
|
|
575
600
|
accessToken: AccessToken | null;
|
|
576
601
|
/**
|
|
577
602
|
* OpenID Connect ID token (JWT).
|