-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGoogleIdTokenVerifier.java
325 lines (293 loc) · 10.1 KB
/
GoogleIdTokenVerifier.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*
* Copyright 2012 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package com.google.api.client.googleapis.auth.oauth2;
import com.google.api.client.auth.openidconnect.IdToken;
import com.google.api.client.auth.openidconnect.IdTokenVerifier;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.util.Beta;
import com.google.api.client.util.Clock;
import com.google.api.client.util.Preconditions;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.security.PublicKey;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
/**
* {@link Beta} <br>
* Thread-safe Google ID token verifier.
*
* <p>Call {@link #verify(IdToken)} to verify a ID token. Use the constructor {@link
* #GoogleIdTokenVerifier(HttpTransport, JsonFactory)} for the typical simpler case if your
* application has only a single instance of {@link GoogleIdTokenVerifier}. Otherwise, ideally you
* should use {@link #GoogleIdTokenVerifier(GooglePublicKeysManager)} with a shared global instance
* of the {@link GooglePublicKeysManager} since that way the Google public keys are cached. Sample
* usage:
*
* <pre>{@code
* GoogleIdTokenVerifier verifier = new GoogleIdTokenVerifier.Builder(transport, jsonFactory)
* .setAudience(Arrays.asList("myClientId"))
* .build();
*
* ...
*
* if (!verifier.verify(googleIdToken)) {...}
* }</pre>
*
* @since 1.7
*/
@Beta
public class GoogleIdTokenVerifier extends IdTokenVerifier {
/** Google public keys manager. */
private final GooglePublicKeysManager publicKeys;
/**
* @param transport HTTP transport
* @param jsonFactory JSON factory
*/
public GoogleIdTokenVerifier(HttpTransport transport, JsonFactory jsonFactory) {
this(new Builder(transport, jsonFactory));
}
/**
* @param publicKeys Google public keys manager
* @since 1.17
*/
public GoogleIdTokenVerifier(GooglePublicKeysManager publicKeys) {
this(new Builder(publicKeys));
}
/**
* @param builder builder
* @since 1.14
*/
protected GoogleIdTokenVerifier(Builder builder) {
super(builder);
publicKeys = builder.publicKeys;
}
/**
* Returns the Google public keys manager.
*
* @since 1.17
*/
public final GooglePublicKeysManager getPublicKeysManager() {
return publicKeys;
}
/**
* Returns the HTTP transport.
*
* @since 1.14
*/
public final HttpTransport getTransport() {
return publicKeys.getTransport();
}
/** Returns the JSON factory. */
public final JsonFactory getJsonFactory() {
return publicKeys.getJsonFactory();
}
/**
* Returns the public certificates encoded URL.
*
* @since 1.15
* @deprecated (scheduled to be removed in 1.18) Use {@link #getPublicKeysManager()} and {@link
* GooglePublicKeysManager#getPublicCertsEncodedUrl()} instead.
*/
@Deprecated
public final String getPublicCertsEncodedUrl() {
return publicKeys.getPublicCertsEncodedUrl();
}
/**
* Returns the public keys.
*
* <p>Upgrade warning: in prior version 1.16 it may return {@code null} and not throw any
* exceptions, but starting with version 1.17 it cannot return {@code null} and may throw {@link
* GeneralSecurityException} or {@link IOException}.
*
* @deprecated (scheduled to be removed in 1.18) Use {@link #getPublicKeysManager()} and {@link
* GooglePublicKeysManager#getPublicKeys()} instead.
*/
@Deprecated
public final List<PublicKey> getPublicKeys() throws GeneralSecurityException, IOException {
return publicKeys.getPublicKeys();
}
/**
* Returns the expiration time in milliseconds to be used with {@link Clock#currentTimeMillis()}
* or {@code 0} for none.
*
* @deprecated (scheduled to be removed in 1.18) Use {@link #getPublicKeysManager()} and {@link
* GooglePublicKeysManager#getExpirationTimeMilliseconds()} instead.
*/
@Deprecated
public final long getExpirationTimeMilliseconds() {
return publicKeys.getExpirationTimeMilliseconds();
}
/**
* Verifies that the given ID token is valid using the cached public keys.
*
* <p>It verifies:
*
* <ul>
* <li>The RS256 signature, which uses RSA and SHA-256 based on the public keys downloaded from
* the public certificate endpoint.
* <li>The current time against the issued at and expiration time (allowing for a 5 minute clock
* skew).
* <li>The issuer is {@code "accounts.google.com"} or {@code "https://accounts.google.com"}.
* </ul>
*
* @param googleIdToken Google ID token
* @return {@code true} if verified successfully or {@code false} if failed
*/
public boolean verify(GoogleIdToken googleIdToken) throws GeneralSecurityException, IOException {
// check the payload only
if (!super.verifyPayload(googleIdToken)) {
return false;
}
// verify signature, try all public keys in turn.
for (PublicKey publicKey : publicKeys.getPublicKeys()) {
if (googleIdToken.verifySignature(publicKey)) {
return true;
}
}
return false;
}
/**
* Verifies that the given ID token is valid using {@link #verify(GoogleIdToken)} and returns the
* ID token if succeeded.
*
* @param idTokenString Google ID token string
* @return Google ID token if verified successfully or {@code null} if failed
* @since 1.9
*/
public GoogleIdToken verify(String idTokenString) throws GeneralSecurityException, IOException {
GoogleIdToken idToken = GoogleIdToken.parse(getJsonFactory(), idTokenString);
return verify(idToken) ? idToken : null;
}
/**
* Downloads the public keys from the public certificates endpoint at {@link
* #getPublicCertsEncodedUrl}.
*
* <p>This method is automatically called if the public keys have not yet been initialized or if
* the expiration time is very close, so normally this doesn't need to be called. Only call this
* method explicitly to force the public keys to be updated.
*
* @deprecated (scheduled to be removed in 1.18) Use {@link #getPublicKeysManager()} and {@link
* GooglePublicKeysManager#refresh()} instead.
*/
@Deprecated
public GoogleIdTokenVerifier loadPublicCerts() throws GeneralSecurityException, IOException {
publicKeys.refresh();
return this;
}
/**
* {@link Beta} <br>
* Builder for {@link GoogleIdTokenVerifier}.
*
* <p>Implementation is not thread-safe.
*
* @since 1.9
*/
@Beta
public static class Builder extends IdTokenVerifier.Builder {
/** Google public keys manager. */
GooglePublicKeysManager publicKeys;
/**
* @param transport HTTP transport
* @param jsonFactory JSON factory
*/
public Builder(HttpTransport transport, JsonFactory jsonFactory) {
this(new GooglePublicKeysManager(transport, jsonFactory));
}
/**
* @param publicKeys Google public keys manager
* @since 1.17
*/
public Builder(GooglePublicKeysManager publicKeys) {
this.publicKeys = Preconditions.checkNotNull(publicKeys);
setIssuers(Arrays.asList("accounts.google.com", "https://accounts.google.com"));
}
/** Builds a new instance of {@link GoogleIdTokenVerifier}. */
@Override
public GoogleIdTokenVerifier build() {
return new GoogleIdTokenVerifier(this);
}
/**
* Returns the Google public keys manager.
*
* @since 1.17
*/
public final GooglePublicKeysManager getPublicCerts() {
return publicKeys;
}
/** Returns the HTTP transport. */
public final HttpTransport getTransport() {
return publicKeys.getTransport();
}
/** Returns the JSON factory. */
public final JsonFactory getJsonFactory() {
return publicKeys.getJsonFactory();
}
/**
* Returns the public certificates encoded URL.
*
* @since 1.15
* @deprecated (scheduled to be removed in 1.18) Use {@link #getPublicCerts()} and {@link
* GooglePublicKeysManager#getPublicCertsEncodedUrl()} instead.
*/
@Deprecated
public final String getPublicCertsEncodedUrl() {
return publicKeys.getPublicCertsEncodedUrl();
}
/**
* Sets the public certificates encoded URL.
*
* <p>The default value is {@link GoogleOAuthConstants#DEFAULT_PUBLIC_CERTS_ENCODED_URL}.
*
* <p>Overriding is only supported for the purpose of calling the super implementation and
* changing the return type, but nothing else.
*
* @since 1.15
* @deprecated (scheduled to be removed in 1.18) Use {@link
* GooglePublicKeysManager.Builder#setPublicCertsEncodedUrl(String)} instead.
*/
@Deprecated
public Builder setPublicCertsEncodedUrl(String publicKeysEncodedUrl) {
// TODO(yanivi): make publicKeys field final when this method is removed
publicKeys =
new GooglePublicKeysManager.Builder(getTransport(), getJsonFactory())
.setPublicCertsEncodedUrl(publicKeysEncodedUrl)
.setClock(publicKeys.getClock())
.build();
return this;
}
@Override
public Builder setIssuer(String issuer) {
return (Builder) super.setIssuer(issuer);
}
/** @since 1.21.0 */
@Override
public Builder setIssuers(Collection<String> issuers) {
return (Builder) super.setIssuers(issuers);
}
@Override
public Builder setAudience(Collection<String> audience) {
return (Builder) super.setAudience(audience);
}
@Override
public Builder setAcceptableTimeSkewSeconds(long acceptableTimeSkewSeconds) {
return (Builder) super.setAcceptableTimeSkewSeconds(acceptableTimeSkewSeconds);
}
@Override
public Builder setClock(Clock clock) {
return (Builder) super.setClock(clock);
}
}
}