@capgo/capacitor-social-login 8.7.4 → 8.7.6

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.
@@ -11,6 +11,7 @@ import android.util.Log;
11
11
  import com.getcapacitor.JSArray;
12
12
  import com.getcapacitor.JSObject;
13
13
  import com.getcapacitor.PluginCall;
14
+ import ee.forgr.capacitor.social.login.helpers.PluginHttpClient;
14
15
  import ee.forgr.capacitor.social.login.helpers.SocialProvider;
15
16
  import java.io.IOException;
16
17
  import java.nio.charset.StandardCharsets;
@@ -20,15 +21,10 @@ import java.security.SecureRandom;
20
21
  import java.util.ArrayList;
21
22
  import java.util.Arrays;
22
23
  import java.util.Collections;
24
+ import java.util.LinkedHashMap;
23
25
  import java.util.List;
26
+ import java.util.Map;
24
27
  import java.util.UUID;
25
- import java.util.concurrent.TimeUnit;
26
- import okhttp3.Call;
27
- import okhttp3.Callback;
28
- import okhttp3.FormBody;
29
- import okhttp3.OkHttpClient;
30
- import okhttp3.Request;
31
- import okhttp3.Response;
32
28
  import org.json.JSONArray;
33
29
  import org.json.JSONException;
34
30
  import org.json.JSONObject;
