@capgo/capacitor-social-login 8.7.4 → 8.7.5

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.
@@ -70,8 +70,6 @@ dependencies {
70
70
  implementation fileTree(dir: 'libs', include: ['*.jar'])
71
71
  implementation project(':capacitor-android')
72
72
  implementation "androidx.appcompat:appcompat:$androidxAppCompatVersion"
73
- implementation 'com.squareup.okhttp3:okhttp:4.12.0'
74
- implementation 'com.auth0.android:jwtdecode:2.0.2'
75
73
  implementation 'androidx.credentials:credentials:1.5.0'
76
74
  implementation 'androidx.concurrent:concurrent-futures:1.3.0'
77
75
  implementation 'androidx.browser:browser:1.9.0'
@@ -125,6 +123,7 @@ dependencies {
125
123
  // Test dependencies (always included)
126
124
  testImplementation "junit:junit:$junitVersion"
127
125
  testImplementation "org.json:json:20240303"
126
+ testImplementation 'org.robolectric:robolectric:4.14.1'
128
127
  androidTestImplementation "androidx.test.ext:junit:$androidxJunitVersion"
129
128
  androidTestImplementation "androidx.test.espresso:espresso-core:$androidxEspressoCoreVersion"
130
129
  }
@@ -15,15 +15,6 @@
15
15
  -keep class com.getcapacitor.** { *; }
16
16
  -keepclassmembers class com.getcapacitor.** { *; }
17
17
 
18
- # OkHttp (used for OAuth2/Twitter token endpoints)
19
- -dontwarn okhttp3.**
20
- -dontwarn okio.**
21
- -keep class okhttp3.** { *; }
22
- -keep interface okhttp3.** { *; }
23
-
24
- # JWT Decode library (used for Apple Sign-In)
25
- -keep class com.auth0.android.jwt.** { *; }
26
-
27
18
  # Google Sign-In (if included)
28
19
  -keep class com.google.android.gms.auth.** { *; }
29
20
  -keep class com.google.android.libraries.identity.googleid.** { *; }
@@ -32,20 +32,15 @@ import androidx.browser.customtabs.CustomTabsIntent;
32
32
  import androidx.browser.customtabs.CustomTabsServiceConnection;
33
33
  import androidx.browser.customtabs.CustomTabsSession;
34
34
  import androidx.browser.trusted.TrustedWebActivityIntentBuilder;
35
- import com.auth0.android.jwt.JWT;
36
35
  import com.getcapacitor.JSObject;
37
36
  import com.getcapacitor.PluginCall;
38
37
  import com.google.androidbrowserhelper.trusted.TwaLauncher;
38
+ import ee.forgr.capacitor.social.login.helpers.PluginHttpClient;
39
39
  import ee.forgr.capacitor.social.login.helpers.SocialProvider;
40
40
  import java.io.IOException;
41
- import java.util.Objects;
41
+ import java.util.LinkedHashMap;
42
+ import java.util.Map;
42
43
  import java.util.UUID;
43
- import okhttp3.Call;
44
- import okhttp3.Callback;
45
- import okhttp3.FormBody;
46
- import okhttp3.OkHttpClient;
47
- import okhttp3.Request;
48
- import okhttp3.Response;
49
44
  import org.json.JSONArray;
50
45
  import org.json.JSONException;
51
46
  import org.json.JSONObject;
@@ -313,8 +308,7 @@ public class AppleProvider implements SocialProvider {
313
308
  public void isLoggedIn(PluginCall call) {
314
309
  if (this.idToken != null && !this.idToken.isEmpty()) {
315
310
  try {
316
- JWT jwt = new JWT(this.idToken);
317
- boolean isLoggedIn = !jwt.isExpired(0);
311
+ boolean isLoggedIn = !SocialLoginPlugin.isJwtExpired(this.idToken, 0);
318
312
  call.resolve(new JSObject().put("isLoggedIn", isLoggedIn));
319
313
  } catch (Exception e) {
320
314
  call.reject("Error checking login status", e);
@@ -404,76 +398,68 @@ public class AppleProvider implements SocialProvider {
404
398
  }
405
399
 
406
400
  private void requestForAccessToken(String code, String clientSecret) {
407
- OkHttpClient client = new OkHttpClient();
408
- FormBody formBody = new FormBody.Builder()
409
- .add("grant_type", "authorization_code")
410
- .add("code", code)
411
- .add("redirect_uri", redirectUrl)
412
- .add("client_id", clientId)
413
- .add("client_secret", clientSecret)
414
- .build();
415
-
416
- Request request = new Request.Builder().url(TOKENURL).post(formBody).build();
417
-
418
- client
419
- .newCall(request)
420
- .enqueue(
421
- new Callback() {
422
- @Override
423
- public void onFailure(@NonNull Call call, @NonNull IOException e) {
424
- if (AppleProvider.this.lastcall != null) {
425
- AppleProvider.this.lastcall.reject("Cannot get access_token", e);
426
- clearPendingLoginCall();
427
- } else {
428
- Log.e(SocialLoginPlugin.LOG_TAG, "Cannot get access_token: lastcall is null. Error: " + e.getMessage(), e);
429
- }
401
+ Map<String, String> form = new LinkedHashMap<>();
402
+ form.put("grant_type", "authorization_code");
403
+ form.put("code", code);
404
+ form.put("redirect_uri", redirectUrl);
405
+ form.put("client_id", clientId);
406
+ form.put("client_secret", clientSecret);
407
+
408
+ PluginHttpClient.DEFAULT.enqueuePostForm(
409
+ TOKENURL,
410
+ form,
411
+ null,
412
+ new PluginHttpClient.Callback() {
413
+ @Override
414
+ public void onFailure(IOException e) {
415
+ if (AppleProvider.this.lastcall != null) {
416
+ AppleProvider.this.lastcall.reject("Cannot get access_token", e);
417
+ clearPendingLoginCall();
418
+ } else {
419
+ Log.e(SocialLoginPlugin.LOG_TAG, "Cannot get access_token: lastcall is null. Error: " + e.getMessage(), e);
430
420
  }
421
+ }
431
422
 
432
- @Override
433
- public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
434
- try {
435
- if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
436
-
437
- String responseData = Objects.requireNonNull(response.body()).string();
438
- JSONObject jsonObject = (JSONObject) new JSONTokener(responseData).nextValue();
439
- String accessToken = jsonObject.getString("access_token");
440
- String refreshToken = jsonObject.getString("refresh_token");
441
- String idToken = jsonObject.getString("id_token");
423
+ @Override
424
+ public void onResponse(int code, String responseData) throws IOException {
425
+ try {
426
+ if (!PluginHttpClient.isSuccessfulHttpCode(code)) {
427
+ throw new IOException("Unexpected code " + code);
428
+ }
442
429
 
443
- persistState(idToken, refreshToken, accessToken);
430
+ JSONObject jsonObject = (JSONObject) new JSONTokener(responseData).nextValue();
431
+ String accessToken = jsonObject.getString("access_token");
432
+ String refreshToken = jsonObject.getString("refresh_token");
433
+ String idToken = jsonObject.getString("id_token");
444
434
 
445
- // Create proper response with all tokens
446
- JSObject result = new JSObject();
447
- result.put("accessToken", createAccessTokenObject(accessToken));
448
- result.put("profile", createProfileObject(idToken));
449
- result.put("idToken", idToken);
435
+ persistState(idToken, refreshToken, accessToken);
450
436
 
451
- // For legacy mode, we don't include authorization code in the response
452
- // since we've already exchanged it for proper tokens
437
+ JSObject result = new JSObject();
438
+ result.put("accessToken", createAccessTokenObject(accessToken));
439
+ result.put("profile", createProfileObject(idToken));
440
+ result.put("idToken", idToken);
453
441
 
454
- JSObject appleResponse = new JSObject();
455
- appleResponse.put("provider", "apple");
456
- appleResponse.put("result", result);
442
+ JSObject appleResponse = new JSObject();
443
+ appleResponse.put("provider", "apple");
444
+ appleResponse.put("result", result);
457
445
 
458
- if (AppleProvider.this.lastcall != null) {
459
- AppleProvider.this.lastcall.resolve(appleResponse);
460
- clearPendingLoginCall();
461
- } else {
462
- Log.e(SocialLoginPlugin.LOG_TAG, "Cannot resolve access_token response: lastcall is null");
463
- }
464
- } catch (Exception e) {
465
- if (AppleProvider.this.lastcall != null) {
466
- AppleProvider.this.lastcall.reject("Cannot get access_token", e);
467
- clearPendingLoginCall();
468
- } else {
469
- Log.e(SocialLoginPlugin.LOG_TAG, "Cannot get access_token: lastcall is null. Error: " + e.getMessage(), e);
470
- }
471
- } finally {
472
- response.close();
446
+ if (AppleProvider.this.lastcall != null) {
447
+ AppleProvider.this.lastcall.resolve(appleResponse);
448
+ clearPendingLoginCall();
449
+ } else {
450
+ Log.e(SocialLoginPlugin.LOG_TAG, "Cannot resolve access_token response: lastcall is null");
451
+ }
452
+ } catch (Exception e) {
453
+ if (AppleProvider.this.lastcall != null) {
454
+ AppleProvider.this.lastcall.reject("Cannot get access_token", e);
455
+ clearPendingLoginCall();
456
+ } else {
457
+ Log.e(SocialLoginPlugin.LOG_TAG, "Cannot get access_token: lastcall is null. Error: " + e.getMessage(), e);
473
458
  }
474
459
  }
475
460
  }
476
- );
461
+ }
462
+ );
477
463
  }
478
464
 
479
465
  private void persistState(String idToken, String refreshToken, String accessToken) throws JSONException {
@@ -34,8 +34,11 @@ import com.google.android.libraries.identity.googleid.GetGoogleIdOption;
34
34
  import com.google.android.libraries.identity.googleid.GetSignInWithGoogleOption;
35
35
  import com.google.android.libraries.identity.googleid.GoogleIdTokenCredential;
36
36
  import com.google.common.util.concurrent.ListenableFuture;
37
+ import ee.forgr.capacitor.social.login.helpers.PluginHttpClient;
37
38
  import ee.forgr.capacitor.social.login.helpers.SocialProvider;
38
39
  import java.io.IOException;
40
+ import java.net.URLEncoder;
41
+ import java.nio.charset.StandardCharsets;
39
42
  import java.security.MessageDigest;
40
43
  import java.security.NoSuchAlgorithmException;
41
44
  import java.util.ArrayList;
@@ -47,12 +50,6 @@ import java.util.concurrent.Executor;
47
50
  import java.util.concurrent.ExecutorService;
48
51
  import java.util.concurrent.Executors;
49
52
  import java.util.concurrent.TimeUnit;
50
- import okhttp3.Call;
51
- import okhttp3.Callback;
52
- import okhttp3.OkHttpClient;
53
- import okhttp3.Request;
54
- import okhttp3.Response;
55
- import okhttp3.ResponseBody;
56
53
  import org.json.JSONArray;
57
54
  import org.json.JSONException;
58
55
  import org.json.JSONObject;
@@ -275,121 +272,117 @@ public class GoogleProvider implements SocialProvider {
275
272
 
276
273
  public ListenableFuture<Boolean> accessTokenIsValid(String accessToken) {
277
274
  return CallbackToFutureAdapter.getFuture((completer) -> {
278
- OkHttpClient client = new OkHttpClient();
279
- Request tokenRequest = new Request.Builder().url(TOKEN_REQUEST_URL + "?" + "access_token=" + accessToken).get().build();
280
-
281
- client
282
- .newCall(tokenRequest)
283
- .enqueue(
284
- new Callback() {
285
- @Override
286
- public void onFailure(@NonNull Call call, @NonNull IOException e) {}
275
+ String url = TOKEN_REQUEST_URL + "?access_token=" + URLEncoder.encode(accessToken, StandardCharsets.UTF_8.name());
276
+ PluginHttpClient.DEFAULT.enqueueGet(
277
+ url,
278
+ null,
279
+ new PluginHttpClient.Callback() {
280
+ @Override
281
+ public void onFailure(IOException e) {
282
+ completer.setException(e);
283
+ }
287
284
 
288
- @Override
289
- public void onResponse(@NonNull Call httpCall, @NonNull Response httpResponse) throws IOException {
290
- if (!httpResponse.isSuccessful()) {
291
- completer.set(false);
292
- Log.i(
293
- LOG_TAG,
294
- String.format(
295
- "Invalid response from %s. Response not successful. Status code: %s. Assuming that the token is not valid",
296
- TOKEN_REQUEST_URL,
297
- httpResponse.code()
298
- )
299
- );
300
- return;
301
- }
285
+ @Override
286
+ public void onResponse(int code, String responseString) throws IOException {
287
+ if (!PluginHttpClient.isSuccessfulHttpCode(code)) {
288
+ completer.set(false);
289
+ Log.i(
290
+ LOG_TAG,
291
+ String.format(
292
+ "Invalid response from %s. Response not successful. Status code: %s. Assuming that the token is not valid",
293
+ TOKEN_REQUEST_URL,
294
+ code
295
+ )
296
+ );
297
+ return;
298
+ }
302
299
 
303
- ResponseBody responseBody = httpResponse.body();
304
- if (responseBody == null) {
305
- completer.setException(
306
- new RuntimeException(
307
- String.format("Invalid response from %s. Response body is null", TOKEN_REQUEST_URL)
308
- )
309
- );
310
- Log.e(LOG_TAG, String.format("Invalid response from %s. Response body is null", TOKEN_REQUEST_URL));
311
- return;
312
- }
300
+ if (responseString == null || responseString.isEmpty()) {
301
+ completer.setException(
302
+ new RuntimeException(String.format("Invalid response from %s. Response body is null", TOKEN_REQUEST_URL))
303
+ );
304
+ Log.e(LOG_TAG, String.format("Invalid response from %s. Response body is null", TOKEN_REQUEST_URL));
305
+ return;
306
+ }
313
307
 
314
- String responseString = responseBody.string();
315
- JSONObject jsonObject;
316
- try {
317
- jsonObject = (JSONObject) new JSONTokener(responseString).nextValue();
318
- } catch (JSONException e) {
319
- completer.setException(
320
- new RuntimeException(
321
- String.format(
322
- "Invalid response from %s. Response body is not a valid JSON. Error: %s",
323
- TOKEN_REQUEST_URL,
324
- e
325
- )
326
- )
327
- );
328
- Log.e(
329
- LOG_TAG,
308
+ JSONObject jsonObject;
309
+ try {
310
+ jsonObject = (JSONObject) new JSONTokener(responseString).nextValue();
311
+ } catch (JSONException e) {
312
+ completer.setException(
313
+ new RuntimeException(
330
314
  String.format(
331
315
  "Invalid response from %s. Response body is not a valid JSON. Error: %s",
332
316
  TOKEN_REQUEST_URL,
333
317
  e
334
318
  )
335
- );
336
- return;
337
- }
319
+ )
320
+ );
321
+ Log.e(
322
+ LOG_TAG,
323
+ String.format(
324
+ "Invalid response from %s. Response body is not a valid JSON. Error: %s",
325
+ TOKEN_REQUEST_URL,
326
+ e
327
+ )
328
+ );
329
+ return;
330
+ }
338
331
 
339
- String expiresIn;
340
- try {
341
- expiresIn = jsonObject.getString("expires_in");
342
- } catch (JSONException e) {
343
- completer.setException(
344
- new RuntimeException(
345
- String.format(
346
- "Invalid response from %s. Response JSON does not include expires_in. Error: %s",
347
- TOKEN_REQUEST_URL,
348
- e
349
- )
350
- )
351
- );
352
- Log.e(
353
- LOG_TAG,
332
+ String expiresIn;
333
+ try {
334
+ expiresIn = jsonObject.getString("expires_in");
335
+ } catch (JSONException e) {
336
+ completer.setException(
337
+ new RuntimeException(
354
338
  String.format(
355
339
  "Invalid response from %s. Response JSON does not include expires_in. Error: %s",
356
340
  TOKEN_REQUEST_URL,
357
341
  e
358
342
  )
359
- );
360
- return;
361
- }
343
+ )
344
+ );
345
+ Log.e(
346
+ LOG_TAG,
347
+ String.format(
348
+ "Invalid response from %s. Response JSON does not include expires_in. Error: %s",
349
+ TOKEN_REQUEST_URL,
350
+ e
351
+ )
352
+ );
353
+ return;
354
+ }
362
355
 
363
- int expressInInt;
364
- try {
365
- expressInInt = Integer.parseInt(expiresIn);
366
- } catch (Exception e) {
367
- completer.setException(
368
- new RuntimeException(
369
- String.format(
370
- "Invalid response from %s. expires_in: %s is not a valid int. Error: %s",
371
- TOKEN_REQUEST_URL,
372
- expiresIn,
373
- e
374
- )
375
- )
376
- );
377
- Log.e(
378
- LOG_TAG,
356
+ int expressInInt;
357
+ try {
358
+ expressInInt = Integer.parseInt(expiresIn);
359
+ } catch (Exception e) {
360
+ completer.setException(
361
+ new RuntimeException(
379
362
  String.format(
380
363
  "Invalid response from %s. expires_in: %s is not a valid int. Error: %s",
381
364
  TOKEN_REQUEST_URL,
382
365
  expiresIn,
383
366
  e
384
367
  )
385
- );
386
- return;
387
- }
388
-
389
- completer.set(expressInInt > 5);
368
+ )
369
+ );
370
+ Log.e(
371
+ LOG_TAG,
372
+ String.format(
373
+ "Invalid response from %s. expires_in: %s is not a valid int. Error: %s",
374
+ TOKEN_REQUEST_URL,
375
+ expiresIn,
376
+ e
377
+ )
378
+ );
379
+ return;
390
380
  }
381
+
382
+ completer.set(expressInInt > 5);
391
383
  }
392
- );
384
+ }
385
+ );
393
386
 
394
387
  return "AccessTokenIsValidOperationTag";
395
388
  });