Class OidcClient
Modern OpenID Connect / OAuth 2.0 client. Built around the authorization-code flow with PKCE (RFC 7636) and the system browser. Use it as the foundation for all new sign-in integrations:
OidcClient.discover("https://accounts.google.com").ready(new SuccessCallback<OidcClient>() {
public void onSucess(OidcClient client) {
client.setClientId("YOUR_CLIENT_ID")
.setRedirectUri("com.example.app:/oauth2redirect")
.setScopes("openid", "email", "profile");
client.authorize().ready(new SuccessCallback<OidcTokens>() {
public void onSucess(OidcTokens tokens) {
// use tokens.getAccessToken() / tokens.getIdToken()
}
});
}
});
What this gives you that Oauth2 does not
- Discovery via
.well-known/openid-configurationso you only configure the issuer URL, not five separate endpoints - PKCE S256 on every flow (mandatory; many providers now require it)
- System-browser sign-in via
SystemBrowser(the previous class used an in-app WebView that modern IdPs reject) - Refresh-token flow surfaced as a first-class method
- ID-token claim decoding via
OidcTokens.getClaim(String) - Pluggable
TokenStorepersistence - Nonce + state verification on every authorization round-trip
What is checked before tokens are handed over
- The authorization response: its
state, and the issuer it names. A response that names another issuer than this client's is refused, and so is one that names none when the provider's discovery document says it always does (RFC 9207). That is what stops a response from one provider being taken for another's. - The ID token's claims:
issis the provider,audis this client,exphas not passed,nonceis the one the request carried, andat_hash, when the token has one, is the hash of the access token it came with. - The ID token's signature, against the provider's keys. The keys are fetched from the
configuration's
jwks_urionce and kept, and fetched again when a token names a key that is not among them.RS256,RS384,RS512,ES256andES384are accepted; an unsigned token, or one signed with the client secret, is not.
A token that fails any of this is not stored and not returned: the resource fails
with OidcException.INVALID_ID_TOKEN, OidcException.NONCE_MISMATCH or
OidcException.ISSUER_MISMATCH. That includes a signature this platform has no way
to check. Nothing is skipped quietly -- see setVerifyIdTokenSignature(boolean) for
the one switch there is, and what turning it off gives up.
Things this class deliberately does NOT do
- Implicit and hybrid flows. Use the lower-level
ConnectionRequestAPIs if you need those.
A device without a browser or a keyboard
requestDeviceAuthorization() and pollDeviceToken(OidcDeviceAuthorization)
run the device authorization grant (RFC 8628): the device shows a short
code, the user approves it on a phone or a computer, and the device
receives its tokens.
Using the tokens
OidcRequestAuthorizer attaches the access token to the application's
requests and renews it with the refresh token when the service refuses it.
-
Method Summary
Modifier and TypeMethodDescriptionLaunches an authorization-code flow with PKCE.Clears any stored tokens for this client.static OidcClientcreate(OidcConfiguration configuration) Constructs a client from an already-knownOidcConfiguration.static AsyncResource<OidcClient> Fetches<issuer>/.well-known/openid-configurationand resolves with anOidcClientpre-populated with the discovered endpoints.Returns previously-saved tokens for this client (ornull).pollDeviceToken(OidcDeviceAuthorization authorization) Waits for the user to approve a device, by asking the token endpoint at the pace the server set.Exchanges a stored refresh token for a fresh access token.refreshIfExpired(int leewaySeconds) Loads stored tokens; if they are withinleewaySecondsof expiring, runs a refresh and saves the new tokens.Starts the device authorization grant (RFC 8628), for a device with no browser or no practical way to type: a television, a watch, a kiosk, a command line.Sends a token-revocation request to the issuer (RFC 7009).setAuthorizationParameters(String... kv) Extraname=valueparameters appended to the authorization-endpoint URL.setClientId(String clientId) setClientSecret(String clientSecret) setEnforceNonce(boolean enforce) falseskips thenonceclaim check on the returned ID token.setIdTokenClockSkew(int seconds) How far the device's clock may be from the provider's when an ID token'sexpis checked.setRedirectUri(String redirectUri) setResponseMode(String mode) Sets theresponse_modeparameter sent on the authorization URL (e.g."form_post"for Apple Sign-In with the web fallback).setStoreKey(String key) Override the key under which tokens are stored.setTokenParameters(String... kv) Extraname=valueparameters sent as form data on every token-endpoint POST.setTokenStore(TokenStore store) Swaps the token persistence strategy.setVerifyIdTokenSignature(boolean verify) Whether an ID token's signature is verified against the provider's keys before the token is accepted.
-
Method Details
-
create
Constructs a client from an already-knownOidcConfiguration. Usediscover(String)when you'd rather pull the endpoints from the provider's.well-known/openid-configurationdocument. -
discover
Fetches
<issuer>/.well-known/openid-configurationand resolves with anOidcClientpre-populated with the discovered endpoints. The returned client still needsclientId,redirectUriandscopesbeforeauthorize()will work.Trailing slashes are removed when building the discovery request URL. The metadata's
issuermust still exactly match the supplied issuer, including its trailing slashes, before any discovered endpoints are accepted. -
getConfiguration
-
setClientId
-
setClientSecret
-
setRedirectUri
-
setScopes
-
setScopes
-
setAuthorizationParameters
Extraname=valueparameters appended to the authorization-endpoint URL. Use for provider-specific options like Google'sprompt=consentor Apple'sresponse_mode=form_post. Values are URL-encoded. -
setTokenParameters
Extraname=valueparameters sent as form data on every token-endpoint POST. -
setTokenStore
Swaps the token persistence strategy. Defaults toTokenStore.DefaultStorageTokenStore. -
setStoreKey
Override the key under which tokens are stored. Defaults to the issuer + client-id pair so that multiple clients can coexist. -
setEnforceNonce
falseskips thenonceclaim check on the returned ID token. Only disable when you have a very good reason (e.g. provider known not to echo the nonce); the default is to enforce. -
setVerifyIdTokenSignature
Whether an ID token's signature is verified against the provider's keys before the token is accepted. True unless set.
With it on, a token whose signature cannot be checked is refused -- because it does not verify, because the configuration names no
jwks_uri, or because the platform the app is running on cannot verify a signature of that algorithm. The last one is the reason this switch exists: turn it off for a provider whose ID tokens cannot be verified on a platform you ship to, and for no other reason.With it off, the claims are still checked, but they are only as good as the TLS connection to the token endpoint. Do not make a decision on your server from an ID token the app forwards: verify it there.
-
setIdTokenClockSkew
How far the device's clock may be from the provider's when an ID token'sexpis checked. Five minutes unless set: phones are set by hand more often than servers. -
setResponseMode
Sets theresponse_modeparameter sent on the authorization URL (e.g."form_post"for Apple Sign-In with the web fallback). -
authorize
Launches an authorization-code flow with PKCE. The user is sent to the system browser to sign in; the returnedAsyncResourcecompletes with the token set or errors withOidcException(e.g.USER_CANCELLED,STATE_MISMATCH). -
refresh
Exchanges a stored refresh token for a fresh access token. Pass the value returned from
OidcTokens.getRefreshToken()on a previous flow. The stored session supplies the original subject for any refreshed ID token. A refresh response cannot change that subject. The new tokens are persisted via the currentTokenStore.Cancelling the returned resource before it completes drops the answer: nothing is stored and no
OidcRequestAuthorizeris given the new tokens. -
loadStoredTokens
Returns previously-saved tokens for this client (ornull). Combine withrefreshIfExpired(int)to silently bring the session back to life on app launch. -
refreshIfExpired
Loads stored tokens; if they are withinleewaySecondsof expiring, runs a refresh and saves the new tokens. Completes withnullwhen nothing is stored or when the stored token has no refresh token and has already expired. -
revoke
Sends a token-revocation request to the issuer (RFC 7009). Silently no-ops when the issuer does not advertise arevocation_endpoint. A refused request reports the OAuth error, or a transport error when the non-success HTTP response contains no OAuth error. -
clearStoredTokens
Clears any stored tokens for this client. Does not call the issuer's revocation endpoint -- combine withrevoke(String)if you want a proper sign-out. The returned resource completes after earlier saves and this clear finish, so a delayed save cannot restore the cleared session. -
requestDeviceAuthorization
Starts the device authorization grant (RFC 8628), for a device with no browser or no practical way to type: a television, a watch, a kiosk, a command line.
The answer holds a short code. Show it with the verification address; the user opens that address on another device, signs in and types the code. Meanwhile pass the answer to
pollDeviceToken(OidcDeviceAuthorization), which completes once they have.Needs a client id, the scopes, and a provider whose configuration names a
device_authorization_endpoint. No redirect URI is involved.Returns
a resource that completes with the codes, or fails with an
OidcExceptioncarrying the server's error codeThrows
IllegalStateException: when the client id is missing, or the provider does not offer the grant
-
pollDeviceToken
Waits for the user to approve a device, by asking the token endpoint at the pace the server set.
The wait is a timer, not a thread: each request is queued when the previous answer's interval has passed. The interval starts at
OidcDeviceAuthorization.getInterval()and grows by five seconds every time the server answersslow_down, and doubles after a request that failed to reach the server at all.The resource completes with the tokens, which are saved to the
TokenStorethe wayauthorize()saves them. It fails with anOidcExceptionwhose code isOidcException.ACCESS_DENIEDwhen the user refused,OidcException.EXPIRED_TOKENwhen the codes ran out -- by the server's word or by the clock -- or whatever other code the server sent. Cancelling the resource stops the polling.Parameters
authorization: the answer ofrequestDeviceAuthorization()
Returns
a resource that completes with the tokens
-