All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.springframework.security.oauth2.provider.token.store.jwk.JwkDefinition Maven / Gradle / Ivy

There is a newer version: 2.5.2.RELEASE
Show newest version
/*
 * Copyright 2012-2017 the original author or authors.
 *
 * 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
 *
 *      https://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 org.springframework.security.oauth2.provider.token.store.jwk;

/**
 * The base representation of a JSON Web Key (JWK).
 *
 * @see JSON Web Key (JWK)
 *
 * @author Joe Grandja
 * @author Michael Duergner
 */
abstract class JwkDefinition {
	private final String keyId;
	private final KeyType keyType;
	private final PublicKeyUse publicKeyUse;
	private final CryptoAlgorithm algorithm;

	/**
	 * Creates an instance with the common attributes of a JWK.
	 *
	 * @param keyId the Key ID
	 * @param keyType the Key Type
	 * @param publicKeyUse the intended use of the Public Key
	 * @param algorithm the algorithm intended to be used
	 */
	protected JwkDefinition(String keyId,
							KeyType keyType,
							PublicKeyUse publicKeyUse,
							CryptoAlgorithm algorithm) {
		this.keyId = keyId;
		this.keyType = keyType;
		this.publicKeyUse = publicKeyUse;
		this.algorithm = algorithm;
	}

	/**
	 * @return the Key ID ("kid")
	 */
	String getKeyId() {
		return this.keyId;
	}

	/**
	 * @return the Key Type ("kty")
	 */
	KeyType getKeyType() {
		return this.keyType;
	}

	/**
	 * @return the intended use of the Public Key ("use")
	 */
	PublicKeyUse getPublicKeyUse() {
		return this.publicKeyUse;
	}

	/**
	 *
	 * @return the algorithm intended to be used ("alg")
	 */
	CryptoAlgorithm getAlgorithm() {
		return this.algorithm;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null || this.getClass() != obj.getClass()) {
			return false;
		}

		JwkDefinition that = (JwkDefinition) obj;
		if (!this.getKeyId().equals(that.getKeyId())) {
			return false;
		}

		return this.getKeyType().equals(that.getKeyType());
	}

	@Override
	public int hashCode() {
		int result = this.getKeyId().hashCode();
		result = 31 * result + this.getKeyType().hashCode();
		return result;
	}

	/**
	 * The defined Key Type ("kty") values.
	 */
	enum KeyType {
		RSA("RSA"),
		EC("EC"),
		OCT("oct");

		private final String value;

		KeyType(String value) {
			this.value = value;
		}

		String value() {
			return this.value;
		}

		static KeyType fromValue(String value) {
			KeyType result = null;
			for (KeyType keyType : values()) {
				if (keyType.value().equals(value)) {
					result = keyType;
					break;
				}
			}
			return result;
		}
	}

	/**
	 * The defined Public Key Use ("use") values.
	 */
	enum PublicKeyUse {
		SIG("sig"),
		ENC("enc");

		private final String value;

		PublicKeyUse(String value) {
			this.value = value;
		}

		String value() {
			return this.value;
		}

		static PublicKeyUse fromValue(String value) {
			PublicKeyUse result = null;
			for (PublicKeyUse publicKeyUse : values()) {
				if (publicKeyUse.value().equals(value)) {
					result = publicKeyUse;
					break;
				}
			}
			return result;
		}
	}

	/**
	 * The defined Algorithm ("alg") values.
	 */
	enum CryptoAlgorithm {
		RS256("SHA256withRSA", "RS256"),
		RS384("SHA384withRSA", "RS384"),
		RS512("SHA512withRSA", "RS512"),
		ES256("SHA256withECDSA", "ES256"),
		ES384("SHA384withECDSA", "ES384"),
		ES512("SHA512withECDSA", "ES512");

		private final String standardName;		// JCA Standard Name
		private final String headerParamValue;

		CryptoAlgorithm(String standardName, String headerParamValue) {
			this.standardName = standardName;
			this.headerParamValue = headerParamValue;
		}

		String standardName() {
			return this.standardName;
		}

		String headerParamValue() {
			return this.headerParamValue;
		}

		static CryptoAlgorithm fromHeaderParamValue(String headerParamValue) {
			CryptoAlgorithm result = null;
			for (CryptoAlgorithm algorithm : values()) {
				if (algorithm.headerParamValue().equals(headerParamValue)) {
					result = algorithm;
					break;
				}
			}
			return result;
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy