Skip to content

Commit

Permalink
Merge branch 'main' into removespacefromtoken
Browse files Browse the repository at this point in the history
  • Loading branch information
sai-sunder-s committed Aug 11, 2022
2 parents 1a5f5ba + 53355a7 commit 96bc155
Show file tree
Hide file tree
Showing 49 changed files with 2,331 additions and 2,069 deletions.
Expand Up @@ -31,13 +31,13 @@

package com.google.auth.appengine;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNotSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import com.google.auth.Credentials;
import com.google.auth.oauth2.AccessToken;
Expand All @@ -51,18 +51,21 @@
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.junit.jupiter.api.Test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/** Unit tests for AppEngineCredentials */
class AppEngineCredentialsTest extends BaseSerializationTest {
@RunWith(JUnit4.class)
public class AppEngineCredentialsTest extends BaseSerializationTest {

private static Collection<String> SCOPES =
Collections.unmodifiableCollection(Arrays.asList("scope1", "scope2"));
private static final URI CALL_URI = URI.create("http://googleapis.com/testapi/v1/foo");
private static final String EXPECTED_ACCOUNT = "serviceAccount";

@Test
void constructor_usesAppIdentityService() throws IOException {
public void constructor_usesAppIdentityService() throws IOException {
String expectedAccessToken = "ExpectedAccessToken";

MockAppIdentityService appIdentity = new MockAppIdentityService();
Expand All @@ -80,7 +83,7 @@ void constructor_usesAppIdentityService() throws IOException {
}

@Test
void refreshAccessToken_sameAs() throws IOException {
public void refreshAccessToken_sameAs() throws IOException {
String expectedAccessToken = "ExpectedAccessToken";

MockAppIdentityService appIdentity = new MockAppIdentityService();
Expand All @@ -97,7 +100,7 @@ void refreshAccessToken_sameAs() throws IOException {
}

@Test
void getAccount_sameAs() {
public void getAccount_sameAs() throws IOException {
MockAppIdentityService appIdentity = new MockAppIdentityService();
appIdentity.setServiceAccountName(EXPECTED_ACCOUNT);
AppEngineCredentials credentials =
Expand All @@ -109,7 +112,7 @@ void getAccount_sameAs() {
}

@Test
void sign_sameAs() {
public void sign_sameAs() throws IOException {
byte[] expectedSignature = {0xD, 0xE, 0xA, 0xD};
MockAppIdentityService appIdentity = new MockAppIdentityService();
appIdentity.setSignature(expectedSignature);
Expand All @@ -122,7 +125,7 @@ void sign_sameAs() {
}

@Test
void createScoped_clonesWithScopes() throws IOException {
public void createScoped_clonesWithScopes() throws IOException {
String expectedAccessToken = "ExpectedAccessToken";
Collection<String> emptyScopes = Collections.emptyList();

Expand All @@ -135,10 +138,11 @@ void createScoped_clonesWithScopes() throws IOException {
.setAppIdentityService(appIdentity)
.build();
assertTrue(credentials.createScopedRequired());
assertThrows(
Exception.class,
() -> credentials.getRequestMetadata(CALL_URI),
"Should not be able to use credential without scopes.");
try {
credentials.getRequestMetadata(CALL_URI);
fail("Should not be able to use credential without scopes.");
} catch (Exception expected) {
}
assertEquals(0, appIdentity.getGetAccessTokenCallCount());

GoogleCredentials scopedCredentials = credentials.createScoped(SCOPES);
Expand All @@ -151,7 +155,7 @@ void createScoped_clonesWithScopes() throws IOException {
}

@Test
void equals_true() {
public void equals_true() throws IOException {
Collection<String> emptyScopes = Collections.emptyList();
MockAppIdentityService appIdentity = new MockAppIdentityService();

Expand All @@ -171,7 +175,7 @@ void equals_true() {
}

@Test
void equals_false_scopes() {
public void equals_false_scopes() throws IOException {
Collection<String> emptyScopes = Collections.emptyList();
Collection<String> scopes = Collections.singleton("SomeScope");
MockAppIdentityService appIdentity = new MockAppIdentityService();
Expand All @@ -191,7 +195,7 @@ void equals_false_scopes() {
}

@Test
void toString_containsFields() {
public void toString_containsFields() throws IOException {
String expectedToString =
String.format(
"AppEngineCredentials{scopes=[%s], scopesRequired=%b, appIdentityServiceClassName=%s}",
Expand All @@ -209,7 +213,7 @@ void toString_containsFields() {
}

@Test
void hashCode_equals() {
public void hashCode_equals() throws IOException {
Collection<String> emptyScopes = Collections.emptyList();
MockAppIdentityService appIdentity = new MockAppIdentityService();
AppEngineCredentials credentials =
Expand All @@ -226,7 +230,7 @@ void hashCode_equals() {
}

@Test
void serialize() throws IOException, ClassNotFoundException {
public void serialize() throws IOException, ClassNotFoundException {
Collection<String> scopes = Collections.singleton("SomeScope");
MockAppIdentityService appIdentity = new MockAppIdentityService();
AppEngineCredentials credentials =
Expand All @@ -245,7 +249,7 @@ private static void assertContainsBearerToken(Map<String, List<String>> metadata
assertNotNull(token);
String expectedValue = "Bearer " + token;
List<String> authorizations = metadata.get("Authorization");
assertNotNull(authorizations, "Authorization headers not found");
assertTrue(authorizations.contains(expectedValue), "Bearer token not found");
assertNotNull("Authorization headers not found", authorizations);
assertTrue("Bearer token not found", authorizations.contains(expectedValue));
}
}
4 changes: 2 additions & 2 deletions appengine/pom.xml
Expand Up @@ -68,8 +68,8 @@
<artifactId>guava</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
23 changes: 12 additions & 11 deletions credentials/javatests/com/google/auth/SigningExceptionTest.java
Expand Up @@ -31,44 +31,45 @@

package com.google.auth;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;

import com.google.auth.ServiceAccountSigner.SigningException;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import org.junit.Test;

class SigningExceptionTest {
public class SigningExceptionTest {

private static final String EXPECTED_MESSAGE = "message";
private static final RuntimeException EXPECTED_CAUSE = new RuntimeException();

@Test
void constructor() {
public void constructor() {
SigningException signingException = new SigningException(EXPECTED_MESSAGE, EXPECTED_CAUSE);
assertEquals(EXPECTED_MESSAGE, signingException.getMessage());
assertSame(EXPECTED_CAUSE, signingException.getCause());
}

@Test
void equals_true() {
public void equals_true() throws IOException {
SigningException signingException = new SigningException(EXPECTED_MESSAGE, EXPECTED_CAUSE);
SigningException otherSigningException = new SigningException(EXPECTED_MESSAGE, EXPECTED_CAUSE);
assertTrue(signingException.equals(otherSigningException));
assertTrue(otherSigningException.equals(signingException));
}

@Test
void equals_false_message() {
public void equals_false_message() throws IOException {
SigningException signingException = new SigningException(EXPECTED_MESSAGE, EXPECTED_CAUSE);
SigningException otherSigningException = new SigningException("otherMessage", EXPECTED_CAUSE);
assertFalse(signingException.equals(otherSigningException));
assertFalse(otherSigningException.equals(signingException));
}

@Test
void equals_false_cause() {
public void equals_false_cause() throws IOException {
SigningException signingException = new SigningException(EXPECTED_MESSAGE, EXPECTED_CAUSE);
SigningException otherSigningException =
new SigningException("otherMessage", new RuntimeException());
Expand All @@ -77,7 +78,7 @@ void equals_false_cause() {
}

@Test
void hashCode_equals() {
public void hashCode_equals() throws IOException {
SigningException signingException = new SigningException(EXPECTED_MESSAGE, EXPECTED_CAUSE);
SigningException otherSigningException = new SigningException(EXPECTED_MESSAGE, EXPECTED_CAUSE);
assertEquals(signingException.hashCode(), otherSigningException.hashCode());
Expand Down
4 changes: 2 additions & 2 deletions credentials/pom.xml
Expand Up @@ -50,8 +50,8 @@

<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
Expand Up @@ -181,6 +181,7 @@ static ServiceAccountCredentials fromJson(
} catch (URISyntaxException e) {
throw new IOException("Token server URI specified in 'token_uri' could not be parsed.");
}

if (clientId == null
|| clientEmail == null
|| privateKeyPkcs8 == null
Expand Down
27 changes: 15 additions & 12 deletions oauth2_http/java/com/google/auth/oauth2/TokenVerifier.java
Expand Up @@ -314,17 +314,13 @@ public static class JsonWebKey {
public Map<String, PublicKey> load(String certificateUrl) throws Exception {
HttpTransport httpTransport = httpTransportFactory.create();
JsonWebKeySet jwks;
try {
HttpRequest request =
httpTransport
.createRequestFactory()
.buildGetRequest(new GenericUrl(certificateUrl))
.setParser(OAuth2Utils.JSON_FACTORY.createJsonObjectParser());
HttpResponse response = request.execute();
jwks = response.parseAs(JsonWebKeySet.class);
} catch (IOException io) {
return ImmutableMap.of();
}
HttpRequest request =
httpTransport
.createRequestFactory()
.buildGetRequest(new GenericUrl(certificateUrl))
.setParser(OAuth2Utils.JSON_FACTORY.createJsonObjectParser());
HttpResponse response = request.execute();
jwks = response.parseAs(JsonWebKeySet.class);

ImmutableMap.Builder<String, PublicKey> keyCacheBuilder = new ImmutableMap.Builder<>();
if (jwks.keys == null) {
Expand All @@ -345,7 +341,14 @@ public Map<String, PublicKey> load(String certificateUrl) throws Exception {
}
}

return keyCacheBuilder.build();
ImmutableMap<String, PublicKey> keyCache = keyCacheBuilder.build();

if (keyCache.isEmpty()) {
throw new VerificationException(
"No valid public key returned by the keystore: " + certificateUrl);
}

return keyCache;
}

private PublicKey buildPublicKey(JsonWebKey key)
Expand Down
22 changes: 13 additions & 9 deletions oauth2_http/javatests/com/google/auth/TestUtils.java
Expand Up @@ -31,9 +31,9 @@

package com.google.auth;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import com.google.api.client.http.HttpHeaders;
import com.google.api.client.http.HttpResponseException;
Expand All @@ -46,8 +46,8 @@
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
Expand All @@ -64,31 +64,35 @@ public class TestUtils {
public static void assertContainsBearerToken(Map<String, List<String>> metadata, String token) {
assertNotNull(metadata);
assertNotNull(token);
assertTrue(hasBearerToken(metadata, token), "Bearer token not found");
assertTrue("Bearer token not found", hasBearerToken(metadata, token));
}

public static void assertNotContainsBearerToken(
Map<String, List<String>> metadata, String token) {
assertNotNull(metadata);
assertNotNull(token);
assertFalse(hasBearerToken(metadata, token), "Bearer token found");
assertFalse("Bearer token found", hasBearerToken(metadata, token));
}

private static boolean hasBearerToken(Map<String, List<String>> metadata, String token) {
String expectedValue = AuthHttpConstants.BEARER + " " + token;
List<String> authorizations = metadata.get(AuthHttpConstants.AUTHORIZATION);
assertNotNull(authorizations, "Authorization headers not found");
assertNotNull("Authorization headers not found", authorizations);
return authorizations.contains(expectedValue);
}

public static InputStream jsonToInputStream(GenericJson json) throws IOException {
json.setFactory(JSON_FACTORY);
String text = json.toPrettyString();
return new ByteArrayInputStream(text.getBytes(StandardCharsets.UTF_8));
return new ByteArrayInputStream(text.getBytes("UTF-8"));
}

public static InputStream stringToInputStream(String text) {
return new ByteArrayInputStream(text.getBytes(StandardCharsets.UTF_8));
try {
return new ByteArrayInputStream(text.getBytes("UTF-8"));
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("Unexpected encoding exception", e);
}
}

public static Map<String, String> parseQuery(String query) throws IOException {
Expand Down

0 comments on commit 96bc155

Please sign in to comment.