com.mercateo.spring.security.jwt.token.config.ImmutableJWTConfigData Maven / Gradle / Ivy
package com.mercateo.spring.security.jwt.token.config;
import com.auth0.jwt.JWTVerifier;
import com.google.common.base.MoreObjects;
import com.google.common.collect.Lists;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.mercateo.spring.security.jwt.token.keyset.JWTKeyset;
import io.vavr.collection.HashSet;
import io.vavr.collection.Set;
import io.vavr.control.Option;
import java.util.ArrayList;
import java.util.Objects;
import javax.annotation.CheckReturnValue;
import javax.annotation.Generated;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import javax.annotation.concurrent.Immutable;
import javax.annotation.concurrent.NotThreadSafe;
/**
* Immutable implementation of {@link JWTConfigData}.
*
* Use the builder to create immutable instances:
* {@code ImmutableJWTConfigData.builder()}.
*/
@SuppressWarnings({"all"})
@ParametersAreNonnullByDefault
@Generated({"Immutables.generator", "JWTConfigData"})
@Immutable
@CheckReturnValue
public final class ImmutableJWTConfigData
implements JWTConfigData {
private final int tokenLeeway;
private final Set tokenAudiences;
private final Set namespaces;
private final Set requiredClaims;
private final Set optionalClaims;
private final Option jwtKeyset;
private final Option jwtVerifier;
private ImmutableJWTConfigData(ImmutableJWTConfigData.Builder builder) {
this.tokenAudiences = builder.tokenAudiences_build();
this.namespaces = builder.namespaces_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 ImmutableJWTConfigData(
int tokenLeeway,
Set tokenAudiences,
Set namespaces,
Set requiredClaims,
Set optionalClaims,
Option jwtKeyset) {
this.tokenLeeway = tokenLeeway;
this.tokenAudiences = tokenAudiences;
this.namespaces = namespaces;
this.requiredClaims = requiredClaims;
this.optionalClaims = optionalClaims;
this.jwtKeyset = jwtKeyset;
initShim.tokenLeeway(this.tokenLeeway);
this.jwtVerifier = initShim.jwtVerifier();
this.initShim = null;
}
private static final int STAGE_INITIALIZING = -1;
private static final int STAGE_UNINITIALIZED = 0;
private static final int STAGE_INITIALIZED = 1;
private transient volatile InitShim initShim = new InitShim();
private final class InitShim {
private int tokenLeeway;
private int tokenLeewayBuildStage;
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 Option jwtVerifier;
private int jwtVerifierBuildStage;
Option jwtVerifier() {
if (jwtVerifierBuildStage == STAGE_INITIALIZING) throw new IllegalStateException(formatInitCycleMessage());
if (jwtVerifierBuildStage == STAGE_UNINITIALIZED) {
jwtVerifierBuildStage = STAGE_INITIALIZING;
this.jwtVerifier = Objects.requireNonNull(jwtVerifierInitialize(), "jwtVerifier");
jwtVerifierBuildStage = STAGE_INITIALIZED;
}
return this.jwtVerifier;
}
private String formatInitCycleMessage() {
ArrayList attributes = Lists.newArrayList();
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;
}
/**
* Namespaces where claims are searched for
*
* @return list of namespaces
*/
@Override
public Set getNamespaces() {
return namespaces;
}
/**
* @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() {
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 ImmutableJWTConfigData withTokenLeeway(int value) {
if (this.tokenLeeway == value) return this;
return new ImmutableJWTConfigData(
value,
this.tokenAudiences,
this.namespaces,
this.requiredClaims,
this.optionalClaims,
this.jwtKeyset);
}
public ImmutableJWTConfigData withTokenAudiences(Set value) {
Set newValue = tokenAudiences_from(value);
if (this.tokenAudiences == newValue) return this;
return new ImmutableJWTConfigData(
this.tokenLeeway,
newValue,
this.namespaces,
this.requiredClaims,
this.optionalClaims,
this.jwtKeyset);
}
public ImmutableJWTConfigData withNamespaces(Set value) {
Set newValue = namespaces_from(value);
if (this.namespaces == newValue) return this;
return new ImmutableJWTConfigData(
this.tokenLeeway,
this.tokenAudiences,
newValue,
this.requiredClaims,
this.optionalClaims,
this.jwtKeyset);
}
public ImmutableJWTConfigData withRequiredClaims(Set value) {
Set newValue = requiredClaims_from(value);
if (this.requiredClaims == newValue) return this;
return new ImmutableJWTConfigData(
this.tokenLeeway,
this.tokenAudiences,
this.namespaces,
newValue,
this.optionalClaims,
this.jwtKeyset);
}
public ImmutableJWTConfigData withOptionalClaims(Set value) {
Set newValue = optionalClaims_from(value);
if (this.optionalClaims == newValue) return this;
return new ImmutableJWTConfigData(
this.tokenLeeway,
this.tokenAudiences,
this.namespaces,
this.requiredClaims,
newValue,
this.jwtKeyset);
}
public ImmutableJWTConfigData withJwtKeyset(Option value) {
Option newValue = Objects.requireNonNull(value);
if (this.jwtKeyset == newValue) return this;
return new ImmutableJWTConfigData(
this.tokenLeeway,
this.tokenAudiences,
this.namespaces,
this.requiredClaims,
this.optionalClaims,
newValue);
}
public ImmutableJWTConfigData withJwtKeyset(JWTKeyset value) {
Option newValue = Option.some(value);
if (this.jwtKeyset == newValue) return this;
return new ImmutableJWTConfigData(
this.tokenLeeway,
this.tokenAudiences,
this.namespaces,
this.requiredClaims,
this.optionalClaims,
newValue);
}
/**
* This instance is equal to all instances of {@code ImmutableJWTConfigData} that have equal attribute values.
* @return {@code true} if {@code this} is equal to {@code another} instance
*/
@Override
public boolean equals(@Nullable Object another) {
if (this == another) return true;
return another instanceof ImmutableJWTConfigData
&& equalTo((ImmutableJWTConfigData) another);
}
private boolean equalTo(ImmutableJWTConfigData another) {
return tokenLeeway == another.tokenLeeway
&& this.getTokenAudiences().equals(another.getTokenAudiences())
&& this.getNamespaces().equals(another.getNamespaces())
&& this.getRequiredClaims().equals(another.getRequiredClaims())
&& this.getOptionalClaims().equals(another.getOptionalClaims())
&& this.jwtKeyset().equals(another.jwtKeyset())
&& jwtVerifier.equals(another.jwtVerifier);
}
/**
* Computes a hash code from attributes: {@code tokenLeeway}, {@code tokenAudiences}, {@code namespaces}, {@code requiredClaims}, {@code optionalClaims}, {@code jwtKeyset}, {@code jwtVerifier}.
* @return hashCode value
*/
@Override
public int hashCode() {
int h = 5381;
h += (h << 5) + tokenLeeway;
h += (h << 5) + (getTokenAudiences().hashCode());
h += (h << 5) + (getNamespaces().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("namespaces", getNamespaces().toString())
.add("requiredClaims", getRequiredClaims().toString())
.add("optionalClaims", getOptionalClaims().toString())
.add("jwtKeyset", jwtKeyset().toString())
.add("jwtVerifier", jwtVerifier)
.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 ImmutableJWTConfigData copyOf(JWTConfigData instance) {
if (instance instanceof ImmutableJWTConfigData) {
return (ImmutableJWTConfigData) instance;
}
return ImmutableJWTConfigData.builder()
.from(instance)
.build();
}
private static Set tokenAudiences_from(Set value) {
return value;
}
private static Set namespaces_from(Set value) {
return value;
}
private static Set requiredClaims_from(Set value) {
return value;
}
private static Set optionalClaims_from(Set value) {
return value;
}
private static Option jwtKeyset_from(Option value) {
return value;
}
/**
* Creates a builder for {@link ImmutableJWTConfigData ImmutableJWTConfigData}.
* @return A new ImmutableJWTConfigData builder
*/
public static ImmutableJWTConfigData.Builder builder() {
return new ImmutableJWTConfigData.Builder();
}
/**
* Builds instances of type {@link ImmutableJWTConfigData ImmutableJWTConfigData}.
* 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.
*/
@NotThreadSafe
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 namespaces_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(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());
namespaces(instance.getNamespaces());
}
}
/**
* 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;
}
public Builder addTokenAudiences(String element) {
this.tokenAudiences_set = this.tokenAudiences_set.add(element);
return this;
}
public Builder addAllTokenAudiences(Iterable element) {
this.tokenAudiences_set = this.tokenAudiences_set.addAll(element);
return this;
}
public Builder tokenAudiences(Set elements) {
this.tokenAudiences_set = elements;
return this;
}
public Builder setIterableTokenAudiences(Iterable elements) {
this.tokenAudiences_set = HashSet.ofAll(elements);
return this;
}
public Builder addNamespaces(String element) {
this.namespaces_set = this.namespaces_set.add(element);
return this;
}
public Builder addAllNamespaces(Iterable element) {
this.namespaces_set = this.namespaces_set.addAll(element);
return this;
}
public Builder namespaces(Set elements) {
this.namespaces_set = elements;
return this;
}
public Builder setIterableNamespaces(Iterable elements) {
this.namespaces_set = HashSet.ofAll(elements);
return this;
}
public Builder addRequiredClaims(String element) {
this.requiredClaims_set = this.requiredClaims_set.add(element);
return this;
}
public Builder addAllRequiredClaims(Iterable element) {
this.requiredClaims_set = this.requiredClaims_set.addAll(element);
return this;
}
public Builder requiredClaims(Set elements) {
this.requiredClaims_set = elements;
return this;
}
public Builder setIterableRequiredClaims(Iterable elements) {
this.requiredClaims_set = HashSet.ofAll(elements);
return this;
}
public Builder addOptionalClaims(String element) {
this.optionalClaims_set = this.optionalClaims_set.add(element);
return this;
}
public Builder addAllOptionalClaims(Iterable element) {
this.optionalClaims_set = this.optionalClaims_set.addAll(element);
return this;
}
public Builder optionalClaims(Set elements) {
this.optionalClaims_set = elements;
return this;
}
public Builder setIterableOptionalClaims(Iterable elements) {
this.optionalClaims_set = HashSet.ofAll(elements);
return this;
}
public Builder jwtKeyset(Option opt) {
this.jwtKeyset_optional = opt;
return this;
}
public Builder setValueJwtKeyset(JWTKeyset x) {
this.jwtKeyset_optional = Option.of(x);
return this;
}
public Builder unsetJwtKeyset() {
this.jwtKeyset_optional = Option.none();
return this;
}
/**
* Builds a new {@link ImmutableJWTConfigData ImmutableJWTConfigData}.
* @return An immutable instance of JWTConfigData
* @throws java.lang.IllegalStateException if any required attributes are missing
*/
public ImmutableJWTConfigData build() {
return new ImmutableJWTConfigData(this);
}
private boolean tokenLeewayIsSet() {
return (optBits & OPT_BIT_TOKEN_LEEWAY) != 0;
}
private Set tokenAudiences_build() {
return this.tokenAudiences_set;
}
private Set namespaces_build() {
return this.namespaces_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;
}
}
}