Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (C) 2021 jsonwebtoken.io
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.jsonwebtoken.impl.security;
import io.jsonwebtoken.impl.io.Streams;
import io.jsonwebtoken.impl.lang.Nameable;
import io.jsonwebtoken.impl.lang.Parameter;
import io.jsonwebtoken.impl.lang.ParameterReadable;
import io.jsonwebtoken.impl.lang.Parameters;
import io.jsonwebtoken.lang.Arrays;
import io.jsonwebtoken.lang.Assert;
import io.jsonwebtoken.lang.Collections;
import io.jsonwebtoken.lang.Objects;
import io.jsonwebtoken.lang.Strings;
import io.jsonwebtoken.lang.Supplier;
import io.jsonwebtoken.security.HashAlgorithm;
import io.jsonwebtoken.security.Jwk;
import io.jsonwebtoken.security.JwkThumbprint;
import io.jsonwebtoken.security.Jwks;
import io.jsonwebtoken.security.KeyOperation;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.security.Key;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
public abstract class AbstractJwk implements Jwk, ParameterReadable, Nameable {
static final Parameter ALG = Parameters.string("alg", "Algorithm");
public static final Parameter KID = Parameters.string("kid", "Key ID");
static final Parameter> KEY_OPS =
Parameters.builder(KeyOperation.class).setConverter(KeyOperationConverter.DEFAULT)
.set().setId("key_ops").setName("Key Operations").build();
static final Parameter KTY = Parameters.string("kty", "Key Type");
static final Set> PARAMS = Collections.setOf(ALG, KID, KEY_OPS, KTY);
public static final String IMMUTABLE_MSG = "JWKs are immutable and may not be modified.";
protected final JwkContext context;
private final List> THUMBPRINT_PARAMS;
private final int hashCode;
/**
* @param ctx the backing JwkContext containing the JWK parameter values.
* @param thumbprintParams the required parameters to include in the JWK Thumbprint canonical JSON representation,
* sorted in lexicographic order as defined by
* RFC 7638, Section 3.2.
*/
AbstractJwk(JwkContext ctx, List> thumbprintParams) {
this.context = Assert.notNull(ctx, "JwkContext cannot be null.");
Assert.isTrue(!ctx.isEmpty(), "JwkContext cannot be empty.");
Assert.hasText(ctx.getType(), "JwkContext type cannot be null or empty.");
Assert.notNull(ctx.getKey(), "JwkContext key cannot be null.");
this.THUMBPRINT_PARAMS = Assert.notEmpty(thumbprintParams, "JWK Thumbprint parameters cannot be null or empty.");
HashAlgorithm idThumbprintAlg = ctx.getIdThumbprintAlgorithm();
if (!Strings.hasText(getId()) && idThumbprintAlg != null) {
JwkThumbprint thumbprint = thumbprint(idThumbprintAlg);
String kid = thumbprint.toString();
ctx.setId(kid);
}
this.hashCode = computeHashCode();
}
/**
* Compute and return the JWK hashCode. As JWKs are immutable, this value will be cached as a final constant
* upon JWK instantiation. This uses the JWK's thumbprint parameters during computation, but differs from
* JwkThumbprint calculation in two ways:
*
*
JwkThumbprints use a MessageDigest calculation, which is unnecessary overhead for a hashcode
*
The hashCode calculation uses each parameter's idiomatic (Java) object value instead of the
* JwkThumbprint-required canonical (String) value.
*
*
* @return the JWK hashcode
*/
private int computeHashCode() {
List