public class GoogleIdTokenVerifier extends Object
The public keys are loaded Google's public certificate endpoint at
"https://www.googleapis.com/oauth2/v1/certs". The public keys are cached in this instance
of GoogleIdTokenVerifier. Therefore, for maximum efficiency, applications should use a
single globally-shared instance of the GoogleIdTokenVerifier. Use
verify(GoogleIdToken) or GoogleIdToken.verify(GoogleIdTokenVerifier) to verify a
Google ID token.
Samples usage:
public static GoogleIdTokenVerifier verifier;
public static void initVerifier(
HttpTransport transport, JsonFactory jsonFactory, String clientId) {
verifier = new GoogleIdTokenVerifier.Builder(transport, jsonFactory)
.setClientId(clientId)
.build();
}
public static boolean verifyToken(GoogleIdToken idToken)
throws GeneralSecurityException, IOException {
return verifier.verify(idToken);
}
| Modifier and Type | Class and Description |
|---|---|
static class |
GoogleIdTokenVerifier.Builder
Builder for
GoogleIdTokenVerifier. |
| Modifier | Constructor and Description |
|---|---|
|
GoogleIdTokenVerifier(com.google.api.client.http.HttpTransport transport,
com.google.api.client.json.JsonFactory jsonFactory)
Constructor with required parameters.
|
protected |
GoogleIdTokenVerifier(Set<String> clientIds,
com.google.api.client.http.HttpTransport transport,
com.google.api.client.json.JsonFactory jsonFactory)
Construct the
GoogleIdTokenVerifier. |
protected |
GoogleIdTokenVerifier(Set<String> clientIds,
com.google.api.client.http.HttpTransport transport,
com.google.api.client.json.JsonFactory jsonFactory,
com.google.api.client.util.Clock clock)
Construct the
GoogleIdTokenVerifier. |
| Modifier and Type | Method and Description |
|---|---|
Set<String> |
getClientIds()
Returns the set of client IDs.
|
long |
getExpirationTimeMilliseconds()
Returns the expiration time in milliseconds to be used with
Clock.currentTimeMillis()
or 0 for none. |
com.google.api.client.json.JsonFactory |
getJsonFactory()
Returns the JSON factory.
|
List<PublicKey> |
getPublicKeys()
Returns the public keys or
null for none. |
GoogleIdTokenVerifier |
loadPublicCerts()
Downloads the public keys from the public certificates endpoint at
"https://www.googleapis.com/oauth2/v1/certs". |
boolean |
verify(GoogleIdToken idToken)
Verifies that the given ID token is valid using
verify(GoogleIdToken, String) with the
getClientIds(). |
boolean |
verify(GoogleIdToken idToken,
String clientId)
Verifies that the given ID token is valid, using the given client ID.
|
boolean |
verify(Set<String> clientIds,
GoogleIdToken idToken)
Verifies that the given ID token is valid, using the given set of client IDs.
|
GoogleIdToken |
verify(String idTokenString)
Returns a Google ID token if the given ID token string is valid using
verify(GoogleIdToken, String) with the getClientIds(). |
public GoogleIdTokenVerifier(com.google.api.client.http.HttpTransport transport,
com.google.api.client.json.JsonFactory jsonFactory)
Use GoogleIdTokenVerifier.Builder to specify client IDs.
transport - HTTP transportjsonFactory - JSON factoryprotected GoogleIdTokenVerifier(Set<String> clientIds, com.google.api.client.http.HttpTransport transport, com.google.api.client.json.JsonFactory jsonFactory)
GoogleIdTokenVerifier.clientIds - set of client IDs or null for nonetransport - HTTP transportjsonFactory - JSON factoryprotected GoogleIdTokenVerifier(Set<String> clientIds, com.google.api.client.http.HttpTransport transport, com.google.api.client.json.JsonFactory jsonFactory, com.google.api.client.util.Clock clock)
GoogleIdTokenVerifier.clientIds - set of client IDs or null for nonetransport - HTTP transportjsonFactory - JSON factoryclock - Clock for expiration checkspublic final com.google.api.client.json.JsonFactory getJsonFactory()
public final List<PublicKey> getPublicKeys()
null for none.public final long getExpirationTimeMilliseconds()
Clock.currentTimeMillis()
or 0 for none.public boolean verify(GoogleIdToken idToken) throws GeneralSecurityException, IOException
verify(GoogleIdToken, String) with the
getClientIds().idToken - Google ID tokentrue if verified successfully or false if failedGeneralSecurityExceptionIOExceptionpublic GoogleIdToken verify(String idTokenString) throws GeneralSecurityException, IOException
verify(GoogleIdToken, String) with the getClientIds().idTokenString - Google ID token stringnull if failedGeneralSecurityExceptionIOExceptionpublic boolean verify(GoogleIdToken idToken, String clientId) throws GeneralSecurityException, IOException
"accounts.google.com".clientId is null.
idToken - Google ID tokenclientId - client ID or null to skip checking ittrue if verified successfully or false if failedGeneralSecurityExceptionIOExceptionpublic boolean verify(Set<String> clientIds, GoogleIdToken idToken) throws GeneralSecurityException, IOException
"accounts.google.com".clientIds is
null.idToken - Google ID tokenclientIds - set of client IDstrue if verified successfully or false if failedGeneralSecurityExceptionIOExceptionpublic GoogleIdTokenVerifier loadPublicCerts() throws GeneralSecurityException, IOException
"https://www.googleapis.com/oauth2/v1/certs".
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.
GeneralSecurityExceptionIOExceptionCopyright © 2010-2013 Google. All Rights Reserved.