Class OidcClient
java.lang.Object
com.codename1.io.oidc.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
Things this class deliberately does NOT do
- Verify the ID token signature. This requires the provider's JWKS and ECDSA/RSA verification, which is not feasible on every supported platform without pulling in a heavy dep. The remedy is: trust the TLS connection to the well-known issuer (i.e. always discover, never pass tokens to a server without re-validating server-side).
- Implicit / hybrid / device flows. Use the lower-level
ConnectionRequestAPIs if you need those.
- Since:
- 8.0
-
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).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.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.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.
-
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 on
issuerare tolerated. -
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. -
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 fromOidcTokens.getRefreshToken()on a previous flow. The new tokens are persisted via the currentTokenStore. -
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. -
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.
-