public static Jwt decode(String data) { if (data == null) { throw new IllegalArgumentException("Data string is null"); } final String[] tokens = data.split("\\."); if (tokens.length != 3) { throw new IllegalArgumentException("Invalid JWT data format: '" + data + "'"); } final JSONObject headerJson = decodeBase64Json(tokens[0]); final String alg = headerJson.optString("alg", null); final String type = headerJson.optString("typ", null); if (alg == null || type == null) { throw new IllegalArgumentException("Invalid jwt header: '" + headerJson + "'"); } final JSONObject payloadJson = decodeBase64Json(tokens[1]); return new Jwt(alg, type, payloadJson); }
/** * Reads session from JWT-token. * Returns <code>null</code> if fails. */ static @Nullable LogMonitorSession readSessionFromJWT(String token) { try { final Jwt jwt = Jwt.decode(token); JSONObject payload = jwt.getPayload(); LogMonitorSession config = new LogMonitorSession(); // recipients JSONArray recipientsJson = payload.optJSONArray("recipients"); if (recipientsJson != null) { String[] recipients = new String[recipientsJson.length()]; for (int i = 0; i < recipientsJson.length(); ++i) { recipients[i] = recipientsJson.optString(i); } config.emailRecipients = recipients; } return config; } catch (Exception e) { ApptentiveLog.e(e, "Exception while parsing access token: '%s'", token); logException(e); return null; } }
final Jwt jwt = Jwt.decode(token); userId = jwt.getPayload().optString("sub"); if (StringUtils.isNullOrEmpty(userId)) { ApptentiveLog.e(CONVERSATION, "Error while extracting user id: Missing field \"sub\"");