@@ -45,7 +41,7 @@ public class TwitterProvider implements SocialProvider {
45
41
 
46
42
  private final Activity activity;
47
43
  private final Context context;
48
- private final OkHttpClient httpClient;
44
+ private final PluginHttpClient httpClient;
49
45
 
50
46
  private String clientId;
51
47
  private String redirectUri;
@@ -74,7 +70,7 @@ public class TwitterProvider implements SocialProvider {
74
70
  public TwitterProvider(Activity activity, Context context) {
75
71
  this.activity = activity;
76
72
  this.context = context;
77
- this.httpClient = new OkHttpClient.Builder().connectTimeout(30, TimeUnit.SECONDS).readTimeout(30, TimeUnit.SECONDS).build();
73
+ this.httpClient = PluginHttpClient.THIRTY_SECOND_TIMEOUTS;
78
74
  }
79
75
 
80
76
  public void initialize(JSONObject config) throws JSONException {
@@ -266,86 +262,80 @@ public class TwitterProvider implements SocialProvider {
266
262
  return;
267
263
  }
268
264
 
269
- FormBody body = new FormBody.Builder()
270
- .add("grant_type", "authorization_code")
271
- .add("client_id", clientId)
272
- .add("code", code)
273
- .add("redirect_uri", pendingState.redirectUri)
274
- .add("code_verifier", pendingState.codeVerifier)
275
- .build();
265
+ Map<String, String> form = new LinkedHashMap<>();
266
+ form.put("grant_type", "authorization_code");
267
+ form.put("client_id", clientId);
268
+ form.put("code", code);
269
+ form.put("redirect_uri", pendingState.redirectUri);
270
+ form.put("code_verifier", pendingState.codeVerifier);
271
+
272
+ httpClient.enqueuePostForm(
273
+ TOKEN_ENDPOINT,
274
+ form,
275
+ null,
276
+ new PluginHttpClient.Callback() {
277
+ @Override
278
+ public void onFailure(IOException e) {
279
+ if (pendingCall != null) {
280
+ pendingCall.reject("Twitter token exchange failed", e);
281
+ }
282
+ cleanupPending();
283
+ }
276
284
 
277
- Request request = new Request.Builder().url(TOKEN_ENDPOINT).post(body).build();
278
- httpClient
279
- .newCall(request)
280
- .enqueue(
281
- new Callback() {
282
- @Override
283
- public void onFailure(Call call, IOException e) {
285
+ @Override
286
+ public void onResponse(int code, String responseBody) throws IOException {
287
+ if (!PluginHttpClient.isSuccessfulHttpCode(code)) {
284
288
  if (pendingCall != null) {
285
- pendingCall.reject("Twitter token exchange failed", e);
289
+ pendingCall.reject("Twitter token exchange failed: " + responseBody);
286
290
  }
287
291
  cleanupPending();
292
+ return;
288
293
  }
289
-
290
- @Override
291
- public void onResponse(Call call, Response response) throws IOException {
292
- if (!response.isSuccessful()) {
293
- String errorBody = response.body() != null ? response.body().string() : "";
294
- if (pendingCall != null) {
295
- pendingCall.reject("Twitter token exchange failed: " + errorBody);
296
- }
297
- cleanupPending();
298
- return;
299
- }
300
- String responseBody = response.body() != null ? response.body().string() : "";
301
- try {
302
- JSONObject tokenPayload = new JSONObject(responseBody);
303
- handleTokenSuccess(tokenPayload);
304
- } catch (JSONException e) {
305
- if (pendingCall != null) {
306
- pendingCall.reject("Failed to parse Twitter token response", e);
307
- }
308
- cleanupPending();
294
+ try {
295
+ JSONObject tokenPayload = new JSONObject(responseBody);
296
+ handleTokenSuccess(tokenPayload);
297
+ } catch (JSONException e) {
298
+ if (pendingCall != null) {
299
+ pendingCall.reject("Failed to parse Twitter token response", e);
309
300
  }
301
+ cleanupPending();
310
302
  }
311
303
  }
312
- );
304
+ }
305
+ );
313
306
  }
314
307
 
315
308
  private void refreshWithToken(final PluginCall pluginCall, String refreshToken) {
316
- FormBody body = new FormBody.Builder()
317
- .add("grant_type", "refresh_token")
318
- .add("refresh_token", refreshToken)
319
- .add("client_id", clientId)
320
- .build();
309
+ Map<String, String> form = new LinkedHashMap<>();
310
+ form.put("grant_type", "refresh_token");
311
+ form.put("refresh_token", refreshToken);
312
+ form.put("client_id", clientId);
313
+
314
+ httpClient.enqueuePostForm(
315
+ TOKEN_ENDPOINT,
316
+ form,
317
+ null,
318
+ new PluginHttpClient.Callback() {
319
+ @Override
320
+ public void onFailure(IOException e) {
321
+ pluginCall.reject("Twitter refresh failed", e);
322
+ }
321
323
 
322
- Request request = new Request.Builder().url(TOKEN_ENDPOINT).post(body).build();
323
- httpClient
324
- .newCall(request)
325
- .enqueue(
326
- new Callback() {
327
- @Override
328
- public void onFailure(Call call, IOException e) {
329
- pluginCall.reject("Twitter refresh failed", e);
324
+ @Override
325
+ public void onResponse(int code, String responseBody) throws IOException {
326
+ if (!PluginHttpClient.isSuccessfulHttpCode(code)) {
327
+ pluginCall.reject("Twitter refresh failed: " + responseBody);
328
+ return;
330
329
  }
331
-
332
- @Override
333
- public void onResponse(Call call, Response response) throws IOException {
334
- if (!response.isSuccessful()) {
335
- String errorBody = response.body() != null ? response.body().string() : "";
336
- pluginCall.reject("Twitter refresh failed: " + errorBody);
337
- return;
338
- }
339
- String responseBody = response.body() != null ? response.body().string() : "";
340
- try {
341
- JSONObject tokenPayload = new JSONObject(responseBody);
342
- handleTokenSuccess(tokenPayload, pluginCall);
343
- } catch (JSONException e) {
344
- pluginCall.reject("Failed to parse Twitter refresh response", e);
345
- }
330
+ try {
331
+ JSONObject tokenPayload = new JSONObject(responseBody);
332
+ handleTokenSuccess(tokenPayload, pluginCall);
333
+ } catch (JSONException e) {
334
+ pluginCall.reject("Failed to parse Twitter refresh response", e);
346
335
  }
347
336
  }
348
- );
337
+ }
338
+ );
349
339
  }
350
340
 
351
341
  private void handleTokenSuccess(JSONObject tokenPayload) throws JSONException {
@@ -409,46 +399,44 @@ public class TwitterProvider implements SocialProvider {
409
399
  .buildUpon()
410
400
  .appendQueryParameter("user.fields", "profile_image_url,verified,name,username")
411
401
  .build();
412
- Request request = new Request.Builder().url(uri.toString()).addHeader("Authorization", "Bearer " + accessToken).build();
413
-
414
- httpClient
415
- .newCall(request)
416
- .enqueue(
417
- new Callback() {
418
- @Override
419
- public void onFailure(Call call, IOException e) {
420
- callback.onError("Failed to fetch Twitter profile: " + e.getMessage());
421
- }
402
+ Map<String, String> headers = Collections.singletonMap("Authorization", "Bearer " + accessToken);
422
403
 
423
- @Override
424
- public void onResponse(Call call, Response response) throws IOException {
425
- if (!response.isSuccessful()) {
426
- String errorBody = response.body() != null ? response.body().string() : "";
427
- callback.onError("Failed to fetch Twitter profile: " + errorBody);
428
- return;
429
- }
430
- String responseBody = response.body() != null ? response.body().string() : "";
431
- try {
432
- JSONObject payload = new JSONObject(responseBody);
433
- JSONObject data = payload.getJSONObject("data");
434
- JSONObject profile = new JSONObject();
435
- profile.put("id", data.optString("id"));
436
- profile.put("username", data.optString("username"));
437
- profile.put("name", data.optString("name"));
438
- profile.put("profileImageUrl", data.optString("profile_image_url", ""));
439
- profile.put("verified", data.optBoolean("verified", false));
440
- if (data.has("email")) {
441
- profile.put("email", data.optString("email", null));
442
- } else {
443
- profile.put("email", JSONObject.NULL);
444
- }
445
- callback.onSuccess(profile);
446
- } catch (JSONException e) {
447
- callback.onError("Failed to parse Twitter profile response");
404
+ httpClient.enqueueGet(
405
+ uri.toString(),
406
+ headers,
407
+ new PluginHttpClient.Callback() {
408
+ @Override
409
+ public void onFailure(IOException e) {
410
+ callback.onError("Failed to fetch Twitter profile: " + e.getMessage());
411
+ }
412
+
413
+ @Override
414
+ public void onResponse(int code, String responseBody) throws IOException {
415
+ if (!PluginHttpClient.isSuccessfulHttpCode(code)) {
416
+ callback.onError("Failed to fetch Twitter profile: " + responseBody);
417
+ return;
418
+ }
419
+ try {
420
+ JSONObject payload = new JSONObject(responseBody);
421
+ JSONObject data = payload.getJSONObject("data");
422
+ JSONObject profile = new JSONObject();
423
+ profile.put("id", data.optString("id"));
424
+ profile.put("username", data.optString("username"));
425
+ profile.put("name", data.optString("name"));
426
+ profile.put("profileImageUrl", data.optString("profile_image_url", ""));
427
+ profile.put("verified", data.optBoolean("verified", false));
428
+ if (data.has("email")) {
429
+ profile.put("email", data.optString("email", null));
430
+ } else {
431
+ profile.put("email", JSONObject.NULL);
448
432
  }
433
+ callback.onSuccess(profile);
434
+ } catch (JSONException e) {
435
+ callback.onError("Failed to parse Twitter profile response");
449
436
  }
450
437
  }
451
- );
438
+ }
439
+ );
452
440
  }
453
441
 
454
442
  private void persistTokens(String accessToken, String refreshToken, String tokenType, int expiresIn, JSONObject profile) {
@@ -194,7 +194,6 @@ public class DependencyAvailabilityChecker {
194
194
 
195
195
  // Check multiple critical classes to be resilient
196
196
  String[] appleClasses = {
197
- "com.auth0.android.jwt.JWT",
198
197
  "androidx.browser.customtabs.CustomTabsSession",
199
198
  "androidx.browser.customtabs.CustomTabsServiceConnection",
200
199
  "androidx.browser.customtabs.CustomTabsClient",
@@ -214,8 +213,7 @@ public class DependencyAvailabilityChecker {
214
213
  if (!allAvailable) {
215
214
  Log.w(
216
215
  LOG_TAG,
217
- "Apple Sign-In dependencies are not available. " +
218
- "Ensure JWT decode and CustomTabs dependencies are included in your app's build.gradle"
216
+ "Apple Sign-In dependencies are not available. " + "Ensure CustomTabs dependencies are included in your app's build.gradle"
219
217
  );
220
218
  }
221
219
 
@@ -223,9 +221,7 @@ public class DependencyAvailabilityChecker {
223
221
  }
224
222
 
225
223
  /**
226
- * Check if Twitter OAuth dependencies are available.
227
- * Checks config first (for "fake disable"), then checks classes.
228
- * Result is cached after first check.
224
+ * Twitter OAuth uses built-in Android APIs only (no external SDK). Respects config disable flag.
229
225
  */
230
226
  private static boolean isTwitterAvailable() {
231
227
  if (twitterDependenciesAvailable != null) {
@@ -239,28 +235,8 @@ public class DependencyAvailabilityChecker {
239
235
  return false;
240
236
  }
241
237
 
242
- // Check multiple critical classes to be resilient
243
- // Twitter uses OkHttp and standard Android APIs, but we check anyway for user control
244
- String[] twitterClasses = { "okhttp3.OkHttpClient", "okhttp3.Request", "okhttp3.Response", "okhttp3.FormBody" };
245
-
246
- boolean allAvailable = true;
247
- for (String className : twitterClasses) {
248
- if (!isClassAvailable(className)) {
249
- allAvailable = false;
250
- Log.w(LOG_TAG, "Twitter dependency class not available: " + className);
251
- }
252
- }
253
-
254
- twitterDependenciesAvailable = allAvailable;
255
-
256
- if (!allAvailable) {
257
- Log.w(
258
- LOG_TAG,
259
- "Twitter OAuth dependencies are not available. " + "Ensure OkHttp dependencies are included in your app's build.gradle"
260
- );
261
- }
262
-
263
- return allAvailable;
238
+ twitterDependenciesAvailable = true;
239
+ return true;
264
240
  }
265
241
 
266
242
  /**