public class IdTokenVerifier extends Object
Call verify(IdToken) to verify an ID token. This is a light-weight object, so you may
use a new instance for each configuration of expected issuer and trusted client IDs. Sample
usage:
IdTokenVerifier verifier = new IdTokenVerifier.Builder()
.setIssuer("issuer.example.com")
.setAudience(Arrays.asList("myClientId"))
.build();
...
if (!verifier.verify(idToken)) {...}
The verifier validates token signature per current OpenID Connect Spec:
https://openid.net/specs/openid-connect-core-1_0.html#IDTokenValidation By default, method gets a
certificate from well-known location A request to certificate location is performed using NetHttpTransport Either or both certificate location and
transport implementation can be overridden via IdTokenVerifier.Builder
IdTokenVerifier verifier = new IdTokenVerifier.Builder()
.setIssuer("issuer.example.com")
.setAudience(Arrays.asList("myClientId"))
.setHttpTransportFactory(customHttpTransportFactory)
.build();
...
if (!verifier.verify(idToken)) {...}
not recommended: this check can be disabled with OAUTH_CLIENT_SKIP_SIGNATURE environment variable
set to true. Use verifyPayload(IdToken) instead.
Note that verify(IdToken) only implements a subset of the verification steps, mostly
just the MUST steps. Please read ID Token
Validation for the full list of verification steps.
| Modifier and Type | Class and Description |
|---|---|
static class |
IdTokenVerifier.Builder
Builder for
IdTokenVerifier. |
| Modifier and Type | Field and Description |
|---|---|
static long |
DEFAULT_TIME_SKEW_SECONDS
Default value for seconds of time skew to accept when verifying time (5 minutes).
|
| Modifier | Constructor and Description |
|---|---|
|
IdTokenVerifier() |
protected |
IdTokenVerifier(IdTokenVerifier.Builder builder) |
| Modifier and Type | Method and Description |
|---|---|
long |
getAcceptableTimeSkewSeconds()
Returns the seconds of time skew to accept when verifying time.
|
Collection<String> |
getAudience()
Returns the unmodifiable list of trusted audience client IDs or
null to suppress the
audience check. |
com.google.api.client.util.Clock |
getClock()
Returns the clock.
|
String |
getIssuer()
Returns the first of equivalent expected issuers or
null if issuer check suppressed. |
Collection<String> |
getIssuers()
Returns the equivalent expected issuers or
null if issuer check suppressed. |
boolean |
verify(IdToken idToken)
Deprecated.
|
boolean |
verifyOrThrow(IdToken idToken)
Verifies that the given ID token is valid using the cached public keys.
|
protected boolean |
verifyPayload(IdToken idToken)
Verifies the payload of the given ID token
It verifies:
The issuer is one of
getIssuers() by calling IdToken.verifyIssuer(String). |
public static final long DEFAULT_TIME_SKEW_SECONDS
public IdTokenVerifier()
protected IdTokenVerifier(IdTokenVerifier.Builder builder)
builder - builderpublic final com.google.api.client.util.Clock getClock()
public final long getAcceptableTimeSkewSeconds()
public final String getIssuer()
null if issuer check suppressed.public final Collection<String> getIssuers()
null if issuer check suppressed.public final Collection<String> getAudience()
null to suppress the
audience check.@Deprecated public boolean verify(IdToken idToken)
It verifies:
getIssuers() by calling IdToken.verifyIssuer(String).
getAudience() by calling IdToken.verifyAudience(Collection).
getClock()
and allowing for a time skew specified in getAcceptableTimeSkewSeconds() , by
calling IdToken.verifyTime(long, long).
NetHttpTransport Both
certificate location and transport implementation can be overridden via IdTokenVerifier.Builder
not recommended: this check can be disabled with OAUTH_CLIENT_SKIP_SIGNATURE environment
variable set to true. Use verifyPayload(IdToken) instead.
IdTokenVerifier.verfyOrThrow(IdToken) instead to differentiate between potentially retryable
network errors and false verification results.idToken - ID tokentrue if verified successfully or false if failedpublic boolean verifyOrThrow(IdToken idToken) throws IOException
It verifies:
getIssuers() by calling IdToken.verifyIssuer(String).
getAudience() by calling IdToken.verifyAudience(Collection).
getClock()
and allowing for a time skew specified in getAcceptableTimeSkewSeconds() , by
calling IdToken.verifyTime(long, long).
NetHttpTransport Both
certificate location and transport implementation can be overridden via IdTokenVerifier.Builder
not recommended: this check can be disabled with OAUTH_CLIENT_SKIP_SIGNATURE environment
variable set to true.
Overriding is allowed, but it must call the super implementation.
idToken - ID tokentrue if verified successfully or false if payload validation failedIOException - if verification fails to run. For example, if it fails to get public keys
for signature verification.protected boolean verifyPayload(IdToken idToken)
It verifies:
getIssuers() by calling IdToken.verifyIssuer(String).
getAudience() by calling IdToken.verifyAudience(Collection).
getClock()
and allowing for a time skew specified in getAcceptableTimeSkewSeconds() , by
calling IdToken.verifyTime(long, long).
Overriding is allowed, but it must call the super implementation.
idToken - ID tokentrue if verified successfully or false if failedCopyright © 2011–2025 Google. All rights reserved.