31
31
32
32
import java .io .IOException ;
33
33
import java .io .InputStream ;
34
- import java .io .ObjectInputStream ;
35
- import java .io .ObjectStreamException ;
36
34
import java .io .Serializable ;
37
35
import java .security .GeneralSecurityException ;
38
36
import java .security .PrivateKey ;
42
40
/**
43
41
* Credentials for accessing Google Cloud services.
44
42
*/
45
- public abstract class AuthCredentials implements Serializable {
46
-
47
- private static final long serialVersionUID = 236297804453464604L ;
43
+ public abstract class AuthCredentials implements Restorable <AuthCredentials > {
48
44
49
45
private static class AppEngineAuthCredentials extends AuthCredentials {
50
46
51
- private static final long serialVersionUID = 7931300552744202954L ;
52
-
53
47
private static final AuthCredentials INSTANCE = new AppEngineAuthCredentials ();
48
+ private static final AppEngineAuthCredentialsState STATE =
49
+ new AppEngineAuthCredentialsState ();
50
+
51
+ private static class AppEngineAuthCredentialsState
52
+ implements RestorableState <AuthCredentials >, Serializable {
53
+
54
+ private static final long serialVersionUID = 3558563960848658928L ;
55
+
56
+ @ Override
57
+ public AuthCredentials restore () {
58
+ return INSTANCE ;
59
+ }
60
+
61
+ @ Override
62
+ public int hashCode () {
63
+ return getClass ().getName ().hashCode ();
64
+ }
65
+
66
+ @ Override
67
+ public boolean equals (Object obj ) {
68
+ return obj instanceof AppEngineAuthCredentialsState ;
69
+ }
70
+ }
54
71
55
72
@ Override
56
73
protected HttpRequestInitializer httpRequestInitializer (HttpTransport transport ,
57
74
Set <String > scopes ) {
58
75
return new AppIdentityCredential (scopes );
59
76
}
60
77
61
- private Object readResolve () throws ObjectStreamException {
62
- return INSTANCE ;
78
+ @ Override
79
+ public RestorableState <AuthCredentials > capture () {
80
+ return STATE ;
63
81
}
64
82
}
65
83
66
84
public static class ServiceAccountAuthCredentials extends AuthCredentials {
67
85
68
- private static final long serialVersionUID = 8007708734318445901L ;
69
86
private final String account ;
70
87
private final PrivateKey privateKey ;
71
88
72
89
private static final AuthCredentials NO_CREDENTIALS = new ServiceAccountAuthCredentials ();
73
90
91
+ private static class ServiceAccountAuthCredentialsState
92
+ implements RestorableState <AuthCredentials >, Serializable {
93
+
94
+ private static final long serialVersionUID = -7302180782414633639L ;
95
+
96
+ private final String account ;
97
+ private final PrivateKey privateKey ;
98
+
99
+ private ServiceAccountAuthCredentialsState (String account , PrivateKey privateKey ) {
100
+ this .account = account ;
101
+ this .privateKey = privateKey ;
102
+ }
103
+
104
+ @ Override
105
+ public AuthCredentials restore () {
106
+ if (account == null && privateKey == null ) {
107
+ return NO_CREDENTIALS ;
108
+ }
109
+ return new ServiceAccountAuthCredentials (account , privateKey );
110
+ }
111
+
112
+ @ Override
113
+ public int hashCode () {
114
+ return Objects .hash (account , privateKey );
115
+ }
116
+
117
+ @ Override
118
+ public boolean equals (Object obj ) {
119
+ if (!(obj instanceof ServiceAccountAuthCredentialsState )) {
120
+ return false ;
121
+ }
122
+ ServiceAccountAuthCredentialsState other = (ServiceAccountAuthCredentialsState ) obj ;
123
+ return Objects .equals (account , other .account )
124
+ && Objects .equals (privateKey , other .privateKey );
125
+ }
126
+ }
127
+
74
128
ServiceAccountAuthCredentials (String account , PrivateKey privateKey ) {
75
129
this .account = checkNotNull (account );
76
130
this .privateKey = checkNotNull (privateKey );
@@ -104,59 +158,94 @@ public PrivateKey privateKey() {
104
158
}
105
159
106
160
@ Override
107
- public int hashCode () {
108
- return Objects .hash (account , privateKey );
109
- }
110
-
111
- @ Override
112
- public boolean equals (Object obj ) {
113
- if (!(obj instanceof ServiceAccountAuthCredentials )) {
114
- return false ;
115
- }
116
- ServiceAccountAuthCredentials other = (ServiceAccountAuthCredentials ) obj ;
117
- return Objects .equals (account , other .account )
118
- && Objects .equals (privateKey , other .privateKey );
161
+ public RestorableState <AuthCredentials > capture () {
162
+ return new ServiceAccountAuthCredentialsState (account , privateKey );
119
163
}
120
164
}
121
165
122
166
private static class ComputeEngineAuthCredentials extends AuthCredentials {
123
167
124
- private static final long serialVersionUID = - 5217355402127260144L ;
168
+ private ComputeCredential computeCredential ;
125
169
126
- private transient ComputeCredential computeCredential ;
170
+ private static final ComputeEngineAuthCredentialsState STATE =
171
+ new ComputeEngineAuthCredentialsState ();
127
172
128
- ComputeEngineAuthCredentials () throws IOException , GeneralSecurityException {
129
- computeCredential = getComputeCredential ();
130
- }
173
+ private static class ComputeEngineAuthCredentialsState
174
+ implements RestorableState <AuthCredentials >, Serializable {
175
+
176
+ private static final long serialVersionUID = -6168594072854417404L ;
177
+
178
+ @ Override
179
+ public AuthCredentials restore () {
180
+ try {
181
+ return new ComputeEngineAuthCredentials ();
182
+ } catch (IOException | GeneralSecurityException e ) {
183
+ throw new IllegalStateException (
184
+ "Could not restore " + ComputeEngineAuthCredentials .class .getSimpleName (), e );
185
+ }
186
+ }
187
+
188
+ @ Override
189
+ public int hashCode () {
190
+ return getClass ().getName ().hashCode ();
191
+ }
131
192
132
- private void readObject (ObjectInputStream in ) throws IOException , ClassNotFoundException {
133
- in .defaultReadObject ();
134
- try {
135
- computeCredential = getComputeCredential ();
136
- } catch (GeneralSecurityException e ) {
137
- throw new IOException (e );
193
+ @ Override
194
+ public boolean equals (Object obj ) {
195
+ return obj instanceof ComputeEngineAuthCredentialsState ;
138
196
}
139
197
}
140
198
199
+ ComputeEngineAuthCredentials () throws IOException , GeneralSecurityException {
200
+ computeCredential = getComputeCredential ();
201
+ }
202
+
141
203
@ Override
142
204
protected HttpRequestInitializer httpRequestInitializer (HttpTransport transport ,
143
205
Set <String > scopes ) {
144
206
return computeCredential ;
145
207
}
208
+
209
+ @ Override
210
+ public RestorableState <AuthCredentials > capture () {
211
+ return STATE ;
212
+ }
146
213
}
147
214
148
215
private static class ApplicationDefaultAuthCredentials extends AuthCredentials {
149
216
150
- private static final long serialVersionUID = - 8306873864136099893L ;
217
+ private GoogleCredentials googleCredentials ;
151
218
152
- private transient GoogleCredentials googleCredentials ;
219
+ private static final ApplicationDefaultAuthCredentialsState STATE =
220
+ new ApplicationDefaultAuthCredentialsState ();
153
221
154
- ApplicationDefaultAuthCredentials () throws IOException {
155
- googleCredentials = GoogleCredentials .getApplicationDefault ();
222
+ private static class ApplicationDefaultAuthCredentialsState
223
+ implements RestorableState <AuthCredentials >, Serializable {
224
+
225
+ private static final long serialVersionUID = -8839085552021212257L ;
226
+
227
+ @ Override
228
+ public AuthCredentials restore () {
229
+ try {
230
+ return new ApplicationDefaultAuthCredentials ();
231
+ } catch (IOException e ) {
232
+ throw new IllegalStateException (
233
+ "Could not restore " + ApplicationDefaultAuthCredentials .class .getSimpleName (), e );
234
+ }
235
+ }
236
+
237
+ @ Override
238
+ public int hashCode () {
239
+ return getClass ().getName ().hashCode ();
240
+ }
241
+
242
+ @ Override
243
+ public boolean equals (Object obj ) {
244
+ return obj instanceof ApplicationDefaultAuthCredentialsState ;
245
+ }
156
246
}
157
247
158
- private void readObject (ObjectInputStream in ) throws IOException , ClassNotFoundException {
159
- in .defaultReadObject ();
248
+ ApplicationDefaultAuthCredentials () throws IOException {
160
249
googleCredentials = GoogleCredentials .getApplicationDefault ();
161
250
}
162
251
@@ -165,6 +254,11 @@ protected HttpRequestInitializer httpRequestInitializer(HttpTransport transport,
165
254
Set <String > scopes ) {
166
255
return new HttpCredentialsAdapter (googleCredentials );
167
256
}
257
+
258
+ @ Override
259
+ public RestorableState <AuthCredentials > capture () {
260
+ return STATE ;
261
+ }
168
262
}
169
263
170
264
protected abstract HttpRequestInitializer httpRequestInitializer (HttpTransport transport ,
0 commit comments