
com.mercateo.spring.security.jwt.token.config.JWTConfigData Maven / Gradle / Ivy
Show all versions of spring-security-jwt Show documentation
package com.mercateo.spring.security.jwt.token.config;
import com.google.common.base.MoreObjects;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.errorprone.annotations.Var;
import com.mercateo.spring.security.jwt.token.keyset.JWTKeyset;
import com.mercateo.spring.security.jwt.token.verifier.JWTVerifier;
import io.vavr.collection.HashSet;
import io.vavr.collection.Set;
import io.vavr.control.Option;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import org.immutables.value.Generated;
/**
* Immutable implementation of {@link _JWTConfigData}.
*
* Use the builder to create immutable instances:
* {@code JWTConfigData.builder()}.
*/
@Generated(from = "_JWTConfigData", generator = "Immutables")
@SuppressWarnings({"all"})
public final class JWTConfigData implements com.mercateo.spring.security.jwt.token.config._JWTConfigData {
private final int tokenLeeway;
private final Set tokenAudiences;
private final Set requiredClaims;
private final Set optionalClaims;
private final Option jwtKeyset;
private final Option jwtVerifier;
private JWTConfigData(JWTConfigData.Builder builder) {
this.tokenAudiences = builder.tokenAudiences_build();
this.requiredClaims = builder.requiredClaims_build();
this.optionalClaims = builder.optionalClaims_build();
this.jwtKeyset = builder.jwtKeyset_build();
if (builder.tokenLeewayIsSet()) {
initShim.tokenLeeway(builder.tokenLeeway);
}
this.tokenLeeway = initShim.getTokenLeeway();
this.jwtVerifier = initShim.jwtVerifier();
this.initShim = null;
}
private JWTConfigData(
int tokenLeeway,
Set tokenAudiences,
Set requiredClaims,
Set optionalClaims,
Option jwtKeyset) {
initShim.tokenLeeway(tokenLeeway);
this.tokenAudiences = tokenAudiences;
this.requiredClaims = requiredClaims;
this.optionalClaims = optionalClaims;
this.jwtKeyset = jwtKeyset;
this.tokenLeeway = initShim.getTokenLeeway();
this.jwtVerifier = initShim.jwtVerifier();
this.initShim = null;
}
private static final byte STAGE_INITIALIZING = -1;
private static final byte STAGE_UNINITIALIZED = 0;
private static final byte STAGE_INITIALIZED = 1;
@SuppressWarnings("Immutable")
private transient volatile InitShim initShim = new InitShim();
@Generated(from = "_JWTConfigData", generator = "Immutables")
@CanIgnoreReturnValue
private final class InitShim {
private byte tokenLeewayBuildStage = STAGE_UNINITIALIZED;
private int tokenLeeway;
int getTokenLeeway() {
if (tokenLeewayBuildStage == STAGE_INITIALIZING) throw new IllegalStateException(formatInitCycleMessage());
if (tokenLeewayBuildStage == STAGE_UNINITIALIZED) {
tokenLeewayBuildStage = STAGE_INITIALIZING;
this.tokenLeeway = getTokenLeewayInitialize();
tokenLeewayBuildStage = STAGE_INITIALIZED;
}
return this.tokenLeeway;
}
void tokenLeeway(int tokenLeeway) {
this.tokenLeeway = tokenLeeway;
tokenLeewayBuildStage = STAGE_INITIALIZED;
}
private byte jwtVerifierBuildStage = STAGE_UNINITIALIZED;
private Option jwtVerifier;
Option jwtVerifier() {
if (jwtVerifierBuildStage == STAGE_INITIALIZING) throw new IllegalStateException(formatInitCycleMessage());
if (jwtVerifierBuildStage == STAGE_UNINITIALIZED) {
jwtVerifierBuildStage = STAGE_INITIALIZING;
this.jwtVerifier = jwtVerifierInitialize();
jwtVerifierBuildStage = STAGE_INITIALIZED;
}
return this.jwtVerifier;
}
private String formatInitCycleMessage() {
List attributes = new ArrayList<>();
if (tokenLeewayBuildStage == STAGE_INITIALIZING) attributes.add("tokenLeeway");
if (jwtVerifierBuildStage == STAGE_INITIALIZING) attributes.add("jwtVerifier");
return "Cannot build JWTConfigData, attribute initializers form cycle " + attributes;
}
}
private int getTokenLeewayInitialize() {
return _JWTConfigData.super.getTokenLeeway();
}
private Option jwtVerifierInitialize() {
return _JWTConfigData.super.jwtVerifier();
}
/**
* @return The default window in seconds in which the Not Before, Issued At and
* Expires At Claims will still be valid.
* Setting a specific leeway value on a given Claim will override this
* value for that Claim.
*/
@Override
public int getTokenLeeway() {
InitShim shim = this.initShim;
return shim != null
? shim.getTokenLeeway()
: this.tokenLeeway;
}
/**
* @return required Audience ("aud") claims
*/
@Override
public Set getTokenAudiences() {
return tokenAudiences;
}
/**
* @return set of required claims
*/
@Override
public Set getRequiredClaims() {
return requiredClaims;
}
/**
* @return set of claims which should be processed if they exist
*/
@Override
public Set getOptionalClaims() {
return optionalClaims;
}
/**
* @return {@link JWTKeyset} to be used for token verification
*/
@Override
public Option jwtKeyset() {
return jwtKeyset;
}
/**
* @return {@link JWTVerifier} for given {@link JWTKeyset} to be used for token
* verification
*/
@Override
public Option jwtVerifier() {
return jwtVerifier$shim();
}
private Option jwtVerifier$shim() {
InitShim shim = this.initShim;
return shim != null ? shim.jwtVerifier() : this.jwtVerifier;
}
/**
* Copy the current immutable object by setting a value for the {@link _JWTConfigData#getTokenLeeway() tokenLeeway} attribute.
* A value equality check is used to prevent copying of the same value by returning {@code this}.
* @param value A new value for tokenLeeway
* @return A modified copy of the {@code this} object
*/
public final JWTConfigData withTokenLeeway(int value) {
if (this.tokenLeeway == value) return this;
return new JWTConfigData(value, this.tokenAudiences, this.requiredClaims, this.optionalClaims, this.jwtKeyset);
}
public JWTConfigData withTokenAudiences(Set value) {
Set newValue = value;
if (this.tokenAudiences == newValue) return this;
return new JWTConfigData(this.tokenLeeway, newValue, this.requiredClaims, this.optionalClaims, this.jwtKeyset);
}
public JWTConfigData withRequiredClaims(Set value) {
Set newValue = value;
if (this.requiredClaims == newValue) return this;
return new JWTConfigData(this.tokenLeeway, this.tokenAudiences, newValue, this.optionalClaims, this.jwtKeyset);
}
public JWTConfigData withOptionalClaims(Set value) {
Set newValue = value;
if (this.optionalClaims == newValue) return this;
return new JWTConfigData(this.tokenLeeway, this.tokenAudiences, this.requiredClaims, newValue, this.jwtKeyset);
}
public JWTConfigData withJwtKeyset(Option value) {
Option newValue = Objects.requireNonNull(value);
if (this.jwtKeyset == newValue) return this;
return new JWTConfigData(this.tokenLeeway, this.tokenAudiences, this.requiredClaims, this.optionalClaims, newValue);
}
public JWTConfigData withJwtKeyset(JWTKeyset value) {
Option newValue = Option.some(value);
if (this.jwtKeyset == newValue) return this;
return new JWTConfigData(this.tokenLeeway, this.tokenAudiences, this.requiredClaims, this.optionalClaims, newValue);
}
/**
* This instance is equal to all instances of {@code JWTConfigData} that have equal attribute values.
* @return {@code true} if {@code this} is equal to {@code another} instance
*/
@Override
public boolean equals(Object another) {
if (this == another) return true;
return another instanceof JWTConfigData
&& equalTo((JWTConfigData) another);
}
private boolean equalTo(JWTConfigData another) {
return tokenLeeway == another.tokenLeeway
&& this.getTokenAudiences().equals(another.getTokenAudiences())
&& this.getRequiredClaims().equals(another.getRequiredClaims())
&& this.getOptionalClaims().equals(another.getOptionalClaims())
&& this.jwtKeyset().equals(another.jwtKeyset())
&& this.jwtVerifier().equals(another.jwtVerifier());
}
/**
* Computes a hash code from attributes: {@code tokenLeeway}, {@code tokenAudiences}, {@code requiredClaims}, {@code optionalClaims}, {@code jwtKeyset}, {@code jwtVerifier}.
* @return hashCode value
*/
@Override
public int hashCode() {
@Var int h = 5381;
h += (h << 5) + tokenLeeway;
h += (h << 5) + (getTokenAudiences().hashCode());
h += (h << 5) + (getRequiredClaims().hashCode());
h += (h << 5) + (getOptionalClaims().hashCode());
h += (h << 5) + (jwtKeyset().hashCode());
h += (h << 5) + (jwtVerifier().hashCode());
return h;
}
/**
* Prints the immutable value {@code JWTConfigData} with attribute values.
* @return A string representation of the value
*/
@Override
public String toString() {
return MoreObjects.toStringHelper("JWTConfigData")
.omitNullValues()
.add("tokenLeeway", tokenLeeway)
.add("tokenAudiences", getTokenAudiences().toString())
.add("requiredClaims", getRequiredClaims().toString())
.add("optionalClaims", getOptionalClaims().toString())
.add("jwtKeyset", jwtKeyset().toString())
.add("jwtVerifier", jwtVerifier().toString())
.toString();
}
/**
* Creates an immutable copy of a {@link _JWTConfigData} value.
* Uses accessors to get values to initialize the new immutable instance.
* If an instance is already immutable, it is returned as is.
* @param instance The instance to copy
* @return A copied immutable JWTConfigData instance
*/
public static JWTConfigData copyOf(_JWTConfigData instance) {
if (instance instanceof JWTConfigData) {
return (JWTConfigData) instance;
}
return JWTConfigData.builder()
.from(instance)
.build();
}
/**
* Creates a builder for {@link JWTConfigData JWTConfigData}.
*
* JWTConfigData.builder()
* .tokenLeeway(int) // optional {@link _JWTConfigData#getTokenLeeway() tokenLeeway}
* .tokenAudiences(io.vavr.collection.Set<String>) // {@link _JWTConfigData#getTokenAudiences() tokenAudiences}
* .requiredClaims(io.vavr.collection.Set<String>) // {@link _JWTConfigData#getRequiredClaims() requiredClaims}
* .optionalClaims(io.vavr.collection.Set<String>) // {@link _JWTConfigData#getOptionalClaims() optionalClaims}
* .jwtKeyset(io.vavr.control.Option<com.mercateo.spring.security.jwt.token.keyset.JWTKeyset>) // {@link _JWTConfigData#jwtKeyset() jwtKeyset}
* .build();
*
* @return A new JWTConfigData builder
*/
public static JWTConfigData.Builder builder() {
return new JWTConfigData.Builder();
}
/**
* Builds instances of type {@link JWTConfigData JWTConfigData}.
* Initialize attributes and then invoke the {@link #build()} method to create an
* immutable instance.
* {@code Builder} is not thread-safe and generally should not be stored in a field or collection,
* but instead used immediately to create instances.
*/
@Generated(from = "_JWTConfigData", generator = "Immutables")
public static final class Builder {
private static final long OPT_BIT_TOKEN_LEEWAY = 0x1L;
private long optBits;
private Set tokenAudiences_set = HashSet.empty();
private Set requiredClaims_set = HashSet.empty();
private Set optionalClaims_set = HashSet.empty();
private Option jwtKeyset_optional = Option.none();
private int tokenLeeway;
private Builder() {
}
/**
* Fill a builder with attribute values from the provided {@code com.mercateo.spring.security.jwt.token.config.JWTConfig} instance.
* @param instance The instance from which to copy values
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder from(JWTConfig instance) {
Objects.requireNonNull(instance, "instance");
from((Object) instance);
return this;
}
/**
* Fill a builder with attribute values from the provided {@code com.mercateo.spring.security.jwt.token.config._JWTConfigData} instance.
* @param instance The instance from which to copy values
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder from(com.mercateo.spring.security.jwt.token.config._JWTConfigData instance) {
Objects.requireNonNull(instance, "instance");
from((Object) instance);
return this;
}
private void from(Object object) {
if (object instanceof JWTConfig) {
JWTConfig instance = (JWTConfig) object;
jwtKeyset(instance.jwtKeyset());
tokenLeeway(instance.getTokenLeeway());
tokenAudiences(instance.getTokenAudiences());
requiredClaims(instance.getRequiredClaims());
optionalClaims(instance.getOptionalClaims());
}
}
/**
* Initializes the value for the {@link _JWTConfigData#getTokenLeeway() tokenLeeway} attribute.
* If not set, this attribute will have a default value as returned by the initializer of {@link _JWTConfigData#getTokenLeeway() tokenLeeway}.
* @param tokenLeeway The value for tokenLeeway
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder tokenLeeway(int tokenLeeway) {
this.tokenLeeway = tokenLeeway;
optBits |= OPT_BIT_TOKEN_LEEWAY;
return this;
}
@CanIgnoreReturnValue
public Builder addTokenAudiences(String element) {
this.tokenAudiences_set = this.tokenAudiences_set.add(element);
return this;
}
@SafeVarargs
@CanIgnoreReturnValue
public final Builder addTokenAudiences(String... elements) {
this.tokenAudiences_set = this.tokenAudiences_set.addAll(HashSet.of(elements));
return this;
}
@CanIgnoreReturnValue
public Builder addAllTokenAudiences(Iterable element) {
this.tokenAudiences_set = this.tokenAudiences_set.addAll(element);
return this;
}
@CanIgnoreReturnValue
public Builder tokenAudiences(Set elements) {
this.tokenAudiences_set = elements;
return this;
}
@CanIgnoreReturnValue
public Builder setIterableTokenAudiences(Iterable elements) {
this.tokenAudiences_set = HashSet.ofAll(elements);
return this;
}
@CanIgnoreReturnValue
public Builder addRequiredClaims(String element) {
this.requiredClaims_set = this.requiredClaims_set.add(element);
return this;
}
@SafeVarargs
@CanIgnoreReturnValue
public final Builder addRequiredClaims(String... elements) {
this.requiredClaims_set = this.requiredClaims_set.addAll(HashSet.of(elements));
return this;
}
@CanIgnoreReturnValue
public Builder addAllRequiredClaims(Iterable element) {
this.requiredClaims_set = this.requiredClaims_set.addAll(element);
return this;
}
@CanIgnoreReturnValue
public Builder requiredClaims(Set elements) {
this.requiredClaims_set = elements;
return this;
}
@CanIgnoreReturnValue
public Builder setIterableRequiredClaims(Iterable elements) {
this.requiredClaims_set = HashSet.ofAll(elements);
return this;
}
@CanIgnoreReturnValue
public Builder addOptionalClaims(String element) {
this.optionalClaims_set = this.optionalClaims_set.add(element);
return this;
}
@SafeVarargs
@CanIgnoreReturnValue
public final Builder addOptionalClaims(String... elements) {
this.optionalClaims_set = this.optionalClaims_set.addAll(HashSet.of(elements));
return this;
}
@CanIgnoreReturnValue
public Builder addAllOptionalClaims(Iterable element) {
this.optionalClaims_set = this.optionalClaims_set.addAll(element);
return this;
}
@CanIgnoreReturnValue
public Builder optionalClaims(Set elements) {
this.optionalClaims_set = elements;
return this;
}
@CanIgnoreReturnValue
public Builder setIterableOptionalClaims(Iterable elements) {
this.optionalClaims_set = HashSet.ofAll(elements);
return this;
}
@CanIgnoreReturnValue
public Builder jwtKeyset(Option opt) {
this.jwtKeyset_optional = opt;
return this;
}
@CanIgnoreReturnValue
public Builder jwtKeyset(JWTKeyset x) {
this.jwtKeyset_optional = Option.of(x);
return this;
}
@CanIgnoreReturnValue
public Builder unsetJwtKeyset() {
this.jwtKeyset_optional = Option.none();
return this;
}
/**
* Builds a new {@link JWTConfigData JWTConfigData}.
* @return An immutable instance of JWTConfigData
* @throws java.lang.IllegalStateException if any required attributes are missing
*/
public JWTConfigData build() {
return new JWTConfigData(this);
}
private boolean tokenLeewayIsSet() {
return (optBits & OPT_BIT_TOKEN_LEEWAY) != 0;
}
private Set tokenAudiences_build() {
return this.tokenAudiences_set;
}
private Set requiredClaims_build() {
return this.requiredClaims_set;
}
private Set optionalClaims_build() {
return this.optionalClaims_set;
}
private Option jwtKeyset_build() {
return this.jwtKeyset_optional;
}
}
}