capacitor-auth-manager 2.4.4 → 3.1.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/AI-INTEGRATION-GUIDE.md +176 -0
- package/CHANGELOG.md +143 -0
- package/CapacitorAuthManager.podspec +3 -3
- package/README.md +99 -76
- package/android/build.gradle +23 -25
- package/android/consumer-rules.pro +14 -0
- package/android/proguard-rules.pro +5 -30
- package/android/src/main/AndroidManifest.xml +4 -9
- package/android/src/main/java/com/aoneahsan/capacitor_auth_manager/ActivityLauncher.java +26 -0
- package/android/src/main/java/com/aoneahsan/capacitor_auth_manager/AuthErrorCodes.java +32 -0
- package/android/src/main/java/com/aoneahsan/capacitor_auth_manager/AuthException.java +34 -0
- package/android/src/main/java/com/aoneahsan/capacitor_auth_manager/AuthStorage.java +46 -25
- package/android/src/main/java/com/aoneahsan/capacitor_auth_manager/CapacitorAuthManager.java +62 -40
- package/android/src/main/java/com/aoneahsan/capacitor_auth_manager/CapacitorAuthManagerPlugin.java +124 -66
- package/android/src/main/java/com/aoneahsan/capacitor_auth_manager/GoogleAuthProvider.java +225 -374
- package/android/src/main/java/com/aoneahsan/capacitor_auth_manager/GoogleCredentialManagerSignIn.java +175 -0
- package/android/src/main/java/com/aoneahsan/capacitor_auth_manager/GoogleLegacySignIn.java +133 -0
- package/android/src/main/java/com/aoneahsan/capacitor_auth_manager/GoogleProfileCallback.java +8 -0
- package/android/src/main/java/com/aoneahsan/capacitor_auth_manager/GoogleResultMapper.java +158 -0
- package/android/src/main/java/com/aoneahsan/capacitor_auth_manager/ProviderFactory.java +11 -3
- package/android/src/main/java/com/aoneahsan/capacitor_auth_manager/SigningDiagnostics.java +76 -0
- package/dist/angular.cjs.js +421 -3376
- package/dist/core.cjs.js +589 -149
- package/dist/esm/angular/auth.guard.d.ts +1 -1
- package/dist/esm/angular/auth.module.d.ts +1 -1
- package/dist/esm/angular/auth.module.js +1 -1
- package/dist/esm/angular/auth.service.d.ts +2 -2
- package/dist/esm/angular/auth.service.js +1 -1
- package/dist/esm/angular/index.d.ts +4 -4
- package/dist/esm/angular/index.js +4 -4
- package/dist/esm/capacitor-plugin.d.ts +2 -3
- package/dist/esm/capacitor-plugin.js +14 -4
- package/dist/esm/core/auth-manager.d.ts +20 -2
- package/dist/esm/core/auth-manager.js +68 -12
- package/dist/esm/core/index.d.ts +5 -4
- package/dist/esm/core/index.js +5 -4
- package/dist/esm/core/provider-manifests.d.ts +1 -1
- package/dist/esm/core/provider-manifests.js +12 -13
- package/dist/esm/core/provider-registry.d.ts +8 -5
- package/dist/esm/core/provider-registry.js +41 -10
- package/dist/esm/core/types.d.ts +2 -2
- package/dist/esm/definitions.d.ts +29 -0
- package/dist/esm/index.d.ts +12 -12
- package/dist/esm/index.js +7 -7
- package/dist/esm/package.json +1 -0
- package/dist/esm/providers/base-provider.d.ts +7 -8
- package/dist/esm/providers/base-provider.js +7 -8
- package/dist/esm/providers/native/google-native-provider.d.ts +13 -2
- package/dist/esm/providers/native/google-native-provider.js +50 -6
- package/dist/esm/providers/oauth-provider.d.ts +2 -2
- package/dist/esm/providers/oauth-provider.js +3 -3
- package/dist/esm/providers/provider-factory.d.ts +5 -5
- package/dist/esm/providers/provider-factory.js +2 -2
- package/dist/esm/providers/web/google-provider.d.ts +74 -18
- package/dist/esm/providers/web/google-provider.js +391 -117
- package/dist/esm/providers/web/index.d.ts +1 -1
- package/dist/esm/providers/web/index.js +1 -1
- package/dist/esm/react/hooks.d.ts +2 -2
- package/dist/esm/react/hooks.js +2 -2
- package/dist/esm/react/index.d.ts +3 -3
- package/dist/esm/react/index.js +3 -3
- package/dist/esm/react/types.d.ts +1 -1
- package/dist/esm/utils/auth-error.d.ts +1 -1
- package/dist/esm/utils/auth-error.js +1 -1
- package/dist/esm/utils/event-emitter.d.ts +1 -1
- package/dist/esm/utils/event-emitter.js +1 -1
- package/dist/esm/utils/pending-verification-store.js +1 -1
- package/dist/esm/utils/storage.d.ts +2 -2
- package/dist/esm/utils/storage.js +31 -7
- package/dist/esm/vue/composables.d.ts +2 -2
- package/dist/esm/vue/composables.js +1 -1
- package/dist/esm/vue/index.d.ts +2 -2
- package/dist/esm/vue/index.js +2 -2
- package/dist/esm/web.d.ts +1 -1
- package/dist/esm/web.js +6 -6
- package/dist/plugin.cjs.js +291 -2771
- package/dist/plugin.js +585 -147
- package/dist/providers-web.cjs.js +1317 -0
- package/dist/react.cjs.js +310 -3265
- package/dist/vue.cjs.js +266 -3221
- package/ios/Plugin/AuthStorage.swift +3 -1
- package/ios/Plugin/GoogleAuthProvider.swift +7 -4
- package/ios/Plugin/Models.swift +28 -0
- package/ios/Plugin/Plugin.swift +64 -40
- package/package.json +54 -32
- package/scripts/configure.js +71 -325
|
@@ -2,92 +2,63 @@ package com.aoneahsan.capacitor_auth_manager;
|
|
|
2
2
|
|
|
3
3
|
import android.app.Activity;
|
|
4
4
|
import android.content.Context;
|
|
5
|
-
import android.net.Uri;
|
|
6
|
-
import android.util.Base64;
|
|
7
|
-
|
|
8
|
-
import androidx.core.content.ContextCompat;
|
|
9
|
-
|
|
10
|
-
import androidx.credentials.ClearCredentialStateRequest;
|
|
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.exceptions.ClearCredentialException;
|
|
18
|
-
import androidx.credentials.exceptions.GetCredentialCancellationException;
|
|
19
|
-
import androidx.credentials.exceptions.GetCredentialException;
|
|
20
|
-
import androidx.credentials.exceptions.NoCredentialException;
|
|
21
5
|
|
|
22
6
|
import com.getcapacitor.JSObject;
|
|
23
|
-
import com.google.android.libraries.identity.googleid.GetGoogleIdOption;
|
|
24
|
-
import com.google.android.libraries.identity.googleid.GoogleIdTokenCredential;
|
|
25
7
|
|
|
26
|
-
import org.json.JSONArray;
|
|
27
8
|
import org.json.JSONObject;
|
|
28
9
|
|
|
29
|
-
import java.nio.charset.StandardCharsets;
|
|
30
|
-
import java.text.SimpleDateFormat;
|
|
31
10
|
import java.util.ArrayList;
|
|
32
|
-
import java.util.Date;
|
|
33
11
|
import java.util.List;
|
|
34
|
-
import java.util.Locale;
|
|
35
|
-
import java.util.TimeZone;
|
|
36
|
-
import java.util.concurrent.Executor;
|
|
37
12
|
|
|
38
13
|
/**
|
|
39
|
-
* Android native Google sign-in
|
|
40
|
-
* ({@code androidx.credentials}) + the Google Identity {@code GetGoogleIdOption}.
|
|
14
|
+
* Android native Google sign-in: legacy <b>GoogleSignIn</b> first, <b>Credential Manager</b> as the fallback.
|
|
41
15
|
*
|
|
42
|
-
* <p>
|
|
43
|
-
* {@code
|
|
44
|
-
*
|
|
45
|
-
*
|
|
16
|
+
* <p>{@code androidFlow: 'auto'} (the default) runs {@link GoogleLegacySignIn} — the exact path
|
|
17
|
+
* {@code @codetrix-studio/capacitor-google-auth} ran — and falls back to {@link GoogleCredentialManagerSignIn}
|
|
18
|
+
* (bottom sheet, then the "Sign in with Google" button) when GoogleSignIn fails for any reason other than
|
|
19
|
+
* the user cancelling. 3.0.0 ran Credential Manager alone, and Play-installed builds of every production
|
|
20
|
+
* app were refused before any account picker appeared, while the same OAuth clients and SHA-1s had worked
|
|
21
|
+
* through GoogleSignIn in the previous release (2026-10-29). On Android 14+ Credential Manager goes
|
|
22
|
+
* through the system credential service, which can fail with no UI when Google is not an enabled
|
|
23
|
+
* credential provider on the device; GoogleSignIn never touches that service. {@code 'legacy'},
|
|
24
|
+
* {@code 'credential-manager'}, {@code 'bottom-sheet'} and {@code 'button'} pin one path.</p>
|
|
46
25
|
*
|
|
47
|
-
* <p>
|
|
48
|
-
*
|
|
49
|
-
* {@code signInWithCredential(GoogleAuthProvider.credential(idToken))} (or anything else) later.</p>
|
|
26
|
+
* <p>Every error keeps Google's own exception class and message, and configuration errors name the
|
|
27
|
+
* installed package + signing SHA-1 ({@link SigningDiagnostics}), so the cause is readable on the device.</p>
|
|
50
28
|
*
|
|
51
|
-
* <p
|
|
52
|
-
*
|
|
53
|
-
*
|
|
54
|
-
* so {@code credential.accessToken} / {@code credential.serverAuthCode} are intentionally absent.
|
|
55
|
-
* Use the Google Authorization API if you need an access token / extra scopes.</li>
|
|
56
|
-
* <li>{@code GetGoogleIdOption} does not request additional OAuth scopes, nor honor
|
|
57
|
-
* {@code hostedDomain} / {@code loginHint}. Those config values are accepted for API parity but
|
|
58
|
-
* are not enforced here.</li>
|
|
59
|
-
* <li>There is no server-side revoke; {@code revokeAccess} clears local + Credential Manager state only.</li>
|
|
60
|
-
* </ul>
|
|
29
|
+
* <p>The package is Firebase-agnostic: it returns {@code { idToken, ... }} and the app signs in with it.
|
|
30
|
+
* Neither path returns an OAuth access token; extra scopes, server-side revoke and refresh tokens are out
|
|
31
|
+
* of scope for this provider.</p>
|
|
61
32
|
*/
|
|
62
33
|
public class GoogleAuthProvider implements BaseAuthProvider {
|
|
63
|
-
private static final String TAG = "GoogleAuthProvider";
|
|
64
|
-
private static final String PROVIDER_ID = "google.com";
|
|
65
34
|
private static final String STORAGE_CREDENTIAL_KEY = "google";
|
|
66
35
|
private static final String STORAGE_USER_KEY = "google_user";
|
|
67
36
|
|
|
68
|
-
private final Context context;
|
|
69
|
-
private final Activity activity;
|
|
70
37
|
private final AuthStorage storage;
|
|
71
38
|
private final AuthLogger logger;
|
|
39
|
+
private final GoogleLegacySignIn legacy;
|
|
40
|
+
private final GoogleCredentialManagerSignIn credentialManager;
|
|
41
|
+
private ActivityLauncher activityLauncher;
|
|
72
42
|
|
|
73
43
|
private String clientId;
|
|
74
44
|
private String serverClientId;
|
|
75
|
-
// Retained for API parity
|
|
76
|
-
// cannot request additional OAuth scopes, so non-default scopes are surfaced as a warning only.
|
|
45
|
+
// Retained for API parity: neither Android path requests extra OAuth scopes.
|
|
77
46
|
private List<String> scopes;
|
|
78
47
|
private boolean filterByAuthorizedAccounts = false;
|
|
79
48
|
private boolean autoSelectEnabled = false;
|
|
80
49
|
private String nonce;
|
|
81
50
|
private String hostedDomain;
|
|
82
51
|
private String loginHint;
|
|
52
|
+
/** "auto" | "legacy" | "credential-manager" | "bottom-sheet" | "button". */
|
|
53
|
+
private String androidFlow = "auto";
|
|
83
54
|
|
|
84
55
|
private JSObject currentUser;
|
|
85
56
|
|
|
86
57
|
public GoogleAuthProvider(Context context, Activity activity, AuthStorage storage, AuthLogger logger) {
|
|
87
|
-
this.context = context;
|
|
88
|
-
this.activity = activity;
|
|
89
58
|
this.storage = storage;
|
|
90
59
|
this.logger = logger;
|
|
60
|
+
this.legacy = new GoogleLegacySignIn(context, activity, logger);
|
|
61
|
+
this.credentialManager = new GoogleCredentialManagerSignIn(context, activity, logger);
|
|
91
62
|
this.scopes = new ArrayList<>();
|
|
92
63
|
this.scopes.add("email");
|
|
93
64
|
this.scopes.add("profile");
|
|
@@ -95,6 +66,10 @@ public class GoogleAuthProvider implements BaseAuthProvider {
|
|
|
95
66
|
|
|
96
67
|
// ---- Configuration (populated by ProviderFactory from the google provider options) ----
|
|
97
68
|
|
|
69
|
+
public void setActivityLauncher(ActivityLauncher activityLauncher) {
|
|
70
|
+
this.activityLauncher = activityLauncher;
|
|
71
|
+
}
|
|
72
|
+
|
|
98
73
|
public void setClientId(String clientId) {
|
|
99
74
|
this.clientId = clientId;
|
|
100
75
|
}
|
|
@@ -129,15 +104,19 @@ public class GoogleAuthProvider implements BaseAuthProvider {
|
|
|
129
104
|
this.loginHint = loginHint;
|
|
130
105
|
}
|
|
131
106
|
|
|
107
|
+
public void setAndroidFlow(String androidFlow) {
|
|
108
|
+
if (androidFlow != null && !androidFlow.isEmpty()) {
|
|
109
|
+
this.androidFlow = androidFlow;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
132
113
|
// ---- BaseAuthProvider ----
|
|
133
114
|
|
|
134
115
|
@Override
|
|
135
116
|
public void initialize(CapacitorAuthManager.AuthCallback<Void> callback) {
|
|
136
|
-
logger.info("Initializing Google auth provider (Credential Manager)");
|
|
117
|
+
logger.info("Initializing Google auth provider (GoogleSignIn + Credential Manager fallback)");
|
|
137
118
|
try {
|
|
138
|
-
|
|
139
|
-
// user from secure storage to keep getCurrentUser() working after a cold start.
|
|
140
|
-
JSObject storedUser = storage.getCredential(STORAGE_USER_KEY);
|
|
119
|
+
JSObject storedUser = storage.getUser(STORAGE_USER_KEY);
|
|
141
120
|
if (storedUser != null) {
|
|
142
121
|
currentUser = storedUser;
|
|
143
122
|
logger.info("Restored previous Google session from storage");
|
|
@@ -145,44 +124,151 @@ public class GoogleAuthProvider implements BaseAuthProvider {
|
|
|
145
124
|
callback.onResult(CapacitorAuthManager.AuthResult.success(null));
|
|
146
125
|
} catch (Exception e) {
|
|
147
126
|
logger.error("Failed to initialize Google auth provider", e);
|
|
148
|
-
callback.onResult(CapacitorAuthManager.AuthResult.error(
|
|
127
|
+
callback.onResult(CapacitorAuthManager.AuthResult.error(new AuthException(
|
|
128
|
+
AuthErrorCodes.PROVIDER_INIT_FAILED,
|
|
129
|
+
"Failed to initialize Google auth provider: " + e.getMessage(), e)));
|
|
149
130
|
}
|
|
150
131
|
}
|
|
151
132
|
|
|
152
133
|
@Override
|
|
153
|
-
public void signIn(JSObject credentials, JSObject options, CapacitorAuthManager.AuthCallback<JSObject> callback) {
|
|
154
|
-
|
|
155
|
-
|
|
134
|
+
public void signIn(JSObject credentials, JSObject options, final CapacitorAuthManager.AuthCallback<JSObject> callback) {
|
|
135
|
+
String flow = androidFlow;
|
|
136
|
+
if (options != null && options.has("androidFlow")) {
|
|
137
|
+
String perCall = options.getString("androidFlow");
|
|
138
|
+
if (perCall != null && !perCall.isEmpty()) {
|
|
139
|
+
flow = perCall;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
logger.info("Starting Google sign-in (flow=" + flow + ")");
|
|
143
|
+
if (resolvedServerClientId() == null) {
|
|
144
|
+
callback.onResult(missingClientId());
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
warnIfUnsupportedOptions();
|
|
148
|
+
switch (flow) {
|
|
149
|
+
case "legacy":
|
|
150
|
+
runLegacy(profileCallback(callback));
|
|
151
|
+
return;
|
|
152
|
+
case "credential-manager":
|
|
153
|
+
runCredentialManager(profileCallback(callback));
|
|
154
|
+
return;
|
|
155
|
+
case "bottom-sheet":
|
|
156
|
+
credentialManager.requestBottomSheet(resolvedServerClientId(), nonce, filterByAuthorizedAccounts,
|
|
157
|
+
autoSelectEnabled, profileCallback(callback));
|
|
158
|
+
return;
|
|
159
|
+
case "button":
|
|
160
|
+
credentialManager.requestButton(resolvedServerClientId(), nonce, hostedDomain, profileCallback(callback));
|
|
161
|
+
return;
|
|
162
|
+
default:
|
|
163
|
+
runAuto(profileCallback(callback));
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** GoogleSignIn first; Credential Manager when it fails for any reason other than a cancel. */
|
|
168
|
+
private void runAuto(final GoogleProfileCallback callback) {
|
|
169
|
+
runLegacy(new GoogleProfileCallback() {
|
|
170
|
+
@Override
|
|
171
|
+
public void onSuccess(GoogleResultMapper.Profile profile) {
|
|
172
|
+
callback.onSuccess(profile);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
@Override
|
|
176
|
+
public void onError(final AuthException legacyError) {
|
|
177
|
+
if (isCancellation(legacyError)) {
|
|
178
|
+
callback.onError(legacyError);
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
logger.warn("Legacy GoogleSignIn failed; trying Credential Manager: " + legacyError.getMessage());
|
|
182
|
+
runCredentialManager(new GoogleProfileCallback() {
|
|
183
|
+
@Override
|
|
184
|
+
public void onSuccess(GoogleResultMapper.Profile profile) {
|
|
185
|
+
callback.onSuccess(profile);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
@Override
|
|
189
|
+
public void onError(AuthException cmError) {
|
|
190
|
+
if (isCancellation(cmError)) {
|
|
191
|
+
callback.onError(cmError);
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
logger.error("Google sign-in failed on both paths", cmError);
|
|
195
|
+
callback.onError(new AuthException(legacyError.getCode(), legacyError.getMessage()
|
|
196
|
+
+ " | Credential Manager: " + cmError.getMessage(), legacyError));
|
|
197
|
+
}
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
private void runLegacy(GoogleProfileCallback callback) {
|
|
204
|
+
legacy.signIn(resolvedServerClientId(), hostedDomain, loginHint, activityLauncher, callback);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/** Credential Manager: bottom sheet, then the button flow when no account is offered. */
|
|
208
|
+
private void runCredentialManager(final GoogleProfileCallback callback) {
|
|
209
|
+
credentialManager.requestBottomSheet(resolvedServerClientId(), nonce, filterByAuthorizedAccounts, autoSelectEnabled,
|
|
210
|
+
new GoogleProfileCallback() {
|
|
211
|
+
@Override
|
|
212
|
+
public void onSuccess(GoogleResultMapper.Profile profile) {
|
|
213
|
+
callback.onSuccess(profile);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
@Override
|
|
217
|
+
public void onError(AuthException error) {
|
|
218
|
+
if (!(error instanceof GoogleCredentialManagerSignIn.NoGoogleCredentialException)) {
|
|
219
|
+
callback.onError(error);
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
logger.info("No Google credential from the bottom sheet; trying the Sign in with Google button flow");
|
|
223
|
+
credentialManager.requestButton(resolvedServerClientId(), nonce, hostedDomain, callback);
|
|
224
|
+
}
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
private static boolean isCancellation(AuthException error) {
|
|
229
|
+
return AuthErrorCodes.USER_CANCELLED.equals(error.getCode());
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/** Adapts a path's profile result to the bridge result, persisting the session on success. */
|
|
233
|
+
private GoogleProfileCallback profileCallback(final CapacitorAuthManager.AuthCallback<JSObject> callback) {
|
|
234
|
+
return new GoogleProfileCallback() {
|
|
235
|
+
@Override
|
|
236
|
+
public void onSuccess(GoogleResultMapper.Profile profile) {
|
|
237
|
+
callback.onResult(CapacitorAuthManager.AuthResult.success(completeSignIn(profile)));
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
@Override
|
|
241
|
+
public void onError(AuthException error) {
|
|
242
|
+
callback.onResult(CapacitorAuthManager.AuthResult.error(error));
|
|
243
|
+
}
|
|
244
|
+
};
|
|
156
245
|
}
|
|
157
246
|
|
|
158
247
|
@Override
|
|
159
248
|
public void signOut(JSObject options, CapacitorAuthManager.AuthCallback<Void> callback) {
|
|
160
|
-
logger.info("Signing out from Google (
|
|
249
|
+
logger.info("Signing out from Google (GoogleSignIn + Credential Manager state)");
|
|
161
250
|
clearCredentialState(callback);
|
|
162
251
|
}
|
|
163
252
|
|
|
164
253
|
@Override
|
|
165
254
|
public void getCurrentUser(CapacitorAuthManager.AuthCallback<JSObject> callback) {
|
|
166
255
|
if (currentUser == null) {
|
|
167
|
-
currentUser = storage.
|
|
256
|
+
currentUser = storage.getUser(STORAGE_USER_KEY);
|
|
168
257
|
}
|
|
169
258
|
callback.onResult(CapacitorAuthManager.AuthResult.success(currentUser));
|
|
170
259
|
}
|
|
171
260
|
|
|
172
261
|
@Override
|
|
173
262
|
public void refreshToken(JSObject options, final CapacitorAuthManager.AuthCallback<JSObject> callback) {
|
|
174
|
-
logger.info("Refreshing Google ID token (silent
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
"Re-authentication required: could not silently refresh the Google ID token. "
|
|
184
|
-
+ "Prompt the user to sign in again.")));
|
|
185
|
-
}
|
|
263
|
+
logger.info("Refreshing Google ID token (silent attempt)");
|
|
264
|
+
silentSignIn(result -> {
|
|
265
|
+
if (result.isSuccess()) {
|
|
266
|
+
callback.onResult(result);
|
|
267
|
+
} else {
|
|
268
|
+
callback.onResult(CapacitorAuthManager.AuthResult.error(new AuthException(
|
|
269
|
+
AuthErrorCodes.TOKEN_REFRESH_FAILED,
|
|
270
|
+
"Re-authentication required: could not silently refresh the Google ID token. "
|
|
271
|
+
+ "Prompt the user to sign in again.")));
|
|
186
272
|
}
|
|
187
273
|
});
|
|
188
274
|
}
|
|
@@ -206,346 +292,111 @@ public class GoogleAuthProvider implements BaseAuthProvider {
|
|
|
206
292
|
public void getIdToken(boolean forceRefresh, final CapacitorAuthManager.AuthCallback<JSObject> callback) {
|
|
207
293
|
if (!forceRefresh) {
|
|
208
294
|
JSObject storedCredential = storage.getCredential(STORAGE_CREDENTIAL_KEY);
|
|
209
|
-
|
|
210
|
-
|
|
295
|
+
String idToken = storedCredential != null ? storedCredential.getString("idToken") : null;
|
|
296
|
+
if (idToken != null && !idToken.isEmpty() && isFreshIdToken(idToken)) {
|
|
297
|
+
JSObject result = new JSObject();
|
|
298
|
+
result.put("token", idToken);
|
|
299
|
+
callback.onResult(CapacitorAuthManager.AuthResult.success(result));
|
|
300
|
+
return;
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
silentSignIn(result -> {
|
|
304
|
+
if (result.isSuccess() && result.getData() != null) {
|
|
305
|
+
JSObject credential = result.getData().getJSObject("credential");
|
|
306
|
+
String idToken = credential != null ? credential.getString("idToken") : null;
|
|
211
307
|
if (idToken != null && !idToken.isEmpty()) {
|
|
212
|
-
JSObject
|
|
213
|
-
|
|
214
|
-
callback.onResult(CapacitorAuthManager.AuthResult.success(
|
|
308
|
+
JSObject out = new JSObject();
|
|
309
|
+
out.put("token", idToken);
|
|
310
|
+
callback.onResult(CapacitorAuthManager.AuthResult.success(out));
|
|
215
311
|
return;
|
|
216
312
|
}
|
|
217
313
|
}
|
|
314
|
+
callback.onResult(CapacitorAuthManager.AuthResult.error(new AuthException(
|
|
315
|
+
AuthErrorCodes.NO_AUTH_SESSION,
|
|
316
|
+
"No Google ID token available. Re-authentication required.")));
|
|
317
|
+
});
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
/** Silent GoogleSignIn, then a silent Credential Manager attempt. Never shows UI. */
|
|
321
|
+
private void silentSignIn(final CapacitorAuthManager.AuthCallback<JSObject> callback) {
|
|
322
|
+
if (resolvedServerClientId() == null) {
|
|
323
|
+
callback.onResult(missingClientId());
|
|
324
|
+
return;
|
|
218
325
|
}
|
|
326
|
+
final GoogleProfileCallback done = profileCallback(callback);
|
|
327
|
+
legacy.silentSignIn(resolvedServerClientId(), new GoogleProfileCallback() {
|
|
328
|
+
@Override
|
|
329
|
+
public void onSuccess(GoogleResultMapper.Profile profile) {
|
|
330
|
+
done.onSuccess(profile);
|
|
331
|
+
}
|
|
219
332
|
|
|
220
|
-
// No cached token (or forced refresh) → silent Credential Manager attempt.
|
|
221
|
-
requestGoogleCredential(true, true, new CapacitorAuthManager.AuthCallback<JSObject>() {
|
|
222
333
|
@Override
|
|
223
|
-
public void
|
|
224
|
-
|
|
225
|
-
JSObject credential = result.getData().getJSObject("credential");
|
|
226
|
-
String idToken = credential != null ? credential.getString("idToken") : null;
|
|
227
|
-
if (idToken != null && !idToken.isEmpty()) {
|
|
228
|
-
JSObject out = new JSObject();
|
|
229
|
-
out.put("token", idToken);
|
|
230
|
-
callback.onResult(CapacitorAuthManager.AuthResult.success(out));
|
|
231
|
-
return;
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
callback.onResult(CapacitorAuthManager.AuthResult.error(new Exception(
|
|
235
|
-
"No Google ID token available. Re-authentication required.")));
|
|
334
|
+
public void onError(AuthException error) {
|
|
335
|
+
credentialManager.requestBottomSheet(resolvedServerClientId(), nonce, true, true, done);
|
|
236
336
|
}
|
|
237
337
|
});
|
|
238
338
|
}
|
|
239
339
|
|
|
340
|
+
private boolean isFreshIdToken(String idToken) {
|
|
341
|
+
JSONObject claims = GoogleResultMapper.parseIdTokenClaims(idToken, logger);
|
|
342
|
+
return claims != null && claims.optLong("exp", 0) > System.currentTimeMillis() / 1000;
|
|
343
|
+
}
|
|
344
|
+
|
|
240
345
|
@Override
|
|
241
346
|
public void revokeAccess(String token, CapacitorAuthManager.AuthCallback<Void> callback) {
|
|
242
|
-
|
|
243
|
-
// the persisted session so the next sign-in re-prompts.
|
|
244
|
-
logger.info("Revoking Google access (clears local + Credential Manager state only; no server-side revoke)");
|
|
347
|
+
logger.info("Revoking Google access (clears local, GoogleSignIn and Credential Manager state; no server-side revoke)");
|
|
245
348
|
clearCredentialState(callback);
|
|
246
349
|
}
|
|
247
350
|
|
|
248
|
-
// ----
|
|
249
|
-
|
|
250
|
-
private void requestGoogleCredential(
|
|
251
|
-
boolean filterByAuthorized,
|
|
252
|
-
boolean autoSelect,
|
|
253
|
-
final CapacitorAuthManager.AuthCallback<JSObject> callback
|
|
254
|
-
) {
|
|
255
|
-
String resolvedServerClientId = (serverClientId != null && !serverClientId.isEmpty())
|
|
256
|
-
? serverClientId
|
|
257
|
-
: clientId;
|
|
258
|
-
|
|
259
|
-
if (resolvedServerClientId == null || resolvedServerClientId.isEmpty()) {
|
|
260
|
-
callback.onResult(CapacitorAuthManager.AuthResult.error(new IllegalStateException(
|
|
261
|
-
"Google sign-in requires a serverClientId (your Web OAuth client id). "
|
|
262
|
-
+ "Provide 'serverClientId' (or 'clientId') in the google provider options.")));
|
|
263
|
-
return;
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
if (activity == null) {
|
|
267
|
-
callback.onResult(CapacitorAuthManager.AuthResult.error(new IllegalStateException(
|
|
268
|
-
"Google sign-in requires a foreground Activity.")));
|
|
269
|
-
return;
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
warnIfUnsupportedOptions();
|
|
273
|
-
|
|
274
|
-
try {
|
|
275
|
-
GetGoogleIdOption.Builder optionBuilder = new GetGoogleIdOption.Builder()
|
|
276
|
-
.setServerClientId(resolvedServerClientId)
|
|
277
|
-
.setFilterByAuthorizedAccounts(filterByAuthorized)
|
|
278
|
-
.setAutoSelectEnabled(autoSelect);
|
|
279
|
-
|
|
280
|
-
if (nonce != null && !nonce.isEmpty()) {
|
|
281
|
-
optionBuilder.setNonce(nonce);
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
GetGoogleIdOption googleIdOption = optionBuilder.build();
|
|
285
|
-
|
|
286
|
-
GetCredentialRequest request = new GetCredentialRequest.Builder()
|
|
287
|
-
.addCredentialOption(googleIdOption)
|
|
288
|
-
.build();
|
|
289
|
-
|
|
290
|
-
CredentialManager credentialManager = CredentialManager.create(context);
|
|
291
|
-
Executor executor = ContextCompat.getMainExecutor(context);
|
|
292
|
-
|
|
293
|
-
credentialManager.getCredentialAsync(
|
|
294
|
-
activity,
|
|
295
|
-
request,
|
|
296
|
-
null,
|
|
297
|
-
executor,
|
|
298
|
-
new CredentialManagerCallback<GetCredentialResponse, GetCredentialException>() {
|
|
299
|
-
@Override
|
|
300
|
-
public void onResult(GetCredentialResponse response) {
|
|
301
|
-
handleSignInResponse(response, callback);
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
@Override
|
|
305
|
-
public void onError(GetCredentialException e) {
|
|
306
|
-
handleSignInError(e, callback);
|
|
307
|
-
}
|
|
308
|
-
}
|
|
309
|
-
);
|
|
310
|
-
} catch (Exception e) {
|
|
311
|
-
logger.error("Failed to start Google Credential Manager request", e);
|
|
312
|
-
callback.onResult(CapacitorAuthManager.AuthResult.error(e));
|
|
313
|
-
}
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
private void handleSignInResponse(
|
|
317
|
-
GetCredentialResponse response,
|
|
318
|
-
CapacitorAuthManager.AuthCallback<JSObject> callback
|
|
319
|
-
) {
|
|
320
|
-
try {
|
|
321
|
-
Credential credential = response.getCredential();
|
|
322
|
-
if (credential instanceof CustomCredential
|
|
323
|
-
&& GoogleIdTokenCredential.TYPE_GOOGLE_ID_TOKEN_CREDENTIAL.equals(credential.getType())) {
|
|
324
|
-
|
|
325
|
-
GoogleIdTokenCredential googleCredential =
|
|
326
|
-
GoogleIdTokenCredential.createFrom(((CustomCredential) credential).getData());
|
|
327
|
-
|
|
328
|
-
JSONObject claims = parseIdTokenClaims(googleCredential.getIdToken());
|
|
329
|
-
|
|
330
|
-
JSObject user = createUserObject(googleCredential, claims);
|
|
331
|
-
JSObject credentialObj = createCredentialObject(googleCredential);
|
|
332
|
-
|
|
333
|
-
currentUser = user;
|
|
334
|
-
persistSession(user, credentialObj);
|
|
335
|
-
|
|
336
|
-
JSObject result = new JSObject();
|
|
337
|
-
result.put("user", user);
|
|
338
|
-
result.put("credential", credentialObj);
|
|
339
|
-
|
|
340
|
-
JSObject additionalUserInfo = new JSObject();
|
|
341
|
-
additionalUserInfo.put("isNewUser", false);
|
|
342
|
-
additionalUserInfo.put("providerId", PROVIDER_ID);
|
|
343
|
-
if (googleCredential.getId() != null) {
|
|
344
|
-
additionalUserInfo.put("username", googleCredential.getId());
|
|
345
|
-
}
|
|
346
|
-
result.put("additionalUserInfo", additionalUserInfo);
|
|
347
|
-
result.put("operationType", "signIn");
|
|
351
|
+
// ---- Helpers ----
|
|
348
352
|
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
String type = credential != null ? credential.getType() : "null";
|
|
353
|
-
logger.error("Unexpected credential type from Credential Manager: " + type, null);
|
|
354
|
-
callback.onResult(CapacitorAuthManager.AuthResult.error(new IllegalStateException(
|
|
355
|
-
"Unexpected credential type returned from Google sign-in: " + type)));
|
|
356
|
-
}
|
|
357
|
-
} catch (Exception e) {
|
|
358
|
-
// Covers a malformed credential / ID-token parse failure. (googleid 1.1.1's
|
|
359
|
-
// GoogleIdTokenCredential.createFrom does not declare a checked exception.)
|
|
360
|
-
logger.error("Unexpected error handling Google credential", e);
|
|
361
|
-
callback.onResult(CapacitorAuthManager.AuthResult.error(e));
|
|
353
|
+
private String resolvedServerClientId() {
|
|
354
|
+
if (serverClientId != null && !serverClientId.isEmpty()) {
|
|
355
|
+
return serverClientId;
|
|
362
356
|
}
|
|
357
|
+
return clientId != null && !clientId.isEmpty() ? clientId : null;
|
|
363
358
|
}
|
|
364
359
|
|
|
365
|
-
private
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
logger.info("Google sign-in cancelled by user");
|
|
371
|
-
callback.onResult(CapacitorAuthManager.AuthResult.error(new Exception("User cancelled Google sign-in")));
|
|
372
|
-
return;
|
|
373
|
-
}
|
|
374
|
-
if (e instanceof NoCredentialException) {
|
|
375
|
-
logger.warn("No Google credentials available for sign-in");
|
|
376
|
-
callback.onResult(CapacitorAuthManager.AuthResult.error(new Exception(
|
|
377
|
-
"No Google account available. Add a Google account to the device, or ensure "
|
|
378
|
-
+ "Google Play services is up to date and the SHA-1 fingerprint is registered.")));
|
|
379
|
-
return;
|
|
380
|
-
}
|
|
381
|
-
logger.error("Google sign-in failed: " + e.getClass().getSimpleName(), e);
|
|
382
|
-
callback.onResult(CapacitorAuthManager.AuthResult.error(new Exception(
|
|
383
|
-
"Google sign-in failed: " + e.getMessage())));
|
|
360
|
+
private static CapacitorAuthManager.AuthResult<JSObject> missingClientId() {
|
|
361
|
+
return CapacitorAuthManager.AuthResult.error(new AuthException(
|
|
362
|
+
AuthErrorCodes.MISSING_CONFIGURATION,
|
|
363
|
+
"Google sign-in requires a serverClientId (your Web OAuth client id). "
|
|
364
|
+
+ "Provide 'serverClientId' (or 'clientId') in the google provider options."));
|
|
384
365
|
}
|
|
385
366
|
|
|
386
367
|
private void clearCredentialState(final CapacitorAuthManager.AuthCallback<Void> callback) {
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
Executor executor = ContextCompat.getMainExecutor(context);
|
|
390
|
-
credentialManager.clearCredentialStateAsync(
|
|
391
|
-
new ClearCredentialStateRequest(),
|
|
392
|
-
null,
|
|
393
|
-
executor,
|
|
394
|
-
new CredentialManagerCallback<Void, ClearCredentialException>() {
|
|
395
|
-
@Override
|
|
396
|
-
public void onResult(Void unused) {
|
|
397
|
-
clearSession();
|
|
398
|
-
callback.onResult(CapacitorAuthManager.AuthResult.success(null));
|
|
399
|
-
}
|
|
400
|
-
|
|
401
|
-
@Override
|
|
402
|
-
public void onError(ClearCredentialException e) {
|
|
403
|
-
logger.error("Failed to clear Credential Manager state", e);
|
|
404
|
-
// Still clear local session so the app reflects a signed-out state.
|
|
405
|
-
clearSession();
|
|
406
|
-
callback.onResult(CapacitorAuthManager.AuthResult.success(null));
|
|
407
|
-
}
|
|
408
|
-
}
|
|
409
|
-
);
|
|
410
|
-
} catch (Exception e) {
|
|
411
|
-
logger.error("Error clearing Credential Manager state", e);
|
|
368
|
+
legacy.signOut();
|
|
369
|
+
credentialManager.clearState(() -> {
|
|
412
370
|
clearSession();
|
|
413
371
|
callback.onResult(CapacitorAuthManager.AuthResult.success(null));
|
|
414
|
-
}
|
|
415
|
-
}
|
|
416
|
-
|
|
417
|
-
// ---- Result building ----
|
|
418
|
-
|
|
419
|
-
private JSObject createUserObject(GoogleIdTokenCredential cred, JSONObject claims) {
|
|
420
|
-
String email = cred.getId();
|
|
421
|
-
String displayName = cred.getDisplayName();
|
|
422
|
-
Uri photoUri = cred.getProfilePictureUri();
|
|
423
|
-
String photoURL = photoUri != null ? photoUri.toString() : null;
|
|
424
|
-
String phoneNumber = cred.getPhoneNumber();
|
|
425
|
-
|
|
426
|
-
String uid = null;
|
|
427
|
-
boolean emailVerified = true;
|
|
428
|
-
if (claims != null) {
|
|
429
|
-
String sub = claims.optString("sub", null);
|
|
430
|
-
if (sub != null && !sub.isEmpty()) {
|
|
431
|
-
uid = sub;
|
|
432
|
-
}
|
|
433
|
-
if (claims.has("email_verified")) {
|
|
434
|
-
emailVerified = claims.optBoolean("email_verified", true);
|
|
435
|
-
}
|
|
436
|
-
if (email == null) {
|
|
437
|
-
email = claims.optString("email", null);
|
|
438
|
-
}
|
|
439
|
-
if (displayName == null) {
|
|
440
|
-
displayName = claims.optString("name", null);
|
|
441
|
-
}
|
|
442
|
-
if (photoURL == null) {
|
|
443
|
-
String picture = claims.optString("picture", null);
|
|
444
|
-
if (picture != null && !picture.isEmpty()) {
|
|
445
|
-
photoURL = picture;
|
|
446
|
-
}
|
|
447
|
-
}
|
|
448
|
-
}
|
|
449
|
-
if (uid == null || uid.isEmpty()) {
|
|
450
|
-
// Credential Manager doesn't expose the Google `sub` directly; fall back to the email id.
|
|
451
|
-
uid = email;
|
|
452
|
-
}
|
|
453
|
-
|
|
454
|
-
JSObject user = new JSObject();
|
|
455
|
-
user.put("uid", uid);
|
|
456
|
-
user.put("email", email);
|
|
457
|
-
user.put("emailVerified", emailVerified);
|
|
458
|
-
user.put("displayName", displayName);
|
|
459
|
-
user.put("photoURL", photoURL);
|
|
460
|
-
user.put("phoneNumber", phoneNumber);
|
|
461
|
-
user.put("isAnonymous", false);
|
|
462
|
-
// tenantId / refreshToken intentionally omitted (not available from Credential Manager).
|
|
463
|
-
|
|
464
|
-
JSONArray providerData = new JSONArray();
|
|
465
|
-
JSObject providerEntry = new JSObject();
|
|
466
|
-
providerEntry.put("providerId", PROVIDER_ID);
|
|
467
|
-
providerEntry.put("uid", uid);
|
|
468
|
-
providerEntry.put("displayName", displayName);
|
|
469
|
-
providerEntry.put("email", email);
|
|
470
|
-
providerEntry.put("phoneNumber", phoneNumber);
|
|
471
|
-
providerEntry.put("photoURL", photoURL);
|
|
472
|
-
providerData.put(providerEntry);
|
|
473
|
-
user.put("providerData", providerData);
|
|
474
|
-
|
|
475
|
-
JSObject metadata = new JSObject();
|
|
476
|
-
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US);
|
|
477
|
-
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
|
|
478
|
-
metadata.put("lastSignInTime", sdf.format(new Date()));
|
|
479
|
-
user.put("metadata", metadata);
|
|
480
|
-
|
|
481
|
-
return user;
|
|
482
|
-
}
|
|
483
|
-
|
|
484
|
-
private JSObject createCredentialObject(GoogleIdTokenCredential cred) {
|
|
485
|
-
JSObject credential = new JSObject();
|
|
486
|
-
credential.put("providerId", PROVIDER_ID);
|
|
487
|
-
credential.put("signInMethod", PROVIDER_ID);
|
|
488
|
-
credential.put("idToken", cred.getIdToken());
|
|
489
|
-
// accessToken and serverAuthCode are NOT provided by Credential Manager sign-in (ID token only) —
|
|
490
|
-
// intentionally absent. Consumers feed credential.idToken into their own signInWithCredential(...).
|
|
491
|
-
return credential;
|
|
372
|
+
});
|
|
492
373
|
}
|
|
493
374
|
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
375
|
+
/** Builds the shared result shape, keeps the user in memory and persists the session. */
|
|
376
|
+
private JSObject completeSignIn(GoogleResultMapper.Profile profile) {
|
|
377
|
+
JSObject result = GoogleResultMapper.buildResult(profile, logger);
|
|
378
|
+
currentUser = result.getJSObject("user");
|
|
497
379
|
try {
|
|
498
|
-
storage.saveCredential(STORAGE_CREDENTIAL_KEY, credential);
|
|
499
|
-
storage.
|
|
380
|
+
storage.saveCredential(STORAGE_CREDENTIAL_KEY, result.getJSObject("credential"));
|
|
381
|
+
storage.saveUser(STORAGE_USER_KEY, currentUser);
|
|
500
382
|
} catch (Exception e) {
|
|
501
383
|
logger.warn("Failed to persist Google session");
|
|
502
384
|
}
|
|
385
|
+
return result;
|
|
503
386
|
}
|
|
504
387
|
|
|
505
388
|
private void clearSession() {
|
|
506
389
|
currentUser = null;
|
|
507
390
|
storage.deleteCredential(STORAGE_CREDENTIAL_KEY);
|
|
508
|
-
storage.
|
|
391
|
+
storage.deleteUser(STORAGE_USER_KEY);
|
|
509
392
|
}
|
|
510
393
|
|
|
511
394
|
private void warnIfUnsupportedOptions() {
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
if (scopes != null && !scopes.isEmpty()) {
|
|
517
|
-
for (String scope : scopes) {
|
|
518
|
-
if (scope != null
|
|
519
|
-
&& !scope.equals("email")
|
|
520
|
-
&& !scope.equals("profile")
|
|
521
|
-
&& !scope.equals("openid")) {
|
|
522
|
-
logger.warn("Additional OAuth scopes are ignored by Android Credential Manager Google "
|
|
523
|
-
+ "sign-in (ID token only). Use the Google Authorization API for extra scopes.");
|
|
524
|
-
break;
|
|
525
|
-
}
|
|
526
|
-
}
|
|
527
|
-
}
|
|
528
|
-
}
|
|
529
|
-
|
|
530
|
-
/**
|
|
531
|
-
* Best-effort decode of the (unverified) ID-token JWT payload to read claims such as {@code sub}
|
|
532
|
-
* and {@code email_verified}. The token's signature is verified server-side by the consumer's
|
|
533
|
-
* own {@code signInWithCredential}; this is only used to enrich the user object.
|
|
534
|
-
*/
|
|
535
|
-
private JSONObject parseIdTokenClaims(String idToken) {
|
|
536
|
-
if (idToken == null || idToken.isEmpty()) {
|
|
537
|
-
return null;
|
|
538
|
-
}
|
|
539
|
-
try {
|
|
540
|
-
String[] parts = idToken.split("\\.");
|
|
541
|
-
if (parts.length < 2) {
|
|
542
|
-
return null;
|
|
395
|
+
for (String scope : scopes) {
|
|
396
|
+
if (scope != null && !scope.equals("email") && !scope.equals("profile") && !scope.equals("openid")) {
|
|
397
|
+
logger.warn("Additional OAuth scopes are ignored by Android Google sign-in (ID token only).");
|
|
398
|
+
break;
|
|
543
399
|
}
|
|
544
|
-
byte[] decoded = Base64.decode(parts[1], Base64.URL_SAFE | Base64.NO_PADDING | Base64.NO_WRAP);
|
|
545
|
-
return new JSONObject(new String(decoded, StandardCharsets.UTF_8));
|
|
546
|
-
} catch (Exception e) {
|
|
547
|
-
logger.warn("Failed to parse Google ID token claims");
|
|
548
|
-
return null;
|
|
549
400
|
}
|
|
550
401
|
}
|
|
551
402
|
}
|