@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.
- package/android/build.gradle +1 -2
- package/android/consumer-proguard-rules.pro +0 -9
- package/android/src/main/java/ee/forgr/capacitor/social/login/AppleProvider.java +56 -70
- package/android/src/main/java/ee/forgr/capacitor/social/login/GoogleProvider.java +91 -98
- package/android/src/main/java/ee/forgr/capacitor/social/login/OAuth2Provider.java +155 -171
- package/android/src/main/java/ee/forgr/capacitor/social/login/SocialLoginPlugin.java +55 -6
- package/android/src/main/java/ee/forgr/capacitor/social/login/TwitterProvider.java +98 -110
- package/android/src/main/java/ee/forgr/capacitor/social/login/helpers/DependencyAvailabilityChecker.java +4 -28
- package/android/src/main/java/ee/forgr/capacitor/social/login/helpers/PluginHttpClient.java +430 -0
- package/ios/Sources/SocialLoginPlugin/SocialLoginPlugin.swift +1 -1
- package/package.json +1 -1
|
@@ -11,6 +11,7 @@ import androidx.browser.customtabs.CustomTabsIntent;
|
|
|
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;
|
|
@@ -22,16 +23,10 @@ import java.util.Arrays;
|
|
|
22
23
|
import java.util.Collections;
|
|
23
24
|
import java.util.HashMap;
|
|
24
25
|
import java.util.Iterator;
|
|
26
|
+
import java.util.LinkedHashMap;
|
|
25
27
|
import java.util.List;
|
|
26
28
|
import java.util.Map;
|
|
27
29
|
import java.util.UUID;
|
|
28
|
-
import java.util.concurrent.TimeUnit;
|
|
29
|
-
import okhttp3.Call;
|
|
30
|
-
import okhttp3.Callback;
|
|
31
|
-
import okhttp3.FormBody;
|
|
32
|
-
import okhttp3.OkHttpClient;
|
|
33
|
-
import okhttp3.Request;
|
|
34
|
-
import okhttp3.Response;
|
|
35
30
|
import org.json.JSONArray;
|
|
36
31
|
import org.json.JSONException;
|
|
37
32
|
import org.json.JSONObject;
|
|
@@ -60,7 +55,7 @@ public class OAuth2Provider implements SocialProvider {
|
|
|
60
55
|
|
|
61
56
|
private final Activity activity;
|
|
62
57
|
private final Context context;
|
|
63
|
-
private final
|
|
58
|
+
private final PluginHttpClient httpClient;
|
|
64
59
|
|
|
65
60
|
// Map of providerId -> OAuth2ProviderConfig
|
|
66
61
|
private final Map<String, OAuth2ProviderConfig> providers = new HashMap<>();
|
|
@@ -183,7 +178,7 @@ public class OAuth2Provider implements SocialProvider {
|
|
|
183
178
|
public OAuth2Provider(Activity activity, Context context) {
|
|
184
179
|
this.activity = activity;
|
|
185
180
|
this.context = context;
|
|
186
|
-
this.httpClient =
|
|
181
|
+
this.httpClient = PluginHttpClient.THIRTY_SECOND_TIMEOUTS;
|
|
187
182
|
}
|
|
188
183
|
|
|
189
184
|
private interface DiscoveryCallback {
|
|
@@ -220,68 +215,66 @@ public class OAuth2Provider implements SocialProvider {
|
|
|
220
215
|
|
|
221
216
|
String issuer = trimTrailingSlashes(config.issuerUrl);
|
|
222
217
|
String discoveryUrl = issuer + "/.well-known/openid-configuration";
|
|
223
|
-
Request req = new Request.Builder().url(discoveryUrl).get().build();
|
|
224
218
|
if (config.logsEnabled) {
|
|
225
219
|
Log.d(LOG_TAG, "Discovering OIDC configuration at: " + discoveryUrl);
|
|
226
220
|
}
|
|
227
|
-
httpClient
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
221
|
+
httpClient.enqueueGet(
|
|
222
|
+
discoveryUrl,
|
|
223
|
+
null,
|
|
224
|
+
new PluginHttpClient.Callback() {
|
|
225
|
+
@Override
|
|
226
|
+
public void onFailure(IOException e) {
|
|
227
|
+
cb.onError("OIDC discovery failed: " + e.getMessage());
|
|
228
|
+
}
|
|
235
229
|
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
cb.onError("Failed to parse OIDC discovery response");
|
|
281
|
-
}
|
|
230
|
+
@Override
|
|
231
|
+
public void onResponse(int code, String body) throws IOException {
|
|
232
|
+
if (!PluginHttpClient.isSuccessfulHttpCode(code)) {
|
|
233
|
+
cb.onError("OIDC discovery failed: HTTP " + code);
|
|
234
|
+
return;
|
|
235
|
+
}
|
|
236
|
+
try {
|
|
237
|
+
JSONObject json = new JSONObject(body);
|
|
238
|
+
String auth = json.optString("authorization_endpoint", null);
|
|
239
|
+
String token = json.optString("token_endpoint", null);
|
|
240
|
+
String endSession = json.optString("end_session_endpoint", null);
|
|
241
|
+
|
|
242
|
+
OAuth2ProviderConfig resolved = new OAuth2ProviderConfig(
|
|
243
|
+
config.appId,
|
|
244
|
+
config.clientSecret,
|
|
245
|
+
config.issuerUrl,
|
|
246
|
+
(config.authorizationBaseUrl != null && !config.authorizationBaseUrl.isEmpty())
|
|
247
|
+
? config.authorizationBaseUrl
|
|
248
|
+
: auth,
|
|
249
|
+
(config.accessTokenEndpoint != null && !config.accessTokenEndpoint.isEmpty())
|
|
250
|
+
? config.accessTokenEndpoint
|
|
251
|
+
: token,
|
|
252
|
+
config.redirectUrl,
|
|
253
|
+
config.resourceUrl,
|
|
254
|
+
config.responseType,
|
|
255
|
+
config.pkceEnabled,
|
|
256
|
+
config.scope,
|
|
257
|
+
config.additionalParameters,
|
|
258
|
+
config.loginHint,
|
|
259
|
+
config.prompt,
|
|
260
|
+
config.additionalTokenParameters,
|
|
261
|
+
config.additionalResourceHeaders,
|
|
262
|
+
(config.logoutUrl != null && !config.logoutUrl.isEmpty()) ? config.logoutUrl : endSession,
|
|
263
|
+
config.postLogoutRedirectUrl,
|
|
264
|
+
config.additionalLogoutParameters,
|
|
265
|
+
config.androidUseCustomTabs,
|
|
266
|
+
config.logsEnabled,
|
|
267
|
+
config.clientIdParamName,
|
|
268
|
+
config.clientSecretParamName
|
|
269
|
+
);
|
|
270
|
+
providers.put(providerId, resolved);
|
|
271
|
+
cb.onSuccess(resolved);
|
|
272
|
+
} catch (JSONException e) {
|
|
273
|
+
cb.onError("Failed to parse OIDC discovery response");
|
|
282
274
|
}
|
|
283
275
|
}
|
|
284
|
-
|
|
276
|
+
}
|
|
277
|
+
);
|
|
285
278
|
}
|
|
286
279
|
|
|
287
280
|
/**
|
|
@@ -1103,67 +1096,64 @@ public class OAuth2Provider implements SocialProvider {
|
|
|
1103
1096
|
}
|
|
1104
1097
|
final String providerId = pendingState.providerId;
|
|
1105
1098
|
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1099
|
+
Map<String, String> form = new LinkedHashMap<>();
|
|
1100
|
+
if (config.additionalTokenParameters != null) {
|
|
1101
|
+
for (Map.Entry<String, String> entry : config.additionalTokenParameters.entrySet()) {
|
|
1102
|
+
form.put(entry.getKey(), entry.getValue());
|
|
1103
|
+
}
|
|
1104
|
+
}
|
|
1105
|
+
|
|
1106
|
+
form.put("grant_type", "authorization_code");
|
|
1107
|
+
form.put(config.clientIdParamName, config.appId);
|
|
1108
|
+
form.put("code", code);
|
|
1109
|
+
form.put("redirect_uri", pendingState.redirectUri);
|
|
1111
1110
|
|
|
1112
1111
|
if (config.pkceEnabled) {
|
|
1113
|
-
|
|
1112
|
+
form.put("code_verifier", pendingState.codeVerifier);
|
|
1114
1113
|
}
|
|
1115
1114
|
|
|
1116
1115
|
if (config.clientSecret != null) {
|
|
1117
|
-
|
|
1116
|
+
form.put(config.clientSecretParamName, config.clientSecret);
|
|
1118
1117
|
}
|
|
1119
1118
|
|
|
1120
|
-
if (config.additionalTokenParameters != null) {
|
|
1121
|
-
for (Map.Entry<String, String> entry : config.additionalTokenParameters.entrySet()) {
|
|
1122
|
-
bodyBuilder.add(entry.getKey(), entry.getValue());
|
|
1123
|
-
}
|
|
1124
|
-
}
|
|
1125
|
-
|
|
1126
|
-
Request request = new Request.Builder().url(config.accessTokenEndpoint).post(bodyBuilder.build()).build();
|
|
1127
|
-
|
|
1128
1119
|
if (config.logsEnabled) {
|
|
1129
1120
|
Log.d(LOG_TAG, "Exchanging code at: " + config.accessTokenEndpoint);
|
|
1130
1121
|
}
|
|
1131
1122
|
|
|
1132
|
-
httpClient
|
|
1133
|
-
.
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1123
|
+
httpClient.enqueuePostForm(
|
|
1124
|
+
config.accessTokenEndpoint,
|
|
1125
|
+
form,
|
|
1126
|
+
null,
|
|
1127
|
+
new PluginHttpClient.Callback() {
|
|
1128
|
+
@Override
|
|
1129
|
+
public void onFailure(IOException e) {
|
|
1130
|
+
if (pendingCall != null) {
|
|
1131
|
+
pendingCall.reject("OAuth2 token exchange failed", e);
|
|
1132
|
+
}
|
|
1133
|
+
cleanupPending();
|
|
1134
|
+
}
|
|
1135
|
+
|
|
1136
|
+
@Override
|
|
1137
|
+
public void onResponse(int code, String responseBody) throws IOException {
|
|
1138
|
+
if (!PluginHttpClient.isSuccessfulHttpCode(code)) {
|
|
1138
1139
|
if (pendingCall != null) {
|
|
1139
|
-
pendingCall.reject("OAuth2 token exchange failed"
|
|
1140
|
+
pendingCall.reject("OAuth2 token exchange failed: " + responseBody);
|
|
1140
1141
|
}
|
|
1141
1142
|
cleanupPending();
|
|
1143
|
+
return;
|
|
1142
1144
|
}
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
pendingCall.reject("OAuth2 token exchange failed: " + errorBody);
|
|
1150
|
-
}
|
|
1151
|
-
cleanupPending();
|
|
1152
|
-
return;
|
|
1153
|
-
}
|
|
1154
|
-
String responseBody = response.body() != null ? response.body().string() : "";
|
|
1155
|
-
try {
|
|
1156
|
-
JSONObject tokenPayload = new JSONObject(responseBody);
|
|
1157
|
-
handleTokenSuccess(providerId, config, tokenPayload);
|
|
1158
|
-
} catch (JSONException e) {
|
|
1159
|
-
if (pendingCall != null) {
|
|
1160
|
-
pendingCall.reject("Failed to parse OAuth2 token response", e);
|
|
1161
|
-
}
|
|
1162
|
-
cleanupPending();
|
|
1145
|
+
try {
|
|
1146
|
+
JSONObject tokenPayload = new JSONObject(responseBody);
|
|
1147
|
+
handleTokenSuccess(providerId, config, tokenPayload);
|
|
1148
|
+
} catch (JSONException e) {
|
|
1149
|
+
if (pendingCall != null) {
|
|
1150
|
+
pendingCall.reject("Failed to parse OAuth2 token response", e);
|
|
1163
1151
|
}
|
|
1152
|
+
cleanupPending();
|
|
1164
1153
|
}
|
|
1165
1154
|
}
|
|
1166
|
-
|
|
1155
|
+
}
|
|
1156
|
+
);
|
|
1167
1157
|
}
|
|
1168
1158
|
|
|
1169
1159
|
private void refreshWithToken(
|
|
@@ -1198,18 +1188,10 @@ public class OAuth2Provider implements SocialProvider {
|
|
|
1198
1188
|
return;
|
|
1199
1189
|
}
|
|
1200
1190
|
|
|
1201
|
-
|
|
1202
|
-
.add("grant_type", "refresh_token")
|
|
1203
|
-
.add("refresh_token", refreshToken)
|
|
1204
|
-
.add(config.clientIdParamName, config.appId);
|
|
1205
|
-
|
|
1206
|
-
if (config.clientSecret != null) {
|
|
1207
|
-
bodyBuilder.add(config.clientSecretParamName, config.clientSecret);
|
|
1208
|
-
}
|
|
1209
|
-
|
|
1191
|
+
Map<String, String> form = new LinkedHashMap<>();
|
|
1210
1192
|
if (config.additionalTokenParameters != null) {
|
|
1211
1193
|
for (Map.Entry<String, String> entry : config.additionalTokenParameters.entrySet()) {
|
|
1212
|
-
|
|
1194
|
+
form.put(entry.getKey(), entry.getValue());
|
|
1213
1195
|
}
|
|
1214
1196
|
}
|
|
1215
1197
|
|
|
@@ -1218,41 +1200,47 @@ public class OAuth2Provider implements SocialProvider {
|
|
|
1218
1200
|
Iterator<String> keys = additionalParameters.keys();
|
|
1219
1201
|
while (keys.hasNext()) {
|
|
1220
1202
|
String key = keys.next();
|
|
1221
|
-
|
|
1203
|
+
form.put(key, additionalParameters.getString(key));
|
|
1222
1204
|
}
|
|
1223
1205
|
} catch (JSONException e) {
|
|
1224
1206
|
Log.w(LOG_TAG, "Failed to parse additionalParameters for refresh", e);
|
|
1225
1207
|
}
|
|
1226
1208
|
}
|
|
1227
1209
|
|
|
1228
|
-
|
|
1210
|
+
form.put(config.clientIdParamName, config.appId);
|
|
1229
1211
|
|
|
1230
|
-
|
|
1231
|
-
.
|
|
1232
|
-
|
|
1233
|
-
new Callback() {
|
|
1234
|
-
@Override
|
|
1235
|
-
public void onFailure(Call call, IOException e) {
|
|
1236
|
-
pluginCall.reject("OAuth2 refresh failed", e);
|
|
1237
|
-
}
|
|
1212
|
+
if (config.clientSecret != null) {
|
|
1213
|
+
form.put(config.clientSecretParamName, config.clientSecret);
|
|
1214
|
+
}
|
|
1238
1215
|
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1216
|
+
form.put("grant_type", "refresh_token");
|
|
1217
|
+
form.put("refresh_token", refreshToken);
|
|
1218
|
+
|
|
1219
|
+
httpClient.enqueuePostForm(
|
|
1220
|
+
config.accessTokenEndpoint,
|
|
1221
|
+
form,
|
|
1222
|
+
null,
|
|
1223
|
+
new PluginHttpClient.Callback() {
|
|
1224
|
+
@Override
|
|
1225
|
+
public void onFailure(IOException e) {
|
|
1226
|
+
pluginCall.reject("OAuth2 refresh failed", e);
|
|
1227
|
+
}
|
|
1228
|
+
|
|
1229
|
+
@Override
|
|
1230
|
+
public void onResponse(int code, String responseBody) throws IOException {
|
|
1231
|
+
if (!PluginHttpClient.isSuccessfulHttpCode(code)) {
|
|
1232
|
+
pluginCall.reject("OAuth2 refresh failed: " + responseBody);
|
|
1233
|
+
return;
|
|
1234
|
+
}
|
|
1235
|
+
try {
|
|
1236
|
+
JSONObject tokenPayload = new JSONObject(responseBody);
|
|
1237
|
+
handleTokenSuccess(providerId, config, tokenPayload, pluginCall, refreshToken, wrapResponse);
|
|
1238
|
+
} catch (JSONException e) {
|
|
1239
|
+
pluginCall.reject("Failed to parse OAuth2 refresh response", e);
|
|
1253
1240
|
}
|
|
1254
1241
|
}
|
|
1255
|
-
|
|
1242
|
+
}
|
|
1243
|
+
);
|
|
1256
1244
|
}
|
|
1257
1245
|
|
|
1258
1246
|
private void handleTokenSuccess(String providerId, OAuth2ProviderConfig config, JSONObject tokenPayload) throws JSONException {
|
|
@@ -1423,40 +1411,36 @@ public class OAuth2Provider implements SocialProvider {
|
|
|
1423
1411
|
}
|
|
1424
1412
|
|
|
1425
1413
|
private void fetchResource(OAuth2ProviderConfig config, String accessToken, ResourceCallback callback) {
|
|
1426
|
-
|
|
1427
|
-
|
|
1414
|
+
Map<String, String> headers = new HashMap<>();
|
|
1415
|
+
headers.put("Authorization", "Bearer " + accessToken);
|
|
1428
1416
|
if (config.additionalResourceHeaders != null) {
|
|
1429
|
-
|
|
1430
|
-
requestBuilder.addHeader(entry.getKey(), entry.getValue());
|
|
1431
|
-
}
|
|
1417
|
+
headers.putAll(config.additionalResourceHeaders);
|
|
1432
1418
|
}
|
|
1433
1419
|
|
|
1434
|
-
httpClient
|
|
1435
|
-
.
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1420
|
+
httpClient.enqueueGet(
|
|
1421
|
+
config.resourceUrl,
|
|
1422
|
+
headers,
|
|
1423
|
+
new PluginHttpClient.Callback() {
|
|
1424
|
+
@Override
|
|
1425
|
+
public void onFailure(IOException e) {
|
|
1426
|
+
callback.onError("Failed to fetch resource: " + e.getMessage());
|
|
1427
|
+
}
|
|
1442
1428
|
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
} catch (JSONException e) {
|
|
1455
|
-
callback.onError("Failed to parse resource response");
|
|
1456
|
-
}
|
|
1429
|
+
@Override
|
|
1430
|
+
public void onResponse(int code, String responseBody) throws IOException {
|
|
1431
|
+
if (!PluginHttpClient.isSuccessfulHttpCode(code)) {
|
|
1432
|
+
callback.onError("Failed to fetch resource: " + responseBody);
|
|
1433
|
+
return;
|
|
1434
|
+
}
|
|
1435
|
+
try {
|
|
1436
|
+
JSONObject data = new JSONObject(responseBody);
|
|
1437
|
+
callback.onSuccess(data);
|
|
1438
|
+
} catch (JSONException e) {
|
|
1439
|
+
callback.onError("Failed to parse resource response");
|
|
1457
1440
|
}
|
|
1458
1441
|
}
|
|
1459
|
-
|
|
1442
|
+
}
|
|
1443
|
+
);
|
|
1460
1444
|
}
|
|
1461
1445
|
|
|
1462
1446
|
private void persistTokens(
|
|
@@ -19,15 +19,17 @@ import ee.forgr.capacitor.social.login.helpers.DependencyAvailabilityChecker;
|
|
|
19
19
|
import ee.forgr.capacitor.social.login.helpers.SocialProvider;
|
|
20
20
|
import java.net.URLDecoder;
|
|
21
21
|
import java.nio.charset.StandardCharsets;
|
|
22
|
+
import java.util.Date;
|
|
22
23
|
import java.util.HashMap;
|
|
23
24
|
import org.json.JSONArray;
|
|
24
25
|
import org.json.JSONException;
|
|
25
26
|
import org.json.JSONObject;
|
|
27
|
+
import org.json.JSONTokener;
|
|
26
28
|
|
|
27
29
|
@CapacitorPlugin(name = "SocialLogin")
|
|
28
30
|
public class SocialLoginPlugin extends Plugin {
|
|
29
31
|
|
|
30
|
-
private final String pluginVersion = "8.7.
|
|
32
|
+
private final String pluginVersion = "8.7.5";
|
|
31
33
|
|
|
32
34
|
public static String LOG_TAG = "CapgoSocialLogin";
|
|
33
35
|
private static final String USER_CANCELLED_CODE = "USER_CANCELLED";
|
|
@@ -166,7 +168,7 @@ public class SocialLoginPlugin extends Plugin {
|
|
|
166
168
|
if (!DependencyAvailabilityChecker.isProviderAvailable("apple")) {
|
|
167
169
|
call.reject(
|
|
168
170
|
"Apple Sign-In provider is disabled. " +
|
|
169
|
-
"Dependencies are not available. Ensure
|
|
171
|
+
"Dependencies are not available. Ensure CustomTabs dependencies are included in your app's build.gradle"
|
|
170
172
|
);
|
|
171
173
|
return;
|
|
172
174
|
}
|
|
@@ -259,10 +261,7 @@ public class SocialLoginPlugin extends Plugin {
|
|
|
259
261
|
if (twitter != null) {
|
|
260
262
|
// Check if Twitter dependencies are available
|
|
261
263
|
if (!DependencyAvailabilityChecker.isProviderAvailable("twitter")) {
|
|
262
|
-
call.reject(
|
|
263
|
-
"Twitter provider is disabled. " +
|
|
264
|
-
"Dependencies are not available. Ensure OkHttp dependencies are included in your app's build.gradle"
|
|
265
|
-
);
|
|
264
|
+
call.reject("Twitter provider is disabled via configuration.");
|
|
266
265
|
return;
|
|
267
266
|
}
|
|
268
267
|
|
|
@@ -719,6 +718,56 @@ public class SocialLoginPlugin extends Plugin {
|
|
|
719
718
|
return new JSONObject(json);
|
|
720
719
|
}
|
|
721
720
|
|
|
721
|
+
static JSONObject decodeJwtHeaderObject(String idToken) throws JSONException {
|
|
722
|
+
byte[] decoded = Base64.decode(getJwtHeaderSegment(idToken), Base64.URL_SAFE | Base64.NO_PADDING | Base64.NO_WRAP);
|
|
723
|
+
String headerJson = new String(decoded, StandardCharsets.UTF_8);
|
|
724
|
+
JSONTokener tokener = new JSONTokener(headerJson);
|
|
725
|
+
JSONObject header = new JSONObject(tokener);
|
|
726
|
+
if (tokener.more()) {
|
|
727
|
+
throw new JSONException("Invalid JWT header");
|
|
728
|
+
}
|
|
729
|
+
return header;
|
|
730
|
+
}
|
|
731
|
+
|
|
732
|
+
static String getJwtHeaderSegment(String idToken) throws JSONException {
|
|
733
|
+
String[] parts = idToken.split("\\.");
|
|
734
|
+
if (parts.length < 1 || parts[0].isEmpty()) {
|
|
735
|
+
throw new JSONException("Invalid JWT");
|
|
736
|
+
}
|
|
737
|
+
return parts[0];
|
|
738
|
+
}
|
|
739
|
+
|
|
740
|
+
/**
|
|
741
|
+
* Returns whether the JWT should be treated as expired for login checks.
|
|
742
|
+
* Matches Auth0 {@code JWT.isExpired(leewaySeconds)} from jwtdecode 2.0.2 (second-truncated clock, exp/iat leeway).
|
|
743
|
+
*/
|
|
744
|
+
static boolean isJwtExpired(String idToken, long leewaySeconds) throws JSONException {
|
|
745
|
+
if (leewaySeconds < 0) {
|
|
746
|
+
throw new IllegalArgumentException("The leeway must be a positive value.");
|
|
747
|
+
}
|
|
748
|
+
assertJwtHasThreeParts(idToken);
|
|
749
|
+
decodeJwtHeaderObject(idToken);
|
|
750
|
+
JSONObject claims = decodeJwtClaims(idToken);
|
|
751
|
+
long todayTime = (long) (Math.floor(System.currentTimeMillis() / 1000.0) * 1000);
|
|
752
|
+
Date futureToday = new Date(todayTime + leewaySeconds * 1000L);
|
|
753
|
+
Date pastToday = new Date(todayTime - leewaySeconds * 1000L);
|
|
754
|
+
Date exp = claims.has("exp") ? new Date(claims.getLong("exp") * 1000L) : null;
|
|
755
|
+
Date iat = claims.has("iat") ? new Date(claims.getLong("iat") * 1000L) : null;
|
|
756
|
+
boolean expValid = exp == null || !pastToday.after(exp);
|
|
757
|
+
boolean iatValid = iat == null || !futureToday.before(iat);
|
|
758
|
+
return !expValid || !iatValid;
|
|
759
|
+
}
|
|
760
|
+
|
|
761
|
+
static void assertJwtHasThreeParts(String idToken) throws JSONException {
|
|
762
|
+
String[] parts = idToken.split("\\.");
|
|
763
|
+
if (parts.length == 2 && idToken.endsWith(".")) {
|
|
764
|
+
return;
|
|
765
|
+
}
|
|
766
|
+
if (parts.length != 3) {
|
|
767
|
+
throw new JSONException(String.format("The token was expected to have 3 parts, but got %s.", parts.length));
|
|
768
|
+
}
|
|
769
|
+
}
|
|
770
|
+
|
|
722
771
|
static String getJwtPayloadSegment(String idToken) throws JSONException {
|
|
723
772
|
String[] parts = idToken.split("\\.");
|
|
724
773
|
if (parts.length < 2) {
|