JWT Access and Refresh Tokens

MODULE 29 · LESSON 29.1

Use short-lived signed access tokens only where their portability is worth revocation and storage complexity.

Practice-firstBeginner-friendlyProduction-aware

The production problem this solves

Good work on JWT Access and Refresh Tokens leaves evidence: a visible behavior, a stable contract or a repeatable operational check. This lesson defines an application trust boundary, where an explicit contract is safer than framework convention or an undocumented assumption.

Here, that decision supports a specific checkpoint: Threat-model a mobile CourseFlow client and a third-party calendar integration, then select the correct authorization flow for each. A reviewable result should include a repeatable request, automated test, query result and failure response rather than a claim that the feature simply works.

JWT Access and Refresh Tokens workflowA four-step visual showing claims, signature verification, access lifetime, refresh rotation.JWT Access and Refresh Tokens workflow1Claims2SignatureVerification3Access Lifetime4Refresh Rotation

JWT Access and Refresh Tokens workflow

  1. 1Claims
  2. 2Signature Verification
  3. 3Access Lifetime
  4. 4Refresh Rotation
JWT Access and Refresh Tokens workflow: a practical sequence used in this lesson.

A practical model for jwt access and refresh tokens

Use short-lived signed access tokens only where their portability is worth revocation and storage complexity. The useful unit of understanding is the boundary: who owns the decision, which input crosses it, what result is visible and how a failure is reported.

  • Claims: Compare the simplest correct approach with one credible alternative.
  • Signature Verification: State the assumption this concept relies on and show how the system behaves when it is false.
  • Access Lifetime: Connect this concept to the module checkpoint and identify the evidence a reviewer should expect.
  • Refresh Rotation: Explain the concept without framework jargon, then point to it in the working example.

Engineering decisions for JWT Access and Refresh Tokens

These are the details that separate a working demonstration from a maintainable production decision.

  • Do not accept algorithms from untrusted token input without a strict verification policy.
  • Keep access tokens short-lived and design refresh rotation, revocation and replay handling before production.
  • Never place secrets or unnecessary personal data in a JWT: signing prevents alteration, not disclosure.

Explain each moving part

The sample is intentionally narrow. Its job is to expose claims without hiding the decision behind unrelated setup.

TS
import { jwtVerify } from 'jose';

const issuer = 'https://identity.example.test';
const audience = 'courseflow-api';

export async function verifyAccessToken(token: string, key: CryptoKey) {
  const { payload } = await jwtVerify(token, key, { issuer, audience });
  if (typeof payload.sub !== 'string') throw new Error('missing subject');
  return payload;
}
Review it as someone else's change

Explain what the sample proves, what it does not prove, and which test would increase your confidence in signature verification.

Trace the implementation boundary

  1. 1
    Claims

    Keep names tied to the product rule so a reviewer can follow the change without decoding abbreviations.

  2. 2
    Signature Verification

    Add a regression check close to the boundary where this behavior can fail.

  3. 3
    Access Lifetime

    Describe the behavior in one sentence, then choose the smallest input that can prove it.

  4. 4
    Refresh Rotation

    Add this responsibility at the narrowest sensible boundary; do not pull an unrelated layer into the change.

Mistakes that create hidden coupling

  • Treating claims as vocabulary instead of defining the behavior it must produce.
  • Testing the expected path while ignoring an empty, invalid, repeated or unauthorized case around signature verification.
  • Allowing access lifetime to cross a boundary without an explicit contract or useful error.
  • Changing several layers before capturing the first piece of evidence, which makes the original cause harder to see.

Debug from the boundary inward

  1. Reduce the problem to the smallest failing JWT Access and Refresh Tokens case.
  2. Capture the actual input and output at the claims boundary.
  3. Read the first relevant error, request, trace or query rather than the loudest downstream symptom.
  4. Test one explanation for the failure in signature verification; avoid changing two variables together.
  5. Keep a regression check that would expose the same defect if it returned.

Security decision

Validate external input, authorize the requested action, use parameterized data access, and keep credentials out of responses, source control and logs.

Performance decision

Bound queries and collections, inspect the actual request or query plan, and optimize only the slow boundary confirmed by evidence.

PRACTICE

Build something you can inspect

Verify issuer, audience, expiry and algorithm; rotate a refresh token and detect one replay attempt.

Stretch challenge

Ask another person to run the exercise from your README. Fix the first place where their result differs from yours.

Definition of done

  • The behavior around claims works with realistic input.
  • A failure involving signature verification is handled clearly and without leaking sensitive detail.
  • The implementation remains keyboard-usable when it produces an interface.
  • Your evidence directly supports the claim made in the exercise.
  • The README records the important trade-off without pretending the solution is universal.

Check your reasoning

Why is decoding a JWT not the same as verifying that the API should trust it?

Answer by naming the expected claims behavior, the layer responsible for it and the evidence that would confirm your explanation.

Where would you investigate the first failure?

Start where signature verification crosses a boundary. Compare the actual input and output there before following downstream symptoms.

What would make this work reviewable?

Show the focused change, repeatable steps, the result of your check and one honest trade-off connected to access lifetime.

What to carry into the next lesson

  • Use short-lived signed access tokens only where their portability is worth revocation and storage complexity.
  • Keep claims visible at the boundary where it can be tested.
  • Use evidence from signature verification before widening the implementation.

References and related reading

Progress is stored only in this browser.

Share this page

Share this page with the people who will use it next.

X Facebook LinkedIn WhatsApp Email

Discussion

No comments yet. Add the first useful question or observation.