Skip to content
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

Create AuthCredentials from JSON stream #180

Merged
merged 1 commit into from
Sep 25, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}

Expand All @@ -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
Expand All @@ -183,8 +184,8 @@ public static AuthCredentials createForComputeEngine()
*
* <p>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.
* </p>
*
* @return the credentials instance.
Expand All @@ -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.
*
* <p>For details on how to obtain Service Account Credentials see
* <a href="https://cloud.google.com/storage/docs/authentication?hl=en#service_accounts">Service
* Account Authentication</a>.
* </p>
*
* @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.
*
* <p>For details on how to obtain Service Account Credentials in JSON format see
* <a href="https://cloud.google.com/storage/docs/authentication?hl=en#service_accounts">Service
* Account Authentication</a>.
* </p>
*
* @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;
}
Expand Down