30
30
import com .google .auth .oauth2 .GoogleCredentials ;
31
31
32
32
import java .io .IOException ;
33
+ import java .io .ObjectInputStream ;
34
+ import java .io .ObjectStreamException ;
35
+ import java .io .Serializable ;
33
36
import java .security .GeneralSecurityException ;
34
37
import java .security .PrivateKey ;
38
+ import java .util .Objects ;
35
39
import java .util .Set ;
36
40
37
41
/**
38
42
* Credentials for accessing Google Cloud services.
39
43
*/
40
- public abstract class AuthCredentials {
44
+ public abstract class AuthCredentials implements Serializable {
45
+
46
+ private static final long serialVersionUID = 236297804453464604L ;
41
47
42
48
private static class AppEngineAuthCredentials extends AuthCredentials {
43
49
50
+ private static final long serialVersionUID = 7931300552744202954L ;
51
+
52
+ private static final AuthCredentials INSTANCE = new AppEngineAuthCredentials ();
53
+
44
54
@ Override
45
- protected HttpRequestInitializer httpRequestInitializer (
46
- HttpTransport transport , Set <String > scopes ) {
55
+ protected HttpRequestInitializer httpRequestInitializer (HttpTransport transport ,
56
+ Set <String > scopes ) {
47
57
return new AppIdentityCredential (scopes );
48
58
}
59
+
60
+ private Object readResolve () throws ObjectStreamException {
61
+ return INSTANCE ;
62
+ }
49
63
}
50
64
51
65
private static class ServiceAccountAuthCredentials extends AuthCredentials {
52
66
67
+ private static final long serialVersionUID = 8007708734318445901L ;
53
68
private final String account ;
54
69
private final PrivateKey privateKey ;
55
70
71
+ private static final AuthCredentials NO_CREDENTIALS = new ServiceAccountAuthCredentials ();
72
+
56
73
ServiceAccountAuthCredentials (String account , PrivateKey privateKey ) {
57
74
this .account = checkNotNull (account );
58
75
this .privateKey = checkNotNull (privateKey );
@@ -76,55 +93,106 @@ protected HttpRequestInitializer httpRequestInitializer(
76
93
}
77
94
return builder .build ();
78
95
}
96
+
97
+ @ Override
98
+ public int hashCode () {
99
+ return Objects .hash (account , privateKey );
100
+ }
101
+
102
+ @ Override
103
+ public boolean equals (Object obj ) {
104
+ if (!(obj instanceof ServiceAccountAuthCredentials )) {
105
+ return false ;
106
+ }
107
+ ServiceAccountAuthCredentials other = (ServiceAccountAuthCredentials ) obj ;
108
+ return Objects .equals (account , other .account )
109
+ && Objects .equals (privateKey , other .privateKey );
110
+ }
111
+ }
112
+
113
+ private static class ComputeEngineAuthCredentials extends AuthCredentials {
114
+
115
+ private static final long serialVersionUID = -5217355402127260144L ;
116
+
117
+ private transient ComputeCredential computeCredential ;
118
+
119
+ ComputeEngineAuthCredentials () throws IOException , GeneralSecurityException {
120
+ computeCredential = getComputeCredential ();
121
+ }
122
+
123
+ private void readObject (ObjectInputStream in ) throws IOException , ClassNotFoundException {
124
+ in .defaultReadObject ();
125
+ try {
126
+ computeCredential = getComputeCredential ();
127
+ } catch (GeneralSecurityException e ) {
128
+ throw new IOException (e );
129
+ }
130
+ }
131
+
132
+ @ Override
133
+ protected HttpRequestInitializer httpRequestInitializer (HttpTransport transport ,
134
+ Set <String > scopes ) {
135
+ return computeCredential ;
136
+ }
137
+ }
138
+
139
+ private static class ApplicationDefaultAuthCredentials extends AuthCredentials {
140
+
141
+ private static final long serialVersionUID = -8306873864136099893L ;
142
+
143
+ private transient GoogleCredentials googleCredentials ;
144
+
145
+ ApplicationDefaultAuthCredentials () throws IOException {
146
+ googleCredentials = GoogleCredentials .getApplicationDefault ();
147
+ }
148
+
149
+ private void readObject (ObjectInputStream in ) throws IOException , ClassNotFoundException {
150
+ in .defaultReadObject ();
151
+ googleCredentials = GoogleCredentials .getApplicationDefault ();
152
+ }
153
+
154
+ @ Override
155
+ protected HttpRequestInitializer httpRequestInitializer (HttpTransport transport ,
156
+ Set <String > scopes ) {
157
+ return new HttpCredentialsAdapter (googleCredentials );
158
+ }
79
159
}
80
160
81
161
protected abstract HttpRequestInitializer httpRequestInitializer (HttpTransport transport ,
82
162
Set <String > scopes );
83
163
84
164
public static AuthCredentials createForAppEngine () {
85
- return new AppEngineAuthCredentials () ;
165
+ return AppEngineAuthCredentials . INSTANCE ;
86
166
}
87
167
88
168
public static AuthCredentials createForComputeEngine ()
89
169
throws IOException , 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
- };
170
+ return new ComputeEngineAuthCredentials ();
98
171
}
99
172
100
173
/**
101
174
* Returns the Application Default Credentials.
102
175
*
103
- * <p>Returns the Application Default Credentials which are credentials that identify and
104
- * authorize the whole application. This is the built-in service account if running on Google
105
- * Compute Engine or the credentials file from the path in the environment variable
106
- * GOOGLE_APPLICATION_CREDENTIALS.</p>
176
+ * <p>
177
+ * Returns the Application Default Credentials which are credentials that identify and authorize
178
+ * the whole application. This is the built-in service account if running on Google Compute Engine
179
+ * or the credentials file from the path in the environment variable
180
+ * GOOGLE_APPLICATION_CREDENTIALS.
181
+ * </p>
107
182
*
108
183
* @return the credentials instance.
109
184
* @throws IOException if the credentials cannot be created in the current environment.
110
185
*/
111
186
public static AuthCredentials createApplicationDefaults () throws IOException {
112
- final GoogleCredentials credentials = GoogleCredentials .getApplicationDefault ();
113
- return new AuthCredentials () {
114
- @ Override
115
- protected HttpRequestInitializer httpRequestInitializer (HttpTransport transport ,
116
- Set <String > scopes ) {
117
- return new HttpCredentialsAdapter (credentials );
118
- }
119
- };
187
+ return new ApplicationDefaultAuthCredentials ();
120
188
}
121
189
122
190
public static AuthCredentials createFor (String account , PrivateKey privateKey ) {
123
191
return new ServiceAccountAuthCredentials (account , privateKey );
124
192
}
125
193
126
194
public static AuthCredentials noCredentials () {
127
- return new ServiceAccountAuthCredentials () ;
195
+ return ServiceAccountAuthCredentials . NO_CREDENTIALS ;
128
196
}
129
197
130
198
static ComputeCredential getComputeCredential () throws IOException , GeneralSecurityException {
0 commit comments