Skip to content

Commit af94b53

Browse files
author
Ajay Kannan
committed
cleanup + cast fix
1 parent 12078e0 commit af94b53

2 files changed

Lines changed: 19 additions & 21 deletions

File tree

gcloud-java-core/src/main/java/com/google/gcloud/AuthCredentials.java

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ private static class AppEngineCredentials extends GoogleCredentials {
4646
private final Method getAccessToken;
4747
private final Method getAccessTokenResult;
4848
private final Collection<String> scopes;
49-
private final boolean scopesRequired;
5049

5150
AppEngineCredentials() {
5251
try {
@@ -61,19 +60,16 @@ private static class AppEngineCredentials extends GoogleCredentials {
6160
this.getAccessTokenResult = serviceClass.getMethod("getAccessToken", Iterable.class);
6261
this.getAccessToken = tokenResultClass.getMethod("getAccessToken");
6362
this.scopes = null;
64-
this.scopesRequired = true;
6563
} catch (Exception e) {
66-
throw new RuntimeException("Could not create AppEngineCredentials using reflection.");
64+
throw new RuntimeException("Could not create AppEngineCredentials.", e);
6765
}
6866
}
6967

70-
AppEngineCredentials(Collection<String> scopes, Object appIdentityService,
71-
Method getAccessToken, Method getAccessTokenResult) {
72-
this.appIdentityService = appIdentityService;
73-
this.getAccessToken = getAccessToken;
74-
this.getAccessTokenResult = getAccessTokenResult;
68+
AppEngineCredentials(Collection<String> scopes, AppEngineCredentials unscoped) {
69+
this.appIdentityService = unscoped.appIdentityService;
70+
this.getAccessToken = unscoped.getAccessToken;
71+
this.getAccessTokenResult = unscoped.getAccessTokenResult;
7572
this.scopes = scopes;
76-
this.scopesRequired = (scopes == null || scopes.isEmpty());
7773
}
7874

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

9692
@Override
9793
public boolean createScopedRequired() {
98-
return scopesRequired;
94+
return scopes == null || scopes.isEmpty();
9995
}
10096

10197
@Override
10298
public GoogleCredentials createScoped(Collection<String> scopes) {
103-
return new AppEngineCredentials(
104-
scopes, appIdentityService, getAccessToken, getAccessTokenResult);
99+
return new AppEngineCredentials(scopes, this);
105100
}
106101
}
107102

@@ -308,6 +303,7 @@ public static ServiceAccountAuthCredentials createForJson(InputStream jsonCreden
308303
tempServiceAccountCredentials.getClientEmail(),
309304
tempServiceAccountCredentials.getPrivateKey());
310305
}
311-
throw new IOException("The given JSON Credentials Stream is not a service account credential.");
306+
throw new IOException(
307+
"The given JSON Credentials Stream is not for a service account credential.");
312308
}
313309
}

gcloud-java-storage/src/main/java/com/google/gcloud/storage/StorageImpl.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -562,16 +562,18 @@ public URL signUrl(BlobInfo blobInfo, long duration, TimeUnit unit, SignUrlOptio
562562
for (SignUrlOption option : options) {
563563
optionMap.put(option.option(), option.value());
564564
}
565-
ServiceAccountAuthCredentials cred =
565+
ServiceAccountAuthCredentials serviceAccountAuthCred =
566566
(ServiceAccountAuthCredentials) optionMap.get(SignUrlOption.Option.SERVICE_ACCOUNT_CRED);
567-
if (cred == null) {
568-
AuthCredentials authCredentials = this.options().authCredentials();
567+
ServiceAccountCredentials cred = (ServiceAccountCredentials) (serviceAccountAuthCred != null
568+
? serviceAccountAuthCred.credentials() : null);
569+
if (serviceAccountAuthCred == null) {
570+
AuthCredentials authCred = this.options().authCredentials();
569571
GoogleCredentials serviceCred =
570-
authCredentials != null ? authCredentials.credentials() : null;
572+
authCred != null ? authCred.credentials() : null;
571573
checkArgument(
572574
serviceCred instanceof ServiceAccountCredentials,
573575
"Signing key was not provided and could not be derived");
574-
cred = (ServiceAccountAuthCredentials) authCredentials;
576+
cred = (ServiceAccountCredentials) serviceCred;
575577
}
576578
// construct signature - see https://cloud.google.com/storage/docs/access-control#Signed-URLs
577579
StringBuilder stBuilder = new StringBuilder();
@@ -607,12 +609,12 @@ public URL signUrl(BlobInfo blobInfo, long duration, TimeUnit unit, SignUrlOptio
607609
stBuilder.append(path);
608610
try {
609611
Signature signer = Signature.getInstance("SHA256withRSA");
610-
signer.initSign(cred.privateKey());
612+
signer.initSign(cred.getPrivateKey());
611613
signer.update(stBuilder.toString().getBytes(UTF_8));
612614
String signature =
613615
URLEncoder.encode(BaseEncoding.base64().encode(signer.sign()), UTF_8.name());
614616
stBuilder = new StringBuilder("https://storage.googleapis.com").append(path);
615-
stBuilder.append("?GoogleAccessId=").append(cred.account());
617+
stBuilder.append("?GoogleAccessId=").append(cred.getClientEmail());
616618
stBuilder.append("&Expires=").append(expiration);
617619
stBuilder.append("&Signature=").append(signature);
618620
return new URL(stBuilder.toString());

0 commit comments

Comments
 (0)