OpenID Connect authentication

Requesting an authorisation code

Code examples how to make an OpenID authentication request to obtain a code (in the OAuth 2.0 authorisation code flow) or token (in the implicit flow) from the OpenID provider.

To compose an OpenID authentication request on the client side (in the code flow):

// The client identifier provisioned by the server
ClientID clientID = new ClientID("123");

// The client callback URL
URL callback = new URL("");

// Generate random state string for pairing the response to the request
State state = new State();

// Generate nonce
Nonce nonce = new Nonce();

// Compose the request (in code flow)
AuthenticationRequest req = new AuthenticationRequest(
    new URL(""),
    new ResponseType("code"),
    Scope.parse("openid email profile address"),

HTTPResponse httpResponse = req.toHTTPRequest().send();

AuthenticationResponse response = AuthenticationResponseParser.parse(httpResponse);

if (response instanceof AuthenticationErrorResponse) {
    // process error

AuthenticationSuccessResponse succesResponse =

// Retrieve the authorisation code
AuthorizationCode code = successResponse.getAuthorizationCode();

// Don't forget to check the state
assert successResponse.getState().equals(state);

Decoding the OpenID authentication request on the server side:

// Get the query string
String query = "";

// Decode the query string
AuthenticationRequest req = AuthenticationRequest(null, query);

// Extract the parameters

// Required to look up the client in the provider's database
ClientID clientID = req.getClientID();

// The client redirection URL, must be registered in the provider's database
URL redirectURI = req.getRedirectionURI();

// The response type (implies code flow)
ResponseType rt = req.getResponseType();

// The state, must be echoed back with the response
State state = req.getState();

// The requested scope
Scope scope = req.getScope();

// Other parameters....

// Process the request and generate a code
AuthorizationCode code = new AuthorizationCode();

// Create response
AuthenticationSuccessResponse(redirectURI, code, null, null, state);

// Output the response depending on your web server framework
// ...

Requesting a specific OpenID claim

This is an example request for a specific OpenID claim, using the optional claims parameter.

You may choose to take this approach for requesting one or more claims in the following situations:

  • When you have a custom claim that is not mapped from a scope value. For example you have an LDAP directory feeding a custom "group" claim that is not tied to a particular claim.

  • When you want to request a specific claim only, e.g. just name without asking for the whole kitchen sink of the profile scope value (from which name maps).

  • When you want to receive selected claims included in the ID token, instead of obtaining them via the UserInfo endpoint.

Note that some OpenID providers do not support this parameter (but the Connect2id server does).


// Make specific request for the group claim, to be returned at the UserInfo endpoint
ClaimsRequest claims = new ClaimsRequest();

AuthenticationRequest request = new AuthenticationRequest.Builder(
    new ResponseType("code"),
    new Scope("openid"),
    new ClientID("000123"),
    .state(new State())

URI requestURI = request.toURI();

This will output a request URL like

To have the group claim returned with the ID token:

ClaimsRequest claims = new ClaimsRequest();

// ...

Signed OpenID authentication requests

Publishers of apps can use sealed OpenID authentication requests to ensure their parameters cannot be modified or tampered with.

This approach can be especially useful for mobile apps that register dynamically with an OpenID provider:

  1. The software publisher creates a software statement that locks down the client registration parameters, such as the grant types to use, mandating PKCE, the scopes that may be requested, and client metadata such as app name, icon, and links (possibly with l10n). The software statement (a JSON Web Token) is signed by the software publisher and put into the distributed app package. The JWT can be additionally encrypted for confidentiality.

  2. The software publisher also creates a signed JWT that locks down the parameters of OpenID authentication requests to be made from a client instance to the OpenID provider. These locked down parameters may include response_type, client_id, scope, redirect_uri and any other parameter that is otherwise supported. This JWT can be included in the app package, or uploaded to a public https URL. Additional encryption is possible too.

  3. Upon installation the app instance registers itself with the OpenID provider of choice. The OpenID provider is previously configured to only accept client registrations from approved software publishers. Unless the signature validation of the software statement succeeds, the client is not allowed to register.

  4. Subsequent OpenID authentication requests include the specified JWT, either directly by value, or by referencing its URL. The latter approach enables updates of the OpenID request parameters without necessitating an update to the app itself.

How to create an OpenID connect request that utilises signed parameters?

Suppose the app publisher wants to lock down the request_type, scope and code_challenge_method parameters like this:

  • request_type=code
  • scope=openid email
  • code_challenge_method=S256

Create a JSON Web Token with the said parameters, then sign it:

import com.nimbusds.jose.*;
import com.nimbusds.jose.crypto.*;
import com.nimbusds.jwt.*;

JWTClaimsSet jwtClaims = new JWTClaimsSet.Builder()
    .claim("response_type", "code")
    .claim("scope", "openid email")
    .claim("code_challenge_method", "S256")

SignedJWT jwt = new SignedJWT(
    new JWSHeader.Builder(JWSAlgorithm.RS256).keyID(keyID).build(),

jwt.sign(new RSASSASigner(rsaPrivateKey));

String jwtString = jwt.serialize();

This is an example JWT containing the above claims:


If the signed parameters are to be included in the OpenID authentication request by value:


import com.nimbusds.oauth2.sdk.*;
import com.nimbusds.oauth2.sdk.pkce.*;
import com.nimbusds.openid.connect.sdk*;

// Compute PKCE
CodeVerifier pkceVerifier = new CodeVerifier();

URI authRequest = new AuthenticationRequest.Builder(
    new ResponseType("code"),
    new Scope("openid"),
    new ClientID("123"),
    .state(new State())
    .codeChallenge(pkceVerifier, CodeChallengeMethod.S256)

The resulting URL to redirect to the OpenID provider with the authentication request:

To pass the signed parameters by URL reference, simply upload the JWT to a web server. OpenID authentication requests must then reference this URL.

If you intend to update the signed parameters at some in future, append the SHA-256 hash of the content to the URL fragment. Because OpenID providers may cache the JWT URL, this is the suggested mechanism for signalling that the JWT has changed and must be fetched again.

// Compute the JWT hash and append it as fragment to the URL Base64URL fragment = Base64URL.encode(MessageDigest.getInstance("SHA-256").digest(jwtString.getBytes(Charset.forName("UTF-8")))); URI requestURI = URI.create("" + fragment); URI authRequest = new AuthenticationRequest.Builder( new ResponseType("code"), new Scope("openid"), new ClientID("123"), URI.create("myapp://openid-connect-callback")) .state(new State()) .codeChallenge(pkceVerifier, CodeChallengeMethod.S256) .requestURI(requestURI) .endpointURI(URI.create("")) .build() .toURI();

The resulting URL to redirect to the OpenID provider with the authentication request will look almost identical, save for the using request_uri to point to the signed parameters:

Important to know:

  1. The request parameters present in the JWT override any top-level parameters of the OpenID authentication request.

  2. If a request_type or client_id is present in the JWT, it must match the top-level parameters, else the OpenID provider will return an error.

  3. When you take out the parameters included in the JWT, the OpenID authentication request must still qualify as a valid request. Otherwise the OpenID provider will return an invalid_request error. The SDK enforces this automatically for you.

The Connect2id server supports signed OpenID authentication requests from version 6 on.