30
30
import com .google .auth .oauth2 .GoogleCredentials ;
31
31
32
32
import java .io .IOException ;
33
+ import java .io .InputStream ;
33
34
import java .io .ObjectInputStream ;
34
35
import java .io .ObjectStreamException ;
35
36
import java .io .Serializable ;
@@ -133,7 +134,7 @@ private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundE
133
134
try {
134
135
computeCredential = getComputeCredential ();
135
136
} catch (GeneralSecurityException e ) {
136
- throw new IOException (e );
137
+ throw new IOException (e );
137
138
}
138
139
}
139
140
@@ -156,7 +157,7 @@ private static class ApplicationDefaultAuthCredentials extends AuthCredentials {
156
157
157
158
private void readObject (ObjectInputStream in ) throws IOException , ClassNotFoundException {
158
159
in .defaultReadObject ();
159
- googleCredentials = GoogleCredentials .getApplicationDefault ();
160
+ googleCredentials = GoogleCredentials .getApplicationDefault ();
160
161
}
161
162
162
163
@ Override
@@ -183,8 +184,8 @@ public static AuthCredentials createForComputeEngine()
183
184
*
184
185
* <p>Returns the Application Default Credentials which are credentials that identify and
185
186
* 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.
188
189
* </p>
189
190
*
190
191
* @return the credentials instance.
@@ -194,10 +195,41 @@ public static AuthCredentials createApplicationDefaults() throws IOException {
194
195
return new ApplicationDefaultAuthCredentials ();
195
196
}
196
197
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
+ */
197
210
public static ServiceAccountAuthCredentials createFor (String account , PrivateKey privateKey ) {
198
211
return new ServiceAccountAuthCredentials (account , privateKey );
199
212
}
200
213
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
+
201
233
public static AuthCredentials noCredentials () {
202
234
return ServiceAccountAuthCredentials .NO_CREDENTIALS ;
203
235
}
0 commit comments