Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1.2k
Remove http initializer from AuthCredentials#430
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ajkannan
merged 6 commits into
googleapis:master
from
ajkannan:remove-initializer-dependencyDec 2, 2015
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
57d6e3f
Remove dependency on http initializer in AuthCredentials and remove d…
b5c1cae
Use null instead of NO_CREDENTIALS
12078e0
Make AppEngineAuthCredentials Restorable
af94b53
cleanup + cast fix
18cbb77
style fix
942ceb4
more style fixes
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
135 changes: 81 additions & 54 deletions
135 gcloud-java-core/src/main/java/com/google/gcloud/AuthCredentials.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -18,21 +18,17 @@ | ||
| import static com.google.common.base.Preconditions.checkNotNull; | ||
| import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; | ||
| import com.google.api.client.googleapis.extensions.appengine.auth.oauth2.AppIdentityCredential; | ||
| import com.google.api.client.http.HttpRequestInitializer; | ||
| import com.google.api.client.http.HttpTransport; | ||
| import com.google.api.client.json.jackson.JacksonFactory; | ||
| import com.google.auth.http.HttpCredentialsAdapter; | ||
| import com.google.auth.oauth2.AccessToken; | ||
| import com.google.auth.oauth2.GoogleCredentials; | ||
| import com.google.auth.oauth2.ServiceAccountCredentials; | ||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.io.Serializable; | ||
| import java.lang.reflect.Method; | ||
| import java.security.PrivateKey; | ||
| import java.util.Collection; | ||
| import java.util.Objects; | ||
| import java.util.Set; | ||
| /** | ||
| * Credentials for accessing Google Cloud services. | ||
| @@ -42,8 +38,67 @@ public abstract class AuthCredentials implements Restorable<AuthCredentials> { | ||
| private static class AppEngineAuthCredentials extends AuthCredentials { | ||
| private static final AuthCredentials INSTANCE = new AppEngineAuthCredentials(); | ||
| private static final AppEngineAuthCredentialsState STATE = | ||
| new AppEngineAuthCredentialsState(); | ||
| private static final AppEngineAuthCredentialsState STATE = new AppEngineAuthCredentialsState(); | ||
| private static class AppEngineCredentials extends GoogleCredentials { | ||
| private final Object appIdentityService; | ||
| private final Method getAccessToken; | ||
| private final Method getAccessTokenResult; | ||
| private final Collection<String> scopes; | ||
| AppEngineCredentials() { | ||
| try { | ||
| Class<?> factoryClass = | ||
| Class.forName("com.google.appengine.api.appidentity.AppIdentityServiceFactory"); | ||
This comment was marked as spam.Sorry, something went wrong. Uh oh!There was an error while loading. Please reload this page. | ||
| Method method = factoryClass.getMethod("getAppIdentityService"); | ||
| this.appIdentityService = method.invoke(null); | ||
| Class<?> serviceClass = | ||
| Class.forName("com.google.appengine.api.appidentity.AppIdentityService"); | ||
| Class<?> tokenResultClass = Class.forName( | ||
| "com.google.appengine.api.appidentity.AppIdentityService$GetAccessTokenResult"); | ||
| this.getAccessTokenResult = serviceClass.getMethod("getAccessToken", Iterable.class); | ||
| this.getAccessToken = tokenResultClass.getMethod("getAccessToken"); | ||
| this.scopes = null; | ||
| } catch (Exception e) { | ||
| throw new RuntimeException("Could not create AppEngineCredentials.", e); | ||
| } | ||
| } | ||
| AppEngineCredentials(Collection<String> scopes, AppEngineCredentials unscoped) { | ||
| this.appIdentityService = unscoped.appIdentityService; | ||
| this.getAccessToken = unscoped.getAccessToken; | ||
| this.getAccessTokenResult = unscoped.getAccessTokenResult; | ||
| this.scopes = scopes; | ||
| } | ||
| /** | ||
| * Refresh the access token by getting it from the App Identity service | ||
| */ | ||
| @Override | ||
| public AccessToken refreshAccessToken() throws IOException { | ||
| if (createScopedRequired()) { | ||
| throw new IOException("AppEngineCredentials requires createScoped call before use."); | ||
| } | ||
| try { | ||
| Object accessTokenResult = getAccessTokenResult.invoke(appIdentityService, scopes); | ||
| String accessToken = (String) getAccessToken.invoke(accessTokenResult); | ||
| return new AccessToken(accessToken, null); | ||
| } catch (Exception e) { | ||
| throw new IOException("Could not get the access token.", e); | ||
| } | ||
| } | ||
| @Override | ||
| public boolean createScopedRequired() { | ||
| return scopes == null || scopes.isEmpty(); | ||
| } | ||
| @Override | ||
| public GoogleCredentials createScoped(Collection<String> scopes) { | ||
| return new AppEngineCredentials(scopes, this); | ||
| } | ||
| } | ||
| private static class AppEngineAuthCredentialsState | ||
| implements RestorableState<AuthCredentials>, Serializable { | ||
| @@ -67,9 +122,8 @@ public boolean equals(Object obj) { | ||
| } | ||
| @Override | ||
| protected HttpRequestInitializer httpRequestInitializer(HttpTransport transport, | ||
| Set<String> scopes) { | ||
| return new AppIdentityCredential(scopes); | ||
| public GoogleCredentials credentials() { | ||
| return new AppEngineCredentials(); | ||
| } | ||
| @Override | ||
| @@ -83,8 +137,6 @@ public static class ServiceAccountAuthCredentials extends AuthCredentials { | ||
| private final String account; | ||
| private final PrivateKey privateKey; | ||
| private static final AuthCredentials NO_CREDENTIALS = new ServiceAccountAuthCredentials(); | ||
| private static class ServiceAccountAuthCredentialsState | ||
| implements RestorableState<AuthCredentials>, Serializable { | ||
| @@ -100,9 +152,6 @@ private ServiceAccountAuthCredentialsState(String account, PrivateKey privateKey | ||
| @Override | ||
| public AuthCredentials restore() { | ||
| if (account == null && privateKey == null) { | ||
| return NO_CREDENTIALS; | ||
| } | ||
| return new ServiceAccountAuthCredentials(account, privateKey); | ||
| } | ||
| @@ -127,23 +176,9 @@ public boolean equals(Object obj) { | ||
| this.privateKey = checkNotNull(privateKey); | ||
| } | ||
| ServiceAccountAuthCredentials() { | ||
| account = null; | ||
| privateKey = null; | ||
| } | ||
| @Override | ||
| protected HttpRequestInitializer httpRequestInitializer( | ||
| HttpTransport transport, Set<String> scopes) { | ||
| GoogleCredential.Builder builder = new GoogleCredential.Builder() | ||
| .setTransport(transport) | ||
| .setJsonFactory(new JacksonFactory()); | ||
| if (privateKey != null) { | ||
| builder.setServiceAccountPrivateKey(privateKey); | ||
| builder.setServiceAccountId(account); | ||
| builder.setServiceAccountScopes(scopes); | ||
| } | ||
| return builder.build(); | ||
| public ServiceAccountCredentials credentials() { | ||
| return new ServiceAccountCredentials(null, account, privateKey, null, null); | ||
| } | ||
| public String account() { | ||
| @@ -198,18 +233,8 @@ public boolean equals(Object obj) { | ||
| } | ||
| @Override | ||
| protected HttpRequestInitializer httpRequestInitializer(HttpTransport transport, | ||
| Set<String> scopes) { | ||
| return new HttpCredentialsAdapter(googleCredentials.createScoped(scopes)); | ||
| } | ||
| public ServiceAccountAuthCredentials toServiceAccountCredentials() { | ||
| if (googleCredentials instanceof ServiceAccountCredentials) { | ||
| ServiceAccountCredentials credentials = (ServiceAccountCredentials) googleCredentials; | ||
| return new ServiceAccountAuthCredentials(credentials.getClientEmail(), | ||
| credentials.getPrivateKey()); | ||
| } | ||
| return null; | ||
| public GoogleCredentials credentials() { | ||
| return googleCredentials; | ||
| } | ||
| @Override | ||
| @@ -218,8 +243,7 @@ public RestorableState<AuthCredentials> capture() { | ||
| } | ||
| } | ||
| protected abstract HttpRequestInitializer httpRequestInitializer(HttpTransport transport, | ||
| Set<String> scopes); | ||
| public abstract GoogleCredentials credentials(); | ||
| public static AuthCredentials createForAppEngine() { | ||
| return AppEngineAuthCredentials.INSTANCE; | ||
| @@ -271,12 +295,15 @@ public static ServiceAccountAuthCredentials createFor(String account, PrivateKey | ||
| */ | ||
| public static ServiceAccountAuthCredentials createForJson(InputStream jsonCredentialStream) | ||
| throws IOException { | ||
| GoogleCredential tempCredentials = GoogleCredential.fromStream(jsonCredentialStream); | ||
| return new ServiceAccountAuthCredentials(tempCredentials.getServiceAccountId(), | ||
| tempCredentials.getServiceAccountPrivateKey()); | ||
| } | ||
| public static AuthCredentials noCredentials() { | ||
| return ServiceAccountAuthCredentials.NO_CREDENTIALS; | ||
| GoogleCredentials tempCredentials = GoogleCredentials.fromStream(jsonCredentialStream); | ||
This comment was marked as spam.Sorry, something went wrong. Uh oh!There was an error while loading. Please reload this page. | ||
| if (tempCredentials instanceof ServiceAccountCredentials) { | ||
| ServiceAccountCredentials tempServiceAccountCredentials = | ||
| (ServiceAccountCredentials) tempCredentials; | ||
| return new ServiceAccountAuthCredentials( | ||
| tempServiceAccountCredentials.getClientEmail(), | ||
| tempServiceAccountCredentials.getPrivateKey()); | ||
| } | ||
| throw new IOException( | ||
| "The given JSON Credentials Stream is not for a service account credential."); | ||
| } | ||
| } | ||
20 changes: 12 additions & 8 deletions
20 gcloud-java-core/src/main/java/com/google/gcloud/ServiceOptions.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2 gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/SerializationTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 13 additions & 19 deletions
32 gcloud-java-storage/src/main/java/com/google/gcloud/storage/StorageImpl.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 1 addition & 3 deletions
4 gcloud-java-storage/src/test/java/com/google/gcloud/storage/SerializationTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 1 addition & 3 deletions
4 gcloud-java-storage/src/test/java/com/google/gcloud/storage/StorageImplTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.