Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ public class OAuth2Credentials extends Credentials {
/**
* Returns the credentials instance from the given access token.
*
* <p>The returned credentials default to {@link Credentials#GOOGLE_DEFAULT_UNIVERSE}. To create
* access-token credentials for a custom universe domain, use {@link
* GoogleCredentials#create(String, AccessToken)}.
*
* @param accessToken the access token
* @return the credentials instance
*/
Expand Down Expand Up @@ -559,6 +563,13 @@ public static <T> T getFromServiceLoader(Class<? extends T> clazz, T defaultInst
return Iterables.getFirst(ServiceLoader.load(clazz), defaultInstance);
}

/**
* Returns a new builder for {@link OAuth2Credentials}.
*
* <p>Built credentials default to {@link Credentials#GOOGLE_DEFAULT_UNIVERSE}. To create
* access-token credentials for a custom universe domain, use {@link
* GoogleCredentials#create(String, AccessToken)} or {@link GoogleCredentials#newBuilder()}.
*/
public static Builder newBuilder() {
return new Builder();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,46 +33,58 @@

import static com.google.common.base.Preconditions.checkNotNull;

import com.google.auth.Credentials;
import com.google.common.annotations.VisibleForTesting;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.Objects;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;

/**
* A refreshable alternative to {@link OAuth2Credentials}.
*
* <p>To enable automatic token refreshes, you must provide an {@link OAuth2RefreshHandler}.
*
* <p>Because {@link OAuth2RefreshHandler} returns {@link AccessToken} values without universe
* domain metadata, credentials default to {@link Credentials#GOOGLE_DEFAULT_UNIVERSE}. Callers
* refreshing tokens for a custom universe domain should configure it explicitly via {@link
* Builder#setUniverseDomain(String)}. If lazy or delegated universe domain resolution is needed,
* subclass this class and override {@link #getUniverseDomain()}.
*/
@NullMarked
public class OAuth2CredentialsWithRefresh extends OAuth2Credentials {

// Matches the computed serialVersionUID prior to adding universeDomain.
private static final long serialVersionUID = -5362111100740719676L;

/** Interface for the refresh handler. */
public interface OAuth2RefreshHandler {
AccessToken refreshAccessToken() throws IOException;
}

private final OAuth2RefreshHandler refreshHandler;
@VisibleForTesting String universeDomain;

protected OAuth2CredentialsWithRefresh(Builder builder) {
super(builder.getAccessToken(), builder.getRefreshMargin(), builder.getExpirationMargin());
// An expiration time must be provided.
// If no expirationTime is provided, the token will never be refreshed.
if (builder.getAccessToken() != null && builder.getAccessToken().getExpirationTime() == null) {
throw new IllegalArgumentException(
"The provided access token must contain the expiration time.");
}
this.refreshHandler = checkNotNull(builder.refreshHandler);
if (builder.universeDomain == null || builder.universeDomain.trim().isEmpty()) {
this.universeDomain = Credentials.GOOGLE_DEFAULT_UNIVERSE;
} else {
this.universeDomain = builder.universeDomain;
}
}

protected OAuth2CredentialsWithRefresh(
AccessToken accessToken, OAuth2RefreshHandler refreshHandler) {
super(accessToken);

// If no expirationTime is provided, the token will never be refreshed.
if (accessToken != null && accessToken.getExpirationTime() == null) {
throw new IllegalArgumentException(
"The provided access token must contain the expiration time.");
}

this.refreshHandler = checkNotNull(refreshHandler);
this(newBuilder().setAccessToken(accessToken).setRefreshHandler(refreshHandler));
}

/** Refreshes the access token using the provided {@link OAuth2RefreshHandler}. */
Expand All @@ -87,13 +99,54 @@ public OAuth2RefreshHandler getRefreshHandler() {
return refreshHandler;
}

/**
* Returns the universe domain for this credential.
*
* <p>Returns the fixed universe domain configured via {@link Builder#setUniverseDomain(String)},
* or {@link Credentials#GOOGLE_DEFAULT_UNIVERSE} if none was configured. Subclasses may override
* this method if lazy or delegated resolution is required.
*
* @return the configured universe domain, or {@link Credentials#GOOGLE_DEFAULT_UNIVERSE} if none
* was explicitly configured
* @throws IOException if a subclass fails while resolving the universe domain
*/
@Override
public String getUniverseDomain() throws IOException {
return this.universeDomain;
}

@Override
public boolean equals(@Nullable Object obj) {
if (!(obj instanceof OAuth2CredentialsWithRefresh)) {
return false;
}
OAuth2CredentialsWithRefresh other = (OAuth2CredentialsWithRefresh) obj;
return super.equals(other)
&& Objects.equals(this.refreshHandler, other.refreshHandler)
&& Objects.equals(this.universeDomain, other.universeDomain);
}

@Override
public int hashCode() {
return Objects.hash(super.hashCode(), this.refreshHandler, this.universeDomain);
}
Comment thread
lqiu96 marked this conversation as resolved.
Resolved

private void readObject(ObjectInputStream input) throws IOException, ClassNotFoundException {
input.defaultReadObject();
// Default absent or blank universeDomain from older serialized streams.
if (this.universeDomain == null || this.universeDomain.trim().isEmpty()) {
this.universeDomain = Credentials.GOOGLE_DEFAULT_UNIVERSE;
}
}

public static Builder newBuilder() {
return new Builder();
}

public static class Builder extends OAuth2Credentials.Builder {

private OAuth2RefreshHandler refreshHandler;
private @Nullable String universeDomain;

private Builder() {}

Expand All @@ -115,6 +168,28 @@ public Builder setRefreshHandler(OAuth2RefreshHandler handler) {
return this;
}

/**
* Sets the fixed universe domain for this credential. If null or blank, defaults to {@link
* Credentials#GOOGLE_DEFAULT_UNIVERSE}.
*
* <p>Because {@link OAuth2RefreshHandler} returns {@link AccessToken} values without universe
* domain metadata, callers refreshing tokens for a custom universe domain must set this
* explicitly.
*
* @param universeDomain the universe domain (for example, {@code googleapis.com})
* @return this builder
*/
@CanIgnoreReturnValue
public Builder setUniverseDomain(@Nullable String universeDomain) {
this.universeDomain = universeDomain;
return this;
}

/** Returns the configured universe domain, or {@code null} if not explicitly set. */
public @Nullable String getUniverseDomain() {
return universeDomain;
}

@Override
public OAuth2CredentialsWithRefresh build() {
return new OAuth2CredentialsWithRefresh(this);
Expand Down
Loading
Loading