diff --git a/gcloud-java-core/src/main/java/com/google/gcloud/AuthCredentials.java b/gcloud-java-core/src/main/java/com/google/gcloud/AuthCredentials.java index 13d91836ae4c..ffc54df77a90 100644 --- a/gcloud-java-core/src/main/java/com/google/gcloud/AuthCredentials.java +++ b/gcloud-java-core/src/main/java/com/google/gcloud/AuthCredentials.java @@ -30,6 +30,7 @@ import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; +import java.io.InputStream; import java.io.ObjectInputStream; import java.io.ObjectStreamException; import java.io.Serializable; @@ -133,7 +134,7 @@ private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundE try { computeCredential = getComputeCredential(); } catch (GeneralSecurityException e) { - throw new IOException(e); + throw new IOException(e); } } @@ -156,7 +157,7 @@ private static class ApplicationDefaultAuthCredentials extends AuthCredentials { private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { in.defaultReadObject(); - googleCredentials = GoogleCredentials.getApplicationDefault(); + googleCredentials = GoogleCredentials.getApplicationDefault(); } @Override @@ -183,8 +184,8 @@ public static AuthCredentials createForComputeEngine() * *

Returns the Application Default Credentials which are credentials that identify and * authorize the whole application. This is the built-in service account if running on - * Google Compute Engine or the credentials file from the path in the environment variable - * GOOGLE_APPLICATION_CREDENTIALS. + * Google Compute Engine or the credentials file can be read from the path in the environment + * variable GOOGLE_APPLICATION_CREDENTIALS. *

* * @return the credentials instance. @@ -194,10 +195,41 @@ public static AuthCredentials createApplicationDefaults() throws IOException { return new ApplicationDefaultAuthCredentials(); } + /** + * Creates Service Account Credentials given an account id and a private key. + * + *

For details on how to obtain Service Account Credentials see + * Service + * Account Authentication. + *

+ * + * @param account id of the Service Account + * @param privateKey private key associated to the account + * @return the credentials instance. + */ public static ServiceAccountAuthCredentials createFor(String account, PrivateKey privateKey) { return new ServiceAccountAuthCredentials(account, privateKey); } + /** + * Creates Service Account Credentials given a stream for credentials in JSON format. + * + *

For details on how to obtain Service Account Credentials in JSON format see + * Service + * Account Authentication. + *

+ * + * @param jsonCredentialStream stream for Service Account Credentials in JSON format + * @return the credentials instance. + * @throws IOException if the credentials cannot be created from the stream. + */ + 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; }