private static String getOptionalJsonString(JsonObject jsonObject, String name) { JsonString value = jsonObject.getJsonString(name); if (value == null) { return null; } return value.getString(); }
@Override public boolean equals(Object obj) { if (this == obj){ return true; } if (!(obj instanceof JsonString)) { return false; } JsonString other = (JsonString)obj; return getString().equals(other.getString()); }
@Override public String getString(String name) { return getJsonString(name).getString(); }
@Override public String getString(int index) { return getJsonString(index).getString(); }
@Override public String getString(String name) { return getJsonString(name).getString(); }
@Override public String getString(int index) { return getJsonString(index).getString(); }
private Object convert(Claims claims, JsonValue value) { Class<?> claimClass = claims.getType(); if (claimClass.equals(String.class)) { if (value instanceof JsonString) { return ((JsonString) value).getString(); } } if (claimClass.equals(Long.class)) { if (value instanceof JsonNumber) { return ((JsonNumber) value).longValue(); } else if (value instanceof JsonString) { return Long.parseLong(((JsonString) value).getString()); } return Long.parseLong(value.toString()); } if (claimClass.equals(JsonObject.class)) { return value; } if (value instanceof JsonString) { return ((JsonString) value).getString(); } return value; }
/** * Extract a key value from json object that is a list of strings if present. * * @param json JsonObject to read key from * @param key key of the value we want to read * @return List of String value if present * @throws JwtException in case the key is of invalid content */ public static Optional<List<String>> getStrings(JsonObject json, String key) throws JwtException { return Optional.ofNullable(json.getJsonArray(key)) .map(it -> { try { return it.stream().map(it2 -> ((JsonString) it2).getString()).collect(Collectors.toList()); } catch (Exception e) { throw new JwtException("Invalid value. Expecting a string array for key " + key); } }); }
/** * A header field validator. * * @param fieldKey name of the header claim * @param name descriptive name of the field * @param expectedValue value to expect * @param mandatory whether the field is mandatory or optional * @return validator instance */ public static FieldValidator createForHeader(String fieldKey, String name, String expectedValue, boolean mandatory) { return create(jwt -> jwt.headerClaim(fieldKey) .map(it -> ((JsonString) it).getString()), name, expectedValue, mandatory); }
/** * A payload field validator. * * @param fieldKey name of the payload claim * @param name descriptive name of the field * @param expectedValue value to expect * @param mandatory whether the field is mandatory or optional * @return validator instance */ public static FieldValidator createForPayload(String fieldKey, String name, String expectedValue, boolean mandatory) { return create(jwt -> jwt.payloadClaim(fieldKey) .map(it -> ((JsonString) it).getString()), name, expectedValue, false); }
private static String asString(JsonValue value) { if (JsonValue.ValueType.STRING.equals(value.getValueType())) { return ((JsonString) value).getString(); } return value.toString(); } }
private PublicKey resolvePublicKey(JsonObject headers) { JsonString kid = headers.getJsonString("kid"); JsonString jku = headers.getJsonString("jku"); if (kid == null) { if (defaultPublicKey == null) { log.debug("Default public key not configured. Cannot validate token without kid claim."); return null; } return defaultPublicKey; } if (jku != null) { try { return jwkManager.getPublicKey(kid.getString(), new URL(jku.getString())); } catch (MalformedURLException e) { log.debug("Invalid jku URL."); return null; } } else { if (namedKeys.isEmpty()) { log.debug("Cannot validate token with kid claim."); return null; } PublicKey res = namedKeys.get(kid.getString()); if (res == null) { log.debug("Unknown kid."); } return res; } }
@Override public String getString() { switch (state) { case KEY_NAME: return ((ObjectScope)current).key; case VALUE_STRING: return ((JsonString)current.getJsonValue()).getString(); case VALUE_NUMBER: return ((JsonNumber)current.getJsonValue()).toString(); default: throw new IllegalStateException(JsonMessages.PARSER_GETSTRING_ERR(state)); } }
String[] queryAccountContactUrls(AcmeAccount account, boolean staging) throws AcmeException { Assert.checkNotNullParam("account", account); HttpURLConnection connection = sendPostAsGetRequest(account, staging, getAccountUrl(account, staging), null, HttpURLConnection.HTTP_OK); JsonObject jsonResponse = getJsonResponse(connection); JsonArray contactsArray = jsonResponse.getJsonArray(CONTACT); if (contactsArray != null && contactsArray.size() > 0) { List<String> contacts = new ArrayList<>(contactsArray.size()); for (JsonString contact : contactsArray.getValuesAs(JsonString.class)) { contacts.add(contact.getString()); } return contacts.toArray(new String[contacts.size()]); } return null; }
private JsonPointer getPointer(JsonObject operation, String member) { JsonString pointerString = operation.getJsonString(member); if (pointerString == null) { missingMember(operation.getString("op"), member); } return Json.createPointer(pointerString.getString()); }
private String resolveAlgorithm(JsonObject headers) { JsonString algClaim = (JsonString) headers.get("alg"); if (algClaim == null) { throw log.tokenRealmJwtSignatureInvalidAlgorithm("not_provided"); } String algorithm = algClaim.getString(); log.debugf("Token is using algorithm [%s]", algorithm); switch (algorithm) { case "RS256": return "SHA256withRSA"; case "RS384": return "SHA384withRSA"; case "RS512": return "SHA512withRSA"; default: throw log.tokenRealmJwtSignatureInvalidAlgorithm(algorithm); } }
/** * Processes a part of package.json (as defined by JsonObject) and update * the specified dependency with relevant info. * * @param engine the dependency-check engine * @param dependency the Dependency to update * @param jsonObject the jsonObject to parse * @param depType the dependency type */ protected void processPackage(Engine engine, Dependency dependency, JsonObject jsonObject, String depType) { for (int i = 0; i < jsonObject.size(); i++) { for (Map.Entry<String, JsonValue> entry : jsonObject.entrySet()) { final String name = entry.getKey(); String version = ""; if (entry.getValue() != null && entry.getValue().getValueType() == JsonValue.ValueType.STRING) { version = ((JsonString) entry.getValue()).getString(); } final Dependency existing = findDependency(engine, name, version); if (existing == null) { final Dependency nodeModule = createDependency(dependency, name, version, depType); engine.addDependency(nodeModule); } else { existing.addProjectReference(dependency.getName() + ": " + depType); } } } }
private boolean hasValidAudience(JsonObject claims) throws RealmUnavailableException { if (this.audiences.isEmpty()) return true; JsonValue audience = claims.get("aud"); if (audience == null) { log.debug("Token does not contain an audience claim"); return false; } JsonArray audClaimArray; if (JsonValue.ValueType.STRING.equals(audience.getValueType())) { audClaimArray = Json.createArrayBuilder().add(audience).build(); } else { audClaimArray = (JsonArray) audience; } boolean valid = audClaimArray.stream() .map(jsonValue -> (JsonString) jsonValue) .anyMatch(audience1 -> audiences.contains(audience1.getString())); if (!valid) { log.debugf("Audience check failed. Provided [%s] but was expected [%s].", audClaimArray.toArray(), this.audiences); } return valid; }
@Override protected Object decode( Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { FullHttpRequest request = (FullHttpRequest) msg; JsonArray result = Json.createReader(new StringReader(request.content().toString(StandardCharsets.UTF_8))) .readArray(); List<Position> positions = new LinkedList<>(); for (int i = 0; i < result.size(); i++) { JsonObject message = result.getJsonObject(i); JsonString ident = message.getJsonString("ident"); if (ident == null) { continue; } DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, ident.getString()); if (deviceSession == null) { continue; } Position position = new Position(getProtocolName()); position.setDeviceId(deviceSession.getDeviceId()); decodePosition(message, position); positions.add(position); } sendResponse(channel, HttpResponseStatus.OK); return positions; }
private void decodeUnknownParam(String name, JsonValue value, Position position) { if (value instanceof JsonNumber) { if (((JsonNumber) value).isIntegral()) { position.set(name, ((JsonNumber) value).longValue()); } else { position.set(name, ((JsonNumber) value).doubleValue()); } position.set(name, ((JsonNumber) value).doubleValue()); } else if (value instanceof JsonString) { position.set(name, ((JsonString) value).getString()); } else if (value == JsonValue.TRUE || value == JsonValue.FALSE) { position.set(name, value == JsonValue.TRUE); } }