/** * Create an address object from json representation. * * @param jsonObject object with expected keys */ public Address(JsonObject jsonObject) { this.formatted = getString(jsonObject, "formatted"); this.streetAddress = getString(jsonObject, "street_address"); this.locality = getString(jsonObject, "locality"); this.region = getString(jsonObject, "region"); this.postalCode = getString(jsonObject, "postal_code"); this.country = getString(jsonObject, "country"); }
T fromJson(JsonObject json) { // key type agnostic values keyType(asString(json, PARAM_KEY_TYPE, "JWK Key type")); keyId(asString(json, PARAM_KEY_ID, "JWK Key id")); getString(json, PARAM_ALGORITHM).ifPresent(this::algorithm); /* sig - signatures or MAC enc - encryption */ getString(json, PARAM_USE).ifPresent(this::usage); /* sign - compute digital signature or MAC verify - verify digital signature encrypt - encrypt content decrypt - decrypt content wrapKey - encrypt key unwrapKey - decrypt key deriveKey - derive key deriveBits - derive bits not to be used as a key */ getStrings(json, PARAM_OPERATIONS).ifPresent(this::operations); return myInstance; } }
static Optional<URI> toUri(JsonObject json, String name) { return getString(json, name) .map(URI::create); }
T fromJson(JsonObject json) { super.fromJson(json); // get cert chain from URL or from fields if present OptionalHelper.from(JwtUtil.getString(json, PARAM_X509_CHAIN_URL) .map(URI::create) .map(Builder::processCertChain)) .or(() -> JwtUtil.getStrings(json, PARAM_X509_CHAIN) // certificate chain as base64 encoded array .map(Builder::processCertChain)) .asOptional() .ifPresent(this::certificateChain); // thumbprints this.sha1Thumbprint = JwtUtil.getByteArray(json, PARAM_X509_SHA_1, "SHA-1 Certificate Thumbprint").orElse(null); this.sha256Thumbprint = JwtUtil.getByteArray(json, PARAM_X509_SHA_256, "SHA-256 Certificate Thumbprint").orElse(null); return myInstance; } }
static Optional<Locale> toLocale(JsonObject json, String name) { return getString(json, name) .map(JwtUtil::toLocale); }
this.algorithm = JwtUtil.getString(headerJson, "alg"); this.keyId = JwtUtil.getString(headerJson, "kid"); this.type = JwtUtil.getString(headerJson, "typ"); this.contentType = JwtUtil.getString(headerJson, "cty"); this.issuer = JwtUtil.getString(payloadJson, "iss"); this.expirationTime = JwtUtil.toInstant(payloadJson, "exp"); this.issueTime = JwtUtil.toInstant(payloadJson, "iat"); this.notBefore = JwtUtil.toInstant(payloadJson, "nbf"); this.subject = JwtUtil.getString(payloadJson, "sub"); JsonValue groups = payloadJson.get("groups"); if (groups instanceof JsonArray) { this.userGroups = JwtUtil.getStrings(payloadJson, "groups"); } else { this.userGroups = JwtUtil.getString(payloadJson, "groups").map(CollectionsHelper::listOf); this.audience = JwtUtil.getStrings(payloadJson, "aud"); } else { this.audience = JwtUtil.getString(payloadJson, "aud").map(CollectionsHelper::listOf); this.jwtId = JwtUtil.getString(payloadJson, "jti"); this.email = JwtUtil.getString(payloadJson, "email"); this.emailVerified = JwtUtil.toBoolean(payloadJson, "email_verified"); this.fullName = JwtUtil.getString(payloadJson, "name"); this.givenName = JwtUtil.getString(payloadJson, "given_name"); this.middleName = JwtUtil.getString(payloadJson, "middle_name"); this.familyName = JwtUtil.getString(payloadJson, "family_name"); this.locale = JwtUtil.toLocale(payloadJson, "locale"); this.nickname = JwtUtil.getString(payloadJson, "nickname");
static Optional<ZoneId> toTimeZone(JsonObject json, String name) { return getString(json, name) .map(ZoneId::of); }
T fromJson(JsonObject json) { super.fromJson(json); // get cert chain from URL or from fields if present OptionalHelper.from(JwtUtil.getString(json, PARAM_X509_CHAIN_URL) .map(URI::create) .map(Builder::processCertChain)) .or(() -> JwtUtil.getStrings(json, PARAM_X509_CHAIN) // certificate chain as base64 encoded array .map(Builder::processCertChain)) .asOptional() .ifPresent(this::certificateChain); // thumbprints this.sha1Thumbprint = JwtUtil.getByteArray(json, PARAM_X509_SHA_1, "SHA-1 Certificate Thumbprint").orElse(null); this.sha256Thumbprint = JwtUtil.getByteArray(json, PARAM_X509_SHA_256, "SHA-256 Certificate Thumbprint").orElse(null); return myInstance; } }
static Optional<List<String>> toScopes(JsonObject json) { return getString(json, "scope") .map(it -> Arrays.asList(it.split(" "))); }
static Optional<LocalDate> toDate(JsonObject json, String name) { return getString(json, name) .map(it -> { if (it.length() == 4) { return LocalDate.parse(it, YEAR_FORMAT); } else { return LocalDate.parse(it, DATE_FORMAT); } }); }
/** * Extract a key value from json object that is string. * * @param json JsonObject to read key from * @param key key of the value we want to read * @param description description of the field for error handling * @return String value * @throws JwtException in case the key is not present or is of invalid content */ public static String asString(JsonObject json, String key, String description) throws JwtException { return getString(json, key) .orElseThrow(() -> new JwtException("Key \"" + key + "\" is mandatory for " + description)); }
/** * Extract a key value from json object that is a base64-url encoded byte array, if present. * * @param json JsonObject to read key from * @param key key of the value we want to read * @param description description of the field for error handling * @return byte array value if present * @throws JwtException in case the key is of invalid content or not base64 encoded */ public static Optional<byte[]> getByteArray(JsonObject json, String key, String description) throws JwtException { return getString(json, key) .map(base64 -> { try { return URL_DECODER.decode(base64); } catch (Exception e) { throw new JwtException("Failed to decode base64 from json for: " + description + ", value: \"" + base64 + '"', e); } }); }
/** * Create an address object from json representation. * * @param jsonObject object with expected keys */ public Address(JsonObject jsonObject) { this.formatted = getString(jsonObject, "formatted"); this.streetAddress = getString(jsonObject, "street_address"); this.locality = getString(jsonObject, "locality"); this.region = getString(jsonObject, "region"); this.postalCode = getString(jsonObject, "postal_code"); this.country = getString(jsonObject, "country"); }
static Optional<ZoneId> toTimeZone(JsonObject json, String name) { return getString(json, name) .map(ZoneId::of); }
static Optional<URI> toUri(JsonObject json, String name) { return getString(json, name) .map(URI::create); }
static Optional<Locale> toLocale(JsonObject json, String name) { return getString(json, name) .map(JwtUtil::toLocale); }
static Optional<List<String>> toScopes(JsonObject json) { return getString(json, "scope") .map(it -> Arrays.asList(it.split(" "))); }
static Optional<LocalDate> toDate(JsonObject json, String name) { return getString(json, name) .map(it -> { if (it.length() == 4) { return LocalDate.parse(it, YEAR_FORMAT); } else { return LocalDate.parse(it, DATE_FORMAT); } }); }
/** * Extract a key value from json object that is string. * * @param json JsonObject to read key from * @param key key of the value we want to read * @param description description of the field for error handling * @return String value * @throws JwtException in case the key is not present or is of invalid content */ public static String asString(JsonObject json, String key, String description) throws JwtException { return getString(json, key) .orElseThrow(() -> new JwtException("Key \"" + key + "\" is mandatory for " + description)); }