Skip to main content
If you have or are building a web or mobile application that doesn’t have its own separate back end server to manage user session data, configure your client with the Authorization Code Grant with PKCE (Proof Key for Code Exchange) flow to obtain tokens from Digital Identity Management. This flow is similar to Authorization Code Grant flow described in the SSO for Web Apps with a Back End topic. However, since your application does not have a back end, it is not ideal for your UI or app to transmit the client’s credentials over a front end channel to obtain an access token, because those credentials are vulnerable to a local attacker. As a best practice, your UI or app shouldn’t know the client credentials at all. In the Authorization Code Grant with PKCE, your app generates a cryptographic random string, and computes a hash of this string called the code challenge. Your app transmits the hash as part of the initial request to Digital Identity Management’s /oidc/authorize endpoint, and then transmits the original (un-hashed) string, called the code verifier, in the final /oidc/token request. Digital Identity Management checks that the verifier hashes to the challenge, to prove that the app making the /oidc/token request is the same that made the previous /oidc/authorize request.

Flow Diagram

The diagram below represents the communication flow between the key actors using the Authorization Code Grant with PKCE: OIDC Authorization Code Flow with PKCE

Interaction Steps

The interaction steps between each actor in the flow are as a follows:
  1. The user visits or opens your application to access a secured service.
  2. Your app determines that the user is not currently signed in.
  3. Your app generates and stores a cryptographically random nonce ASCII string. This will be used to validate the OIDC response, and must not be guessable by an outside attacker.
  4. Your app generates and stores a cryptographically random verifier string. The string must consist exclusively of:
    • lowercase and uppercase letters
    • numbers
    • period, underscore, hyphen, or tilde punctuation characters To produce the string, we recommend that your application generates at least 32 bytes of random binary data, then encodes it using the base64 URL scheme. Store this for later.
  5. Taking the PKCE Verifier string generated in the previous step, your application must compute the SHA-256 hash of the string and base64url-encode it. This value will be used immediately in the OIDC redirect.
  6. Your app generates and stores a cryptographically random state ASCII string. This will be used to validate the OIDC redirect back to your app, and it must not be guessable by an outside attacker. If your application is web-based, you may store this value in a cookie.
  7. Your application redirects the end-user’s browser (or in a mobile application, opens a browser window) to Digital Identity Management’s /oidc/authorize endpoint with the following HTTP GET query parameters:
  1. The user’s browser loads the oidc/authorize URL as per the HTTP 302 response from step 7. Digital Identity Management inspects the browser cookies to determine if the user is already authenticated. The next step depends on whether the user is already authenticated in Digital Identity Management.
  2. (Conditional) If the request is not valid, or if the user is already authenticated and re-authentication is not being forced, then Digital Identity Management immediately responds with an HTTP 302 Temporary Redirect to the redirect_uri with certain HTTP Query parameters. Skip steps 10 through 12 and go to step 13.
  3. (Conditional) If the user is not authenticated, then Digital Identity Management redirects them to the configured IdP Portal (that is, the central sign-up/sign-in page for all of your registered RPs).
  4. In the most conventional use case, the user authenticates with Digital Identity Management’s POST /sesion/start API. This API invokes Digital Identity Management’s multi-step Authenticate User Process. Alternatively, the user may need to first onboard with the Onboard User with Email or Mobile Process, which is invoked by the POST /proces/start/{processName} API. This and other variations are not included in the flow diagram.
  5. Eventually, whether from a /session/start or other Process call, and potentially after several more steps (for example, to satisfy multi-factor authentication rules, if enabled), the user is authenticated. The final HTTP response from Digital Identity Management includes a special X-Redirect-Location header with a URL constructed from the redirect_uri parameter provided by the RP in step 7. The IdP then redirects the browser to that location.
  6. The user’s browser loads a URL which Digital Identity Management has constructed from the redirect_uri parameter, to which the following HTTP Query parameters will have been added:
  7. Your app checks the state value in the request against the original state value stored in a cookie or HTML Web Storage. If there is a mismatch, a cross-site request forgery (CSRF/XSRF) attack is suspected, and the operation must be aborted with an error.
  8. Your app makes a background call to Digital Identity Management’s POST /oidc/token endpoint to redeem the authorization code for OIDC tokens: Unlike in the base Authorization Code Flow (as described in the SSO for Web Apps with a Back End topic), you do not pass any client credentials in the PKCE version of this flow. The body of the request is of type application/x-www-form-urlencoded.
  9. Digital Identity Management checks that the code_verifier supplied in the token request hashes to the code_challenge supplied in the authorization request (step7).
  10. If all parameters are correct and the Authorization Code is valid, Digital Identity Management replies with a JSON payload that contains the requested token(s). The payload is of type application/json, and it consists of a JSON object resembling the following:
    The fields of this JSON object are as follows:
  1. Your app decodes the ID token, and inspects the payload to make sure the nonce key is present and its value matches the nonce value N from the original request (generated in step 3). If not, a replay attack is suspected and the operation must be aborted with an error.
  2. Your app stores the tokens in an appropriate place (for example, cookies or HTML5 Web Storage for a web UI, or device storage for a mobile app).
  3. As long as the access token remains valid, the user remains authenticated. For more details on how to refresh (that is, get replacement) a token, refer to the Refreshing Tokens topic.
Parent topic:Using Digital Identity Management for Single Sign-On