@capgo/capacitor-social-login 8.4.4 → 8.5.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 +67 -3
- package/android/src/main/java/ee/forgr/capacitor/social/login/GoogleProvider.java +121 -4
- package/android/src/main/java/ee/forgr/capacitor/social/login/GoogleRestoreCredentialBackupHelper.java +72 -0
- package/android/src/main/java/ee/forgr/capacitor/social/login/GoogleRestoreCredentialHelper.java +319 -0
- package/android/src/main/java/ee/forgr/capacitor/social/login/SocialLoginPlugin.java +28 -2
- package/dist/docs.json +137 -5
- package/dist/esm/definitions.d.ts +71 -1
- package/dist/esm/definitions.js.map +1 -1
- package/dist/esm/google-provider.js +65 -80
- package/dist/esm/google-provider.js.map +1 -1
- package/dist/esm/oauth-popup-bridge.d.ts +35 -0
- package/dist/esm/oauth-popup-bridge.js +182 -0
- package/dist/esm/oauth-popup-bridge.js.map +1 -0
- package/dist/esm/oauth-popup-redirect.d.ts +1 -0
- package/dist/esm/oauth-popup-redirect.js +102 -0
- package/dist/esm/oauth-popup-redirect.js.map +1 -0
- package/dist/esm/social-login.d.ts +1 -0
- package/dist/esm/social-login.js +2 -0
- package/dist/esm/social-login.js.map +1 -1
- package/dist/esm/web.d.ts +0 -1
- package/dist/esm/web.js +33 -47
- package/dist/esm/web.js.map +1 -1
- package/dist/plugin.cjs.js +2031 -1783
- package/dist/plugin.cjs.js.map +1 -1
- package/dist/plugin.js +2031 -1783
- package/dist/plugin.js.map +1 -1
- package/ios/Sources/SocialLoginPlugin/SocialLoginPlugin.swift +6 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1430,6 +1430,37 @@ Configuration for a single OAuth2 provider instance
|
|
|
1430
1430
|
| **`status`** | <code>'authorized' \| 'denied' \| 'notDetermined' \| 'restricted'</code> | App tracking authorization status |
|
|
1431
1431
|
|
|
1432
1432
|
|
|
1433
|
+
#### GoogleCreateRestoreCredentialResponse
|
|
1434
|
+
|
|
1435
|
+
Response from `google#createRestoreCredential`.
|
|
1436
|
+
|
|
1437
|
+
Send `responseJson` to your server to complete restore key registration (same as passkey creation).
|
|
1438
|
+
|
|
1439
|
+
| Prop | Type | Description |
|
|
1440
|
+
| ------------------ | ------------------- | ------------------------------------------------------------------------- |
|
|
1441
|
+
| **`responseJson`** | <code>string</code> | Public key credential registration response JSON from Credential Manager. |
|
|
1442
|
+
|
|
1443
|
+
|
|
1444
|
+
#### GoogleGetRestoreCredentialResponse
|
|
1445
|
+
|
|
1446
|
+
Response from `google#getRestoreCredential`.
|
|
1447
|
+
|
|
1448
|
+
Send `responseJson` to your server to sign the user in (same server path as passkey authentication).
|
|
1449
|
+
|
|
1450
|
+
| Prop | Type | Description |
|
|
1451
|
+
| ------------------ | ------------------- | ------------------------------------------------------------------------ |
|
|
1452
|
+
| **`responseJson`** | <code>string</code> | Restore credential authentication response JSON from Credential Manager. |
|
|
1453
|
+
|
|
1454
|
+
|
|
1455
|
+
#### GoogleClearRestoreCredentialResponse
|
|
1456
|
+
|
|
1457
|
+
Response from `google#clearRestoreCredential`.
|
|
1458
|
+
|
|
1459
|
+
| Prop | Type | Description |
|
|
1460
|
+
| ------------- | -------------------- | -------------------------------------------------------------------- |
|
|
1461
|
+
| **`cleared`** | <code>boolean</code> | Whether the restore credential clear request completed successfully. |
|
|
1462
|
+
|
|
1463
|
+
|
|
1433
1464
|
#### FacebookGetProfileOptions
|
|
1434
1465
|
|
|
1435
1466
|
| Prop | Type | Description |
|
|
@@ -1437,6 +1468,32 @@ Configuration for a single OAuth2 provider instance
|
|
|
1437
1468
|
| **`fields`** | <code>string[]</code> | Fields to retrieve from Facebook profile |
|
|
1438
1469
|
|
|
1439
1470
|
|
|
1471
|
+
#### GoogleCreateRestoreCredentialOptions
|
|
1472
|
+
|
|
1473
|
+
Options for `providerSpecificCall` with `google#createRestoreCredential`.
|
|
1474
|
+
|
|
1475
|
+
Android-only. Creates a Restore Credential (restore key) via Credential Manager after the user
|
|
1476
|
+
signs in. Your backend must supply WebAuthn PublicKeyCredentialCreationOptionsJSON (the same
|
|
1477
|
+
FIDO2/passkey registration flow used for passkeys).
|
|
1478
|
+
|
|
1479
|
+
| Prop | Type | Description | Default |
|
|
1480
|
+
| -------------------------- | -------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ----------------- |
|
|
1481
|
+
| **`requestJson`** | <code>string</code> | Credential creation options from your server in WebAuthn PublicKeyCredentialCreationOptionsJSON format. | |
|
|
1482
|
+
| **`isCloudBackupEnabled`** | <code>boolean</code> | When `true` (default), the restore key is backed up to the cloud when the user has backup and end-to-end encryption (e.g. screen lock) enabled. If cloud backup creation fails with `E2eeUnavailableException` on Android, the plugin automatically retries with local-only storage. | <code>true</code> |
|
|
1483
|
+
|
|
1484
|
+
|
|
1485
|
+
#### GoogleGetRestoreCredentialOptions
|
|
1486
|
+
|
|
1487
|
+
Options for `providerSpecificCall` with `google#getRestoreCredential`.
|
|
1488
|
+
|
|
1489
|
+
Android-only. Retrieves a Restore Credential silently (e.g. on first launch on a new device or from
|
|
1490
|
+
BackupAgent `onRestoreFinished`). Your backend must supply WebAuthn authentication request JSON.
|
|
1491
|
+
|
|
1492
|
+
| Prop | Type | Description |
|
|
1493
|
+
| ----------------- | ------------------- | ----------------------------------------------------------------------------------- |
|
|
1494
|
+
| **`requestJson`** | <code>string</code> | Credential request options from your server (WebAuthn authentication request JSON). |
|
|
1495
|
+
|
|
1496
|
+
|
|
1440
1497
|
#### OpenSecureWindowResponse
|
|
1441
1498
|
|
|
1442
1499
|
| Prop | Type | Description |
|
|
@@ -1492,23 +1549,30 @@ Construct a type with a set of properties K of type T
|
|
|
1492
1549
|
|
|
1493
1550
|
#### ProviderSpecificCallResponseMap
|
|
1494
1551
|
|
|
1495
|
-
<code>{ 'facebook#getProfile': <a href="#facebookgetprofileresponse">FacebookGetProfileResponse</a>; 'facebook#requestTracking': <a href="#facebookrequesttrackingresponse">FacebookRequestTrackingResponse</a>; }</code>
|
|
1552
|
+
<code>{ 'facebook#getProfile': <a href="#facebookgetprofileresponse">FacebookGetProfileResponse</a>; 'facebook#requestTracking': <a href="#facebookrequesttrackingresponse">FacebookRequestTrackingResponse</a>; 'google#createRestoreCredential': <a href="#googlecreaterestorecredentialresponse">GoogleCreateRestoreCredentialResponse</a>; 'google#getRestoreCredential': <a href="#googlegetrestorecredentialresponse">GoogleGetRestoreCredentialResponse</a>; 'google#clearRestoreCredential': <a href="#googleclearrestorecredentialresponse">GoogleClearRestoreCredentialResponse</a>; }</code>
|
|
1496
1553
|
|
|
1497
1554
|
|
|
1498
1555
|
#### ProviderSpecificCall
|
|
1499
1556
|
|
|
1500
|
-
<code>'facebook#getProfile' | 'facebook#requestTracking'</code>
|
|
1557
|
+
<code>'facebook#getProfile' | 'facebook#requestTracking' | 'google#createRestoreCredential' | 'google#getRestoreCredential' | 'google#clearRestoreCredential'</code>
|
|
1501
1558
|
|
|
1502
1559
|
|
|
1503
1560
|
#### ProviderSpecificCallOptionsMap
|
|
1504
1561
|
|
|
1505
|
-
<code>{ 'facebook#getProfile': <a href="#facebookgetprofileoptions">FacebookGetProfileOptions</a>; 'facebook#requestTracking': <a href="#facebookrequesttrackingoptions">FacebookRequestTrackingOptions</a>; }</code>
|
|
1562
|
+
<code>{ 'facebook#getProfile': <a href="#facebookgetprofileoptions">FacebookGetProfileOptions</a>; 'facebook#requestTracking': <a href="#facebookrequesttrackingoptions">FacebookRequestTrackingOptions</a>; 'google#createRestoreCredential': <a href="#googlecreaterestorecredentialoptions">GoogleCreateRestoreCredentialOptions</a>; 'google#getRestoreCredential': <a href="#googlegetrestorecredentialoptions">GoogleGetRestoreCredentialOptions</a>; 'google#clearRestoreCredential': <a href="#googleclearrestorecredentialoptions">GoogleClearRestoreCredentialOptions</a>; }</code>
|
|
1506
1563
|
|
|
1507
1564
|
|
|
1508
1565
|
#### FacebookRequestTrackingOptions
|
|
1509
1566
|
|
|
1510
1567
|
<code><a href="#record">Record</a><string, never></code>
|
|
1511
1568
|
|
|
1569
|
+
|
|
1570
|
+
#### GoogleClearRestoreCredentialOptions
|
|
1571
|
+
|
|
1572
|
+
Options for `google#clearRestoreCredential` (no fields).
|
|
1573
|
+
|
|
1574
|
+
<code><a href="#record">Record</a><string, never></code>
|
|
1575
|
+
|
|
1512
1576
|
</docgen-api>
|
|
1513
1577
|
|
|
1514
1578
|
|
|
@@ -1040,16 +1040,34 @@ public class GoogleProvider implements SocialProvider {
|
|
|
1040
1040
|
);
|
|
1041
1041
|
}
|
|
1042
1042
|
|
|
1043
|
+
private void completeLocalLogout(CredentialManagerCallback<Void, Exception> handler) {
|
|
1044
|
+
context.getSharedPreferences(SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE).edit().clear().apply();
|
|
1045
|
+
GoogleProvider.this.accessToken = null;
|
|
1046
|
+
GoogleProvider.this.idToken = null;
|
|
1047
|
+
handler.onResult(null);
|
|
1048
|
+
}
|
|
1049
|
+
|
|
1043
1050
|
private void rawLogout(CredentialManagerCallback<Void, Exception> handler) {
|
|
1044
1051
|
Log.i(LOG_TAG, "Logout requested");
|
|
1045
1052
|
clearCredentialManagerState(
|
|
1046
1053
|
new CredentialManagerCallback<Void, Exception>() {
|
|
1047
1054
|
@Override
|
|
1048
1055
|
public void onResult(Void unused) {
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1056
|
+
GoogleRestoreCredentialHelper.clearRestoreCredential(
|
|
1057
|
+
context,
|
|
1058
|
+
new CredentialManagerCallback<Void, Exception>() {
|
|
1059
|
+
@Override
|
|
1060
|
+
public void onResult(Void unusedRestore) {
|
|
1061
|
+
completeLocalLogout(handler);
|
|
1062
|
+
}
|
|
1063
|
+
|
|
1064
|
+
@Override
|
|
1065
|
+
public void onError(@NonNull Exception e) {
|
|
1066
|
+
Log.e(LOG_TAG, "Failed to clear restore credential during logout", e);
|
|
1067
|
+
handler.onError(e);
|
|
1068
|
+
}
|
|
1069
|
+
}
|
|
1070
|
+
);
|
|
1053
1071
|
}
|
|
1054
1072
|
|
|
1055
1073
|
@Override
|
|
@@ -1061,6 +1079,105 @@ public class GoogleProvider implements SocialProvider {
|
|
|
1061
1079
|
);
|
|
1062
1080
|
}
|
|
1063
1081
|
|
|
1082
|
+
public void createRestoreCredential(PluginCall call) {
|
|
1083
|
+
JSObject options = call.getObject("options", new JSObject());
|
|
1084
|
+
String requestJson = options.getString("requestJson");
|
|
1085
|
+
boolean isCloudBackupEnabled = !options.has("isCloudBackupEnabled") || options.getBoolean("isCloudBackupEnabled", true);
|
|
1086
|
+
|
|
1087
|
+
try {
|
|
1088
|
+
GoogleRestoreCredentialHelper.validateRequestJson(requestJson);
|
|
1089
|
+
} catch (IllegalArgumentException e) {
|
|
1090
|
+
call.reject(e.getMessage());
|
|
1091
|
+
return;
|
|
1092
|
+
}
|
|
1093
|
+
|
|
1094
|
+
GoogleRestoreCredentialHelper.createRestoreCredential(
|
|
1095
|
+
context,
|
|
1096
|
+
requestJson,
|
|
1097
|
+
isCloudBackupEnabled,
|
|
1098
|
+
new CredentialManagerCallback<
|
|
1099
|
+
androidx.credentials.CreateRestoreCredentialResponse,
|
|
1100
|
+
androidx.credentials.exceptions.CreateCredentialException
|
|
1101
|
+
>() {
|
|
1102
|
+
@Override
|
|
1103
|
+
public void onResult(androidx.credentials.CreateRestoreCredentialResponse response) {
|
|
1104
|
+
try {
|
|
1105
|
+
JSObject result = new JSObject().put("responseJson", response.getResponseJson());
|
|
1106
|
+
call.resolve(result);
|
|
1107
|
+
} catch (Exception e) {
|
|
1108
|
+
call.reject("Failed to create restore credential: " + e.getMessage());
|
|
1109
|
+
}
|
|
1110
|
+
}
|
|
1111
|
+
|
|
1112
|
+
@Override
|
|
1113
|
+
public void onError(@NonNull androidx.credentials.exceptions.CreateCredentialException e) {
|
|
1114
|
+
String message = e.getErrorMessage() != null ? e.getErrorMessage().toString() : e.getMessage();
|
|
1115
|
+
call.reject("Failed to create restore credential: " + message);
|
|
1116
|
+
}
|
|
1117
|
+
}
|
|
1118
|
+
);
|
|
1119
|
+
}
|
|
1120
|
+
|
|
1121
|
+
public void getRestoreCredential(PluginCall call) {
|
|
1122
|
+
JSObject options = call.getObject("options", new JSObject());
|
|
1123
|
+
String requestJson = options.getString("requestJson");
|
|
1124
|
+
|
|
1125
|
+
try {
|
|
1126
|
+
GoogleRestoreCredentialHelper.validateRequestJson(requestJson);
|
|
1127
|
+
} catch (IllegalArgumentException e) {
|
|
1128
|
+
call.reject(e.getMessage());
|
|
1129
|
+
return;
|
|
1130
|
+
}
|
|
1131
|
+
|
|
1132
|
+
GoogleRestoreCredentialHelper.getRestoreCredential(
|
|
1133
|
+
context,
|
|
1134
|
+
requestJson,
|
|
1135
|
+
new CredentialManagerCallback<String, Exception>() {
|
|
1136
|
+
@Override
|
|
1137
|
+
public void onResult(String responseJson) {
|
|
1138
|
+
try {
|
|
1139
|
+
JSObject result = new JSObject().put("responseJson", responseJson);
|
|
1140
|
+
call.resolve(result);
|
|
1141
|
+
} catch (Exception e) {
|
|
1142
|
+
call.reject("Failed to get restore credential: " + e.getMessage());
|
|
1143
|
+
}
|
|
1144
|
+
}
|
|
1145
|
+
|
|
1146
|
+
@Override
|
|
1147
|
+
public void onError(@NonNull Exception e) {
|
|
1148
|
+
if (e instanceof NoCredentialException) {
|
|
1149
|
+
call.reject("No restore credential available");
|
|
1150
|
+
return;
|
|
1151
|
+
}
|
|
1152
|
+
if (e instanceof GetCredentialCancellationException) {
|
|
1153
|
+
call.reject("Restore credential request cancelled", USER_CANCELLED_CODE, e);
|
|
1154
|
+
return;
|
|
1155
|
+
}
|
|
1156
|
+
String message = e.getMessage() != null ? e.getMessage() : e.getClass().getSimpleName();
|
|
1157
|
+
call.reject("Failed to get restore credential: " + message);
|
|
1158
|
+
}
|
|
1159
|
+
}
|
|
1160
|
+
);
|
|
1161
|
+
}
|
|
1162
|
+
|
|
1163
|
+
public void clearRestoreCredential(PluginCall call) {
|
|
1164
|
+
GoogleRestoreCredentialHelper.clearRestoreCredential(
|
|
1165
|
+
context,
|
|
1166
|
+
new CredentialManagerCallback<Void, Exception>() {
|
|
1167
|
+
@Override
|
|
1168
|
+
public void onResult(Void unused) {
|
|
1169
|
+
call.resolve(new JSObject().put("cleared", true));
|
|
1170
|
+
}
|
|
1171
|
+
|
|
1172
|
+
@Override
|
|
1173
|
+
public void onError(@NonNull Exception e) {
|
|
1174
|
+
String message = e.getMessage() != null ? e.getMessage() : e.getClass().getSimpleName();
|
|
1175
|
+
call.reject("Failed to clear restore credential: " + message);
|
|
1176
|
+
}
|
|
1177
|
+
}
|
|
1178
|
+
);
|
|
1179
|
+
}
|
|
1180
|
+
|
|
1064
1181
|
@Override
|
|
1065
1182
|
public void logout(PluginCall call) {
|
|
1066
1183
|
if (this.mode == GoogleProviderLoginType.OFFLINE) {
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
package ee.forgr.capacitor.social.login;
|
|
2
|
+
|
|
3
|
+
import android.content.Context;
|
|
4
|
+
import androidx.annotation.NonNull;
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Helper for silent Google Restore Credential retrieval after Android backup restore.
|
|
8
|
+
*
|
|
9
|
+
* <p>Register a {@link android.app.backup.BackupAgent} in your app and call
|
|
10
|
+
* {@link #onRestoreFinished(Context, String, Callback)} from {@code onRestoreFinished()} (not
|
|
11
|
+
* {@code onRestore()} — the latter is only used for key-value backups).
|
|
12
|
+
*
|
|
13
|
+
* <p>Your server must supply WebAuthn authentication request JSON ({@code requestJson}). Send the
|
|
14
|
+
* returned {@code responseJson} to your backend to complete sign-in (same path as passkey auth).
|
|
15
|
+
*
|
|
16
|
+
* @see <a href="https://developer.android.com/identity/sign-in/restore-credentials-implementation">Restore Credentials implementation guide</a>
|
|
17
|
+
*/
|
|
18
|
+
public final class GoogleRestoreCredentialBackupHelper {
|
|
19
|
+
|
|
20
|
+
private GoogleRestoreCredentialBackupHelper() {}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Callback for restore credential retrieval after backup restore.
|
|
24
|
+
*/
|
|
25
|
+
public interface Callback {
|
|
26
|
+
/** Restore credential authentication response JSON for your server. */
|
|
27
|
+
void onSuccess(String responseJson);
|
|
28
|
+
|
|
29
|
+
/** No restore credential is available on this device (user not eligible or not yet created). */
|
|
30
|
+
void onNoCredential();
|
|
31
|
+
|
|
32
|
+
/** Retrieval failed or was cancelled. */
|
|
33
|
+
void onError(String message);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Fetch a restore credential after app data backup restore completes.
|
|
38
|
+
*
|
|
39
|
+
* @param context Application or activity context
|
|
40
|
+
* @param requestJson WebAuthn authentication request JSON from your server
|
|
41
|
+
* @param callback Result handler
|
|
42
|
+
*/
|
|
43
|
+
public static void onRestoreFinished(@NonNull Context context, @NonNull String requestJson, @NonNull Callback callback) {
|
|
44
|
+
try {
|
|
45
|
+
GoogleRestoreCredentialHelper.validateRequestJson(requestJson);
|
|
46
|
+
} catch (IllegalArgumentException e) {
|
|
47
|
+
callback.onError(e.getMessage());
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
GoogleRestoreCredentialHelper.getRestoreCredentialSynchronously(
|
|
52
|
+
context,
|
|
53
|
+
requestJson,
|
|
54
|
+
new androidx.credentials.CredentialManagerCallback<String, Exception>() {
|
|
55
|
+
@Override
|
|
56
|
+
public void onResult(String responseJson) {
|
|
57
|
+
callback.onSuccess(responseJson);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
@Override
|
|
61
|
+
public void onError(@NonNull Exception e) {
|
|
62
|
+
if (e instanceof androidx.credentials.exceptions.NoCredentialException) {
|
|
63
|
+
callback.onNoCredential();
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
String message = e.getMessage() != null ? e.getMessage() : e.getClass().getSimpleName();
|
|
67
|
+
callback.onError(message);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
}
|
package/android/src/main/java/ee/forgr/capacitor/social/login/GoogleRestoreCredentialHelper.java
ADDED
|
@@ -0,0 +1,319 @@
|
|
|
1
|
+
package ee.forgr.capacitor.social.login;
|
|
2
|
+
|
|
3
|
+
import android.content.Context;
|
|
4
|
+
import android.util.Log;
|
|
5
|
+
import androidx.annotation.NonNull;
|
|
6
|
+
import androidx.concurrent.futures.CallbackToFutureAdapter;
|
|
7
|
+
import androidx.credentials.ClearCredentialStateRequest;
|
|
8
|
+
import androidx.credentials.CreateCredentialResponse;
|
|
9
|
+
import androidx.credentials.CreateRestoreCredentialRequest;
|
|
10
|
+
import androidx.credentials.CreateRestoreCredentialResponse;
|
|
11
|
+
import androidx.credentials.Credential;
|
|
12
|
+
import androidx.credentials.CredentialManager;
|
|
13
|
+
import androidx.credentials.CredentialManagerCallback;
|
|
14
|
+
import androidx.credentials.CustomCredential;
|
|
15
|
+
import androidx.credentials.GetCredentialRequest;
|
|
16
|
+
import androidx.credentials.GetCredentialResponse;
|
|
17
|
+
import androidx.credentials.GetRestoreCredentialOption;
|
|
18
|
+
import androidx.credentials.RestoreCredential;
|
|
19
|
+
import androidx.credentials.exceptions.ClearCredentialException;
|
|
20
|
+
import androidx.credentials.exceptions.CreateCredentialException;
|
|
21
|
+
import androidx.credentials.exceptions.CreateCredentialUnknownException;
|
|
22
|
+
import androidx.credentials.exceptions.GetCredentialCancellationException;
|
|
23
|
+
import androidx.credentials.exceptions.GetCredentialException;
|
|
24
|
+
import androidx.credentials.exceptions.NoCredentialException;
|
|
25
|
+
import androidx.credentials.exceptions.restorecredential.E2eeUnavailableException;
|
|
26
|
+
import com.google.common.util.concurrent.ListenableFuture;
|
|
27
|
+
import java.util.concurrent.ExecutionException;
|
|
28
|
+
import java.util.concurrent.ExecutorService;
|
|
29
|
+
import java.util.concurrent.Executors;
|
|
30
|
+
import java.util.concurrent.TimeUnit;
|
|
31
|
+
import java.util.concurrent.TimeoutException;
|
|
32
|
+
import org.json.JSONException;
|
|
33
|
+
import org.json.JSONObject;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Shared Restore Credentials operations for Google Sign-In (Android Credential Manager).
|
|
37
|
+
*
|
|
38
|
+
* <p>Used by {@link GoogleProvider} and {@link GoogleRestoreCredentialBackupHelper} so apps can
|
|
39
|
+
* retrieve restore credentials from {@link android.app.backup.BackupAgent#onRestoreFinished()}
|
|
40
|
+
* without going through the Capacitor bridge.
|
|
41
|
+
*/
|
|
42
|
+
public final class GoogleRestoreCredentialHelper {
|
|
43
|
+
|
|
44
|
+
private static final String LOG_TAG = "GoogleRestoreCredential";
|
|
45
|
+
|
|
46
|
+
private GoogleRestoreCredentialHelper() {}
|
|
47
|
+
|
|
48
|
+
public static ClearCredentialStateRequest buildClearRestoreCredentialRequest() {
|
|
49
|
+
return new ClearCredentialStateRequest(ClearCredentialStateRequest.TYPE_CLEAR_RESTORE_CREDENTIAL);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
public static void validateRequestJson(String requestJson) {
|
|
53
|
+
if (requestJson == null || requestJson.trim().isEmpty()) {
|
|
54
|
+
throw new IllegalArgumentException("requestJson is required");
|
|
55
|
+
}
|
|
56
|
+
try {
|
|
57
|
+
new JSONObject(requestJson);
|
|
58
|
+
} catch (JSONException e) {
|
|
59
|
+
throw new IllegalArgumentException("requestJson must be valid JSON");
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
static void validateCreateRequestJson(String requestJson) {
|
|
64
|
+
validateRequestJson(requestJson);
|
|
65
|
+
try {
|
|
66
|
+
JSONObject object = new JSONObject(requestJson);
|
|
67
|
+
if (!object.has("challenge")) {
|
|
68
|
+
throw new IllegalArgumentException("requestJson must include challenge");
|
|
69
|
+
}
|
|
70
|
+
JSONObject user = object.optJSONObject("user");
|
|
71
|
+
if (user == null || !user.has("id")) {
|
|
72
|
+
throw new IllegalArgumentException("requestJson must include user.id");
|
|
73
|
+
}
|
|
74
|
+
} catch (JSONException e) {
|
|
75
|
+
throw new IllegalArgumentException("requestJson must be valid JSON");
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
static void validateGetRequestJson(String requestJson) {
|
|
80
|
+
validateRequestJson(requestJson);
|
|
81
|
+
try {
|
|
82
|
+
JSONObject object = new JSONObject(requestJson);
|
|
83
|
+
if (!object.has("challenge")) {
|
|
84
|
+
throw new IllegalArgumentException("requestJson must include challenge");
|
|
85
|
+
}
|
|
86
|
+
} catch (JSONException e) {
|
|
87
|
+
throw new IllegalArgumentException("requestJson must be valid JSON");
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
private static void reportCreateRequestJsonError(
|
|
92
|
+
IllegalArgumentException error,
|
|
93
|
+
CredentialManagerCallback<CreateRestoreCredentialResponse, CreateCredentialException> callback
|
|
94
|
+
) {
|
|
95
|
+
String message = error.getMessage() != null ? error.getMessage() : "Invalid requestJson";
|
|
96
|
+
callback.onError(new CreateCredentialUnknownException(message));
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
public static CredentialManager credentialManagerOrCreate(Context context) {
|
|
100
|
+
return CredentialManager.create(context);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
public static void createRestoreCredential(
|
|
104
|
+
Context context,
|
|
105
|
+
String requestJson,
|
|
106
|
+
boolean isCloudBackupEnabled,
|
|
107
|
+
CredentialManagerCallback<CreateRestoreCredentialResponse, CreateCredentialException> callback
|
|
108
|
+
) {
|
|
109
|
+
try {
|
|
110
|
+
validateCreateRequestJson(requestJson);
|
|
111
|
+
CreateRestoreCredentialRequest request = new CreateRestoreCredentialRequest(requestJson, isCloudBackupEnabled);
|
|
112
|
+
CredentialManager credentialManager = credentialManagerOrCreate(context);
|
|
113
|
+
createRestoreCredentialInternal(credentialManager, context, request, isCloudBackupEnabled, requestJson, callback);
|
|
114
|
+
} catch (IllegalArgumentException e) {
|
|
115
|
+
reportCreateRequestJsonError(e, callback);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
private static void createRestoreCredentialInternal(
|
|
120
|
+
CredentialManager credentialManager,
|
|
121
|
+
Context context,
|
|
122
|
+
CreateRestoreCredentialRequest request,
|
|
123
|
+
boolean isCloudBackupEnabled,
|
|
124
|
+
String requestJson,
|
|
125
|
+
CredentialManagerCallback<CreateRestoreCredentialResponse, CreateCredentialException> callback
|
|
126
|
+
) {
|
|
127
|
+
ExecutorService executor = Executors.newSingleThreadExecutor();
|
|
128
|
+
credentialManager.createCredentialAsync(
|
|
129
|
+
context,
|
|
130
|
+
request,
|
|
131
|
+
null,
|
|
132
|
+
executor,
|
|
133
|
+
new CredentialManagerCallback<CreateCredentialResponse, CreateCredentialException>() {
|
|
134
|
+
@Override
|
|
135
|
+
public void onResult(CreateCredentialResponse response) {
|
|
136
|
+
executor.shutdown();
|
|
137
|
+
if (response instanceof CreateRestoreCredentialResponse) {
|
|
138
|
+
callback.onResult((CreateRestoreCredentialResponse) response);
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
try {
|
|
142
|
+
CreateRestoreCredentialResponse typed = CreateRestoreCredentialResponse.createFrom(response.getData());
|
|
143
|
+
callback.onResult(typed);
|
|
144
|
+
} catch (Exception e) {
|
|
145
|
+
callback.onError(
|
|
146
|
+
new CreateCredentialUnknownException("Unexpected create credential response type: " + response.getType())
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
@Override
|
|
152
|
+
public void onError(@NonNull CreateCredentialException e) {
|
|
153
|
+
if (isCloudBackupEnabled && e instanceof E2eeUnavailableException) {
|
|
154
|
+
executor.shutdown();
|
|
155
|
+
Log.w(LOG_TAG, "Cloud backup unavailable for restore credential; retrying with local-only storage.");
|
|
156
|
+
try {
|
|
157
|
+
CreateRestoreCredentialRequest retryRequest = new CreateRestoreCredentialRequest(requestJson, false);
|
|
158
|
+
createRestoreCredentialInternal(credentialManager, context, retryRequest, false, requestJson, callback);
|
|
159
|
+
} catch (IllegalArgumentException retryError) {
|
|
160
|
+
reportCreateRequestJsonError(retryError, callback);
|
|
161
|
+
}
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
executor.shutdown();
|
|
165
|
+
callback.onError(e);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
public static void getRestoreCredential(Context context, String requestJson, CredentialManagerCallback<String, Exception> callback) {
|
|
172
|
+
try {
|
|
173
|
+
validateGetRequestJson(requestJson);
|
|
174
|
+
GetRestoreCredentialOption restoreOption = new GetRestoreCredentialOption(requestJson);
|
|
175
|
+
CredentialManager credentialManager = credentialManagerOrCreate(context);
|
|
176
|
+
GetCredentialRequest getRequest = new GetCredentialRequest.Builder().addCredentialOption(restoreOption).build();
|
|
177
|
+
getRestoreCredentialInternal(credentialManager, context, getRequest, callback);
|
|
178
|
+
} catch (IllegalArgumentException e) {
|
|
179
|
+
callback.onError(e);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Blocks until restore credential retrieval completes. Intended for {@link
|
|
185
|
+
* android.app.backup.BackupAgent#onRestoreFinished()} where the callback must finish before the
|
|
186
|
+
* method returns.
|
|
187
|
+
*/
|
|
188
|
+
public static void getRestoreCredentialSynchronously(
|
|
189
|
+
Context context,
|
|
190
|
+
String requestJson,
|
|
191
|
+
CredentialManagerCallback<String, Exception> callback
|
|
192
|
+
) {
|
|
193
|
+
try {
|
|
194
|
+
validateGetRequestJson(requestJson);
|
|
195
|
+
GetRestoreCredentialOption restoreOption = new GetRestoreCredentialOption(requestJson);
|
|
196
|
+
CredentialManager credentialManager = credentialManagerOrCreate(context);
|
|
197
|
+
GetCredentialRequest getRequest = new GetCredentialRequest.Builder().addCredentialOption(restoreOption).build();
|
|
198
|
+
|
|
199
|
+
ListenableFuture<String> future = CallbackToFutureAdapter.getFuture((completer) -> {
|
|
200
|
+
getRestoreCredentialInternal(
|
|
201
|
+
credentialManager,
|
|
202
|
+
context,
|
|
203
|
+
getRequest,
|
|
204
|
+
new CredentialManagerCallback<String, Exception>() {
|
|
205
|
+
@Override
|
|
206
|
+
public void onResult(String result) {
|
|
207
|
+
completer.set(result);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
@Override
|
|
211
|
+
public void onError(@NonNull Exception e) {
|
|
212
|
+
completer.setException(e);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
);
|
|
216
|
+
return "GoogleRestoreCredentialHelper.getRestoreCredentialSynchronously";
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
try {
|
|
220
|
+
callback.onResult(future.get(60, TimeUnit.SECONDS));
|
|
221
|
+
} catch (ExecutionException e) {
|
|
222
|
+
Throwable cause = e.getCause();
|
|
223
|
+
if (cause instanceof Exception) {
|
|
224
|
+
callback.onError((Exception) cause);
|
|
225
|
+
} else {
|
|
226
|
+
callback.onError(new IllegalStateException(cause != null ? cause.getMessage() : e.getMessage()));
|
|
227
|
+
}
|
|
228
|
+
} catch (InterruptedException e) {
|
|
229
|
+
Thread.currentThread().interrupt();
|
|
230
|
+
callback.onError(e);
|
|
231
|
+
} catch (TimeoutException e) {
|
|
232
|
+
callback.onError(e);
|
|
233
|
+
}
|
|
234
|
+
} catch (IllegalArgumentException e) {
|
|
235
|
+
callback.onError(e);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
private static void getRestoreCredentialInternal(
|
|
240
|
+
CredentialManager credentialManager,
|
|
241
|
+
Context context,
|
|
242
|
+
GetCredentialRequest getRequest,
|
|
243
|
+
CredentialManagerCallback<String, Exception> callback
|
|
244
|
+
) {
|
|
245
|
+
ExecutorService executor = Executors.newSingleThreadExecutor();
|
|
246
|
+
credentialManager.getCredentialAsync(
|
|
247
|
+
context,
|
|
248
|
+
getRequest,
|
|
249
|
+
null,
|
|
250
|
+
executor,
|
|
251
|
+
new CredentialManagerCallback<GetCredentialResponse, GetCredentialException>() {
|
|
252
|
+
@Override
|
|
253
|
+
public void onResult(GetCredentialResponse response) {
|
|
254
|
+
executor.shutdown();
|
|
255
|
+
try {
|
|
256
|
+
String responseJson = extractRestoreCredentialJson(response);
|
|
257
|
+
if (responseJson == null) {
|
|
258
|
+
callback.onError(new IllegalStateException("Restore credential response JSON is empty"));
|
|
259
|
+
return;
|
|
260
|
+
}
|
|
261
|
+
callback.onResult(responseJson);
|
|
262
|
+
} catch (Exception e) {
|
|
263
|
+
callback.onError(e);
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
@Override
|
|
268
|
+
public void onError(@NonNull GetCredentialException e) {
|
|
269
|
+
executor.shutdown();
|
|
270
|
+
if (e instanceof NoCredentialException) {
|
|
271
|
+
callback.onError(e);
|
|
272
|
+
return;
|
|
273
|
+
}
|
|
274
|
+
if (e instanceof GetCredentialCancellationException) {
|
|
275
|
+
callback.onError(e);
|
|
276
|
+
return;
|
|
277
|
+
}
|
|
278
|
+
callback.onError(e);
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
);
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
public static String extractRestoreCredentialJson(GetCredentialResponse response) {
|
|
285
|
+
Credential credential = response.getCredential();
|
|
286
|
+
if (credential instanceof RestoreCredential) {
|
|
287
|
+
return ((RestoreCredential) credential).getAuthenticationResponseJson();
|
|
288
|
+
}
|
|
289
|
+
if (credential instanceof CustomCredential && RestoreCredential.TYPE_RESTORE_CREDENTIAL.equals(credential.getType())) {
|
|
290
|
+
RestoreCredential restoreCredential = RestoreCredential.createFrom$credentials_release(credential.getData());
|
|
291
|
+
return restoreCredential.getAuthenticationResponseJson();
|
|
292
|
+
}
|
|
293
|
+
return null;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
public static void clearRestoreCredential(Context context, CredentialManagerCallback<Void, Exception> callback) {
|
|
297
|
+
CredentialManager credentialManager = credentialManagerOrCreate(context);
|
|
298
|
+
ClearCredentialStateRequest request = buildClearRestoreCredentialRequest();
|
|
299
|
+
ExecutorService executor = Executors.newSingleThreadExecutor();
|
|
300
|
+
credentialManager.clearCredentialStateAsync(
|
|
301
|
+
request,
|
|
302
|
+
null,
|
|
303
|
+
executor,
|
|
304
|
+
new CredentialManagerCallback<Void, ClearCredentialException>() {
|
|
305
|
+
@Override
|
|
306
|
+
public void onResult(Void unused) {
|
|
307
|
+
executor.shutdown();
|
|
308
|
+
callback.onResult(null);
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
@Override
|
|
312
|
+
public void onError(@NonNull ClearCredentialException e) {
|
|
313
|
+
executor.shutdown();
|
|
314
|
+
callback.onError(e);
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
);
|
|
318
|
+
}
|
|
319
|
+
}
|