SECURITY ENGINEERING · PART 8 · WRITING SECURE CODE · AUTH · 115 OF 229

OAuth 2.0 and OpenID Connect Explained Simply

OAuth 2.0 is for delegated authorization; OpenID Connect adds authentication on top. This episode explains both in plain terms: the four roles, why OAuth is about access not login, the authorization code flow with PKCE as the default, access and refresh tokens and scopes, what OIDC adds with the ID token, the common mistakes (implicit flow, missing state, open redirects), and how to use them safely.

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 9
  9. 10
  10. 11
  11. 12
  12. 13
  13. 14
  14. 15
  15. 16
  16. 17
  17. 18
  18. 19
  19. 20
  20. 21
  21. 22
  22. 23
  23. 24
  24. 25
  25. 26
  26. 27
  27. 28
  28. 29
  29. 30
  30. 31
  31. 32
  32. 33
  33. 34
  34. 35
  35. 36
  36. 37
  37. 38
  38. 39
  39. 40
  40. 41
  41. 42
  42. 43
  43. 44
  44. 45
  45. 46
  46. 47
  47. 48
  48. 49
  49. 50
  50. 51
  51. 52
  52. 53
  53. 54
  54. 55
  55. 56
  56. 57
  57. 58
  58. 59
  59. 60
  60. 61
  61. 62
  62. 63
  63. 64
  64. 106
  65. 107
  66. 108
  67. 109
  68. 110
  69. 111
  70. 112
  71. 113
  72. 114
  73. 115
  74. 116
  75. 117
  76. 118
  77. 119
  78. 120
  79. 121
  80. 122
  81. 123
  82. 124
  83. 125
  84. 126
  85. 127
  86. 128
  87. 129
  88. 130
  89. 131
  90. 132
  91. 133
  92. 134
  93. 135
  94. 136
  95. 137
  96. 138
  97. 139
  98. 140
  99. 141
  100. 142
  101. 143
  102. 144
1 / 12

Press play to hear the narration, or step through at your own pace.

Key takeaways

  • OAuth = authorization, OIDC = authentication
  • code flow with PKCE
  • scoped short tokens
  • verify the ID token

Notes

The four roles

OAuth has four roles, and naming them clears up most confusion. The resource owner is the user who owns the data. The client is the app that wants access. The authorization server issues tokens after the user consents. The resource server is the API that holds the data. Example: you are the resource owner, a photo-printing app is the client, Google's login is the authorization server, and Google Photos is the resource server. The app reaches your photos without seeing your password.

Who is who
  • resource owner: the user who owns the data
  • client: the app that wants access
  • authorization server: issues tokens after consent
  • resource server: the API holding the data
  • example: you, a print app, Google auth, Google Photos

OAuth is authorization

Here is the point people miss. OAuth 2.0 is about delegated authorization: this app may read my photos. It grants access to resources. It was never designed to tell the client who you are. Many apps misuse an access token as proof of login, which is a known anti-pattern, because an access token says what you may do, not who you are, and it can be obtained in ways that do not prove identity. For login specifically, you need OpenID Connect, which we come to. Keep the two ideas separate.

The key distinction
  • OAuth 2.0: delegated AUTHORIZATION "this app may read my photos"
  • not designed to tell the client WHO you are an access token as login proof is an anti-pattern
  • for login you need OpenID Connect (later)

The code flow

The flow you should use is the authorization code flow. The client redirects the user to the authorization server. The user logs in there and consents. The server redirects back to the client with a short-lived, one-time authorization code in the URL. The client then exchanges that code, from its backend, for the actual tokens. The clever part is that the code seen in the browser is useless on its own: an attacker who captures it cannot complete the exchange, which needs a second step.

Authorization code flow
  • 1. client redirects user to the authorization server
  • 2. user logs in and consents
  • 3. server redirects back with a one-time code
  • 4. client exchanges the code (on its backend) for tokens
  • the code is useless to an eavesdropper without the exchange step

PKCE

PKCE, proof key for code exchange, makes the code flow safe everywhere. The client generates a random secret, the verifier, and sends its hash, the challenge, when it starts the flow. When it exchanges the code, it sends the original verifier, and the server checks they match. So a stolen code is useless to anyone without the matching verifier. Originally for mobile and single-page apps, PKCE is now recommended for all clients.

PKCE (proof key)
  • client makes a random secret (verifier) and its hash (challenge)
  • sends the challenge when starting, the verifier when exchanging the code
  • server checks they match
  • stops a stolen code being used by anyone else
  • now recommended for ALL clients, not just mobile

Tokens and scopes

Two kinds of token flow out. The access token is what the client sends to the API; it is short-lived and carries scopes, the specific permissions it grants, such as read-only photos. The refresh token is longer-lived and gets new access tokens without a fresh login, and it can be revoked. Scopes apply least privilege: request only what you need, so a leaked token does little. An API should always check the token's scopes.

Tokens
  • access token: sent to the API, short-lived, carries scopes
  • scopes: the specific permissions ("photos.read")
  • refresh token: longer-lived, gets new access tokens, can be revoked
  • principle of least privilege: request only the scopes you need

OpenID Connect

