Skip to content
This repository was archived by the owner on May 12, 2026. It is now read-only.

Commit 39ef931

Browse files
committed
fix: use enum for possible algorithms
1 parent b3085a2 commit 39ef931

3 files changed

Lines changed: 13 additions & 6 deletions

File tree

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,13 +228,14 @@ static GdchCredentials fromJson(Map<String, Object> json, HttpTransportFactory t
228228
/**
229229
* Internal constructor.
230230
*
231-
* @param privateKeyPkcs8 RSA private key object for the service account in PKCS#8 format.
231+
* @param privateKeyPkcs8 EC private key object for the service account in PKCS#8 format.
232232
* @param builder A builder for GdchCredentials.
233233
* @return an instance of GdchCredentials.
234234
*/
235235
static GdchCredentials fromPkcs8(String privateKeyPkcs8, GdchCredentials.Builder builder)
236236
throws IOException {
237-
PrivateKey privateKey = OAuth2Utils.privateKeyFromPkcs8(privateKeyPkcs8, "EC");
237+
PrivateKey privateKey =
238+
OAuth2Utils.privateKeyFromPkcs8(privateKeyPkcs8, OAuth2Utils.Pkcs8Algorithm.EC);
238239
builder.setPrivateKey(privateKey);
239240

240241
return new GdchCredentials(builder);

oauth2_http/java/com/google/auth/oauth2/OAuth2Utils.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@
7979
*/
8080
public class OAuth2Utils {
8181

82+
public enum Pkcs8Algorithm {
83+
RSA,
84+
EC
85+
}
86+
8287
static final String SIGNATURE_ALGORITHM = "SHA256withRSA";
8388

8489
public static final String TOKEN_TYPE_ACCESS_TOKEN =
@@ -266,7 +271,7 @@ static Map<String, Object> validateMap(Map<String, Object> map, String key, Stri
266271
* key creation.
267272
*/
268273
public static PrivateKey privateKeyFromPkcs8(String privateKeyPkcs8) throws IOException {
269-
return privateKeyFromPkcs8(privateKeyPkcs8, "RSA");
274+
return privateKeyFromPkcs8(privateKeyPkcs8, Pkcs8Algorithm.RSA);
270275
}
271276

272277
/**
@@ -278,7 +283,7 @@ public static PrivateKey privateKeyFromPkcs8(String privateKeyPkcs8) throws IOEx
278283
* @throws IOException if the PKCS#8 data is invalid or if an unexpected exception occurs during
279284
* key creation.
280285
*/
281-
public static PrivateKey privateKeyFromPkcs8(String privateKeyPkcs8, String algorithm)
286+
public static PrivateKey privateKeyFromPkcs8(String privateKeyPkcs8, Pkcs8Algorithm algorithm)
282287
throws IOException {
283288
Reader reader = new StringReader(privateKeyPkcs8);
284289
Section section = PemReader.readFirstSectionAndClose(reader, "PRIVATE KEY");
@@ -289,7 +294,7 @@ public static PrivateKey privateKeyFromPkcs8(String privateKeyPkcs8, String algo
289294
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(bytes);
290295
Exception unexpectedException;
291296
try {
292-
KeyFactory keyFactory = KeyFactory.getInstance(algorithm);
297+
KeyFactory keyFactory = KeyFactory.getInstance(algorithm.toString());
293298
return keyFactory.generatePrivate(keySpec);
294299
} catch (NoSuchAlgorithmException | InvalidKeySpecException exception) {
295300
unexpectedException = exception;

oauth2_http/javatests/com/google/auth/oauth2/GdchCredentialsTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1208,7 +1208,8 @@ void signUsingEsSha256_producesVerifiableSignature() throws Exception {
12081208

12091209
@Test
12101210
void signUsingEsSha256_validStructure() throws Exception {
1211-
PrivateKey privateKey = OAuth2Utils.privateKeyFromPkcs8(PRIVATE_KEY_PKCS8, "EC");
1211+
PrivateKey privateKey =
1212+
OAuth2Utils.privateKeyFromPkcs8(PRIVATE_KEY_PKCS8, OAuth2Utils.Pkcs8Algorithm.EC);
12121213
JsonFactory jsonFactory = GsonFactory.getDefaultInstance();
12131214

12141215
JsonWebSignature.Header header = new JsonWebSignature.Header();

0 commit comments

Comments
 (0)