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

org.interledger.spsp.server.auth.ImmutableSimpleCredentials Maven / Gradle / Ivy

Go to download

An Interledger SPSP server with a stateless STREAM receiver that publishes events.

The newest version!
package org.interledger.spsp.server.auth;

import com.google.common.base.MoreObjects;
import com.google.common.primitives.Booleans;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.errorprone.annotations.Var;
import java.security.Principal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import javax.annotation.CheckReturnValue;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import javax.annotation.concurrent.Immutable;
import javax.annotation.concurrent.NotThreadSafe;
import org.immutables.value.Generated;
import org.interledger.connector.accounts.AccountId;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;

/**
 * Immutable implementation of {@link SimpleCredentials}.
 * 

* Use the builder to create immutable instances: * {@code ImmutableSimpleCredentials.builder()}. */ @Generated(from = "SimpleCredentials", generator = "Immutables") @SuppressWarnings({"all"}) @ParametersAreNonnullByDefault @javax.annotation.Generated("org.immutables.processor.ProxyProcessor") @Deprecated @Immutable @CheckReturnValue public final class ImmutableSimpleCredentials implements SimpleCredentials { private final AccountId principal; private final boolean isAuthenticated; private final byte[] authToken; private final Collection authorities; private final Object credentials; private final @Nullable Object details; private final String name; private ImmutableSimpleCredentials(ImmutableSimpleCredentials.Builder builder) { this.principal = builder.principal; this.authToken = builder.authToken; if (builder.isAuthenticatedIsSet()) { initShim.isAuthenticated(builder.isAuthenticated); } if (builder.name != null) { initShim.name(builder.name); } this.isAuthenticated = initShim.isAuthenticated(); this.authorities = initShim.getAuthorities(); this.credentials = initShim.getCredentials(); this.details = initShim.getDetails(); this.name = initShim.getName(); this.initShim = null; } private ImmutableSimpleCredentials( AccountId principal, boolean isAuthenticated, byte[] authToken, String name) { this.principal = principal; initShim.isAuthenticated(isAuthenticated); this.authToken = authToken; initShim.name(name); this.isAuthenticated = initShim.isAuthenticated(); this.authorities = initShim.getAuthorities(); this.credentials = initShim.getCredentials(); this.details = initShim.getDetails(); this.name = initShim.getName(); 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 = "SimpleCredentials", generator = "Immutables") private final class InitShim { private byte isAuthenticatedBuildStage = STAGE_UNINITIALIZED; private boolean isAuthenticated; boolean isAuthenticated() { if (isAuthenticatedBuildStage == STAGE_INITIALIZING) throw new IllegalStateException(formatInitCycleMessage()); if (isAuthenticatedBuildStage == STAGE_UNINITIALIZED) { isAuthenticatedBuildStage = STAGE_INITIALIZING; this.isAuthenticated = isAuthenticatedInitialize(); isAuthenticatedBuildStage = STAGE_INITIALIZED; } return this.isAuthenticated; } void isAuthenticated(boolean isAuthenticated) { this.isAuthenticated = isAuthenticated; isAuthenticatedBuildStage = STAGE_INITIALIZED; } private byte authoritiesBuildStage = STAGE_UNINITIALIZED; private Collection authorities; Collection getAuthorities() { if (authoritiesBuildStage == STAGE_INITIALIZING) throw new IllegalStateException(formatInitCycleMessage()); if (authoritiesBuildStage == STAGE_UNINITIALIZED) { authoritiesBuildStage = STAGE_INITIALIZING; this.authorities = Objects.requireNonNull(getAuthoritiesInitialize(), "authorities"); authoritiesBuildStage = STAGE_INITIALIZED; } return this.authorities; } private byte credentialsBuildStage = STAGE_UNINITIALIZED; private Object credentials; Object getCredentials() { if (credentialsBuildStage == STAGE_INITIALIZING) throw new IllegalStateException(formatInitCycleMessage()); if (credentialsBuildStage == STAGE_UNINITIALIZED) { credentialsBuildStage = STAGE_INITIALIZING; this.credentials = Objects.requireNonNull(getCredentialsInitialize(), "credentials"); credentialsBuildStage = STAGE_INITIALIZED; } return this.credentials; } private byte detailsBuildStage = STAGE_UNINITIALIZED; private Object details; Object getDetails() { if (detailsBuildStage == STAGE_INITIALIZING) throw new IllegalStateException(formatInitCycleMessage()); if (detailsBuildStage == STAGE_UNINITIALIZED) { detailsBuildStage = STAGE_INITIALIZING; this.details = getDetailsInitialize(); detailsBuildStage = STAGE_INITIALIZED; } return this.details; } private byte nameBuildStage = STAGE_UNINITIALIZED; private String name; String getName() { if (nameBuildStage == STAGE_INITIALIZING) throw new IllegalStateException(formatInitCycleMessage()); if (nameBuildStage == STAGE_UNINITIALIZED) { nameBuildStage = STAGE_INITIALIZING; this.name = Objects.requireNonNull(getNameInitialize(), "name"); nameBuildStage = STAGE_INITIALIZED; } return this.name; } void name(String name) { this.name = name; nameBuildStage = STAGE_INITIALIZED; } private String formatInitCycleMessage() { List attributes = new ArrayList<>(); if (isAuthenticatedBuildStage == STAGE_INITIALIZING) attributes.add("isAuthenticated"); if (authoritiesBuildStage == STAGE_INITIALIZING) attributes.add("authorities"); if (credentialsBuildStage == STAGE_INITIALIZING) attributes.add("credentials"); if (detailsBuildStage == STAGE_INITIALIZING) attributes.add("details"); if (nameBuildStage == STAGE_INITIALIZING) attributes.add("name"); return "Cannot build SimpleCredentials, attribute initializers form cycle " + attributes; } } private boolean isAuthenticatedInitialize() { return SimpleCredentials.super.isAuthenticated(); } private Collection getAuthoritiesInitialize() { return SimpleCredentials.super.getAuthorities(); } private Object getCredentialsInitialize() { return SimpleCredentials.super.getCredentials(); } private @Nullable Object getDetailsInitialize() { return SimpleCredentials.super.getDetails(); } private String getNameInitialize() { return SimpleCredentials.super.getName(); } /** * @return The value of the {@code principal} attribute */ @Override public AccountId getPrincipal() { return principal; } /** * @return The value of the {@code isAuthenticated} attribute */ @Override public boolean isAuthenticated() { InitShim shim = this.initShim; return shim != null ? shim.isAuthenticated() : this.isAuthenticated; } /** * @return A cloned {@code authToken} array */ @Override public byte[] getAuthToken() { return authToken.clone(); } /** * @return The computed-at-construction value of the {@code authorities} attribute */ @Override public Collection getAuthorities() { InitShim shim = this.initShim; return shim != null ? shim.getAuthorities() : this.authorities; } /** * @return The computed-at-construction value of the {@code credentials} attribute */ @Override public Object getCredentials() { InitShim shim = this.initShim; return shim != null ? shim.getCredentials() : this.credentials; } /** * Stores additional details about the authentication request. These might be an IP address, certificate serial number * etc. * @return additional details about the authentication request, or null if not used */ @Override public @Nullable Object getDetails() { InitShim shim = this.initShim; return shim != null ? shim.getDetails() : this.details; } /** * Returns the name of this principal. * @return the name of this principal. */ @Override public String getName() { InitShim shim = this.initShim; return shim != null ? shim.getName() : this.name; } /** * Copy the current immutable object by setting a value for the {@link SimpleCredentials#getPrincipal() principal} attribute. * A shallow reference equality check is used to prevent copying of the same value by returning {@code this}. * @param value A new value for principal * @return A modified copy of the {@code this} object */ public final ImmutableSimpleCredentials withPrincipal(AccountId value) { if (this.principal == value) return this; AccountId newValue = Objects.requireNonNull(value, "principal"); return new ImmutableSimpleCredentials(newValue, this.isAuthenticated, this.authToken, this.name); } /** * Copy the current immutable object by setting a value for the {@link SimpleCredentials#isAuthenticated() isAuthenticated} attribute. * A value equality check is used to prevent copying of the same value by returning {@code this}. * @param value A new value for isAuthenticated * @return A modified copy of the {@code this} object */ public final ImmutableSimpleCredentials withIsAuthenticated(boolean value) { if (this.isAuthenticated == value) return this; return new ImmutableSimpleCredentials(this.principal, value, this.authToken, this.name); } /** * Copy the current immutable object with elements that replace the content of {@link SimpleCredentials#getAuthToken() authToken}. * The array is cloned before being saved as attribute values. * @param elements The non-null elements for authToken * @return A modified copy of {@code this} object */ public final ImmutableSimpleCredentials withAuthToken(byte... elements) { byte[] newValue = elements.clone(); return new ImmutableSimpleCredentials(this.principal, this.isAuthenticated, newValue, this.name); } /** * Copy the current immutable object by setting a value for the {@link SimpleCredentials#getName() name} attribute. * An equals check used to prevent copying of the same value by returning {@code this}. * @param value A new value for name * @return A modified copy of the {@code this} object */ public final ImmutableSimpleCredentials withName(String value) { String newValue = Objects.requireNonNull(value, "name"); if (this.name.equals(newValue)) return this; return new ImmutableSimpleCredentials(this.principal, this.isAuthenticated, this.authToken, newValue); } /** * This instance is equal to all instances of {@code ImmutableSimpleCredentials} 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 ImmutableSimpleCredentials && equalTo((ImmutableSimpleCredentials) another); } private boolean equalTo(ImmutableSimpleCredentials another) { return principal.equals(another.principal) && isAuthenticated == another.isAuthenticated && Arrays.equals(authToken, another.authToken) && authorities.equals(another.authorities) && credentials.equals(another.credentials) && Objects.equals(details, another.details) && name.equals(another.name); } /** * Computes a hash code from attributes: {@code principal}, {@code isAuthenticated}, {@code authToken}, {@code authorities}, {@code credentials}, {@code details}, {@code name}. * @return hashCode value */ @Override public int hashCode() { @Var int h = 5381; h += (h << 5) + principal.hashCode(); h += (h << 5) + Booleans.hashCode(isAuthenticated); h += (h << 5) + Arrays.hashCode(authToken); h += (h << 5) + authorities.hashCode(); h += (h << 5) + credentials.hashCode(); h += (h << 5) + Objects.hashCode(details); h += (h << 5) + name.hashCode(); return h; } /** * Prints the immutable value {@code SimpleCredentials} with attribute values. * @return A string representation of the value */ @Override public String toString() { return MoreObjects.toStringHelper("SimpleCredentials") .omitNullValues() .add("principal", principal) .add("isAuthenticated", isAuthenticated) .add("authorities", authorities) .add("credentials", credentials) .add("details", details) .add("name", name) .toString(); } /** * Creates an immutable copy of a {@link SimpleCredentials} 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 SimpleCredentials instance */ public static ImmutableSimpleCredentials copyOf(SimpleCredentials instance) { if (instance instanceof ImmutableSimpleCredentials) { return (ImmutableSimpleCredentials) instance; } return ImmutableSimpleCredentials.builder() .from(instance) .build(); } /** * Creates a builder for {@link ImmutableSimpleCredentials ImmutableSimpleCredentials}. *

   * ImmutableSimpleCredentials.builder()
   *    .principal(org.interledger.connector.accounts.AccountId) // required {@link SimpleCredentials#getPrincipal() principal}
   *    .isAuthenticated(boolean) // optional {@link SimpleCredentials#isAuthenticated() isAuthenticated}
   *    .authToken(byte) // required {@link SimpleCredentials#getAuthToken() authToken}
   *    .name(String) // optional {@link SimpleCredentials#getName() name}
   *    .build();
   * 
* @return A new ImmutableSimpleCredentials builder */ public static ImmutableSimpleCredentials.Builder builder() { return new ImmutableSimpleCredentials.Builder(); } /** * Builds instances of type {@link ImmutableSimpleCredentials ImmutableSimpleCredentials}. * 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 = "SimpleCredentials", generator = "Immutables") @NotThreadSafe public static final class Builder { private static final long INIT_BIT_PRINCIPAL = 0x1L; private static final long INIT_BIT_AUTH_TOKEN = 0x2L; private static final long OPT_BIT_IS_AUTHENTICATED = 0x1L; private long initBits = 0x3L; private long optBits; private @Nullable AccountId principal; private boolean isAuthenticated; private @Nullable byte[] authToken; private @Nullable String name; private Builder() { } /** * Fill a builder with attribute values from the provided {@code org.interledger.spsp.server.auth.SimpleCredentials} 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(SimpleCredentials instance) { Objects.requireNonNull(instance, "instance"); from((Object) instance); return this; } /** * Fill a builder with attribute values from the provided {@code java.security.Principal} 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(Principal instance) { Objects.requireNonNull(instance, "instance"); from((Object) instance); return this; } /** * Fill a builder with attribute values from the provided {@code org.springframework.security.core.Authentication} 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(Authentication instance) { Objects.requireNonNull(instance, "instance"); from((Object) instance); return this; } private void from(Object object) { @Var long bits = 0; if (object instanceof SimpleCredentials) { SimpleCredentials instance = (SimpleCredentials) object; principal(instance.getPrincipal()); if ((bits & 0x1L) == 0) { name(instance.getName()); bits |= 0x1L; } if ((bits & 0x2L) == 0) { isAuthenticated(instance.isAuthenticated()); bits |= 0x2L; } authToken(instance.getAuthToken()); } if (object instanceof Principal) { Principal instance = (Principal) object; if ((bits & 0x1L) == 0) { name(instance.getName()); bits |= 0x1L; } } if (object instanceof Authentication) { Authentication instance = (Authentication) object; if ((bits & 0x2L) == 0) { isAuthenticated(instance.isAuthenticated()); bits |= 0x2L; } } } /** * Initializes the value for the {@link SimpleCredentials#getPrincipal() principal} attribute. * @param principal The value for principal * @return {@code this} builder for use in a chained invocation */ @CanIgnoreReturnValue public final Builder principal(AccountId principal) { this.principal = Objects.requireNonNull(principal, "principal"); initBits &= ~INIT_BIT_PRINCIPAL; return this; } /** * Initializes the value for the {@link SimpleCredentials#isAuthenticated() isAuthenticated} attribute. *

If not set, this attribute will have a default value as returned by the initializer of {@link SimpleCredentials#isAuthenticated() isAuthenticated}. * @param isAuthenticated The value for isAuthenticated * @return {@code this} builder for use in a chained invocation */ @CanIgnoreReturnValue public final Builder isAuthenticated(boolean isAuthenticated) { this.isAuthenticated = isAuthenticated; optBits |= OPT_BIT_IS_AUTHENTICATED; return this; } /** * Initializes the value for the {@link SimpleCredentials#getAuthToken() authToken} attribute. * @param authToken The elements for authToken * @return {@code this} builder for use in a chained invocation */ @CanIgnoreReturnValue public final Builder authToken(byte... authToken) { this.authToken = authToken.clone(); initBits &= ~INIT_BIT_AUTH_TOKEN; return this; } /** * Initializes the value for the {@link SimpleCredentials#getName() name} attribute. *

If not set, this attribute will have a default value as returned by the initializer of {@link SimpleCredentials#getName() name}. * @param name The value for name * @return {@code this} builder for use in a chained invocation */ @CanIgnoreReturnValue public final Builder name(String name) { this.name = Objects.requireNonNull(name, "name"); return this; } /** * Builds a new {@link ImmutableSimpleCredentials ImmutableSimpleCredentials}. * @return An immutable instance of SimpleCredentials * @throws java.lang.IllegalStateException if any required attributes are missing */ public ImmutableSimpleCredentials build() { if (initBits != 0) { throw new IllegalStateException(formatRequiredAttributesMessage()); } return new ImmutableSimpleCredentials(this); } private boolean isAuthenticatedIsSet() { return (optBits & OPT_BIT_IS_AUTHENTICATED) != 0; } private String formatRequiredAttributesMessage() { List attributes = new ArrayList<>(); if ((initBits & INIT_BIT_PRINCIPAL) != 0) attributes.add("principal"); if ((initBits & INIT_BIT_AUTH_TOKEN) != 0) attributes.add("authToken"); return "Cannot build SimpleCredentials, some of required attributes are not set " + attributes; } } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy