@capgo/capacitor-social-login 8.5.18 → 8.6.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 +105 -0
- package/android/src/main/java/ee/forgr/capacitor/social/login/OAuth2Provider.java +36 -14
- package/android/src/main/java/ee/forgr/capacitor/social/login/SocialLoginPlugin.java +231 -4
- package/dist/docs.json +92 -0
- package/dist/esm/definitions.d.ts +45 -0
- package/dist/esm/definitions.js.map +1 -1
- package/dist/esm/social-login.d.ts +2 -1
- package/dist/esm/social-login.js +3 -0
- package/dist/esm/social-login.js.map +1 -1
- package/dist/esm/web-auth-popup-close.d.ts +9 -0
- package/dist/esm/web-auth-popup-close.js +15 -0
- package/dist/esm/web-auth-popup-close.js.map +1 -0
- package/dist/esm/web.d.ts +4 -1
- package/dist/esm/web.js +139 -25
- package/dist/esm/web.js.map +1 -1
- package/dist/plugin.cjs.js +155 -24
- package/dist/plugin.cjs.js.map +1 -1
- package/dist/plugin.js +155 -24
- package/dist/plugin.js.map +1 -1
- package/ios/Sources/SocialLoginPlugin/OAuth2Provider.swift +1 -1
- package/ios/Sources/SocialLoginPlugin/PresentationWindowResolver.swift +1 -1
- package/ios/Sources/SocialLoginPlugin/SocialLoginPlugin.swift +88 -18
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -835,6 +835,67 @@ await SocialLogin.refresh({
|
|
|
835
835
|
3. **Store tokens securely** using [@capgo/capacitor-persistent-account](https://github.com/Cap-go/capacitor-persistent-account)
|
|
836
836
|
4. **Use HTTPS** for all endpoints and redirect URLs in production
|
|
837
837
|
|
|
838
|
+
## Generic OAuth / OIDC auth session (SSO)
|
|
839
|
+
|
|
840
|
+
Use `openAuthSession()` when your app already has an authorize URL (from your IdP, OutSystems, Keycloak, Azure AD, etc.) and you only need a **system browser session** that returns the **full redirect callback URL**. You keep token exchange and session management in your own code.
|
|
841
|
+
|
|
842
|
+
**Use case:** Enterprise SSO or OIDC where the login page URL is built server-side or with your SDK, and mobile must reuse the platform secure browser (Custom Tabs / `ASWebAuthenticationSession`) instead of an embedded WebView.
|
|
843
|
+
|
|
844
|
+
### Quick start
|
|
845
|
+
|
|
846
|
+
```typescript
|
|
847
|
+
import { SocialLogin } from '@capgo/capacitor-social-login';
|
|
848
|
+
|
|
849
|
+
const authorizeUrl =
|
|
850
|
+
'https://login.example.com/oauth2/authorize?client_id=APP&redirect_uri=myapp%3A%2F%2Fauth%2Fcallback&response_type=code&state=xyz';
|
|
851
|
+
|
|
852
|
+
try {
|
|
853
|
+
const { callbackURL } = await SocialLogin.openAuthSession({
|
|
854
|
+
url: authorizeUrl,
|
|
855
|
+
callbackURLScheme: 'myapp',
|
|
856
|
+
prefersEphemeralSession: false, // iOS: allow shared SSO cookies when false
|
|
857
|
+
});
|
|
858
|
+
// callbackURL e.g. myapp://auth/callback?code=...&state=...
|
|
859
|
+
const parsed = new URL(callbackURL.replace('myapp://', 'https://placeholder/'));
|
|
860
|
+
const code = parsed.searchParams.get('code');
|
|
861
|
+
} catch (error) {
|
|
862
|
+
if ((error as { code?: string }).code === 'USER_CANCELLED') {
|
|
863
|
+
// user closed the browser tab or cancelled the sheet
|
|
864
|
+
}
|
|
865
|
+
}
|
|
866
|
+
```
|
|
867
|
+
|
|
868
|
+
On **web**, host a small redirect page that posts the final URL to a `BroadcastChannel` (same pattern as `openSecureWindow()`). Default channel name for `openAuthSession` is `capgo-auth-session`.
|
|
869
|
+
|
|
870
|
+
### Platform setup
|
|
871
|
+
|
|
872
|
+
**iOS:** Register your callback URL scheme in `Info.plist` (`CFBundleURLTypes` / `CFBundleURLSchemes`). `callbackURLScheme` must match that scheme (without `://`).
|
|
873
|
+
|
|
874
|
+
**Android:** Add an intent filter on your main activity for the redirect URI scheme and host, and use `launchMode` `singleTask` or `singleTop` so the redirect arrives in `onNewIntent`:
|
|
875
|
+
|
|
876
|
+
```xml
|
|
877
|
+
<intent-filter>
|
|
878
|
+
<action android:name="android.intent.action.VIEW" />
|
|
879
|
+
<category android:name="android.intent.category.DEFAULT" />
|
|
880
|
+
<category android:name="android.intent.category.BROWSABLE" />
|
|
881
|
+
<data android:scheme="myapp" android:host="auth" android:pathPrefix="/callback" />
|
|
882
|
+
</intent-filter>
|
|
883
|
+
```
|
|
884
|
+
|
|
885
|
+
On **web**, the plugin opens the authorize URL in a **popup** via `window.open`. Call `openAuthSession()` from a **user gesture** (button click). Popup blockers must allow your origin, or the call fails immediately with an error about popups.
|
|
886
|
+
|
|
887
|
+
Host an HTTPS redirect page on the **same origin** as your app (BroadcastChannel only delivers to pages that share the app's origin). Register that URL with your IdP. The landing page can post `location.href` when the authorize URL includes the same `redirect_uri`, or post a `${callbackURLScheme}://...` string for prefix matching:
|
|
888
|
+
|
|
889
|
+
```html
|
|
890
|
+
<script>
|
|
891
|
+
// Option A: post the HTTPS callback (works when redirect_uri is in the authorize URL)
|
|
892
|
+
new BroadcastChannel('capgo-auth-session').postMessage(location.href);
|
|
893
|
+
window.close();
|
|
894
|
+
</script>
|
|
895
|
+
```
|
|
896
|
+
|
|
897
|
+
See also [`openSecureWindow()`](#opensecurewindow) for the legacy option names (`authEndpoint`, `redirectUri`, `redirectedUri`).
|
|
898
|
+
|
|
838
899
|
## Troubleshooting
|
|
839
900
|
|
|
840
901
|
|
|
@@ -957,6 +1018,7 @@ For iOS, it will store data in the Keychain, which is Apple's secure credential
|
|
|
957
1018
|
* [`providerSpecificCall(...)`](#providerspecificcall)
|
|
958
1019
|
* [`getPluginVersion()`](#getpluginversion)
|
|
959
1020
|
* [`openSecureWindow(...)`](#opensecurewindow)
|
|
1021
|
+
* [`openAuthSession(...)`](#openauthsession)
|
|
960
1022
|
* [Interfaces](#interfaces)
|
|
961
1023
|
* [Type Aliases](#type-aliases)
|
|
962
1024
|
|
|
@@ -1300,6 +1362,32 @@ And in the AndroidManifest.xml file:
|
|
|
1300
1362
|
--------------------
|
|
1301
1363
|
|
|
1302
1364
|
|
|
1365
|
+
### openAuthSession(...)
|
|
1366
|
+
|
|
1367
|
+
```typescript
|
|
1368
|
+
openAuthSession(options: OpenAuthSessionOptions) => Promise<OpenAuthSessionResult>
|
|
1369
|
+
```
|
|
1370
|
+
|
|
1371
|
+
Opens a system auth session for generic OAuth/OIDC or enterprise SSO.
|
|
1372
|
+
|
|
1373
|
+
Use this when you build the authorize URL yourself (or your IdP SDK returns one) and only need
|
|
1374
|
+
the plugin to run the secure browser flow and return the redirect callback URL. Exchange the
|
|
1375
|
+
authorization code or tokens on your backend or with your own client logic.
|
|
1376
|
+
|
|
1377
|
+
**iOS/Android:** system browser session (`ASWebAuthenticationSession` / Chrome Custom Tabs).
|
|
1378
|
+
**Web:** popup window plus `BroadcastChannel` (not a native browser session).
|
|
1379
|
+
|
|
1380
|
+
On user cancel or dismiss, the Promise rejects with `code === 'USER_CANCELLED'`.
|
|
1381
|
+
|
|
1382
|
+
| Param | Type | Description |
|
|
1383
|
+
| ------------- | ------------------------------------------------------------------------- | ---------------------- |
|
|
1384
|
+
| **`options`** | <code><a href="#openauthsessionoptions">OpenAuthSessionOptions</a></code> | - auth session options |
|
|
1385
|
+
|
|
1386
|
+
**Returns:** <code>Promise<<a href="#openauthsessionresult">OpenAuthSessionResult</a>></code>
|
|
1387
|
+
|
|
1388
|
+
--------------------
|
|
1389
|
+
|
|
1390
|
+
|
|
1303
1391
|
### Interfaces
|
|
1304
1392
|
|
|
1305
1393
|
|
|
@@ -1699,6 +1787,23 @@ BackupAgent `onRestoreFinished`). Your backend must supply WebAuthn authenticati
|
|
|
1699
1787
|
| **`broadcastChannelName`** | <code>string</code> | The name of the broadcast channel to listen to, relevant only for web |
|
|
1700
1788
|
|
|
1701
1789
|
|
|
1790
|
+
#### OpenAuthSessionResult
|
|
1791
|
+
|
|
1792
|
+
| Prop | Type | Description |
|
|
1793
|
+
| ----------------- | ------------------- | ----------------------------------------------------------------------------------------------------------------------------------- |
|
|
1794
|
+
| **`callbackURL`** | <code>string</code> | Full redirect URI returned by the identity provider, including query or fragment (e.g. `myapp://auth/callback?code=...&state=...`). |
|
|
1795
|
+
|
|
1796
|
+
|
|
1797
|
+
#### OpenAuthSessionOptions
|
|
1798
|
+
|
|
1799
|
+
| Prop | Type | Description | Default |
|
|
1800
|
+
| ----------------------------- | -------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------- |
|
|
1801
|
+
| **`url`** | <code>string</code> | Authorization or login URL to open in the system browser session (OAuth/OIDC authorize endpoint, SSO login page, etc.). | |
|
|
1802
|
+
| **`callbackURLScheme`** | <code>string</code> | Custom URL scheme that receives the redirect (without `://`). Must match the scheme in your registered redirect URI and native URL handler (iOS/Android). On Web, the plugin also accepts HTTPS redirect URLs when `redirect_uri` is present in `url`, or messages prefixed with `${callbackURLScheme}://` on the BroadcastChannel. | |
|
|
1803
|
+
| **`prefersEphemeralSession`** | <code>boolean</code> | iOS-only: prefer an ephemeral `ASWebAuthenticationSession` (no shared cookies with Safari). Defaults to `false` so SSO cookies in the system browser can be reused. | |
|
|
1804
|
+
| **`broadcastChannelName`** | <code>string</code> | Web-only: `BroadcastChannel` name used by the redirect page to post the callback URL back to the app. | <code>'capgo-auth-session'</code> |
|
|
1805
|
+
|
|
1806
|
+
|
|
1702
1807
|
### Type Aliases
|
|
1703
1808
|
|
|
1704
1809
|
|
|
@@ -54,6 +54,10 @@ public class OAuth2Provider implements SocialProvider {
|
|
|
54
54
|
void launchForResult(Intent intent, int requestCode);
|
|
55
55
|
}
|
|
56
56
|
|
|
57
|
+
public interface AuthBrowserSessionGuard {
|
|
58
|
+
boolean isAnotherSessionInProgress();
|
|
59
|
+
}
|
|
60
|
+
|
|
57
61
|
private final Activity activity;
|
|
58
62
|
private final Context context;
|
|
59
63
|
private final OkHttpClient httpClient;
|
|
@@ -65,11 +69,16 @@ public class OAuth2Provider implements SocialProvider {
|
|
|
65
69
|
private OAuth2PendingState pendingState;
|
|
66
70
|
private boolean pendingUseCustomTabs;
|
|
67
71
|
private ActivityLauncher activityLauncher;
|
|
72
|
+
private AuthBrowserSessionGuard authBrowserSessionGuard;
|
|
68
73
|
|
|
69
74
|
public void setActivityLauncher(ActivityLauncher launcher) {
|
|
70
75
|
this.activityLauncher = launcher;
|
|
71
76
|
}
|
|
72
77
|
|
|
78
|
+
public void setAuthBrowserSessionGuard(AuthBrowserSessionGuard guard) {
|
|
79
|
+
this.authBrowserSessionGuard = guard;
|
|
80
|
+
}
|
|
81
|
+
|
|
73
82
|
public PluginCall getPendingCall() {
|
|
74
83
|
return pendingCall;
|
|
75
84
|
}
|
|
@@ -458,9 +467,6 @@ public class OAuth2Provider implements SocialProvider {
|
|
|
458
467
|
return;
|
|
459
468
|
}
|
|
460
469
|
|
|
461
|
-
pendingState = new OAuth2PendingState(providerId, finalState, finalCodeVerifier, finalRedirect, finalLoginScope);
|
|
462
|
-
pendingCall = call;
|
|
463
|
-
|
|
464
470
|
Uri.Builder builder = Uri.parse(resolved.authorizationBaseUrl)
|
|
465
471
|
.buildUpon()
|
|
466
472
|
.appendQueryParameter("response_type", resolved.responseType)
|
|
@@ -511,24 +517,40 @@ public class OAuth2Provider implements SocialProvider {
|
|
|
511
517
|
Log.d(LOG_TAG, "Opening authorization URL: " + builder.build().toString());
|
|
512
518
|
}
|
|
513
519
|
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
520
|
+
final String authUrl = builder.build().toString();
|
|
521
|
+
final OAuth2PendingState loginState = new OAuth2PendingState(
|
|
522
|
+
providerId,
|
|
523
|
+
finalState,
|
|
524
|
+
finalCodeVerifier,
|
|
525
|
+
finalRedirect,
|
|
526
|
+
finalLoginScope
|
|
527
|
+
);
|
|
528
|
+
final boolean useCustomTabs = resolved.androidUseCustomTabs;
|
|
529
|
+
|
|
530
|
+
activity.runOnUiThread(() -> {
|
|
531
|
+
if (authBrowserSessionGuard != null && authBrowserSessionGuard.isAnotherSessionInProgress()) {
|
|
532
|
+
call.reject("Another auth session is already in progress");
|
|
533
|
+
return;
|
|
518
534
|
}
|
|
519
|
-
|
|
520
|
-
|
|
535
|
+
|
|
536
|
+
pendingState = loginState;
|
|
537
|
+
pendingCall = call;
|
|
538
|
+
pendingUseCustomTabs = useCustomTabs;
|
|
539
|
+
|
|
540
|
+
if (useCustomTabs) {
|
|
541
|
+
if (resolved.logsEnabled) {
|
|
542
|
+
Log.d(LOG_TAG, "Using Custom Tabs for OAuth2 authorization");
|
|
543
|
+
}
|
|
521
544
|
if (!launchCustomTabs(authUrl)) {
|
|
522
|
-
// No Custom Tabs browser — fall back to embedded WebView
|
|
523
545
|
pendingUseCustomTabs = false;
|
|
524
546
|
clearPersistedCustomTabsState();
|
|
525
547
|
launchWebViewActivity(authUrl, finalRedirect);
|
|
526
548
|
}
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
}
|
|
549
|
+
return;
|
|
550
|
+
}
|
|
530
551
|
|
|
531
|
-
|
|
552
|
+
launchWebViewActivity(authUrl, finalRedirect);
|
|
553
|
+
});
|
|
532
554
|
}
|
|
533
555
|
|
|
534
556
|
@Override
|
|
@@ -2,6 +2,8 @@ package ee.forgr.capacitor.social.login;
|
|
|
2
2
|
|
|
3
3
|
import android.content.Intent;
|
|
4
4
|
import android.net.Uri;
|
|
5
|
+
import android.os.Handler;
|
|
6
|
+
import android.os.Looper;
|
|
5
7
|
import android.util.Base64;
|
|
6
8
|
import android.util.Log;
|
|
7
9
|
import androidx.activity.result.ActivityResult;
|
|
@@ -15,6 +17,7 @@ import com.getcapacitor.annotation.ActivityCallback;
|
|
|
15
17
|
import com.getcapacitor.annotation.CapacitorPlugin;
|
|
16
18
|
import ee.forgr.capacitor.social.login.helpers.DependencyAvailabilityChecker;
|
|
17
19
|
import ee.forgr.capacitor.social.login.helpers.SocialProvider;
|
|
20
|
+
import java.net.URLDecoder;
|
|
18
21
|
import java.nio.charset.StandardCharsets;
|
|
19
22
|
import java.util.HashMap;
|
|
20
23
|
import org.json.JSONArray;
|
|
@@ -24,15 +27,134 @@ import org.json.JSONObject;
|
|
|
24
27
|
@CapacitorPlugin(name = "SocialLogin")
|
|
25
28
|
public class SocialLoginPlugin extends Plugin {
|
|
26
29
|
|
|
27
|
-
private final String pluginVersion = "8.
|
|
30
|
+
private final String pluginVersion = "8.6.0";
|
|
28
31
|
|
|
29
32
|
public static String LOG_TAG = "CapgoSocialLogin";
|
|
33
|
+
private static final String USER_CANCELLED_CODE = "USER_CANCELLED";
|
|
34
|
+
private static final long OPEN_AUTH_SESSION_TIMEOUT_MS = 600_000L;
|
|
30
35
|
public HashMap<String, SocialProvider> socialProviderHashMap = new HashMap<>();
|
|
31
36
|
|
|
32
37
|
private PluginCall openSecureWindowSavedCall;
|
|
33
38
|
private String openSecureWindowRedirectUri;
|
|
39
|
+
private PluginCall openAuthSessionSavedCall;
|
|
40
|
+
private String openAuthSessionCallbackScheme;
|
|
41
|
+
private String openAuthSessionExpectedCallbackPrefix;
|
|
42
|
+
private boolean openAuthSessionPaused;
|
|
43
|
+
private long openAuthSessionLaunchTimeMs;
|
|
44
|
+
private Handler openAuthSessionHandler;
|
|
45
|
+
private Runnable openAuthSessionTimeoutRunnable;
|
|
34
46
|
private Uri pendingOAuth2RedirectUri;
|
|
35
47
|
|
|
48
|
+
private boolean isAnotherAuthBrowserSessionInProgress() {
|
|
49
|
+
if (openAuthSessionSavedCall != null || openSecureWindowSavedCall != null) {
|
|
50
|
+
return true;
|
|
51
|
+
}
|
|
52
|
+
SocialProvider oauth2 = socialProviderHashMap.get("oauth2");
|
|
53
|
+
if (oauth2 instanceof OAuth2Provider) {
|
|
54
|
+
return ((OAuth2Provider) oauth2).getPendingCall() != null;
|
|
55
|
+
}
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
private boolean rejectIfAnotherAuthBrowserSessionPending(PluginCall call) {
|
|
60
|
+
if (isAnotherAuthBrowserSessionInProgress()) {
|
|
61
|
+
call.reject("Another auth session is already in progress");
|
|
62
|
+
return true;
|
|
63
|
+
}
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
private void cancelOpenAuthSessionTimeout() {
|
|
68
|
+
if (openAuthSessionHandler != null && openAuthSessionTimeoutRunnable != null) {
|
|
69
|
+
openAuthSessionHandler.removeCallbacks(openAuthSessionTimeoutRunnable);
|
|
70
|
+
}
|
|
71
|
+
openAuthSessionTimeoutRunnable = null;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
private void clearOpenAuthSessionState() {
|
|
75
|
+
cancelOpenAuthSessionTimeout();
|
|
76
|
+
openAuthSessionSavedCall = null;
|
|
77
|
+
openAuthSessionCallbackScheme = null;
|
|
78
|
+
openAuthSessionExpectedCallbackPrefix = null;
|
|
79
|
+
openAuthSessionPaused = false;
|
|
80
|
+
openAuthSessionLaunchTimeMs = 0L;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
static String resolveOpenAuthSessionExpectedCallbackPrefix(String authorizeUrl, String callbackURLScheme) {
|
|
84
|
+
if (authorizeUrl != null && !authorizeUrl.isEmpty()) {
|
|
85
|
+
try {
|
|
86
|
+
String redirectUri = extractRedirectUriFromAuthorizeQuery(authorizeUrl);
|
|
87
|
+
if (redirectUri == null || redirectUri.isEmpty()) {
|
|
88
|
+
redirectUri = Uri.parse(authorizeUrl).getQueryParameter("redirect_uri");
|
|
89
|
+
}
|
|
90
|
+
if (redirectUri != null && !redirectUri.isEmpty()) {
|
|
91
|
+
return redirectUri;
|
|
92
|
+
}
|
|
93
|
+
} catch (Exception ignored) {
|
|
94
|
+
// fall back to scheme prefix below
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
if (callbackURLScheme == null || callbackURLScheme.isEmpty()) {
|
|
98
|
+
return null;
|
|
99
|
+
}
|
|
100
|
+
return callbackURLScheme + ":";
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
static boolean matchesOpenAuthSessionCallback(String callbackUrl, String expectedPrefix, String callbackURLScheme) {
|
|
104
|
+
if (callbackUrl == null || expectedPrefix == null || expectedPrefix.isEmpty()) {
|
|
105
|
+
return false;
|
|
106
|
+
}
|
|
107
|
+
if (callbackURLScheme != null && expectedPrefix.equals(callbackURLScheme + ":")) {
|
|
108
|
+
String schemePrefix = callbackURLScheme + ":";
|
|
109
|
+
return (
|
|
110
|
+
callbackUrl.regionMatches(true, 0, schemePrefix, 0, schemePrefix.length()) && callbackUrl.length() > schemePrefix.length()
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
return (
|
|
114
|
+
callbackUrl.equals(expectedPrefix) ||
|
|
115
|
+
callbackUrl.startsWith(expectedPrefix + "?") ||
|
|
116
|
+
callbackUrl.startsWith(expectedPrefix + "&") ||
|
|
117
|
+
callbackUrl.startsWith(expectedPrefix + "#")
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
private static String extractRedirectUriFromAuthorizeQuery(String authorizeUrl) {
|
|
122
|
+
int queryStart = authorizeUrl.indexOf('?');
|
|
123
|
+
if (queryStart < 0 || queryStart >= authorizeUrl.length() - 1) {
|
|
124
|
+
return null;
|
|
125
|
+
}
|
|
126
|
+
String query = authorizeUrl.substring(queryStart + 1);
|
|
127
|
+
int fragmentStart = query.indexOf('#');
|
|
128
|
+
if (fragmentStart >= 0) {
|
|
129
|
+
query = query.substring(0, fragmentStart);
|
|
130
|
+
}
|
|
131
|
+
for (String part : query.split("&")) {
|
|
132
|
+
int equals = part.indexOf('=');
|
|
133
|
+
if (equals <= 0) {
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
136
|
+
if (!"redirect_uri".equals(part.substring(0, equals))) {
|
|
137
|
+
continue;
|
|
138
|
+
}
|
|
139
|
+
return URLDecoder.decode(part.substring(equals + 1), StandardCharsets.UTF_8);
|
|
140
|
+
}
|
|
141
|
+
return null;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
private void scheduleOpenAuthSessionTimeout() {
|
|
145
|
+
if (openAuthSessionHandler == null) {
|
|
146
|
+
openAuthSessionHandler = new Handler(Looper.getMainLooper());
|
|
147
|
+
}
|
|
148
|
+
cancelOpenAuthSessionTimeout();
|
|
149
|
+
openAuthSessionTimeoutRunnable = () -> {
|
|
150
|
+
if (openAuthSessionSavedCall != null) {
|
|
151
|
+
openAuthSessionSavedCall.reject("The sign-in flow timed out", USER_CANCELLED_CODE);
|
|
152
|
+
clearOpenAuthSessionState();
|
|
153
|
+
}
|
|
154
|
+
};
|
|
155
|
+
openAuthSessionHandler.postDelayed(openAuthSessionTimeoutRunnable, OPEN_AUTH_SESSION_TIMEOUT_MS);
|
|
156
|
+
}
|
|
157
|
+
|
|
36
158
|
@PluginMethod
|
|
37
159
|
public void initialize(PluginCall call) {
|
|
38
160
|
// Set plugin instance for config access
|
|
@@ -196,6 +318,7 @@ public class SocialLoginPlugin extends Plugin {
|
|
|
196
318
|
return;
|
|
197
319
|
}
|
|
198
320
|
this.socialProviderHashMap.put("oauth2", oauth2Provider);
|
|
321
|
+
oauth2Provider.setAuthBrowserSessionGuard(this::isAnotherAuthBrowserSessionInProgress);
|
|
199
322
|
oauth2Provider.setActivityLauncher((intent, requestCode) -> {
|
|
200
323
|
PluginCall loginCall = oauth2Provider.getPendingCall();
|
|
201
324
|
if (loginCall != null) {
|
|
@@ -256,6 +379,10 @@ public class SocialLoginPlugin extends Plugin {
|
|
|
256
379
|
return;
|
|
257
380
|
}
|
|
258
381
|
|
|
382
|
+
if (rejectIfAnotherAuthBrowserSessionPending(call)) {
|
|
383
|
+
return;
|
|
384
|
+
}
|
|
385
|
+
|
|
259
386
|
provider.login(call, options);
|
|
260
387
|
}
|
|
261
388
|
|
|
@@ -664,11 +791,74 @@ public class SocialLoginPlugin extends Plugin {
|
|
|
664
791
|
return;
|
|
665
792
|
}
|
|
666
793
|
|
|
794
|
+
if (rejectIfAnotherAuthBrowserSessionPending(call)) {
|
|
795
|
+
return;
|
|
796
|
+
}
|
|
797
|
+
|
|
667
798
|
openSecureWindowSavedCall = call;
|
|
668
799
|
openSecureWindowRedirectUri = redirectUri;
|
|
669
800
|
|
|
670
|
-
|
|
671
|
-
|
|
801
|
+
try {
|
|
802
|
+
launchCustomTab(authEndpoint);
|
|
803
|
+
} catch (Exception e) {
|
|
804
|
+
openSecureWindowSavedCall = null;
|
|
805
|
+
openSecureWindowRedirectUri = null;
|
|
806
|
+
call.reject("Unable to open browser", e);
|
|
807
|
+
}
|
|
808
|
+
}
|
|
809
|
+
|
|
810
|
+
@PluginMethod
|
|
811
|
+
public void openAuthSession(PluginCall call) {
|
|
812
|
+
String url = call.getString("url");
|
|
813
|
+
if (url == null || url.isEmpty()) {
|
|
814
|
+
call.reject("url is required");
|
|
815
|
+
return;
|
|
816
|
+
}
|
|
817
|
+
|
|
818
|
+
String callbackURLScheme = call.getString("callbackURLScheme");
|
|
819
|
+
if (callbackURLScheme == null || callbackURLScheme.isEmpty()) {
|
|
820
|
+
call.reject("callbackURLScheme is required");
|
|
821
|
+
return;
|
|
822
|
+
}
|
|
823
|
+
|
|
824
|
+
if (rejectIfAnotherAuthBrowserSessionPending(call)) {
|
|
825
|
+
return;
|
|
826
|
+
}
|
|
827
|
+
|
|
828
|
+
if (getActivity() == null) {
|
|
829
|
+
call.reject("Activity not available");
|
|
830
|
+
return;
|
|
831
|
+
}
|
|
832
|
+
|
|
833
|
+
getActivity().runOnUiThread(() -> startOpenAuthSessionOnMainThread(call, url, callbackURLScheme));
|
|
834
|
+
}
|
|
835
|
+
|
|
836
|
+
private void startOpenAuthSessionOnMainThread(PluginCall call, String url, String callbackURLScheme) {
|
|
837
|
+
if (rejectIfAnotherAuthBrowserSessionPending(call)) {
|
|
838
|
+
return;
|
|
839
|
+
}
|
|
840
|
+
|
|
841
|
+
openAuthSessionSavedCall = call;
|
|
842
|
+
openAuthSessionCallbackScheme = callbackURLScheme;
|
|
843
|
+
openAuthSessionExpectedCallbackPrefix = resolveOpenAuthSessionExpectedCallbackPrefix(url, callbackURLScheme);
|
|
844
|
+
openAuthSessionPaused = false;
|
|
845
|
+
openAuthSessionLaunchTimeMs = System.currentTimeMillis();
|
|
846
|
+
|
|
847
|
+
try {
|
|
848
|
+
launchCustomTab(url);
|
|
849
|
+
scheduleOpenAuthSessionTimeout();
|
|
850
|
+
} catch (Exception e) {
|
|
851
|
+
clearOpenAuthSessionState();
|
|
852
|
+
call.reject("Unable to open browser", e);
|
|
853
|
+
}
|
|
854
|
+
}
|
|
855
|
+
|
|
856
|
+
@Override
|
|
857
|
+
protected void handleOnPause() {
|
|
858
|
+
super.handleOnPause();
|
|
859
|
+
if (openAuthSessionSavedCall != null) {
|
|
860
|
+
openAuthSessionPaused = true;
|
|
861
|
+
}
|
|
672
862
|
}
|
|
673
863
|
|
|
674
864
|
private void launchCustomTab(String url) {
|
|
@@ -697,10 +887,23 @@ public class SocialLoginPlugin extends Plugin {
|
|
|
697
887
|
((OAuth2Provider) oauth2Provider).handleUserReturnedWithoutCallback();
|
|
698
888
|
}
|
|
699
889
|
|
|
700
|
-
//
|
|
890
|
+
// User returned without redirect: cancel when we saw pause, or after launch grace if pause was missed
|
|
891
|
+
if (openAuthSessionSavedCall != null) {
|
|
892
|
+
boolean pausedAfterLaunch = openAuthSessionPaused;
|
|
893
|
+
boolean missedPauseGraceElapsed =
|
|
894
|
+
!openAuthSessionPaused &&
|
|
895
|
+
openAuthSessionLaunchTimeMs > 0L &&
|
|
896
|
+
System.currentTimeMillis() - openAuthSessionLaunchTimeMs > 750L;
|
|
897
|
+
if (pausedAfterLaunch || missedPauseGraceElapsed) {
|
|
898
|
+
openAuthSessionSavedCall.reject("User cancelled", USER_CANCELLED_CODE);
|
|
899
|
+
clearOpenAuthSessionState();
|
|
900
|
+
}
|
|
901
|
+
}
|
|
902
|
+
|
|
701
903
|
if (openSecureWindowSavedCall != null) {
|
|
702
904
|
openSecureWindowSavedCall.reject("OAuth cancelled or no callback received");
|
|
703
905
|
openSecureWindowSavedCall = null;
|
|
906
|
+
openSecureWindowRedirectUri = null;
|
|
704
907
|
}
|
|
705
908
|
}
|
|
706
909
|
|
|
@@ -727,6 +930,30 @@ public class SocialLoginPlugin extends Plugin {
|
|
|
727
930
|
pendingOAuth2RedirectUri = uri;
|
|
728
931
|
}
|
|
729
932
|
|
|
933
|
+
if (openSecureWindowRedirectUri == null && openAuthSessionCallbackScheme == null) {
|
|
934
|
+
return;
|
|
935
|
+
}
|
|
936
|
+
|
|
937
|
+
if (
|
|
938
|
+
openAuthSessionExpectedCallbackPrefix != null &&
|
|
939
|
+
matchesOpenAuthSessionCallback(uri.toString(), openAuthSessionExpectedCallbackPrefix, openAuthSessionCallbackScheme)
|
|
940
|
+
) {
|
|
941
|
+
try {
|
|
942
|
+
if (openAuthSessionSavedCall != null) {
|
|
943
|
+
final JSObject ret = new JSObject();
|
|
944
|
+
ret.put("callbackURL", uri.toString());
|
|
945
|
+
openAuthSessionSavedCall.resolve(ret);
|
|
946
|
+
clearOpenAuthSessionState();
|
|
947
|
+
}
|
|
948
|
+
} catch (Exception e) {
|
|
949
|
+
if (openAuthSessionSavedCall != null) {
|
|
950
|
+
openAuthSessionSavedCall.reject("Failed to process OAuth callback", e);
|
|
951
|
+
clearOpenAuthSessionState();
|
|
952
|
+
}
|
|
953
|
+
}
|
|
954
|
+
return;
|
|
955
|
+
}
|
|
956
|
+
|
|
730
957
|
if (openSecureWindowRedirectUri == null) {
|
|
731
958
|
return;
|
|
732
959
|
}
|
package/dist/docs.json
CHANGED
|
@@ -342,6 +342,30 @@
|
|
|
342
342
|
"OpenSecureWindowOptions"
|
|
343
343
|
],
|
|
344
344
|
"slug": "opensecurewindow"
|
|
345
|
+
},
|
|
346
|
+
{
|
|
347
|
+
"name": "openAuthSession",
|
|
348
|
+
"signature": "(options: OpenAuthSessionOptions) => Promise<OpenAuthSessionResult>",
|
|
349
|
+
"parameters": [
|
|
350
|
+
{
|
|
351
|
+
"name": "options",
|
|
352
|
+
"docs": "- auth session options",
|
|
353
|
+
"type": "OpenAuthSessionOptions"
|
|
354
|
+
}
|
|
355
|
+
],
|
|
356
|
+
"returns": "Promise<OpenAuthSessionResult>",
|
|
357
|
+
"tags": [
|
|
358
|
+
{
|
|
359
|
+
"name": "param",
|
|
360
|
+
"text": "options - auth session options"
|
|
361
|
+
}
|
|
362
|
+
],
|
|
363
|
+
"docs": "Opens a system auth session for generic OAuth/OIDC or enterprise SSO.\n\nUse this when you build the authorize URL yourself (or your IdP SDK returns one) and only need\nthe plugin to run the secure browser flow and return the redirect callback URL. Exchange the\nauthorization code or tokens on your backend or with your own client logic.\n\n**iOS/Android:** system browser session (`ASWebAuthenticationSession` / Chrome Custom Tabs).\n**Web:** popup window plus `BroadcastChannel` (not a native browser session).\n\nOn user cancel or dismiss, the Promise rejects with `code === 'USER_CANCELLED'`.",
|
|
364
|
+
"complexTypes": [
|
|
365
|
+
"OpenAuthSessionResult",
|
|
366
|
+
"OpenAuthSessionOptions"
|
|
367
|
+
],
|
|
368
|
+
"slug": "openauthsession"
|
|
345
369
|
}
|
|
346
370
|
],
|
|
347
371
|
"properties": []
|
|
@@ -2209,6 +2233,74 @@
|
|
|
2209
2233
|
"type": "string | undefined"
|
|
2210
2234
|
}
|
|
2211
2235
|
]
|
|
2236
|
+
},
|
|
2237
|
+
{
|
|
2238
|
+
"name": "OpenAuthSessionResult",
|
|
2239
|
+
"slug": "openauthsessionresult",
|
|
2240
|
+
"docs": "",
|
|
2241
|
+
"tags": [],
|
|
2242
|
+
"methods": [],
|
|
2243
|
+
"properties": [
|
|
2244
|
+
{
|
|
2245
|
+
"name": "callbackURL",
|
|
2246
|
+
"tags": [],
|
|
2247
|
+
"docs": "Full redirect URI returned by the identity provider, including query or fragment (e.g. `myapp://auth/callback?code=...&state=...`).",
|
|
2248
|
+
"complexTypes": [],
|
|
2249
|
+
"type": "string"
|
|
2250
|
+
}
|
|
2251
|
+
]
|
|
2252
|
+
},
|
|
2253
|
+
{
|
|
2254
|
+
"name": "OpenAuthSessionOptions",
|
|
2255
|
+
"slug": "openauthsessionoptions",
|
|
2256
|
+
"docs": "",
|
|
2257
|
+
"tags": [],
|
|
2258
|
+
"methods": [],
|
|
2259
|
+
"properties": [
|
|
2260
|
+
{
|
|
2261
|
+
"name": "url",
|
|
2262
|
+
"tags": [
|
|
2263
|
+
{
|
|
2264
|
+
"text": "'https://login.example.com/oauth2/authorize?client_id=...&redirect_uri=...&state=...'",
|
|
2265
|
+
"name": "example"
|
|
2266
|
+
}
|
|
2267
|
+
],
|
|
2268
|
+
"docs": "Authorization or login URL to open in the system browser session (OAuth/OIDC authorize endpoint, SSO login page, etc.).",
|
|
2269
|
+
"complexTypes": [],
|
|
2270
|
+
"type": "string"
|
|
2271
|
+
},
|
|
2272
|
+
{
|
|
2273
|
+
"name": "callbackURLScheme",
|
|
2274
|
+
"tags": [
|
|
2275
|
+
{
|
|
2276
|
+
"text": "'myapp'",
|
|
2277
|
+
"name": "example"
|
|
2278
|
+
}
|
|
2279
|
+
],
|
|
2280
|
+
"docs": "Custom URL scheme that receives the redirect (without `://`).\nMust match the scheme in your registered redirect URI and native URL handler (iOS/Android).\nOn Web, the plugin also accepts HTTPS redirect URLs when `redirect_uri` is present in `url`, or messages prefixed with `${callbackURLScheme}://` on the BroadcastChannel.",
|
|
2281
|
+
"complexTypes": [],
|
|
2282
|
+
"type": "string"
|
|
2283
|
+
},
|
|
2284
|
+
{
|
|
2285
|
+
"name": "prefersEphemeralSession",
|
|
2286
|
+
"tags": [],
|
|
2287
|
+
"docs": "iOS-only: prefer an ephemeral `ASWebAuthenticationSession` (no shared cookies with Safari).\nDefaults to `false` so SSO cookies in the system browser can be reused.",
|
|
2288
|
+
"complexTypes": [],
|
|
2289
|
+
"type": "boolean | undefined"
|
|
2290
|
+
},
|
|
2291
|
+
{
|
|
2292
|
+
"name": "broadcastChannelName",
|
|
2293
|
+
"tags": [
|
|
2294
|
+
{
|
|
2295
|
+
"text": "'capgo-auth-session'",
|
|
2296
|
+
"name": "default"
|
|
2297
|
+
}
|
|
2298
|
+
],
|
|
2299
|
+
"docs": "Web-only: `BroadcastChannel` name used by the redirect page to post the callback URL back to the app.",
|
|
2300
|
+
"complexTypes": [],
|
|
2301
|
+
"type": "string | undefined"
|
|
2302
|
+
}
|
|
2303
|
+
]
|
|
2212
2304
|
}
|
|
2213
2305
|
],
|
|
2214
2306
|
"enums": [],
|