com.rt.storage.auth.oauth2.JwtClaims Maven / Gradle / Ivy
package com.rt.storage.auth.oauth2;
import com.google.auto.value.AutoValue;
import com.google.common.collect.ImmutableMap;
import java.io.Serializable;
import java.util.Map;
import javax.annotation.Nullable;
/**
* Value class representing the set of fields used as the payload of a JWT token.
*
* To create and customize claims, use the builder:
*
*
* Claims claims = Claims.newBuilder()
* .setAudience("https://example.com/some-audience")
* .setIssuer("[email protected]")
* .setSubject("[email protected]")
* .build();
*
*/
@AutoValue
public abstract class JwtClaims implements Serializable {
private static final long serialVersionUID = 4974444151019426702L;
@Nullable
abstract String getAudience();
@Nullable
abstract String getIssuer();
@Nullable
abstract String getSubject();
/**
* Returns additional claims for this object. The returned map is not guaranteed to be mutable.
*
* @return additional claims
*/
abstract Map getAdditionalClaims();
public static Builder newBuilder() {
return new AutoValue_JwtClaims.Builder().setAdditionalClaims(ImmutableMap.of());
}
/**
* Returns a new Claims instance with overridden fields.
*
* Any non-null field will overwrite the value from the original claims instance.
*
* @param other claims to override
* @return new claims
*/
public JwtClaims merge(JwtClaims other) {
ImmutableMap.Builder newClaimsBuilder = ImmutableMap.builder();
newClaimsBuilder.putAll(getAdditionalClaims());
newClaimsBuilder.putAll(other.getAdditionalClaims());
return newBuilder()
.setAudience(other.getAudience() == null ? getAudience() : other.getAudience())
.setIssuer(other.getIssuer() == null ? getIssuer() : other.getIssuer())
.setSubject(other.getSubject() == null ? getSubject() : other.getSubject())
.setAdditionalClaims(newClaimsBuilder.build())
.build();
}
/**
* Returns whether or not this set of claims is complete.
*
* Audience, issuer, and subject are required to be set in order to use the claim set for a JWT
* token. An incomplete Claims instance is useful for overriding claims when using {@link
* ServiceAccountJwtAccessCredentials#jwtWithClaims(JwtClaims)} or {@link
* JwtCredentials#jwtWithClaims(JwtClaims)}.
*
* @return true if all required fields have been set; false otherwise
*/
public boolean isComplete() {
return getAudience() != null && getIssuer() != null && getSubject() != null;
}
@AutoValue.Builder
public abstract static class Builder {
public abstract Builder setAudience(String audience);
public abstract Builder setIssuer(String issuer);
public abstract Builder setSubject(String subject);
public abstract Builder setAdditionalClaims(Map additionalClaims);
public abstract JwtClaims build();
}
}