OpenID Connect is a thin layer on top of OAuth 2.0 that adds the missing piece: authentication. It introduces the ID token, a signed JWT that states who the user is, with claims like a stable subject identifier, email and name. You request it with the openid scope, and there is a userinfo endpoint for additional claims. This, not a raw access token, is the correct way to implement sign in with Google or any federated login. If you need to know who the user is, you want OIDC and the ID token, verified properly.

OIDC adds identity
  • a thin layer on OAuth 2.0
  • adds the ID token: a signed JWT that says WHO the user is (sub, email, name)
  • the "openid" scope requests it
  • a userinfo endpoint for more claims
  • this, not a raw access token, is how you do "log in with..." correctly

The mistakes

Now the mistakes, which cause real breaches. The old implicit flow returns the token in the URL where it leaks; it is deprecated in favour of code plus PKCE. Skipping PKCE. Not validating state, which protects the flow against cross-site request forgery. Loose redirect URI matching enables token theft; URIs must be exact-matched against an allowlist. Using an access token as login proof instead of the ID token. And not fully verifying the ID token.

Known security pitfalls
  • implicit flow (token in the URL): deprecated
  • no PKCE
  • not validating state (CSRF on the flow)
  • open redirect_uri: exact- match, allowlist only access token used as login proof (use the ID token)
  • ID token not verified: sig, iss, aud, exp, nonce

Using them safely

A short checklist for using these safely. Always use the authorization code flow with PKCE. Exact-match and allowlist your redirect URIs. Validate the state and nonce parameters. Request minimal scopes. Keep access tokens short-lived and make refresh tokens revocable. Verify ID tokens fully, and do it with a vetted OpenID Connect library rather than parsing JWTs by hand, which is where many mistakes happen. And run all of this through a real identity provider rather than implementing the protocol yourself.

Do this
  • code flow + PKCE, always
  • exact-match redirect URIs, allowlisted
  • validate state and nonce
  • request minimal scopes
  • short access tokens; revocable refresh tokens
  • verify ID tokens fully with a vetted library
  • use a real identity provider

Common mistakes

The recurring findings, together. The implicit flow, or the code flow with no PKCE. An access token treated as proof of login. Missing state or nonce validation. Loose redirect URI matching that allows token redirection to an attacker. Over-broad scopes that grant far more than needed. An ID token whose signature or claims are not verified. And refresh tokens that never expire or cannot be revoked, so a single theft is permanent. Most of these are avoided by using a good library and provider correctly.

Avoid these
  • implicit flow or no PKCE
  • access token treated as login proof
  • missing state / nonce validation
  • loose redirect_uri matching
  • over-broad scopes
  • ID token signature or claims not verified
  • refresh tokens that never expire or cannot be revoked

Say it like this

In the interview: OAuth 2.0 is delegated authorization, granting an app scoped access to resources through tokens, without sharing the password. OpenID Connect adds authentication on top, a signed ID token that tells the client who the user is. For login I use OIDC and the ID token, not a raw access token. I use the code flow with PKCE everywhere, exact-match redirect URIs, validate state and nonce, request minimal scopes, and fully verify the ID token with a vetted library.

In the interview
  • "OAuth 2.0 is delegated
  • authorization: access to resources via scoped tokens.
  • OIDC adds authentication on top, with a signed ID token
  • saying who the user is. I use
  • the authorization code flow
  • with PKCE, exact redirect
  • URIs, state and nonce, and fully verify the ID token."

Interview questions

Q1What is the difference between OAuth 2.0 and OpenID Connect?

OAuth 2.0 is a framework for delegated authorization: granting an application scoped access to resources on a user's behalf. OpenID Connect is an identity layer on top of OAuth 2.0 that adds authentication, providing a signed ID token that tells the client who the user is.

Q2Why should you not use an OAuth access token for login?

An access token conveys what an app may do, not who the user is, and can be obtained without proving the user's presence to the client. Using it as login proof enables token substitution attacks. OpenID Connect's ID token is the correct mechanism for authentication.

Q3What is the authorization code flow?

The recommended OAuth flow: the client redirects the user to the authorization server to log in and consent, receives a one-time authorization code on the redirect, and exchanges that code from its backend for tokens. The code alone is useless without the exchange.

Q4What is PKCE and why is it important?

Proof Key for Code Exchange: the client sends a hashed secret (challenge) when starting the flow and the original secret (verifier) when exchanging the code, so a stolen authorization code cannot be used by anyone else. It is now recommended for all client types.

Q5What are scopes in OAuth?

Named permissions that limit what an access token can do, such as read-only access to a particular resource. Requesting only the scopes an application needs applies least privilege and reduces the impact of a leaked token.

Q6What is the difference between an access token and a refresh token?

An access token is short-lived and sent to the resource server to access data. A refresh token is longer-lived and used to obtain new access tokens without re-authenticating the user; it should be revocable and stored securely.

Q7Why is the implicit flow deprecated?

It returned tokens directly in the redirect URL, where they could leak through browser history, referrer headers and logs, and it lacked the protection of the code exchange. The authorization code flow with PKCE replaces it for all client types.

Q8What must you validate on an OpenID Connect ID token?

Its signature against the provider's keys, and the issuer, audience and expiry claims, plus the nonce to bind it to the request. Validation should be done with a vetted OIDC library rather than by parsing the JWT manually.