Skip to content

Commit d638926

Browse files
committed
Merge pull request #180 from mziccard/auth-credentials-from-json
Create AuthCredentials from JSON stream
2 parents 1318e33 + 210355d commit d638926

File tree

1 file changed

+36
-4
lines changed

1 file changed

+36
-4
lines changed

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

+36-4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import com.google.auth.oauth2.GoogleCredentials;
3131

3232
import java.io.IOException;
33+
import java.io.InputStream;
3334
import java.io.ObjectInputStream;
3435
import java.io.ObjectStreamException;
3536
import java.io.Serializable;
@@ -133,7 +134,7 @@ private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundE
133134
try {
134135
computeCredential = getComputeCredential();
135136
} catch (GeneralSecurityException e) {
136-
throw new IOException(e);
137+
throw new IOException(e);
137138
}
138139
}
139140

@@ -156,7 +157,7 @@ private static class ApplicationDefaultAuthCredentials extends AuthCredentials {
156157

157158
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
158159
in.defaultReadObject();
159-
googleCredentials = GoogleCredentials.getApplicationDefault();
160+
googleCredentials = GoogleCredentials.getApplicationDefault();
160161
}
161162

162163
@Override
@@ -183,8 +184,8 @@ public static AuthCredentials createForComputeEngine()
183184
*
184185
* <p>Returns the Application Default Credentials which are credentials that identify and
185186
* authorize the whole application. This is the built-in service account if running on
186-
* Google Compute Engine or the credentials file from the path in the environment variable
187-
* GOOGLE_APPLICATION_CREDENTIALS.
187+
* Google Compute Engine or the credentials file can be read from the path in the environment
188+
* variable GOOGLE_APPLICATION_CREDENTIALS.
188189
* </p>
189190
*
190191
* @return the credentials instance.
@@ -194,10 +195,41 @@ public static AuthCredentials createApplicationDefaults() throws IOException {
194195
return new ApplicationDefaultAuthCredentials();
195196
}
196197

198+
/**
199+
* Creates Service Account Credentials given an account id and a private key.
200+
*
201+
* <p>For details on how to obtain Service Account Credentials see
202+
* <a href="https://cloud.google.com/storage/docs/authentication?hl=en#service_accounts">Service
203+
* Account Authentication</a>.
204+
* </p>
205+
*
206+
* @param account id of the Service Account
207+
* @param privateKey private key associated to the account
208+
* @return the credentials instance.
209+
*/
197210
public static ServiceAccountAuthCredentials createFor(String account, PrivateKey privateKey) {
198211
return new ServiceAccountAuthCredentials(account, privateKey);
199212
}
200213

214+
/**
215+
* Creates Service Account Credentials given a stream for credentials in JSON format.
216+
*
217+
* <p>For details on how to obtain Service Account Credentials in JSON format see
218+
* <a href="https://cloud.google.com/storage/docs/authentication?hl=en#service_accounts">Service
219+
* Account Authentication</a>.
220+
* </p>
221+
*
222+
* @param jsonCredentialStream stream for Service Account Credentials in JSON format
223+
* @return the credentials instance.
224+
* @throws IOException if the credentials cannot be created from the stream.
225+
*/
226+
public static ServiceAccountAuthCredentials createForJson(InputStream jsonCredentialStream)
227+
throws IOException {
228+
GoogleCredential tempCredentials = GoogleCredential.fromStream(jsonCredentialStream);
229+
return new ServiceAccountAuthCredentials(tempCredentials.getServiceAccountId(),
230+
tempCredentials.getServiceAccountPrivateKey());
231+
}
232+
201233
public static AuthCredentials noCredentials() {
202234
return ServiceAccountAuthCredentials.NO_CREDENTIALS;
203235
}

0 commit comments

Comments
 (0)