Okta OIDC with PKCE Example

by oktadeveloper

GitHub Readme.md

This repo let's you see the OAuth 2.0 Implicit flow and the Authorization Code with PKCE flow in action.

The Implicit flow is effectively deprecated and should no longer be used.

Quick Start: Automatic Setup with Heroku

You need a Heroku account to follow these instructions.

You can create a free Okta Developer org and deploy this app directly to Heroku by clicking the purple button:


After you deploy the app, click on View on the result screen to navigate to the newly deployed app.

Open your browser's developer tools view and choose the Network tab.

Click Profile (pkce) at the top of the app. You are redirected to your Okta org to authenticate. BUT, you won't yet know the credentials.

Back on the result tab from the deployment, click on Manage App. This brings you to the Heroku dashboard for the app. Click Settings and then Reveal Config Vars. Here, you see values for OKTA_ADMIN_EMAIL and OKTA_ADMIN_PASSWORD. You can use these values to login to the app on the other tab.

After you login, you'll see an ID Token, Access Token and profile details.

Digging into PKCE

You should see both the /authorize and the /token calls in the call list from the Network tab on your browser.

In the auth/index.js file, there's a function named callback. In this function, you'll find this line:


The parseFromUrl() function detects when an authorization code has been returned as the result of the Authorization Code with PKCE flow. In this case, it automatically exchanges the authorization code for a set of tokens by posting to the /token endpoint.

The Implicit Flow and Why We Hate It

Click Logout link in the navbar.

Click Profile (implicit). In your browser's developer tools, you'll see an error:

The response type is not supported by the authorization server.

This is because, the SPA app that's automatically set up in your Okta org is configured to not allow the inmplicit flow. This is the best current practice. But, for demonstration purposes, we want to allow this flow.

Jump back to the tab in your browser that has the Heroku dashboard. Click okta to Single Sign-On (SSO) to your Okta organization. Click Applications. Click Heroku Created OIDC App - browser. Click General.

Click the Edit button, click the Implicit checkbox, click the Allow Access Token with implicit grant type, scroll down and click the Save button.

Now, when you click the Profile (implicit) link in the app, you'll be redirected to authenticate as before. In your browser developer tools view, you'll see a call to the /redirect endpoint. If you look at the Location header in the list of Response headers, you'll see something like this:


Unlike the PKCE flow from earlier, this response has the tokens right on the url of the redirect. This is problematic since anything that passes thtough your browser's address bar could be intercepted or altered (if you had malware, a virus or a malicious browser extension, for instance).

This is why the Authorization Code Flow with PKCE Flow is a superior alternative to the Implicit Flow. The temporary authorization code is exchanged for tokens using a POST request, which does not pass through your browser's address bar.

Quick Start: Manual Setup

You need a free Okta Developer Org to get started.

In the admin console of your Okta org, Navigate to: Applications

  • Click the green Add Application button
  • Select Single-Page App application, and click Next
  • Give the app a Name.
  • Change the value for Login redirect URIs to http://localhost:8080/authorization-code/callback
  • Check Authorization Code (and leave Implicit checked)
  • Add http://localhost:8080 to Logout redirect URIs
  • Click Done

Take note of the Client ID at the bottom of the page.

Project setup

npm install

Compiles and hot-reloads for development

OKTA_CLIENT_ORGURL=<your Okta org> OKTA_OAUTH2_CLIENT_ID_SPA=<client id> npm run serve