Skip to content

Commit

Permalink
Merge pull request #60 from sduskis/master
Browse files Browse the repository at this point in the history
Adding expiration date to AppEngineCredentials.
  • Loading branch information
anthmgoogle committed Apr 8, 2016
2 parents eab5410 + a2585ae commit 3f70f8a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@
import com.google.auth.oauth2.GoogleCredentials;
import com.google.common.collect.ImmutableList;
import com.google.appengine.api.appidentity.AppIdentityService;
import com.google.appengine.api.appidentity.AppIdentityService.GetAccessTokenResult;
import com.google.appengine.api.appidentity.AppIdentityServiceFactory;

import java.io.IOException;
import java.util.Collection;
import java.util.Date;

/**
* OAuth2 credentials representing the built-in service account for Google App ENgine.
Expand Down Expand Up @@ -72,8 +74,10 @@ public AccessToken refreshAccessToken() throws IOException {
if (createScopedRequired()) {
throw new IOException("AppEngineCredentials requires createScoped call before use.");
}
String accessToken = appIdentityService.getAccessToken(scopes).getAccessToken();
return new AccessToken(accessToken, null);
GetAccessTokenResult accessTokenResponse = appIdentityService.getAccessToken(scopes);
String accessToken = accessTokenResponse.getAccessToken();
Date expirationTime = accessTokenResponse.getExpirationTime();
return new AccessToken(accessToken, expirationTime);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import static org.junit.Assert.assertNotSame;

import com.google.auth.Credentials;
import com.google.auth.oauth2.AccessToken;
import com.google.auth.oauth2.GoogleCredentials;

import org.junit.Test;
Expand All @@ -49,6 +50,7 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -76,6 +78,19 @@ public void constructor_usesAppIdentityService() throws IOException {
assertContainsBearerToken(metadata, expectedAccessToken);
}

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

MockAppIdentityService appIdentity = new MockAppIdentityService();
appIdentity.setAccessTokenText(expectedAccessToken);
appIdentity.setExpiration(new Date(System.currentTimeMillis() + 60L * 60L * 100L));
AppEngineCredentials credentials = new AppEngineCredentials(SCOPES, appIdentity);
AccessToken accessToken = credentials.refreshAccessToken();
assertEquals(appIdentity.getAccessTokenText(), accessToken.getTokenValue());
assertEquals(appIdentity.getExpiration(), accessToken.getExpirationTime());
}

@Test
public void createScoped_clonesWithScopes() throws IOException {
final String expectedAccessToken = "ExpectedAccessToken";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import com.google.appengine.api.appidentity.PublicCertificate;

import java.util.Collection;
import java.util.Date;

/**
* Mock implementation of AppIdentityService interface for testing.
Expand All @@ -44,6 +45,7 @@ public class MockAppIdentityService implements AppIdentityService {

private int getAccessTokenCallCount = 0;
private String accessTokenText = null;
private Date expiration = null;

public MockAppIdentityService() {
}
Expand All @@ -60,6 +62,14 @@ public void setAccessTokenText(String text) {
accessTokenText = text;
}

public Date getExpiration() {
return expiration;
}

public void setExpiration(Date expiration) {
this.expiration = expiration;
}

@Override
public SigningResult signForApp(byte[] signBlob) {
return null;
Expand All @@ -82,7 +92,7 @@ public GetAccessTokenResult getAccessToken(Iterable<String> scopes) {
if (scopeCount == 0) {
throw new AppIdentityServiceFailureException("No scopes specified.");
}
return new GetAccessTokenResult(accessTokenText, null);
return new GetAccessTokenResult(accessTokenText, expiration);
}

@Override
Expand Down

0 comments on commit 3f70f8a

Please sign in to comment.