Essential CSS
JavaScript Roadmap
Learn JavaScript
Mastering Web Typography
Practical Astro
Understanding Async JS
Unorthodox Tailwind
API Masterclass
Build and Deploy
The Beginner Freelancer
Real World Node
Profile Billing Logout
Understanding Async JS
Lesson Plan
  1. Introduction
    1. What is Asynchronous JS?
    2. Callbacks are not automatically asynchronous
    3. What is an API?
    4. Evolution of HTTP APIs
    5. Ajax vs Asynchronous JavaScript
  2. Fundamental Concepts
    1. How to fetch resources
    2. Using XHR
    3. Using the Fetch API
    4. Using libraries
    5. Core JavaScript concepts
    6. Understanding Promises
    7. Understanding Async Functions
    8. Understanding the Event Loop
    9. Understanding JSON
    10. Async JS Best Practices
    11. Using Optimistic UI
  3. Understanding Rest API
    1. Diving into Rest APIs
    2. The Rest Architecture
    3. The Rest Structure
    4. Anatomy of a Request
    5. What's an endpoint
    6. Request methods
    7. Understanding request headers
    8. Understanding the request body
    9. Authentication
    10. Basic Authentication
    11. Bearer Authentication
    12. Authenticating with API keys and API Secrets
    13. Cross Origin Resource Sharing
    14. Anatomy of a response
    15. HTTP Statuses
    16. Response headers
    17. The response body
    18. Handling Errors
    19. Reading API documentations
    20. Understanding Curl
  4. Node Basics
    1. Why we are using Node
    2. Command Line Basics
    3. Installing Node
    4. Managing dependencies
    5. Using ES Modules in Node
    6. Using Node
    7. Restarting the server automatically
    8. Sending requests via Node
    9. Protecting API keys and secrets
  5. Building a server
    1. What's a server
    2. Building a simple server with Express
    3. Two kinds of servers
    4. API Server architecture
    5. Handling routes
    6. Understanding Middlewares
    7. Enabling CORS
    8. Receiving data
    9. Sending responses
    10. Handling errors in Express
    11. Using the server as a proxy
    12. Minimum viable server
  6. Open Authorization
    1. Open Authorisation (OAuth)
    2. Authenticating with Access Tokens
    3. Obtaining Access Tokens
    4. Public and Confidential Clients
    5. Authorisation Code Grant
    6. Understanding scope
    7. Proof Key for Code Exchange (PKCE)
    8. PKCE with Twitter
    9. Dynamic state and `code_verifier`
    10. Saving the access token in cookies
    11. Passing access tokens to the browser
    12. Saving the access token in local storage
    13. Refresh Token Grant
    14. Device Code Grant
    15. Client libraries vs REST
  7. Building Social Media Logins
    1. Introduction
    2. Starter Files
    3. Creating SSL certificates
    4. Login in with Github
    5. Github: Saving the token to local storage
    6. Github: Persisting the login status
    7. Login with Twitter
    8. Twitter: Saving the token to local storage
    9. Twitter: Persisting the login status
    10. Twitter: Refreshing the access token
    11. Login with Facebook
    12. Facebook: Saving the token to localStorage
    13. Facebook: Persisting the login status
    14. Facebook: Refreshing the access token
    15. Login with google
    16. Google: Saving the token to localStorage
    17. Google: Refreshing the access token

Proof Key for Code Exchange (PKCE)

3m:9s

Proof key for Code Exchange (PKCE) extends Authorization Code Flow to make it more secure. The extra security allows browsers to authenticate with identity providers (assuming the identity provider supports PKCE).

Since PKCE extends Authorization Code Flow, it follows the same steps as Authorization Code Flow.

  1. You create a “Sign in with XX” link for users
  2. Users click the link and sign in to the provider
  3. The provider redirects the users to a callback_url
  4. You generate an access token from the code value sent to the the callback_url
  5. You save the token for subsequent requests

There are minor additions in Step 1 and 4:

  1. In Step 1, you provide two extra parameters — code_challenge and code_challenge_method.
  2. In Step 4, you provide one extra parameter — code_verifier.

Using PKCE

These are the following steps to use PKCE:

  1. You create a secret between 43 to 128 characters long.
  2. In Step 1 (when creating the sign-in link):
    1. You hash and encode this secret and send it to the identity provider as code_challenge.
    2. You send a code_challenge_method to tell the identity provider what hashing method you used, so they can decode it later.
  3. In Step 4 (when getting the access token):
    1. You send the secret as code_verifier.
    2. The identity provider decodes code_challenge and compares in with your code_verifier. If the decoded code_challenge value matches code_verifier, PKCE will be successful.

Creating the Code Challenge

First you need to choose between two code challenge methods.

  1. plain — the code challenge is not hashed.
  2. S256 — the code challenge is hashed with the SHA-256 hashing algorithm.

The code challenge method must be S256 in most cases. It can only be plain if the server is unable to support S256.

Plain Method

code_verifier and code_challenge will be the same values if the code_challenge_method is set to plain.

Example:

  • Code Verifier: challenge
  • Code Challenge: challenge
  • Code Challenge Method: plain

S256 Method

The code_challenge must be encrypted with the SHA-256 hashing algorithm and converted into a Base64-url-encoded string if the code_challenge_method is set to S256.

You can create the code challenge with the following functions in browsers and in Node:

// Browsers
async function getCodeChallengeInBrowser(verifier) {
// Hash the code verifier with SHA-256
const encoder = new TextEncoder()
const data = encoder.encode(verifier)
const hashed = await window.crypto.subtle.digest('SHA-256', data)
// Base64url encode the hash
return window
.btoa(String.fromCharCode.apply(null, new Uint8Array(hashed)))
.replace(/\+/g, '-')
.replace(/\//g, '_')
.replace(/=+$/, '')
}
// Node
import crypto from 'crypto'
function getCodeChallengeInNode(verifier) {
// Hash to code verifier with SHA-256
const hash = crypto.createHash('sha256').update(verifier).digest()
// Base64url encode the hash
return hash
.toString('base64')
.replace(/\+/g, '-')
.replace(/\//g, '_')
.replace(/=/g, '')
}

Creating a strong PKCE code_verifier

To make your requests more secure, you should generate a cryptographically strong code_verifier each time you trigger a PKCE flow.

You can generate a strong code_verifier with the following code:

// Browsers
function randomString() {
return crypto.getRandomValues(new Uint32Array(5)).join('')
}
// Node
import crypto from 'crypto'
function randomString() {
return crypto.randomBytes(32).toString('base64')
}

A PKCE library

I put everything you need about PKCE into a function that’s easy to use. You can find this function in my JavaScript utilities library.

You can install the library like this:

Terminal window
npm install @zellwk/javascript

You can use the library like this:

import PKCE from '@zellwk/javascript/pkce'
const pkce = await PKCE()
console.log(pkce)

Note: You can use this library in browsers without using bundlers like Rollup and Webpack. Here’s how.

Here’s some pictures to show what await PCKE() returns in browsers and node.

That’s it for this lesson.

I’ll talk about the what state is used for in a later lesson.