3030import com .google .auth .oauth2 .GoogleCredentials ;
3131
3232import java .io .IOException ;
33+ import java .io .ObjectInputStream ;
34+ import java .io .Serializable ;
3335import java .security .GeneralSecurityException ;
3436import java .security .PrivateKey ;
3537import java .util .Set ;
3638
3739/**
3840 * Credentials for accessing Google Cloud services.
3941 */
40- public abstract class AuthCredentials {
42+ public abstract class AuthCredentials implements Serializable {
43+
44+ private static final long serialVersionUID = 236297804453464604L ;
4145
4246 private static class AppEngineAuthCredentials extends AuthCredentials {
4347
48+ private static final long serialVersionUID = 7931300552744202954L ;
49+
4450 @ Override
4551 protected HttpRequestInitializer httpRequestInitializer (HttpTransport transport ,
4652 Set <String > scopes ) {
@@ -50,6 +56,7 @@ protected HttpRequestInitializer httpRequestInitializer(HttpTransport transport,
5056
5157 private static class ServiceAccountAuthCredentials extends AuthCredentials {
5258
59+ private static final long serialVersionUID = 8007708734318445901L ;
5360 private final String account ;
5461 private final PrivateKey privateKey ;
5562
@@ -78,23 +85,64 @@ protected HttpRequestInitializer httpRequestInitializer(
7885 }
7986 }
8087
88+ private static class ComputeEngineAuthCredentials extends AuthCredentials {
89+
90+ private static final long serialVersionUID = -5217355402127260144L ;
91+
92+ private transient ComputeCredential computeCredential ;
93+
94+ ComputeEngineAuthCredentials () throws IOException , GeneralSecurityException {
95+ computeCredential = getComputeCredential ();
96+ }
97+
98+ private void readObject (ObjectInputStream in ) throws IOException , ClassNotFoundException {
99+ in .defaultReadObject ();
100+ try {
101+ computeCredential = getComputeCredential ();
102+ } catch (GeneralSecurityException e ) {
103+ throw new IOException (e );
104+ }
105+ }
106+
107+ @ Override
108+ protected HttpRequestInitializer httpRequestInitializer (HttpTransport transport ,
109+ Set <String > scopes ) {
110+ return computeCredential ;
111+ }
112+ }
113+
114+ private static class ApplicationDefaultAuthCredentials extends AuthCredentials {
115+
116+ private static final long serialVersionUID = -8306873864136099893L ;
117+
118+ private transient GoogleCredentials googleCredentials ;
119+
120+ ApplicationDefaultAuthCredentials () throws IOException {
121+ googleCredentials = GoogleCredentials .getApplicationDefault ();
122+ }
123+
124+ private void readObject (ObjectInputStream in ) throws IOException , ClassNotFoundException {
125+ in .defaultReadObject ();
126+ googleCredentials = GoogleCredentials .getApplicationDefault ();
127+ }
128+
129+ @ Override
130+ protected HttpRequestInitializer httpRequestInitializer (HttpTransport transport ,
131+ Set <String > scopes ) {
132+ return new HttpCredentialsAdapter (googleCredentials );
133+ }
134+ }
135+
81136 protected abstract HttpRequestInitializer httpRequestInitializer (HttpTransport transport ,
82137 Set <String > scopes );
83138
84139 public static AuthCredentials createForAppEngine () {
85140 return new AppEngineAuthCredentials ();
86141 }
87142
88- public static AuthCredentials createForComputeEngine () throws IOException ,
89- GeneralSecurityException {
90- final ComputeCredential cred = getComputeCredential ();
91- return new AuthCredentials () {
92- @ Override
93- protected HttpRequestInitializer httpRequestInitializer (HttpTransport transport ,
94- Set <String > scopes ) {
95- return cred ;
96- }
97- };
143+ public static AuthCredentials createForComputeEngine ()
144+ throws IOException , GeneralSecurityException {
145+ return new ComputeEngineAuthCredentials ();
98146 }
99147
100148 /**
@@ -111,14 +159,7 @@ protected HttpRequestInitializer httpRequestInitializer(HttpTransport transport,
111159 * @throws IOException if the credentials cannot be created in the current environment.
112160 */
113161 public static AuthCredentials createApplicationDefaults () throws IOException {
114- final GoogleCredentials credentials = GoogleCredentials .getApplicationDefault ();
115- return new AuthCredentials () {
116- @ Override
117- protected HttpRequestInitializer httpRequestInitializer (HttpTransport transport ,
118- Set <String > scopes ) {
119- return new HttpCredentialsAdapter (credentials );
120- }
121- };
162+ return new ApplicationDefaultAuthCredentials ();
122163 }
123164
124165 public static AuthCredentials createFor (String account , PrivateKey privateKey ) {
0 commit comments