Skip to main content

Can you explain the difference between OAuth and JWT in the context of API authentication?

OAuth is an authorization framework that allows third-party applications to access user data without exposing credentials. JWT, or JSON Web Token, is a compact token format that can be used…

CY
Can you explain the difference between OAuth and JWT in the context of API authentication?

OAuth is an authorization framework that allows third-party applications to access user data without exposing credentials. JWT, or JSON Web Token, is a compact token format that can be used to securely transmit information between parties as a JSON object, often used in OAuth implementations to convey user identity.

In depth

OAuth is primarily focused on authorization, enabling third-party applications to obtain limited access to user accounts on an HTTP service, such as granting access to a user's information without sharing their password. It involves redirecting users to a service provider to grant permissions and then returning an access token to the application. JWT, on the other hand, is a token format that is used to represent claims securely between two parties. It can be signed or encrypted to verify the authenticity of the transferred data. JWT can be used as an access token in the OAuth flow, containing user identity and scopes, allowing the server to validate requests efficiently without needing to store session state on the server side, enhancing scalability and performance. Both concepts are often used together where OAuth manages the authorization, and JWT is the method of token exchange.

Example scenario

In a marketplace application, when a user logs in with Google, OAuth might be utilized to authorize access to their profile information. The application will then receive a JWT that includes details like the user ID and permissions. This token is sent with every API request to authenticate the user and ensure they can only access resources they are entitled to, without needing to manage session states on the server.

Common mistakes

A common mistake is confusing OAuth with JWT, thinking that they serve the same purpose when they fulfill different roles. OAuth is about authorization, while JWT is a token format used within that context. Another mistake is not validating the JWT properly, leaving applications vulnerable to attacks; all JWTs should be signed and verified to ensure they haven't been tampered with. Developers also often neglect to set expiration times on JWTs, increasing security risks if a token is stolen.

Follow-up questions

  • How do you implement token expiration in JWTs?
  • What measures can you take to secure JWTs against common vulnerabilities?
  • Can you explain how you would refresh an expired token in an OAuth flow?
  • How does the use of scopes in OAuth affect permissions for different API endpoints?

They are not alternatives: different layers

The comparison confuses many candidates because OAuth 2.0 and JWT do different jobs. OAuth 2.0 is an authorization framework: it defines how an app obtains permission to call an API on a user's behalf. JWT (JSON Web Token) is a token format: a signed, self-contained string. An OAuth server may hand out access tokens that are JWTs, or it may hand out opaque random strings. Both are valid OAuth.

What a JWT looks like and how to check it

A JWT has three Base64URL parts separated by dots: header, payload (the claims) and signature. The payload is encoded, not encrypted, so anyone holding the token can read it. The signature only proves nobody changed it. When your API receives one, verify at least the signature, expiry, issuer and audience:

import jwt  # PyJWT

claims = jwt.decode(
    token,
    public_key,
    algorithms=["RS256"],            # never trust the alg in the token itself
    audience="api://orders",
    issuer="https://auth.example.com",
)
user_id = claims["sub"]

Where OAuth fits in a real flow

For a browser or mobile app the safe choice is the authorization code flow with PKCE. The app redirects the user to the login page, the user approves the requested scopes, the app receives a short-lived code and exchanges it for an access token (and often a refresh token). The app then sends Authorization: Bearer <token> to the API.

Pitfalls

  • Accepting any algorithm, including none. Always pin the allowed algorithms on the server.
  • Putting secrets or personal data in the payload because it "looks" private.
  • Long-lived access tokens. A JWT cannot easily be revoked before it expires, so keep access tokens short (minutes) and use refresh tokens for longevity.
  • Storing tokens in localStorage, where any XSS bug can read them. HttpOnly cookies or in-memory storage are safer.

How to say it in an interview

"OAuth 2.0 is the protocol for getting delegated access; JWT is one way to format the token it issues. I would use OAuth with the code flow and PKCE, issue short-lived JWT access tokens, validate signature, expiry, issuer and audience on every request, and rotate refresh tokens."

2,000 Interview Questions and Answers PDF cover
Developer Interview Kit · PDF

Want all 2,000 questions in one PDF?

  • Short answers, in-depth explanations, common mistakes and follow-ups
  • 89 topics, from Beginner to Architect, with a clickable contents list
  • 226 extra questions that are not on this website
  • Free Bonus Pack: 30-day study plan, behavioral questions, salary scripts

Includes 226 extra questions that are not on this site, plus a free Bonus Pack.

$7.99 one-time · instant email delivery
260 Developer Learning Paths cover
Also available: 260 Developer Learning Paths $4.99 Week-by-week roadmaps for 16 career tracks (Python, React, Java, DevOps, SQL, AI and more), with a progress tracker on every path. See what's inside →

Get both books for $9.99 (save $2.99):

The material stays free to read online. This is the organised, offline, print-ready PDF edition. Secure payment by PayPal; your personal download link is emailed after payment is confirmed. Digital product, no shipping.

Let's Talk

Have a Project in Mind?

Whether it's a software challenge, an AI integration, or a course enquiry — I'm always open to a real conversation.

hello@debasisbhattacharjee.com · Mon–Fri, 9AM–6PM IST