Skip to content

Commit b1f117c

Browse files
committed
fix(auth): clean up JSpecify nullability annotations across auth library
1 parent 68979d8 commit b1f117c

27 files changed

Lines changed: 166 additions & 182 deletions

google-auth-library-java/credentials/java/com/google/auth/Credentials.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public Map<String, List<String>> getRequestMetadata() throws IOException {
121121
* @param callback Callback to execute when the request is finished.
122122
*/
123123
public void getRequestMetadata(
124-
final URI uri, Executor executor, final RequestMetadataCallback callback) {
124+
final @Nullable URI uri, Executor executor, final RequestMetadataCallback callback) {
125125
executor.execute(
126126
new Runnable() {
127127
@Override
@@ -137,7 +137,7 @@ public void run() {
137137
* @param uri URI of the entry point for the request.
138138
* @param callback Callback handler to execute when the metadata completes.
139139
*/
140-
protected final void blockingGetToCallback(URI uri, RequestMetadataCallback callback) {
140+
protected final void blockingGetToCallback(@Nullable URI uri, RequestMetadataCallback callback) {
141141
Map<String, List<String>> result;
142142
try {
143143
result = getRequestMetadata(uri);

google-auth-library-java/oauth2_http/java/com/google/auth/mtls/MtlsUtils.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import java.io.InputStream;
4141
import java.util.Locale;
4242
import org.jspecify.annotations.NullMarked;
43+
import org.jspecify.annotations.Nullable;
4344

4445
/**
4546
* Utility class for mTLS related operations.
@@ -65,7 +66,9 @@ private MtlsUtils() {
6566
* @throws IOException if the certificate configuration cannot be found or loaded.
6667
*/
6768
public static String getCertificatePath(
68-
EnvironmentProvider envProvider, PropertyProvider propProvider, String certConfigPathOverride)
69+
EnvironmentProvider envProvider,
70+
PropertyProvider propProvider,
71+
@Nullable String certConfigPathOverride)
6972
throws IOException {
7073
String certPath =
7174
getWorkloadCertificateConfiguration(envProvider, propProvider, certConfigPathOverride)
@@ -92,7 +95,9 @@ public static String getCertificatePath(
9295
* @throws IOException if the configuration file cannot be found, read, or parsed
9396
*/
9497
static WorkloadCertificateConfiguration getWorkloadCertificateConfiguration(
95-
EnvironmentProvider envProvider, PropertyProvider propProvider, String certConfigPathOverride)
98+
EnvironmentProvider envProvider,
99+
PropertyProvider propProvider,
100+
@Nullable String certConfigPathOverride)
96101
throws IOException {
97102
File certConfig;
98103
if (certConfigPathOverride != null) {

google-auth-library-java/oauth2_http/java/com/google/auth/mtls/X509Provider.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
public class X509Provider implements MtlsProvider {
5555
private final EnvironmentProvider envProvider;
5656
private final PropertyProvider propProvider;
57-
private final String certConfigPathOverride;
57+
private final @Nullable String certConfigPathOverride;
5858

5959
/**
6060
* Creates an X509 provider with an override path for the certificate configuration, bypassing the
@@ -69,7 +69,7 @@ public class X509Provider implements MtlsProvider {
6969
public X509Provider(
7070
EnvironmentProvider envProvider,
7171
PropertyProvider propProvider,
72-
String certConfigPathOverride) {
72+
@Nullable String certConfigPathOverride) {
7373
this.envProvider = envProvider;
7474
this.propProvider = propProvider;
7575
this.certConfigPathOverride = certConfigPathOverride;

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -287,9 +287,9 @@ static class Builder {
287287
private final String url;
288288
private final String region;
289289

290-
@Nullable private String requestPayload;
291-
@Nullable private Map<String, String> additionalHeaders;
292-
@Nullable private AwsDates dates;
290+
private @Nullable String requestPayload;
291+
private @Nullable Map<String, String> additionalHeaders;
292+
private @Nullable AwsDates dates;
293293

294294
private Builder(
295295
AwsSecurityCredentials awsSecurityCredentials,
@@ -303,7 +303,7 @@ private Builder(
303303
}
304304

305305
@CanIgnoreReturnValue
306-
Builder setRequestPayload(String requestPayload) {
306+
Builder setRequestPayload(@Nullable String requestPayload) {
307307
this.requestPayload = requestPayload;
308308
return this;
309309
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public class AwsSecurityCredentials {
4444
private final String accessKeyId;
4545
private final String secretAccessKey;
4646

47-
@Nullable private final String sessionToken;
47+
private final @Nullable String sessionToken;
4848

4949
/**
5050
* Constructor for AWSSecurityCredentials.
@@ -83,8 +83,7 @@ public String getSecretAccessKey() {
8383
*
8484
* @return the AWS session token.
8585
*/
86-
@Nullable
87-
public String getSessionToken() {
86+
public @Nullable String getSessionToken() {
8887
return sessionToken;
8988
}
9089
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ public enum GoogleAuthTransport {
154154

155155
private final String label;
156156

157-
private GoogleAuthTransport(String label) {
157+
GoogleAuthTransport(String label) {
158158
this.label = label;
159159
}
160160

@@ -185,7 +185,7 @@ public enum BindingEnforcement {
185185

186186
private final String label;
187187

188-
private BindingEnforcement(String label) {
188+
BindingEnforcement(String label) {
189189
this.label = label;
190190
}
191191

@@ -455,7 +455,7 @@ public AccessToken refreshAccessToken() throws IOException {
455455
OAuth2Utils.validateString(responseData, "access_token", PARSE_ERROR_PREFIX);
456456
int expiresInSeconds =
457457
OAuth2Utils.validateInt32(responseData, "expires_in", PARSE_ERROR_PREFIX);
458-
long expiresAtMilliseconds = clock.currentTimeMillis() + expiresInSeconds * 1000;
458+
long expiresAtMilliseconds = clock.currentTimeMillis() + expiresInSeconds * 1000L;
459459

460460
return new AccessToken(accessToken, new Date(expiresAtMilliseconds));
461461
}
@@ -828,7 +828,7 @@ public byte[] sign(byte[] toSign) {
828828
this.getUniverseDomain(),
829829
transportFactory.create(),
830830
toSign,
831-
Collections.<String, Object>emptyMap());
831+
Collections.emptyMap());
832832
} catch (SigningException ex) {
833833
throw ex;
834834
} catch (RuntimeException ex) {

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

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ public static final class AccessBoundaryRule {
166166
private final String availableResource;
167167
private final List<String> availablePermissions;
168168

169-
@Nullable private final AvailabilityCondition availabilityCondition;
169+
private final @Nullable AvailabilityCondition availabilityCondition;
170170

171171
AccessBoundaryRule(
172172
String availableResource,
@@ -197,8 +197,7 @@ public List<String> getAvailablePermissions() {
197197
return availablePermissions;
198198
}
199199

200-
@Nullable
201-
public AvailabilityCondition getAvailabilityCondition() {
200+
public @Nullable AvailabilityCondition getAvailabilityCondition() {
202201
return availabilityCondition;
203202
}
204203

@@ -210,7 +209,7 @@ public static class Builder {
210209
private String availableResource;
211210
private List<String> availablePermissions;
212211

213-
@Nullable private AvailabilityCondition availabilityCondition;
212+
private @Nullable AvailabilityCondition availabilityCondition;
214213

215214
private Builder() {}
216215

@@ -269,7 +268,8 @@ public Builder addAvailablePermission(String availablePermission) {
269268
* @return this {@code Builder} object
270269
*/
271270
@CanIgnoreReturnValue
272-
public Builder setAvailabilityCondition(AvailabilityCondition availabilityCondition) {
271+
public Builder setAvailabilityCondition(
272+
@Nullable AvailabilityCondition availabilityCondition) {
273273
this.availabilityCondition = availabilityCondition;
274274
return this;
275275
}
@@ -296,8 +296,8 @@ public AccessBoundaryRule build() {
296296
public static final class AvailabilityCondition {
297297
private final String expression;
298298

299-
@Nullable private final String title;
300-
@Nullable private final String description;
299+
private final @Nullable String title;
300+
private final @Nullable String description;
301301

302302
AvailabilityCondition(
303303
String expression, @Nullable String title, @Nullable String description) {
@@ -312,13 +312,11 @@ public String getExpression() {
312312
return expression;
313313
}
314314

315-
@Nullable
316-
public String getTitle() {
315+
public @Nullable String getTitle() {
317316
return title;
318317
}
319318

320-
@Nullable
321-
public String getDescription() {
319+
public @Nullable String getDescription() {
322320
return description;
323321
}
324322

@@ -329,8 +327,8 @@ public static Builder newBuilder() {
329327
public static final class Builder {
330328
private String expression;
331329

332-
@Nullable private String title;
333-
@Nullable private String description;
330+
private @Nullable String title;
331+
private @Nullable String description;
334332

335333
private Builder() {}
336334

@@ -358,7 +356,7 @@ public Builder setExpression(String expression) {
358356
* @return this {@code Builder} object
359357
*/
360358
@CanIgnoreReturnValue
361-
public Builder setTitle(String title) {
359+
public Builder setTitle(@Nullable String title) {
362360
this.title = title;
363361
return this;
364362
}
@@ -370,7 +368,7 @@ public Builder setTitle(String title) {
370368
* @return this {@code Builder} object
371369
*/
372370
@CanIgnoreReturnValue
373-
public Builder setDescription(String description) {
371+
public Builder setDescription(@Nullable String description) {
374372
this.description = description;
375373
return this;
376374
}

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

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@
5656
*/
5757
@NullMarked
5858
class DefaultCredentialsProvider {
59+
static final Logger LOGGER = Logger.getLogger(DefaultCredentialsProvider.class.getName());
5960
static final DefaultCredentialsProvider DEFAULT = new DefaultCredentialsProvider();
61+
6062
static final String CREDENTIAL_ENV_VAR = "GOOGLE_APPLICATION_CREDENTIALS";
6163
static final String QUOTA_PROJECT_ENV_VAR = "GOOGLE_CLOUD_QUOTA_PROJECT";
6264

@@ -66,14 +68,11 @@ class DefaultCredentialsProvider {
6668
static final String CLOUD_SHELL_ENV_VAR = "DEVSHELL_CLIENT_PORT";
6769
static final String SKIP_APP_ENGINE_ENV_VAR = "GOOGLE_APPLICATION_CREDENTIALS_SKIP_APP_ENGINE";
6870
static final String SPECIFICATION_VERSION = System.getProperty("java.specification.version");
69-
static final String GAE_RUNTIME_VERSION =
71+
static final @Nullable String GAE_RUNTIME_VERSION =
7072
System.getProperty("com.google.appengine.runtime.version");
71-
static final String RUNTIME_JETTY_LOGGER = System.getProperty("org.eclipse.jetty.util.log.class");
72-
static final Logger LOGGER = Logger.getLogger(DefaultCredentialsProvider.class.getName());
73+
static final @Nullable String RUNTIME_JETTY_LOGGER = System.getProperty("org.eclipse.jetty.util.log.class");
7374
static final String NO_GCE_CHECK_ENV_VAR = "NO_GCE_CHECK";
7475
static final String GCE_METADATA_HOST_ENV_VAR = "GCE_METADATA_HOST";
75-
static final String CLOUDSDK_CLIENT_ID =
76-
"764086051850-6qr4p6gpi6hn506pt8ejuq83di341hur.apps.googleusercontent.com";
7776
static final String CLOUDSDK_CREDENTIALS_WARNING =
7877
"You are authenticating using user credentials. "
7978
+ "For production, we recommend using service account credentials.\n\n"
@@ -136,7 +135,7 @@ final GoogleCredentials getDefaultCredentials(HttpTransportFactory transportFact
136135
// First try the environment variable
137136
GoogleCredentials credentials = null;
138137
String credentialsPath = getEnv(CREDENTIAL_ENV_VAR);
139-
if (credentialsPath != null && credentialsPath.length() > 0) {
138+
if (credentialsPath != null && !credentialsPath.isEmpty()) {
140139
LOGGER.log(
141140
Level.FINE,
142141
String.format("Attempting to load credentials from file: %s", credentialsPath));
@@ -234,7 +233,7 @@ final GoogleCredentials getDefaultCredentials(HttpTransportFactory transportFact
234233
if (credentials != null) {
235234
String quotaFromEnv = getEnv(QUOTA_PROJECT_ENV_VAR);
236235

237-
if (quotaFromEnv != null && quotaFromEnv.trim().length() > 0) {
236+
if (quotaFromEnv != null && !quotaFromEnv.trim().isEmpty()) {
238237
credentials = credentials.createWithQuotaProject(quotaFromEnv);
239238
}
240239
}
@@ -357,8 +356,8 @@ Class<?> forName(String className) throws ClassNotFoundException {
357356
return System.getenv(name);
358357
}
359358

360-
@Nullable String getProperty(String property, @Nullable String def) {
361-
return System.getProperty(property, def);
359+
String getProperty(String property, String defaultValue) {
360+
return System.getProperty(property, defaultValue);
362361
}
363362

364363
boolean isFile(File file) {

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ class ExecutableResponse {
5050
private final int version;
5151
private final boolean success;
5252

53-
@Nullable private Long expirationTime;
54-
@Nullable private String tokenType;
55-
@Nullable private String subjectToken;
56-
@Nullable private String errorCode;
57-
@Nullable private String errorMessage;
53+
private @Nullable Long expirationTime;
54+
private @Nullable String tokenType;
55+
private @Nullable String subjectToken;
56+
private @Nullable String errorCode;
57+
private @Nullable String errorMessage;
5858

5959
ExecutableResponse(GenericJson json) throws IOException {
6060
if (!json.containsKey("version")) {

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

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -675,8 +675,8 @@ public GdchCredentials build() {
675675
}
676676
}
677677

678-
private static String validateField(String field, String fieldName) throws IOException {
679-
if (field == null || field.isEmpty()) {
678+
private static String validateField(@Nullable String field, String fieldName) throws IOException {
679+
if (Strings.isNullOrEmpty(field)) {
680680
throw new IOException(
681681
String.format(
682682
"Error reading GDCH service account credential from JSON, %s is misconfigured.",
@@ -712,8 +712,8 @@ public HttpTransport create() {
712712
return transport;
713713
}
714714

715-
private void setTransport(String caCertPath) throws IOException {
716-
if (caCertPath == null || caCertPath.isEmpty()) {
715+
private void setTransport(@Nullable String caCertPath) throws IOException {
716+
if (Strings.isNullOrEmpty(caCertPath)) {
717717
this.transport = new NetHttpTransport();
718718
return;
719719
}
@@ -752,7 +752,6 @@ private void setTransport(String caCertPath) throws IOException {
752752
* @param payload The JWS payload containing claims like "iss", "sub", and "aud".
753753
* @return A complete, signed JWS string in the format {@code [header].[payload].[signature]}.
754754
* @throws GeneralSecurityException If signing fails due to cryptographic errors.
755-
* @throws IOException If serialization or transcoding fails.
756755
*/
757756
@VisibleForTesting
758757
static String signUsingEsSha256(
@@ -778,7 +777,7 @@ static String signUsingEsSha256(
778777
SecurityUtils.sign(SecurityUtils.getEs256SignatureAlgorithm(), privateKey, contentBytes);
779778

780779
// 3. Transcode the signature from DER to Concatenated R|S.
781-
byte[] jwsSignature = transcodeDerToConcat(signature, 64);
780+
byte[] jwsSignature = transcodeDerToConcat(signature);
782781

783782
// 4. Return final JWS: [Signing Input] + '.' + Base64URL(Signature)
784783
return content + "." + Base64.getUrlEncoder().withoutPadding().encodeToString(jwsSignature);
@@ -795,13 +794,11 @@ static String signUsingEsSha256(
795794
* <p>Concatenated format: {@code r | s} (where {@code |} is concatenation).
796795
*
797796
* @param derSignature The raw bytes of the DER-encoded signature.
798-
* @param outputLength The total expected length of the concatenated signature (64 bytes for
799-
* ES256).
800797
* @return The signature in concatenated R|S format.
801798
* @throws IOException If the DER format is invalid.
802799
*/
803800
@VisibleForTesting
804-
static byte[] transcodeDerToConcat(byte[] derSignature, int outputLength)
801+
static byte[] transcodeDerToConcat(byte[] derSignature)
805802
throws GoogleAuthException {
806803
// Validate basic ASN.1 DER structure (0x30 = SEQUENCE)
807804
if (derSignature.length < 8 || derSignature[0] != 0x30) {
@@ -847,7 +844,7 @@ static byte[] transcodeDerToConcat(byte[] derSignature, int outputLength)
847844
System.arraycopy(derSignature, offset, s, 0, sLength);
848845

849846
// Concatenate r and s into fixed-length segments (32 bytes each for ES256)
850-
int keySizeBytes = outputLength / 2;
847+
int keySizeBytes = 64 / 2;
851848
if (r.length > keySizeBytes || s.length > keySizeBytes) {
852849
throw new GoogleAuthException(
853850
false,
@@ -858,9 +855,9 @@ static byte[] transcodeDerToConcat(byte[] derSignature, int outputLength)
858855
null);
859856
}
860857

861-
byte[] result = new byte[outputLength];
858+
byte[] result = new byte[64];
862859
System.arraycopy(r, 0, result, keySizeBytes - r.length, r.length);
863-
System.arraycopy(s, 0, result, outputLength - s.length, s.length);
860+
System.arraycopy(s, 0, result, 64 - s.length, s.length);
864861

865862
return result;
866863
}

0 commit comments

Comments
 (0)