Skip to content

Commit

Permalink
cleanup + cast fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Ajay Kannan committed Dec 1, 2015
1 parent 12078e0 commit af94b53
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ private static class AppEngineCredentials extends GoogleCredentials {
private final Method getAccessToken;
private final Method getAccessTokenResult;
private final Collection<String> scopes;
private final boolean scopesRequired;

AppEngineCredentials() {
try {
Expand All @@ -61,19 +60,16 @@ private static class AppEngineCredentials extends GoogleCredentials {
this.getAccessTokenResult = serviceClass.getMethod("getAccessToken", Iterable.class);
this.getAccessToken = tokenResultClass.getMethod("getAccessToken");
this.scopes = null;
this.scopesRequired = true;
} catch (Exception e) {
throw new RuntimeException("Could not create AppEngineCredentials using reflection.");
throw new RuntimeException("Could not create AppEngineCredentials.", e);
}
}

AppEngineCredentials(Collection<String> scopes, Object appIdentityService,
Method getAccessToken, Method getAccessTokenResult) {
this.appIdentityService = appIdentityService;
this.getAccessToken = getAccessToken;
this.getAccessTokenResult = getAccessTokenResult;
AppEngineCredentials(Collection<String> scopes, AppEngineCredentials unscoped) {
this.appIdentityService = unscoped.appIdentityService;
this.getAccessToken = unscoped.getAccessToken;
this.getAccessTokenResult = unscoped.getAccessTokenResult;
this.scopes = scopes;
this.scopesRequired = (scopes == null || scopes.isEmpty());
}

/**
Expand All @@ -89,19 +85,18 @@ public AccessToken refreshAccessToken() throws IOException {
String accessToken = (String) getAccessToken.invoke(accessTokenResult);
return new AccessToken(accessToken, null);
} catch (Exception e) {
throw new RuntimeException("Could not get the access token using reflection.");
throw new IOException("Could not get the access token.", e);
}
}

@Override
public boolean createScopedRequired() {
return scopesRequired;
return scopes == null || scopes.isEmpty();
}

@Override
public GoogleCredentials createScoped(Collection<String> scopes) {
return new AppEngineCredentials(
scopes, appIdentityService, getAccessToken, getAccessTokenResult);
return new AppEngineCredentials(scopes, this);
}
}

Expand Down Expand Up @@ -308,6 +303,7 @@ public static ServiceAccountAuthCredentials createForJson(InputStream jsonCreden
tempServiceAccountCredentials.getClientEmail(),
tempServiceAccountCredentials.getPrivateKey());
}
throw new IOException("The given JSON Credentials Stream is not a service account credential.");
throw new IOException(
"The given JSON Credentials Stream is not for a service account credential.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -562,16 +562,18 @@ public URL signUrl(BlobInfo blobInfo, long duration, TimeUnit unit, SignUrlOptio
for (SignUrlOption option : options) {
optionMap.put(option.option(), option.value());
}
ServiceAccountAuthCredentials cred =
ServiceAccountAuthCredentials serviceAccountAuthCred =
(ServiceAccountAuthCredentials) optionMap.get(SignUrlOption.Option.SERVICE_ACCOUNT_CRED);
if (cred == null) {
AuthCredentials authCredentials = this.options().authCredentials();
ServiceAccountCredentials cred = (ServiceAccountCredentials) (serviceAccountAuthCred != null
? serviceAccountAuthCred.credentials() : null);
if (serviceAccountAuthCred == null) {
AuthCredentials authCred = this.options().authCredentials();
GoogleCredentials serviceCred =
authCredentials != null ? authCredentials.credentials() : null;
authCred != null ? authCred.credentials() : null;
checkArgument(
serviceCred instanceof ServiceAccountCredentials,
"Signing key was not provided and could not be derived");
cred = (ServiceAccountAuthCredentials) authCredentials;
cred = (ServiceAccountCredentials) serviceCred;
}
// construct signature - see https://cloud.google.com/storage/docs/access-control#Signed-URLs
StringBuilder stBuilder = new StringBuilder();
Expand Down Expand Up @@ -607,12 +609,12 @@ public URL signUrl(BlobInfo blobInfo, long duration, TimeUnit unit, SignUrlOptio
stBuilder.append(path);
try {
Signature signer = Signature.getInstance("SHA256withRSA");
signer.initSign(cred.privateKey());
signer.initSign(cred.getPrivateKey());
signer.update(stBuilder.toString().getBytes(UTF_8));
String signature =
URLEncoder.encode(BaseEncoding.base64().encode(signer.sign()), UTF_8.name());
stBuilder = new StringBuilder("https://storage.googleapis.com").append(path);
stBuilder.append("?GoogleAccessId=").append(cred.account());
stBuilder.append("?GoogleAccessId=").append(cred.getClientEmail());
stBuilder.append("&Expires=").append(expiration);
stBuilder.append("&Signature=").append(signature);
return new URL(stBuilder.toString());
Expand Down

0 comments on commit af94b53

Please sign in to comment.