Skip to content

Commit ee0abbc

Browse files
committed
fix(auth): refine JSpecify nullability for ServiceAccountCredentials and UserCredentials
1 parent 0d5fac0 commit ee0abbc

5 files changed

Lines changed: 86 additions & 88 deletions

File tree

google-auth-library-java/oauth2_http/java/com/google/auth/oauth2/GoogleCredentials.java

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@
3333

3434
import com.google.api.client.json.GenericJson;
3535
import com.google.api.client.json.JsonObjectParser;
36-
import com.google.api.client.util.Preconditions;
3736
import com.google.api.core.ObsoleteApi;
3837
import com.google.auth.Credentials;
3938
import com.google.auth.http.HttpTransportFactory;
4039
import com.google.common.annotations.VisibleForTesting;
4140
import com.google.common.base.MoreObjects;
4241
import com.google.common.base.MoreObjects.ToStringHelper;
42+
import com.google.common.base.Preconditions;
4343
import com.google.common.base.Strings;
4444
import com.google.common.collect.ImmutableList;
4545
import com.google.common.collect.ImmutableMap;
@@ -79,7 +79,7 @@ enum GoogleCredentialsInfo {
7979
COMPUTE_ENGINE_CREDENTIALS("Compute Engine Credentials", null);
8080

8181
private final String credentialName;
82-
@Nullable private final String fileType;
82+
private final @Nullable String fileType;
8383

8484
GoogleCredentialsInfo(String credentialName, @Nullable String fileType) {
8585
this.credentialName = credentialName;
@@ -97,17 +97,17 @@ String getCredentialName() {
9797

9898
// The following package-private fields to provide additional info for errors message
9999
// Source of the credential (e.g. env var value or well know file location)
100-
String source;
100+
@Nullable String source;
101101
// User-friendly name of the Credential class
102-
String name;
102+
@Nullable String name;
103103
// Identity of the credential
104104
// Note: This field may contain data such as serviceAccountEmail which should not be serialized
105-
transient String principal;
105+
transient @Nullable String principal;
106106

107107
private final String universeDomain;
108108
private final boolean isExplicitUniverseDomain;
109109

110-
protected final String quotaProjectId;
110+
protected final @Nullable String quotaProjectId;
111111

112112
private static final DefaultCredentialsProvider defaultCredentialsProvider =
113113
new DefaultCredentialsProvider();
@@ -230,7 +230,8 @@ public static GoogleCredentials getApplicationDefault(HttpTransportFactory trans
230230
* @throws IOException if the credential cannot be created from the stream.
231231
*/
232232
@ObsoleteApi(
233-
"This method is obsolete because of a potential security risk. Use the credential specific load method instead")
233+
"This method is obsolete because of a potential security risk. Use the credential specific"
234+
+ " load method instead")
234235
public static GoogleCredentials fromStream(InputStream credentialsStream) throws IOException {
235236
return fromStream(credentialsStream, OAuth2Utils.HTTP_TRANSPORT_FACTORY);
236237
}
@@ -296,7 +297,8 @@ static String extractFromJson(Map<String, Object> json, String field) throws IOE
296297
* @throws IOException if the credential cannot be created from the stream.
297298
*/
298299
@ObsoleteApi(
299-
"This method is obsolete because of a potential security risk. Use the credential specific load method instead")
300+
"This method is obsolete because of a potential security risk. Use the credential specific"
301+
+ " load method instead")
300302
public static GoogleCredentials fromStream(
301303
InputStream credentialsStream, HttpTransportFactory transportFactory) throws IOException {
302304
Preconditions.checkNotNull(transportFactory);
@@ -389,7 +391,7 @@ boolean isDefaultUniverseDomain() throws IOException {
389391
* @return a new map with quotaProjectId added if needed
390392
*/
391393
static Map<String, List<String>> addQuotaProjectIdToRequestMetadata(
392-
String quotaProjectId, Map<String, List<String>> requestMetadata) {
394+
@Nullable String quotaProjectId, Map<String, List<String>> requestMetadata) {
393395
Preconditions.checkNotNull(requestMetadata);
394396
Map<String, List<String>> newRequestMetadata = new HashMap<>(requestMetadata);
395397
if (quotaProjectId != null && !requestMetadata.containsKey(QUOTA_PROJECT_ID_HEADER_KEY)) {
@@ -527,7 +529,7 @@ public Builder toBuilder() {
527529
}
528530

529531
@Override
530-
public String getQuotaProjectId() {
532+
public @Nullable String getQuotaProjectId() {
531533
return this.quotaProjectId;
532534
}
533535

@@ -538,7 +540,7 @@ public String getQuotaProjectId() {
538540
*
539541
* @return the project id for a Credential type
540542
*/
541-
public String getProjectId() {
543+
public @Nullable String getProjectId() {
542544
return null;
543545
}
544546

@@ -653,8 +655,8 @@ public Map<String, String> getCredentialInfo() {
653655
}
654656

655657
public static class Builder extends OAuth2Credentials.Builder {
656-
@Nullable protected String quotaProjectId;
657-
@Nullable protected String universeDomain;
658+
protected @Nullable String quotaProjectId;
659+
protected @Nullable String universeDomain;
658660
@Nullable String source;
659661

660662
protected Builder() {}
@@ -679,7 +681,7 @@ public GoogleCredentials build() {
679681
}
680682

681683
@CanIgnoreReturnValue
682-
public Builder setQuotaProjectId(String quotaProjectId) {
684+
public Builder setQuotaProjectId(@Nullable String quotaProjectId) {
683685
this.quotaProjectId = quotaProjectId;
684686
return this;
685687
}
@@ -689,22 +691,23 @@ public Builder setUniverseDomain(String universeDomain) {
689691
return this;
690692
}
691693

692-
public String getQuotaProjectId() {
694+
public @Nullable String getQuotaProjectId() {
693695
return this.quotaProjectId;
694696
}
695697

696-
public String getUniverseDomain() {
698+
public @Nullable String getUniverseDomain() {
697699
return this.universeDomain;
698700
}
699701

700-
Builder setSource(String source) {
702+
@CanIgnoreReturnValue
703+
Builder setSource(@Nullable String source) {
701704
this.source = source;
702705
return this;
703706
}
704707

705708
@Override
706709
@CanIgnoreReturnValue
707-
public Builder setAccessToken(AccessToken token) {
710+
public Builder setAccessToken(@Nullable AccessToken token) {
708711
super.setAccessToken(token);
709712
return this;
710713
}

google-auth-library-java/oauth2_http/java/com/google/auth/oauth2/OAuth2Credentials.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,11 @@ public class OAuth2Credentials extends Credentials {
8282

8383
// byte[] is serializable, so the lock variable can be final
8484
@VisibleForTesting final Object lock = new byte[0];
85-
@Nullable private volatile OAuthValue value = null;
85+
private volatile @Nullable OAuthValue value = null;
8686
@Nullable @VisibleForTesting transient RefreshTask refreshTask;
8787

8888
// Change listeners are not serialized
89-
private transient List<CredentialsChangedListener> changeListeners;
89+
private transient @Nullable List<CredentialsChangedListener> changeListeners;
9090
// Until we expose this to the users it can remain transient and non-serializable
9191
transient Clock clock = Clock.SYSTEM;
9292

@@ -115,7 +115,7 @@ protected OAuth2Credentials(@Nullable AccessToken accessToken) {
115115
}
116116

117117
protected OAuth2Credentials(
118-
AccessToken accessToken, Duration refreshMargin, Duration expirationMargin) {
118+
@Nullable AccessToken accessToken, Duration refreshMargin, Duration expirationMargin) {
119119
if (accessToken != null) {
120120
this.value = OAuthValue.create(accessToken, EMPTY_EXTRA_HEADERS);
121121
}
@@ -149,8 +149,7 @@ public boolean hasRequestMetadataOnly() {
149149
*
150150
* @return The cached access token.
151151
*/
152-
@Nullable
153-
public final AccessToken getAccessToken() {
152+
public final @Nullable AccessToken getAccessToken() {
154153
OAuthValue localState = value;
155154
if (localState != null) {
156155
return localState.temporaryAccess;
@@ -440,8 +439,7 @@ public int hashCode() {
440439
return Objects.hashCode(value);
441440
}
442441

443-
@Nullable
444-
protected Map<String, List<String>> getRequestMetadataInternal() {
442+
protected @Nullable Map<String, List<String>> getRequestMetadataInternal() {
445443
OAuthValue localValue = value;
446444
if (localValue != null) {
447445
return localValue.requestMetadata;
@@ -711,7 +709,7 @@ public void run() {
711709

712710
public static class Builder {
713711

714-
private AccessToken accessToken;
712+
private @Nullable AccessToken accessToken;
715713
private Duration refreshMargin = DEFAULT_REFRESH_MARGIN;
716714
private Duration expirationMargin = DEFAULT_EXPIRATION_MARGIN;
717715

@@ -724,7 +722,7 @@ protected Builder(OAuth2Credentials credentials) {
724722
}
725723

726724
@CanIgnoreReturnValue
727-
public Builder setAccessToken(AccessToken token) {
725+
public Builder setAccessToken(@Nullable AccessToken token) {
728726
this.accessToken = token;
729727
return this;
730728
}
@@ -749,7 +747,7 @@ public Duration getExpirationMargin() {
749747
return expirationMargin;
750748
}
751749

752-
public AccessToken getAccessToken() {
750+
public @Nullable AccessToken getAccessToken() {
753751
return accessToken;
754752
}
755753

google-auth-library-java/oauth2_http/java/com/google/auth/oauth2/QuotaProjectIdProvider.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,13 @@
3232
package com.google.auth.oauth2;
3333

3434
import org.jspecify.annotations.NullMarked;
35+
import org.jspecify.annotations.Nullable;
3536

3637
/** Interface for {@link GoogleCredentials} that return a quota project ID. */
3738
@NullMarked
3839
public interface QuotaProjectIdProvider {
3940
/**
4041
* @return the quota project ID used for quota and billing purposes
4142
*/
42-
String getQuotaProjectId();
43+
@Nullable String getQuotaProjectId();
4344
}

0 commit comments

Comments
 (0)