Skip to content

Commit

Permalink
fix: use more compatible com.google.common.io.BaseEncoding
Browse files Browse the repository at this point in the history
Use it instead of java.util.Base64 (requires newer android)
and also instead of com.google.api.client.util.Base64 (which is deprecated)

Legacy android doesn't support java.util.Base64. It was added in API level 26 - see https://developer.android.com/reference/java/util/Base64?hl=en
  • Loading branch information
Noam Lewis committed Nov 21, 2023
1 parent b7bf6b0 commit 792f608
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Base64;
import com.google.common.io.BaseEncoding;

/**
* Implements PKCE using only the Java standard library. See https://www.rfc-editor.org/rfc/rfc7636.
Expand All @@ -50,7 +50,7 @@ private String createCodeVerifier() {
SecureRandom sr = new SecureRandom();
byte[] code = new byte[MAX_CODE_VERIFIER_LENGTH];
sr.nextBytes(code);
return Base64.getUrlEncoder().encodeToString(code);
return BaseEncoding.base64().base64Url().encode(code);
}

private CodeChallenge createCodeChallenge(String codeVerifier) {
Expand Down Expand Up @@ -90,7 +90,7 @@ private class CodeChallenge {

byte[] digest = md.digest();

this.codeChallenge = Base64.getUrlEncoder().encodeToString(digest);
this.codeChallenge = BaseEncoding.base64().base64Url().encode(digest);
this.codeChallengeMethod = "S256";
} catch (NoSuchAlgorithmException e) {
this.codeChallenge = codeVerifier;
Expand Down
10 changes: 5 additions & 5 deletions oauth2_http/java/com/google/auth/oauth2/TokenVerifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.GenericJson;
import com.google.api.client.json.webtoken.JsonWebSignature;
import com.google.api.client.util.Base64;
import com.google.common.io.BaseEncoding;
import com.google.api.client.util.Clock;
import com.google.api.client.util.ExponentialBackOff;
import com.google.api.client.util.Key;
Expand Down Expand Up @@ -395,8 +395,8 @@ private PublicKey buildRs256PublicKey(JsonWebKey key)
Preconditions.checkNotNull(key.e);
Preconditions.checkNotNull(key.n);

BigInteger modulus = new BigInteger(1, Base64.decodeBase64(key.n));
BigInteger exponent = new BigInteger(1, Base64.decodeBase64(key.e));
BigInteger modulus = new BigInteger(1, BaseEncoding.base64().decode(key.n));
BigInteger exponent = new BigInteger(1, BaseEncoding.base64().decode(key.e));

RSAPublicKeySpec spec = new RSAPublicKeySpec(modulus, exponent);
KeyFactory factory = KeyFactory.getInstance("RSA");
Expand All @@ -408,8 +408,8 @@ private PublicKey buildEs256PublicKey(JsonWebKey key)
Preconditions.checkArgument("EC".equals(key.kty));
Preconditions.checkArgument("P-256".equals(key.crv));

BigInteger x = new BigInteger(1, Base64.decodeBase64(key.x));
BigInteger y = new BigInteger(1, Base64.decodeBase64(key.y));
BigInteger x = new BigInteger(1, BaseEncoding.base64().decode(key.x));
BigInteger y = new BigInteger(1, BaseEncoding.base64().decode(key.y));
ECPoint pubPoint = new ECPoint(x, y);
AlgorithmParameters parameters = AlgorithmParameters.getInstance("EC");
parameters.init(new ECGenParameterSpec("secp256r1"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import com.google.common.io.BaseEncoding;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
Expand All @@ -52,7 +52,7 @@ public void testPkceExpected() throws NoSuchAlgorithmException {

byte[] digest = md.digest();

String expectedCodeChallenge = Base64.getUrlEncoder().encodeToString(digest);
String expectedCodeChallenge = BaseEncoding.base64().base64Url().encode(digest);
String expectedCodeChallengeMethod = "S256";

assertEquals(pkce.getCodeChallenge(), expectedCodeChallenge);
Expand Down

0 comments on commit 792f608

Please sign in to comment.