@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
package/README.md
CHANGED
|
@@ -91,7 +91,7 @@ const config: CapacitorConfig = {
|
|
|
91
91
|
providers: {
|
|
92
92
|
google: true, // true = enabled (bundled), false = disabled (not bundled)
|
|
93
93
|
facebook: true, // Use false to reduce app size
|
|
94
|
-
apple: true, // Apple uses system APIs
|
|
94
|
+
apple: true, // Apple uses system APIs; Alamofire only for redirectUrl backend flows
|
|
95
95
|
twitter: false // false = disabled (not bundled)
|
|
96
96
|
},
|
|
97
97
|
logLevel: 1 // Warnings and errors only
|
|
@@ -115,6 +115,7 @@ export default config;
|
|
|
115
115
|
- This configuration only affects iOS and Android platforms; it does not affect the web platform.
|
|
116
116
|
- **Important**: Using `false` means the dependency won't be bundled, but the plugin code still compiles against it. Ensure the consuming app includes the dependency if needed.
|
|
117
117
|
- **Facebook**: When `facebook: false`, the Facebook SDK is omitted and the plugin compiles a provider stub in its own package — no `com.facebook.*` classes are shipped (avoids privacy-scanner false positives).
|
|
118
|
+
- **Apple (iOS)**: Basic Sign in with Apple always works via AuthenticationServices (no external SDK). Alamofire is only required when using `redirectUrl` / backend token exchange. CocoaPods toggles Alamofire via `apple: true|false`; SPM always resolves Alamofire from `Package.swift` when the plugin is linked.
|
|
118
119
|
- Apple Sign-In on Android uses OAuth flow without external SDK dependencies
|
|
119
120
|
- Twitter uses standard OAuth 2.0 flow without external SDK dependencies
|
|
120
121
|
|
|
@@ -709,13 +710,14 @@ await SocialLogin.refresh({
|
|
|
709
710
|
| `additionalParameters` | Record<string, string> | No | Extra params for authorization URL |
|
|
710
711
|
| `additionalResourceHeaders` | Record<string, string> | No | Extra headers for resource request |
|
|
711
712
|
| `logoutUrl` | string | No | URL to open on logout |
|
|
713
|
+
| `androidUseCustomTabs` | boolean | No | Android-only: use Chrome Custom Tabs instead of WebView (default: false). Requires a deep-linkable `redirectUrl` |
|
|
712
714
|
| `logsEnabled` | boolean | No | Enable debug logging (default: false) |
|
|
713
715
|
|
|
714
716
|
### Platform-Specific Notes
|
|
715
717
|
|
|
716
718
|
**iOS**: Uses `ASWebAuthenticationSession` for secure authentication.
|
|
717
719
|
|
|
718
|
-
**Android**: Uses a
|
|
720
|
+
**Android**: Uses an embedded WebView by default. Set `androidUseCustomTabs: true` to use Chrome Custom Tabs instead (RFC 8252 / system browser). Custom Tabs require a custom-scheme or App Link `redirectUrl` with a matching intent filter in your app's `AndroidManifest.xml`, and `android:launchMode="singleTask"` (or `singleTop`) on the main activity so redirects arrive via `onNewIntent`.
|
|
719
721
|
|
|
720
722
|
**Web**: Opens a popup window for OAuth flow.
|
|
721
723
|
|
|
@@ -1207,34 +1209,35 @@ And in the AndroidManifest.xml file:
|
|
|
1207
1209
|
|
|
1208
1210
|
Configuration for a single OAuth2 provider instance
|
|
1209
1211
|
|
|
1210
|
-
| Prop | Type | Description
|
|
1211
|
-
| ------------------------------------------ | --------------------------------------------------------------- |
|
|
1212
|
-
| **`appId`** | <code>string</code> | The OAuth 2.0 client identifier (App ID / Client ID). Note: this configuration object is only used by the plugin's built-in `oauth2` provider (i.e. `SocialLogin.initialize({ oauth2: { ... } })`). It does not affect Google/Apple/Facebook/Twitter.
|
|
1213
|
-
| **`clientId`** | <code>string</code> | Alias for `appId` to match common OAuth/OIDC naming (`clientId`). If both are provided, `appId` takes precedence.
|
|
1214
|
-
| **`issuerUrl`** | <code>string</code> | OpenID Connect issuer URL (enables discovery via `/.well-known/openid-configuration`). When set, you may omit explicit endpoints like `authorizationBaseUrl` and `accessTokenEndpoint`. Notes: - Explicit endpoints (authorization/token/logout) take precedence over discovered values. - Discovery is supported for `oauth2` on Web, iOS, and Android.
|
|
1215
|
-
| **`authorizationBaseUrl`** | <code>string</code> | The base URL of the authorization endpoint
|
|
1216
|
-
| **`authorizationEndpoint`** | <code>string</code> | Alias for `authorizationBaseUrl` (to match common OAuth/OIDC naming).
|
|
1217
|
-
| **`clientSecret`** | <code>string</code> | OAuth 2.0 client secret for token requests (e.g., when exchanging the code). This value is sent as `client_secret` in token/refresh requests when provided.
|
|
1218
|
-
| **`accessTokenEndpoint`** | <code>string</code> | The URL to exchange the authorization code for tokens Required for authorization code flow
|
|
1219
|
-
| **`tokenEndpoint`** | <code>string</code> | Alias for `accessTokenEndpoint` (to match common OAuth/OIDC naming).
|
|
1220
|
-
| **`redirectUrl`** | <code>string</code> | Redirect URL that receives the OAuth callback
|
|
1221
|
-
| **`resourceUrl`** | <code>string</code> | Optional URL to fetch user profile/resource data after authentication The access token will be sent as Bearer token in the Authorization header
|
|
1222
|
-
| **`responseType`** | <code>'code' \| 'token'</code> | The OAuth response type - 'code': Authorization Code flow (recommended, requires accessTokenEndpoint) - 'token': Implicit flow (less secure, tokens returned directly)
|
|
1223
|
-
| **`pkceEnabled`** | <code>boolean</code> | Enable PKCE (Proof Key for Code Exchange) Strongly recommended for public clients (mobile/web apps)
|
|
1224
|
-
| **`scope`** | <code>string \| string[]</code> | Default scopes to request during authorization
|
|
1225
|
-
| **`scopes`** | <code>string[]</code> | Alias for `scope` using common naming (`scopes`). If both are provided, `scope` takes precedence.
|
|
1226
|
-
| **`additionalParameters`** | <code><a href="#record">Record</a><string, string></code> | Additional parameters to include in the authorization request
|
|
1227
|
-
| **`loginHint`** | <code>string</code> | Convenience option for OIDC `login_hint`. Equivalent to passing `additionalParameters.login_hint`.
|
|
1228
|
-
| **`prompt`** | <code>string</code> | Convenience option for OAuth/OIDC `prompt`. Equivalent to passing `additionalParameters.prompt`.
|
|
1229
|
-
| **`additionalTokenParameters`** | <code><a href="#record">Record</a><string, string></code> | Additional parameters to include in token requests (code exchange / refresh). Useful for providers that require non-standard parameters.
|
|
1230
|
-
| **`additionalResourceHeaders`** | <code><a href="#record">Record</a><string, string></code> | Additional headers to include when fetching the resource URL
|
|
1231
|
-
| **`logoutUrl`** | <code>string</code> | Custom logout URL for ending the session
|
|
1232
|
-
| **`endSessionEndpoint`** | <code>string</code> | Alias for `logoutUrl` to match OIDC naming (`endSessionEndpoint`).
|
|
1233
|
-
| **`postLogoutRedirectUrl`** | <code>string</code> | OIDC post logout redirect URL (sent as `post_logout_redirect_uri` when building the end-session URL).
|
|
1234
|
-
| **`additionalLogoutParameters`** | <code><a href="#record">Record</a><string, string></code> | Additional parameters to include in logout / end-session URL.
|
|
1235
|
-
| **`iosPrefersEphemeralWebBrowserSession`** | <code>boolean</code> | iOS-only: Whether to prefer an ephemeral browser session for ASWebAuthenticationSession. Defaults to true to match existing behavior in this plugin.
|
|
1236
|
-
| **`iosPrefersEphemeralSession`** | <code>boolean</code> | Alias for `iosPrefersEphemeralWebBrowserSession` (to match Capawesome OAuth naming).
|
|
1237
|
-
| **`
|
|
1212
|
+
| Prop | Type | Description | Default |
|
|
1213
|
+
| ------------------------------------------ | --------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------- |
|
|
1214
|
+
| **`appId`** | <code>string</code> | The OAuth 2.0 client identifier (App ID / Client ID). Note: this configuration object is only used by the plugin's built-in `oauth2` provider (i.e. `SocialLogin.initialize({ oauth2: { ... } })`). It does not affect Google/Apple/Facebook/Twitter. | |
|
|
1215
|
+
| **`clientId`** | <code>string</code> | Alias for `appId` to match common OAuth/OIDC naming (`clientId`). If both are provided, `appId` takes precedence. | |
|
|
1216
|
+
| **`issuerUrl`** | <code>string</code> | OpenID Connect issuer URL (enables discovery via `/.well-known/openid-configuration`). When set, you may omit explicit endpoints like `authorizationBaseUrl` and `accessTokenEndpoint`. Notes: - Explicit endpoints (authorization/token/logout) take precedence over discovered values. - Discovery is supported for `oauth2` on Web, iOS, and Android. | |
|
|
1217
|
+
| **`authorizationBaseUrl`** | <code>string</code> | The base URL of the authorization endpoint | |
|
|
1218
|
+
| **`authorizationEndpoint`** | <code>string</code> | Alias for `authorizationBaseUrl` (to match common OAuth/OIDC naming). | |
|
|
1219
|
+
| **`clientSecret`** | <code>string</code> | OAuth 2.0 client secret for token requests (e.g., when exchanging the code). This value is sent as `client_secret` in token/refresh requests when provided. | |
|
|
1220
|
+
| **`accessTokenEndpoint`** | <code>string</code> | The URL to exchange the authorization code for tokens Required for authorization code flow | |
|
|
1221
|
+
| **`tokenEndpoint`** | <code>string</code> | Alias for `accessTokenEndpoint` (to match common OAuth/OIDC naming). | |
|
|
1222
|
+
| **`redirectUrl`** | <code>string</code> | Redirect URL that receives the OAuth callback | |
|
|
1223
|
+
| **`resourceUrl`** | <code>string</code> | Optional URL to fetch user profile/resource data after authentication The access token will be sent as Bearer token in the Authorization header | |
|
|
1224
|
+
| **`responseType`** | <code>'code' \| 'token'</code> | The OAuth response type - 'code': Authorization Code flow (recommended, requires accessTokenEndpoint) - 'token': Implicit flow (less secure, tokens returned directly) | <code>'code'</code> |
|
|
1225
|
+
| **`pkceEnabled`** | <code>boolean</code> | Enable PKCE (Proof Key for Code Exchange) Strongly recommended for public clients (mobile/web apps) | <code>true</code> |
|
|
1226
|
+
| **`scope`** | <code>string \| string[]</code> | Default scopes to request during authorization | |
|
|
1227
|
+
| **`scopes`** | <code>string[]</code> | Alias for `scope` using common naming (`scopes`). If both are provided, `scope` takes precedence. | |
|
|
1228
|
+
| **`additionalParameters`** | <code><a href="#record">Record</a><string, string></code> | Additional parameters to include in the authorization request | |
|
|
1229
|
+
| **`loginHint`** | <code>string</code> | Convenience option for OIDC `login_hint`. Equivalent to passing `additionalParameters.login_hint`. | |
|
|
1230
|
+
| **`prompt`** | <code>string</code> | Convenience option for OAuth/OIDC `prompt`. Equivalent to passing `additionalParameters.prompt`. | |
|
|
1231
|
+
| **`additionalTokenParameters`** | <code><a href="#record">Record</a><string, string></code> | Additional parameters to include in token requests (code exchange / refresh). Useful for providers that require non-standard parameters. | |
|
|
1232
|
+
| **`additionalResourceHeaders`** | <code><a href="#record">Record</a><string, string></code> | Additional headers to include when fetching the resource URL | |
|
|
1233
|
+
| **`logoutUrl`** | <code>string</code> | Custom logout URL for ending the session | |
|
|
1234
|
+
| **`endSessionEndpoint`** | <code>string</code> | Alias for `logoutUrl` to match OIDC naming (`endSessionEndpoint`). | |
|
|
1235
|
+
| **`postLogoutRedirectUrl`** | <code>string</code> | OIDC post logout redirect URL (sent as `post_logout_redirect_uri` when building the end-session URL). | |
|
|
1236
|
+
| **`additionalLogoutParameters`** | <code><a href="#record">Record</a><string, string></code> | Additional parameters to include in logout / end-session URL. | |
|
|
1237
|
+
| **`iosPrefersEphemeralWebBrowserSession`** | <code>boolean</code> | iOS-only: Whether to prefer an ephemeral browser session for ASWebAuthenticationSession. Defaults to true to match existing behavior in this plugin. | |
|
|
1238
|
+
| **`iosPrefersEphemeralSession`** | <code>boolean</code> | Alias for `iosPrefersEphemeralWebBrowserSession` (to match Capawesome OAuth naming). | |
|
|
1239
|
+
| **`androidUseCustomTabs`** | <code>boolean</code> | Android-only: Use Chrome Custom Tabs (system browser) instead of an embedded WebView for the authorization request. Custom Tabs follow RFC 8252 (OAuth 2.0 for Native Apps) and fix issues with brokered IdPs (Microsoft Entra Conditional Access / Authenticator, passkeys/WebAuthn, Google `disallowed_useragent`, SSO cookie sharing, password managers). Requires a custom-scheme or App Link `redirectUrl` with a matching intent filter in your app's `AndroidManifest.xml` (same setup as `openSecureWindow()` / Apple on Android). Defaults to `false` to preserve the historical WebView flow. | <code>false</code> |
|
|
1240
|
+
| **`logsEnabled`** | <code>boolean</code> | Enable debug logging | <code>false</code> |
|
|
1238
1241
|
|
|
1239
1242
|
|
|
1240
1243
|
#### FacebookLoginResponse
|
|
@@ -1265,12 +1268,12 @@ Configuration for a single OAuth2 provider instance
|
|
|
1265
1268
|
|
|
1266
1269
|
#### GoogleLoginResponseOnline
|
|
1267
1270
|
|
|
1268
|
-
| Prop | Type | Description
|
|
1269
|
-
| ------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
|
1270
|
-
| **`accessToken`** | <code><a href="#accesstoken">AccessToken</a> \| null</code> |
|
|
1271
|
-
| **`idToken`** | <code>string \| null</code> | OpenID Connect ID token (JWT). Includes an `email_verified` claim when the `email` scope is granted. Use `SocialLogin.decodeIdToken({ idToken })` to read claims on the client, but always verify the token signature, `iss`, `aud`, and `exp` on your backend before trusting `email_verified` for account linking.
|
|
1272
|
-
| **`profile`** | <code>{ email: string \| null; familyName: string \| null; givenName: string \| null; id: string \| null; name: string \| null; imageUrl: string \| null; }</code> |
|
|
1273
|
-
| **`responseType`** | <code>'online'</code> |
|
|
1271
|
+
| Prop | Type | Description |
|
|
1272
|
+
| ------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
1273
|
+
| **`accessToken`** | <code><a href="#accesstoken">AccessToken</a> \| null</code> | OAuth access token for Google APIs. May be `null` on Android when Credential Manager authentication succeeds but AuthorizationClient does not return an access token (authentication-only / ID-token login with default OIDC scopes). Use `idToken` for user authentication; request additional Google API scopes when an access token is required. |
|
|
1274
|
+
| **`idToken`** | <code>string \| null</code> | OpenID Connect ID token (JWT). Includes an `email_verified` claim when the `email` scope is granted. Use `SocialLogin.decodeIdToken({ idToken })` to read claims on the client, but always verify the token signature, `iss`, `aud`, and `exp` on your backend before trusting `email_verified` for account linking. |
|
|
1275
|
+
| **`profile`** | <code>{ email: string \| null; familyName: string \| null; givenName: string \| null; id: string \| null; name: string \| null; imageUrl: string \| null; }</code> | |
|
|
1276
|
+
| **`responseType`** | <code>'online'</code> | |
|
|
1274
1277
|
|
|
1275
1278
|
|
|
1276
1279
|
#### GoogleLoginResponseOffline
|
|
@@ -189,7 +189,8 @@ public class GoogleProvider implements SocialProvider {
|
|
|
189
189
|
try {
|
|
190
190
|
JSONObject object = new JSONObject(data);
|
|
191
191
|
GoogleProvider.this.idToken = object.optString("idToken", null);
|
|
192
|
-
|
|
192
|
+
String restoredAccessToken = object.optString("accessToken", null);
|
|
193
|
+
GoogleProvider.this.accessToken = restoredAccessToken != null && !restoredAccessToken.isEmpty() ? restoredAccessToken : null;
|
|
193
194
|
JSONArray storedScopes = object.optJSONArray("scopes");
|
|
194
195
|
if (storedScopes != null && storedScopes.length() > 0) {
|
|
195
196
|
List<String> scopesList = new ArrayList<>(storedScopes.length());
|
|
@@ -204,12 +205,74 @@ public class GoogleProvider implements SocialProvider {
|
|
|
204
205
|
}
|
|
205
206
|
}
|
|
206
207
|
|
|
207
|
-
Log.i(
|
|
208
|
+
Log.i(
|
|
209
|
+
SocialLoginPlugin.LOG_TAG,
|
|
210
|
+
String.format(
|
|
211
|
+
"Google restoreState: restored idToken=%s accessToken=%s scopes=%d",
|
|
212
|
+
GoogleProvider.this.idToken != null && !GoogleProvider.this.idToken.isEmpty(),
|
|
213
|
+
GoogleProvider.this.accessToken != null && !GoogleProvider.this.accessToken.isEmpty(),
|
|
214
|
+
GoogleProvider.this.scopes != null ? GoogleProvider.this.scopes.length : 0
|
|
215
|
+
)
|
|
216
|
+
);
|
|
208
217
|
} catch (JSONException e) {
|
|
209
218
|
Log.e(SocialLoginPlugin.LOG_TAG, "Google restoreState: Failed to parse JSON", e);
|
|
210
219
|
}
|
|
211
220
|
}
|
|
212
221
|
|
|
222
|
+
/**
|
|
223
|
+
* Authentication-only (OIDC) scopes that Credential Manager already covers via the ID token.
|
|
224
|
+
* AuthorizationClient is only needed when requesting additional Google API scopes.
|
|
225
|
+
*/
|
|
226
|
+
private static boolean isAuthenticationOnlyScopes(String[] scopes) {
|
|
227
|
+
if (scopes == null || scopes.length == 0) {
|
|
228
|
+
return true;
|
|
229
|
+
}
|
|
230
|
+
for (String scope : scopes) {
|
|
231
|
+
if (scope == null || scope.isEmpty()) {
|
|
232
|
+
continue;
|
|
233
|
+
}
|
|
234
|
+
boolean isDefault =
|
|
235
|
+
scope.equals("openid") ||
|
|
236
|
+
scope.equals("https://www.googleapis.com/auth/userinfo.email") ||
|
|
237
|
+
scope.equals("https://www.googleapis.com/auth/userinfo.profile") ||
|
|
238
|
+
scope.equals("email") ||
|
|
239
|
+
scope.equals("profile");
|
|
240
|
+
if (!isDefault) {
|
|
241
|
+
return false;
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
return true;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
private void resolveOnlineLogin(
|
|
248
|
+
PluginCall call,
|
|
249
|
+
JSObject response,
|
|
250
|
+
JSObject resultObj,
|
|
251
|
+
JSObject profile,
|
|
252
|
+
String idToken,
|
|
253
|
+
String accessToken
|
|
254
|
+
) {
|
|
255
|
+
try {
|
|
256
|
+
if (accessToken != null && !accessToken.isEmpty()) {
|
|
257
|
+
JSObject accessTokenObj = new JSObject();
|
|
258
|
+
accessTokenObj.put("token", accessToken);
|
|
259
|
+
resultObj.put("accessToken", accessTokenObj);
|
|
260
|
+
} else {
|
|
261
|
+
// GoogleLoginResponseOnline.accessToken is nullable: ID-token auth can succeed without an access token.
|
|
262
|
+
resultObj.put("accessToken", JSONObject.NULL);
|
|
263
|
+
Log.i(LOG_TAG, "AuthorizationClient returned no access token; resolving online login with idToken and profile only");
|
|
264
|
+
}
|
|
265
|
+
resultObj.put("profile", profile);
|
|
266
|
+
resultObj.put("idToken", idToken);
|
|
267
|
+
resultObj.put("responseType", "online");
|
|
268
|
+
response.put("result", resultObj);
|
|
269
|
+
persistState(idToken, accessToken);
|
|
270
|
+
call.resolve(response);
|
|
271
|
+
} catch (Exception e) {
|
|
272
|
+
call.reject("Error resolving Google login: " + e.getMessage());
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
|
|
213
276
|
public ListenableFuture<Boolean> accessTokenIsValid(String accessToken) {
|
|
214
277
|
return CallbackToFutureAdapter.getFuture((completer) -> {
|
|
215
278
|
OkHttpClient client = new OkHttpClient();
|
|
@@ -502,7 +565,7 @@ public class GoogleProvider implements SocialProvider {
|
|
|
502
565
|
private void persistState(String idToken, String accessToken, String[] scopes) throws JSONException {
|
|
503
566
|
JSONObject object = new JSONObject();
|
|
504
567
|
object.put("idToken", idToken);
|
|
505
|
-
object.put("accessToken", accessToken);
|
|
568
|
+
object.put("accessToken", accessToken != null ? accessToken : JSONObject.NULL);
|
|
506
569
|
if (scopes != null && scopes.length > 0) {
|
|
507
570
|
JSONArray scopesArray = new JSONArray();
|
|
508
571
|
for (String scope : scopes) {
|
|
@@ -601,18 +664,16 @@ public class GoogleProvider implements SocialProvider {
|
|
|
601
664
|
completer.setException(e);
|
|
602
665
|
}
|
|
603
666
|
} else {
|
|
604
|
-
// Access already granted, continue with user action
|
|
605
|
-
//
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
completer.setException(new RuntimeException("getAccessToken() is null"));
|
|
609
|
-
return;
|
|
610
|
-
}
|
|
611
|
-
} else if (this.mode == GoogleProviderLoginType.OFFLINE) {
|
|
667
|
+
// Access already granted, continue with user action.
|
|
668
|
+
// Online mode: a null access token is allowed (ID-token auth still succeeds).
|
|
669
|
+
// Offline mode still requires serverAuthCode.
|
|
670
|
+
if (this.mode == GoogleProviderLoginType.OFFLINE) {
|
|
612
671
|
if (authorizationResult.getServerAuthCode() == null) {
|
|
613
|
-
completer.setException(new RuntimeException("
|
|
672
|
+
completer.setException(new RuntimeException("getServerAuthCode() is null"));
|
|
614
673
|
return;
|
|
615
674
|
}
|
|
675
|
+
} else if (this.mode == GoogleProviderLoginType.ONLINE && authorizationResult.getAccessToken() == null) {
|
|
676
|
+
Log.i(LOG_TAG, "AuthorizationResult.getAccessToken() is null; continuing with ID token authentication");
|
|
616
677
|
}
|
|
617
678
|
|
|
618
679
|
completer.set(authorizationResult);
|
|
@@ -648,6 +709,8 @@ public class GoogleProvider implements SocialProvider {
|
|
|
648
709
|
}
|
|
649
710
|
|
|
650
711
|
GoogleIdTokenCredential googleIdTokenCredential = GoogleIdTokenCredential.createFrom(credential.getData());
|
|
712
|
+
String idToken = googleIdTokenCredential.getIdToken();
|
|
713
|
+
|
|
651
714
|
ListenableFuture<AuthorizationResult> future = getAuthorizationResult(forceRefreshToken);
|
|
652
715
|
|
|
653
716
|
// Use ExecutorService to retrieve the access token
|
|
@@ -658,27 +721,14 @@ public class GoogleProvider implements SocialProvider {
|
|
|
658
721
|
@Override
|
|
659
722
|
public void run() {
|
|
660
723
|
try {
|
|
661
|
-
AuthorizationResult
|
|
724
|
+
AuthorizationResult authResult = future.get();
|
|
662
725
|
if (GoogleProvider.this.mode == GoogleProviderLoginType.ONLINE) {
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
accessTokenObj.put("token", result.getAccessToken());
|
|
666
|
-
// accessTokenObj.put("userId", accessToken.userId);
|
|
667
|
-
|
|
668
|
-
resultObj.put("accessToken", accessTokenObj);
|
|
669
|
-
resultObj.put("profile", user);
|
|
670
|
-
resultObj.put("idToken", googleIdTokenCredential.getIdToken());
|
|
671
|
-
resultObj.put("responseType", "online");
|
|
672
|
-
response.put("result", resultObj);
|
|
673
|
-
persistState(googleIdTokenCredential.getIdToken(), result.getAccessToken());
|
|
674
|
-
call.resolve(response);
|
|
675
|
-
} else {
|
|
676
|
-
call.reject("Failed to get access token");
|
|
677
|
-
}
|
|
726
|
+
// accessToken may be null: Credential Manager auth still succeeded.
|
|
727
|
+
resolveOnlineLogin(call, response, resultObj, user, idToken, authResult.getAccessToken());
|
|
678
728
|
} else {
|
|
679
|
-
if (
|
|
729
|
+
if (authResult.getServerAuthCode() != null) {
|
|
680
730
|
resultObj.put("responseType", "offline");
|
|
681
|
-
resultObj.put("serverAuthCode",
|
|
731
|
+
resultObj.put("serverAuthCode", authResult.getServerAuthCode());
|
|
682
732
|
response.put("result", resultObj);
|
|
683
733
|
call.resolve(response);
|
|
684
734
|
} else {
|
|
@@ -686,7 +736,22 @@ public class GoogleProvider implements SocialProvider {
|
|
|
686
736
|
}
|
|
687
737
|
}
|
|
688
738
|
} catch (Exception e) {
|
|
689
|
-
|
|
739
|
+
// AuthorizationClient failed after Credential Manager returned a valid ID token.
|
|
740
|
+
// For authentication-only online scopes, resolve with idToken + profile.
|
|
741
|
+
if (
|
|
742
|
+
GoogleProvider.this.mode == GoogleProviderLoginType.ONLINE &&
|
|
743
|
+
idToken != null &&
|
|
744
|
+
isAuthenticationOnlyScopes(GoogleProvider.this.scopes)
|
|
745
|
+
) {
|
|
746
|
+
Log.w(
|
|
747
|
+
LOG_TAG,
|
|
748
|
+
"AuthorizationClient failed after successful Credential Manager auth; resolving with idToken only",
|
|
749
|
+
e
|
|
750
|
+
);
|
|
751
|
+
resolveOnlineLogin(call, response, resultObj, user, idToken, null);
|
|
752
|
+
} else {
|
|
753
|
+
call.reject("Error retrieving access token: " + e.getMessage());
|
|
754
|
+
}
|
|
690
755
|
} finally {
|
|
691
756
|
executor.shutdown();
|
|
692
757
|
}
|
|
@@ -1023,40 +1088,51 @@ public class GoogleProvider implements SocialProvider {
|
|
|
1023
1088
|
call.reject("getAuthorizationCode is not implemented when using offline mode");
|
|
1024
1089
|
return;
|
|
1025
1090
|
}
|
|
1026
|
-
if (GoogleProvider.this.idToken
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
}
|
|
1091
|
+
if (GoogleProvider.this.idToken == null || GoogleProvider.this.idToken.isEmpty()) {
|
|
1092
|
+
call.reject("User is not logged in");
|
|
1093
|
+
return;
|
|
1094
|
+
}
|
|
1095
|
+
try {
|
|
1096
|
+
boolean isValidIdToken = idTokenValid(GoogleProvider.this.idToken);
|
|
1097
|
+
if (!isValidIdToken) {
|
|
1098
|
+
rawLogout(
|
|
1099
|
+
new CredentialManagerCallback<>() {
|
|
1100
|
+
@Override
|
|
1101
|
+
public void onResult(Void unused) {
|
|
1102
|
+
call.reject("User is not logged in");
|
|
1103
|
+
}
|
|
1040
1104
|
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
call.reject("User is not logged in");
|
|
1046
|
-
}
|
|
1105
|
+
@Override
|
|
1106
|
+
public void onError(@NonNull Exception e) {
|
|
1107
|
+
Log.e(LOG_TAG, "Saved id token isn't valid, but logout failed", e);
|
|
1108
|
+
call.reject("User is not logged in");
|
|
1047
1109
|
}
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1110
|
+
}
|
|
1111
|
+
);
|
|
1112
|
+
return;
|
|
1113
|
+
}
|
|
1114
|
+
|
|
1115
|
+
// Access token may be null for authentication-only sessions.
|
|
1116
|
+
if (GoogleProvider.this.accessToken != null && !GoogleProvider.this.accessToken.isEmpty()) {
|
|
1117
|
+
ListenableFuture<Boolean> accessTokenValidFuture = accessTokenIsValid(GoogleProvider.this.accessToken);
|
|
1118
|
+
boolean isValidAccessToken = accessTokenValidFuture.get(7, TimeUnit.SECONDS);
|
|
1119
|
+
if (!isValidAccessToken) {
|
|
1120
|
+
// Keep the valid ID token session; clear only the stale access token.
|
|
1121
|
+
GoogleProvider.this.accessToken = null;
|
|
1122
|
+
persistState(GoogleProvider.this.idToken, null, GoogleProvider.this.scopes);
|
|
1053
1123
|
}
|
|
1054
|
-
} catch (Exception e) {
|
|
1055
|
-
Log.e(LOG_TAG, "Error validating tokens", e);
|
|
1056
|
-
call.reject("Error validating tokens: " + e.getMessage());
|
|
1057
1124
|
}
|
|
1058
|
-
|
|
1059
|
-
|
|
1125
|
+
|
|
1126
|
+
JSObject result = new JSObject().put("jwt", GoogleProvider.this.idToken);
|
|
1127
|
+
if (GoogleProvider.this.accessToken != null && !GoogleProvider.this.accessToken.isEmpty()) {
|
|
1128
|
+
result.put("accessToken", GoogleProvider.this.accessToken);
|
|
1129
|
+
} else {
|
|
1130
|
+
result.put("accessToken", JSONObject.NULL);
|
|
1131
|
+
}
|
|
1132
|
+
call.resolve(result);
|
|
1133
|
+
} catch (Exception e) {
|
|
1134
|
+
Log.e(LOG_TAG, "Error validating tokens", e);
|
|
1135
|
+
call.reject("Error validating tokens: " + e.getMessage());
|
|
1060
1136
|
}
|
|
1061
1137
|
}
|
|
1062
1138
|
|
|
@@ -1066,38 +1142,43 @@ public class GoogleProvider implements SocialProvider {
|
|
|
1066
1142
|
call.reject("isLoggedIn is not implemented when using offline mode");
|
|
1067
1143
|
return;
|
|
1068
1144
|
}
|
|
1069
|
-
if (GoogleProvider.this.idToken
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
}
|
|
1145
|
+
if (GoogleProvider.this.idToken == null || GoogleProvider.this.idToken.isEmpty()) {
|
|
1146
|
+
call.resolve(new JSObject().put("isLoggedIn", false));
|
|
1147
|
+
return;
|
|
1148
|
+
}
|
|
1149
|
+
try {
|
|
1150
|
+
boolean isValidIdToken = idTokenValid(GoogleProvider.this.idToken);
|
|
1151
|
+
if (!isValidIdToken) {
|
|
1152
|
+
rawLogout(
|
|
1153
|
+
new CredentialManagerCallback<>() {
|
|
1154
|
+
@Override
|
|
1155
|
+
public void onResult(Void unused) {
|
|
1156
|
+
call.resolve(new JSObject().put("isLoggedIn", false));
|
|
1157
|
+
}
|
|
1083
1158
|
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
call.resolve(new JSObject().put("isLoggedIn", false));
|
|
1089
|
-
}
|
|
1159
|
+
@Override
|
|
1160
|
+
public void onError(@NonNull Exception e) {
|
|
1161
|
+
Log.e(LOG_TAG, "Saved id token isn't valid, but logout failed", e);
|
|
1162
|
+
call.resolve(new JSObject().put("isLoggedIn", false));
|
|
1090
1163
|
}
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1164
|
+
}
|
|
1165
|
+
);
|
|
1166
|
+
return;
|
|
1167
|
+
}
|
|
1168
|
+
|
|
1169
|
+
// Valid ID token is enough for authentication-only sessions.
|
|
1170
|
+
if (GoogleProvider.this.accessToken != null && !GoogleProvider.this.accessToken.isEmpty()) {
|
|
1171
|
+
ListenableFuture<Boolean> accessTokenValidFuture = accessTokenIsValid(GoogleProvider.this.accessToken);
|
|
1172
|
+
boolean isValidAccessToken = accessTokenValidFuture.get(7, TimeUnit.SECONDS);
|
|
1173
|
+
if (!isValidAccessToken) {
|
|
1174
|
+
GoogleProvider.this.accessToken = null;
|
|
1175
|
+
persistState(GoogleProvider.this.idToken, null, GoogleProvider.this.scopes);
|
|
1094
1176
|
}
|
|
1095
|
-
} catch (Exception e) {
|
|
1096
|
-
Log.e(LOG_TAG, "Error validating tokens", e);
|
|
1097
|
-
call.reject("Error validating tokens: " + e.getMessage());
|
|
1098
1177
|
}
|
|
1099
|
-
|
|
1100
|
-
|
|
1178
|
+
call.resolve(new JSObject().put("isLoggedIn", true));
|
|
1179
|
+
} catch (Exception e) {
|
|
1180
|
+
Log.e(LOG_TAG, "Error validating tokens", e);
|
|
1181
|
+
call.reject("Error validating tokens: " + e.getMessage());
|
|
1101
1182
|
}
|
|
1102
1183
|
}
|
|
1103
1184
|
|
|
@@ -1116,12 +1197,15 @@ public class GoogleProvider implements SocialProvider {
|
|
|
1116
1197
|
this.credentialManager = CredentialManager.create(activity);
|
|
1117
1198
|
}
|
|
1118
1199
|
|
|
1119
|
-
// If
|
|
1200
|
+
// If tokens needed for the current session type are still valid, do nothing.
|
|
1120
1201
|
try {
|
|
1121
|
-
if (GoogleProvider.this.idToken != null
|
|
1202
|
+
if (GoogleProvider.this.idToken != null) {
|
|
1122
1203
|
boolean isValidIdToken = idTokenValid(GoogleProvider.this.idToken);
|
|
1123
|
-
boolean
|
|
1124
|
-
if (
|
|
1204
|
+
boolean accessOk = true;
|
|
1205
|
+
if (GoogleProvider.this.accessToken != null && !GoogleProvider.this.accessToken.isEmpty()) {
|
|
1206
|
+
accessOk = accessTokenIsValid(GoogleProvider.this.accessToken).get(7, TimeUnit.SECONDS);
|
|
1207
|
+
}
|
|
1208
|
+
if (isValidIdToken && accessOk) {
|
|
1125
1209
|
call.resolve();
|
|
1126
1210
|
return;
|
|
1127
1211
|
}
|
|
@@ -1137,7 +1221,7 @@ public class GoogleProvider implements SocialProvider {
|
|
|
1137
1221
|
}
|
|
1138
1222
|
|
|
1139
1223
|
// 1) Retrieve a fresh ID token via Credential Manager (may be silent or may require user interaction).
|
|
1140
|
-
// 2) Retrieve a fresh access token via the Authorization API.
|
|
1224
|
+
// 2) Retrieve a fresh access token via the Authorization API (may be null for auth-only sessions).
|
|
1141
1225
|
GetCredentialRequest request = new GetCredentialRequest.Builder()
|
|
1142
1226
|
.addCredentialOption(
|
|
1143
1227
|
new GetGoogleIdOption.Builder()
|
|
@@ -1184,15 +1268,25 @@ public class GoogleProvider implements SocialProvider {
|
|
|
1184
1268
|
try {
|
|
1185
1269
|
AuthorizationResult authResult = future.get(60, TimeUnit.SECONDS);
|
|
1186
1270
|
String newAccessToken = authResult.getAccessToken();
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1271
|
+
// Persist ID token even when access token is unavailable.
|
|
1272
|
+
persistState(
|
|
1273
|
+
newIdToken,
|
|
1274
|
+
newAccessToken != null && !newAccessToken.isEmpty() ? newAccessToken : null,
|
|
1275
|
+
GoogleProvider.this.scopes
|
|
1276
|
+
);
|
|
1193
1277
|
call.resolve();
|
|
1194
1278
|
} catch (Exception e) {
|
|
1195
|
-
|
|
1279
|
+
if (isAuthenticationOnlyScopes(GoogleProvider.this.scopes)) {
|
|
1280
|
+
try {
|
|
1281
|
+
// Authorization failed; still keep the refreshed ID token.
|
|
1282
|
+
persistState(newIdToken, null, GoogleProvider.this.scopes);
|
|
1283
|
+
call.resolve();
|
|
1284
|
+
} catch (JSONException persistError) {
|
|
1285
|
+
call.reject("Failed to refresh tokens: " + e.getMessage());
|
|
1286
|
+
}
|
|
1287
|
+
} else {
|
|
1288
|
+
call.reject("Failed to refresh tokens: " + e.getMessage());
|
|
1289
|
+
}
|
|
1196
1290
|
} finally {
|
|
1197
1291
|
authExecutor.shutdown();
|
|
1198
1292
|
}
|