# How OAuth 2.0 works: every flow, drawn out

> OAuth 2.0 lets an app act for you without seeing your password. Here is every flow drawn step by step: authorization code with PKCE, with and without a client secret, the retired implicit flow, client credentials and the device flow, and which one to use.

## Key takeaways

- OAuth 2.0 is about authorization: what an app may do for you. Who you are is OpenID Connect's job.
- For any app with a user, use the authorization code flow with PKCE. It works with or without a client secret.
- The implicit flow and the password grant are retired. Don't build new apps on them.
- Use client credentials for service-to-service calls with no user, and the device flow for TVs and command-line tools.

[Open this diagram in archhy](https://archhy.com/app#d=N4IgLglmA2CmIC4QHkCCBXMALABAJgDoAGHAQ0ywHsAnCAL1MkoDscBjSgE1hwDNpKAdxyCouAAoBpAMIBREABoQ0UgE9KmACIRqsNk2aIQAGQBKikAFtYYUolABzAQCNS0AM72Q7qoIByXLAAKqoADrCeCLxu7rBKEO4AysykobIpznCciGDU6HEgDrScifTwCHhESlhuvADixaakEB6I0R4F1M3QiWCqcEZscKSGSqEQ+ui6XpAw5bn5AL7LSsyBkQDaoBDZSOix1BYqzrDQRgCaGjgAFNg8uu4a1Gw8Qsyw1ACUFn3hRgCqByOag0YC8AA9EFUQKpEHgAGzQ0ScbCIACMiOqsAgDiwYIQaLRRBW212ICGEFgzDBSmOpyM0mglOpZFCoRuEFYpBwzmoQgOOBogq5OFCVHe3yUv3KIDqVI+E1MESeLwsPiEIXCxlIJzOURiBQmLHEjCwRgA9AA3KmcGjm0iCdzm7jRdDQMDmpWPKYvAC0AEkOMx3AB9IgAdgAzGjKngAKyeiIhuXvLrQANBp1ekMAFgAHCHjDi8YnQ4zmWBcwWi7iwAR3JaHMD1JgIYgc0RobCKpiQMjUQTe1hsbX0USSSAdkZUhBgbqjKhxP6bg8VTwDtavj8wjKUwq2F612rfJrYNr5-qOvEgyaByArTa7Q6nS7yO7S2uMyxQxHo7GE9me5pl+wallWhbFh62aJB8m7gTWeL1o2zagm2CAdl2iBxpUSj9mag7QsOkFjsSiwKKS06eLSOr0kgGDYDQ9CMBALA4BuHzbn8SAAEI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fde-Cz5uI8V43hbDKAcL8UIwJt8xFLEkc-JAFI4ngHfuMi6P8kBxn-uIZAiQghhW4DgXJWSQybggBfCu5JCl9xAP3UgeY4xxl4OGcxlTLHVLngwuxDSnFIA3lvIyO93HcMabQ5pgiAk31EQge+oThmTOOpXWJ1JpkgHhP-dJLx3ChnOlSG4uTdDFwiFgEMCLmBRTwfsw5xzTnnPKVQy5M8am3PqYldpzivmtPeQ88Am9iI9J+UEgZEjQBhLBXdGJjlxgQvDP-bixDdDUDIGwWF8LDogt2UYgeGJSCRhzHYIlFjSU3LqfY2AVLHm+lErJaRtLPE8M+b4ouvTfnBMGRyoFM4FFIF5vzCFeZ-7SGHG3fyotGBTDiGQdAnAYFWVCAguBBTZUHKOScs5Fyp5XPAGSjV9yWFPKZeALhRqPnH1Pt84R-T-niNpJyWAkkWEyTktkRY6dFhAA)

How OAuth 2.0 works is easier to see than to read. OAuth lets an app act for you without ever seeing your password: you sign in at a server you trust, and the app gets a short-lived **access token** with limited rights. There are several ways (grants, or flows) to get that token, for apps with a secret, apps without one, apps with no user at all and devices with no keyboard. This guide draws each one, step by step, and says which to use today.

## The four parties

Every OAuth flow has the same cast. The **resource owner** is you, the person whose data it is. The **client** is the app that wants access. The **authorization server** signs you in, asks your consent and issues tokens. The **resource server** is the API that holds your data and accepts tokens. In a real system the authorization server is often a product such as Microsoft Entra ID, Okta, Auth0, Amazon Cognito or Keycloak.

Messages travel two ways. The **front channel** goes through your browser as redirects, so the URL bar sees them; those are blue in the diagrams. The **back channel** is a direct server-to-server HTTPS call that the browser never sees; those are purple. Green is the app calling the API with its token. Anything that can leak through a URL belongs on the back channel.

## Authorization code flow with PKCE, step by step

This is the flow in the diagram above and the one to reach for first.

1. You click *Sign in*. The app makes a random secret, the `code_verifier` (43 to 128 characters), and hashes it with SHA-256 into a `code_challenge`.
2. The app redirects your browser to the server's `/authorize` endpoint with its `client_id`, the `scope` it wants, a random `state` and the `code_challenge`.
3. You log in at the authorization server, not in the app, and approve what the app asked for.
4. The server redirects back to the app's registered `redirect_uri` with a one-time **authorization code** and the same `state`. The app checks that `state` matches, which stops a forged callback.
5. The app posts the code and the original `code_verifier` to the `/token` endpoint on the back channel.
6. The server hashes the verifier, checks that it matches the challenge from step 2, and returns an access token and, often, a refresh token.
7. The app calls the API with `Authorization: Bearer <access_token>`.
8. The API checks the token: its signature against the server's published keys (JWKS) and its audience, expiry and scopes. For opaque tokens, the API asks the server's introspection endpoint instead.

Why PKCE? The code in step 4 passes through the browser, so someone could intercept it. Without the verifier, which never left the app, a stolen code is useless. That is why the current security best practice (RFC 9700) requires PKCE for public clients and recommends it for every client.

## With a secret or without: confidential and public clients

Every client has a `client_id`. It is an identifier, not a secret: it appears in every redirect URL. The real difference between flows is whether the client can also keep a `client_secret` (or a private key) hidden.

A **confidential client** runs on a server you control, such as a traditional web app backend. It sends its secret with the code at the token step, so the server knows the right app is redeeming it. The tokens stay on the server, and the browser only gets a session cookie.

![Authorization code flow for a server-side web app: the browser is sent to /authorize, returns with a code to the web app's callback; the backend exchanges the code with its client secret at /token, keeps the tokens and gives the browser only a session cookie.](https://archhy.com/blog/oauth-2-flows-explained/flow-confidential-client.png)

*A confidential client: the secret and the tokens never leave the server. [Open this flow in archhy](https://archhy.com/app#d=N4IgLglmA2CmIC4QEECuYAWB7AThAXgIaRYB2ABAMZYAms5AZtFgO6O7mHkDOsOAbnwC03CHXItYAI04AHWeQAU1UgzGxSkQtCrQIGsAEoQAGhDRCATyzoAIhByxKJUohAAZAEqmQAW1hghIigAObMUtrcwSDc2CwAcrSwACqWsrBRCAyRsGYQ3ADKpISyAKLFUnA0iGA4qLkgIXg0BQTwCABMAAxmGNoMAOLNnoQQ0JnZ4w04o9AFYJZwbpRwhK5mshDOqI7RkDDttfUAvqdmpEmZANqgYm5SOKy8OD4WUrDQbgCaNibkEBQuA8nnwfAt0m4Bho+FtPBkbDhKPAzLFWKl0u5CO9PlkcnkVAAFYgYNwAekEpBouFJhBY3FJdGyqGgYFJcO4C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4T7SDAp7Lx2djHSh+Q7hYCE-ykH8JSyI8gh-NLDhOGAEhQBgo10D4he4G4ZcV1XNczB11YA3NwTc1hhGIh3Lu48YiT2nuAIeARR7dzAfAnws957qCXnUFelk15YDdrvD2zwwQ71-NIG+rU3AihPgMUoyQo6UG0NAfMBYAD86dYAADJk7EALlgIuv9y6V2rrXHA9dKRgLwBA1uMRoFjx7kIPAoEcJIJHjAlsYAhBwAYCTZ2EA54tGwTUXBq8XaELcCmKh293YggPljByfoT6Xm4KIMgo10b6HIGQaAwUQDf2LiAEufp1SpU1FIcRkjG4yJblA9CKDYG910fowIOBh6JK0co0yM9DFYMXqYk4+CLFEIoamBBaMwHow0NQpA6oT5cPIAAanjjWOsDZmnkGvBANQXtAm-0IJqKUUoGBCiiSAqRqNYmQIWgo1BSBe4qMKmo9JyDNFoJSRgvJxiCkIGXuY9eqNqmY0qcmShaZD4OSlCfCOGNKx8MsD4igmAMIeQcf00ugzhmjPGSwUBUzm4zLbgk9ZCydFsVSeozJaCln6MwTsjIhS8GgBKVY85FTyEwVqSAIUJ8fIV0cDgTglAkRuP5LjGp1tBE-1LmGQgIp1RBCARIiZMTAVyOpkiaFYLQqxUsSsjJoLwDoNyUYheiK9lmItscIAA)*

A **public client** is anything that runs on the user's device: a single-page app, a mobile app, a desktop app or a CLI. Anything shipped to a device can be unpacked, so it cannot hold a secret. It uses the same flow with PKCE standing in for the secret. That is the "with an ID only" case: `client_id` plus PKCE, no secret.

## Implicit versus explicit: why the implicit flow was retired

Early single-page apps could not make cross-origin calls to a token endpoint, so OAuth offered a shortcut. With `response_type=token` the server skipped the code and put the access token straight into the redirect URL's fragment. People call the code flow "explicit" to contrast it with this one.

![Implicit flow: the single-page app sends the browser to /authorize with response_type=token; after login the access token comes back in the URL fragment, then the app calls the API with it. A warning note says the token in the URL can leak through history and logs.](https://archhy.com/blog/oauth-2-flows-explained/flow-implicit.png)

*The implicit flow hands the token over in the URL, with no way to bind it to the app that asked. [Open this flow in archhy](https://arch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kgDEkViKo3B8Hg2g6FwPAZBF0lfCQvjSG9sBMIILHgAKzhJQekqpe46XnllTI5ZU97agV-lFaa5qlZ+35ISD1XQYBDUgS1EFtfGcEYd1YEznAdAFl140VpNRg1tC9YgI2ZnEe26LrTiHbbcSQ5HAAuoYmrRvYfEwYgVwgAQ-yoLc4LgMQzg0nSdNsQA8gkME3Ty6B8oLxAkA8SAAMQoomunJnI7hSf8pAFIxZQQMsbJg0eAASyxDYK0jsjUlnxVJ3JgO7EnzEgYDsnABAkoYBAQDCbD1DQwQIPssAjqAUuSIrtJAgr7mBd5p1MiA2J4AA7XgS7oABsBeglAC8EXCCsIASAbzhG2030hrAACcVt0DbdvOA7TvzHKbse6HoTshFuvgAHsBB1Pgrh5H0eS3HCe1Ocqfp5L0vjgXTS57LQmsWdLTl3gOjEL43x4B7j86-8xruG3Zod13TA9-3hjW2QYeYcahjzSpPEOXtZ7EHnl4Oggdg6exHlA+esd451B3snPe+1M4y3zrwLyJ94p51CDnC+xd-TXyNooGU0hvRN1EPXQAZAQP3UHdXQ799af2Nl+RMvDEwDyHhJEeIDWTj1COAxB1II5fg3rA+BK8kFz3cKg7eSdSjlDTtgw+1JSHy3HGsIuzhEzXxEt9BgdBH7YBoXQy6HD27G1xEwf0iZlYANtkI4BjtRFgNSgg6e3s5JqSzv7OBS8-Gr2Qcore6C1Epw0fvHBh0MgnSIdnZWvli4AHUeAmBUGAPApBmKVyXrYNgdiuEgCNjwvhAjAEePACI52E9fEKKkevIEi9l4QKQb7HmMdomJ13hoiEqhYBKWnqpdSIADh8wOEAA)*

The token in the URL can leak through browser history, logs and referrer headers. It can be injected by an attacker, and there is no back-channel step where PKCE or a secret could protect it. RFC 9700 says clients should not use it, and the OAuth 2.1 draft leaves it out altogether. Browsers now support CORS, so a single-page app can run the code flow with PKCE instead.

## Client credentials: no user at all

When a backend job calls another service on its own behalf, there is no person to log in. The client authenticates directly at the token endpoint, with a secret or better a signed JWT (`private_key_jwt`) or mutual TLS, and receives an access token. There is no redirect, no consent screen and usually no refresh token: the client just asks again.

![Client credentials flow: a backend service authenticates at the token endpoint with grant_type client_credentials and receives an access token, then calls the API, which checks the token against the signing keys.](https://archhy.com/blog/oauth-2-flows-explained/flow-client-credentials.png)

*Client credentials: two hops, and only for confidential clients. [Open this flow in archhy](https://archhy.com/app#d=N4IgLglmA2CmIC4QGFoVgOzAAgMYCdYATTSAQ2gGdsAzaAewHcFsBbM3ACwg1mzHpsO3XgBpsGQQFdKsfCFEhoZAJ70pYACIRCuSPQyIQAGQBKCkK1hgyiUAHMGAIwqU7ISpyYA5eiQAqKgAOsG4INK6wihCUAMoYZEEAoglOcESIYPhSUSD2+BBEsRAAXvAIAEwADIqcFDQA4gVEpmQQVIgRVLn4bdCxYCpwRrhwZIaKQRB6UoTukDDlWTkAvmuKkiRhANqghUay+ABu0-CKyk6w0EYAQhwA1phE2IcnuHz0+NgAVvRO2AAKXAGGiFUgQCh4NCkACUFkGISMDUwcmmplC6nw7wsniYgRCxjIl2u4Ui0WBGAACmQwJwjAB6I5PT70siMSj0kgRKTQMD09GUTHvAC0AEkKZQAPpVADsAGYAIzVCoAVn5oUlyN4vWgYol6qlABYAByS4wQeycPkCyWxORM-BG03my1gAB0lCO9gsyjUGncAA9EDUQCpg4pGIVaYgFQA2EOcWAWq0xhVVdZ7DJIRIQH1Eq5GACClNFgMIgtm7xe9rkcMUCPKIC1qNwAqFZw8XkY+NghOJnTJIGmBmp0aQjOZ+FZ7M5sG5vIN7b1BilssVyrVNubOuXGA5NpNZuT1o1duOcklh5dVo9XrzfrAgfDocQFXjEajdIQcYTSddqfTFZREzIwyDcc58xJEBCw0LwChKGkIAMatz3ketgkbG51AwIgyHwMNFCcT4SHwAYhkbIh6DAMBiHvdRHwQUAgwQOVDRDMMEGFd8QEjIgxwqY1f2PRBDQAThDEpRRw2BmOFNMqkA4ChyzcB6EeCYlEghkBHU7AniCegeEfdDESQbc0QxSsO1xbsML7AtSW6ckRxpL8QAnHCWTZDkuTIHkTwrLFYF3Vd5SVKpVQNTUUR3cUVyiq9jyi5BCBILAISoS9nWPW9vUmPDSFFFSwLo-1GJAZjuI47jeLHH9aj-FNvzTDNlKMb5GHucDNP7JBinsDAeHsbBHhUagAQAKQAdQAaViOtwAwpEYoswLsUUGye3skkulkZyqVchkmU8qdvNnecAqXOK92lMKNyi7cKBChLstdKLYm4K4iCyo9XVyiwggKrAitA7rfXop8ECqxA5WqD8+K-eqQETYTmsAgBdRQKV4PQkL3RBdhAGh8HoVgDntU54XoIwdMwPNepABVsAAdqhdAsElAhiHBVxsAAajZ0hsD86MsfoBh5CQABiMhjRVFUaBlCw8JJxgUhUrIIQwRxrPbAAJcYiGGJBKGFAp-3rPD7GsA2cON8BhTgGhjKJiAA2IYotkybJYCA0BidJmm1Lp+tqZNin1p6hyQAqFnhdwd5KClWmMEBSRsBkORxHTwhidCTgFuBCWjBluWFaVxQVaYdWaYKcYdZxfXDft02nZdmx8GtsBbaNxswDN4TFFBd2ijBMJll9pSA7J8Pjkp0PQKmeno7lOOblgAqvg4RPk+DjSi8+Eu4zIVjbEr-BVZrpBNfr4YNqbu3G1NojqMDy3O5t5u++FAQggsYePZj29qsKeJMZ4gBzFTdqnUwZaSQIaOOyBEy4HuC8C0CQwCzCiMLKQRBxCUGBIiMWxdpay3lorZWF9q44VrlrBu98rI9xbgPC24Araf0fjTR2c4XYANHl7BAE9zg8FgGRe2lFqK0RWOjFYQA)*

## Device authorization: TVs, consoles and CLIs

A smart TV or a terminal has no comfortable browser, so the device flow (RFC 8628) moves the login to your phone. The device asks the `/device_authorization` endpoint for a `device_code` and a short `user_code`, and shows you the code and a URL. You enter the code on your phone and approve. Meanwhile the device polls the token endpoint with the grant type `urn:ietf:params:oauth:grant-type:device_code`. It gets `authorization_pending` until you approve, and `slow_down` if it polls too fast.

![Device authorization flow: the device gets a device code and user code, shows the code and URL to you, you open the URL on your phone and approve, while the device polls the token endpoint until it receives tokens.](https://archhy.com/blog/oauth-2-flows-explained/flow-device-code.png)

*The device flow: the login happens on a second screen. [Open this flow in archhy](https://archhy.com/app#d=N4IgLglmA2CmIC4QBFYDcIGNYAICGArmABYD2AThAF56SkB2OAZtKQO4I4DOEA5oxEZ4cAFQBqOCjgDCAGQCSzcqQC2OAJ6kC5HAAcy9eABoQ0PJqLII5WJjr1EIWQCUQJlbDB5EoXqwBGeNBcPiBcZGwAcqQAJrAi6rqwIQhMQVywJhBcAMr0eLoAovn+cDGIYOQEmSC8lDE51PAIAEwADCbEQUwA4vXOeBDBiGnBNeSD0Dlg6nCOmHB4Dia6WGDazaCQMM2V1QC+hyb0scmIANqgEOVIBBnkbqZ4-rDQjgCaWo8zSY4AqvdHmYLGBQgAPRAdEDqRAtABsULY1xIiAAjAjOrA+MRQQhUai2kcrjcQHEMNggc9Xo5UOTYJxxEYZApJDoANYQUhcNnfRLNEA9WCGSiYZzJLTkCkmcLsBJJWRUt6pdI1LAMAAKtGIjgA9GghTEKDq8GwuDq4mkCNAwDqxVwJdgALTyTAMLgAfTaAHYAMyo9otACstuS7sFwqCztd9DNdvdABYABzu2TYm1x6TQCBCsAJ5Op3g4gB0XDQvEpIPBiHjbShMNaGJASJiKLxjeIWMLuPxhP2RmJjn0DGMTxeSpAn20egMuCkZl0YFIuhw-mUptgDxMP354Y3WDtDpHMrYctgCrHIxVWWjmtbID1BqNJrNFsI1pD9u0Tpdbs9vv9bRBh+YZChukY-jGwFJimabAQAsqQ-hDLAjoLNm9C5tBBbFqW5YmMCWi4qAEIIHWiCBu0JjNq26JQh2aZogSRIgNcjh4CE+GKo4ACCRBkJQND2NwG76pu4B8o4ABCWj0DEeDkDCJj+BQcTkNMsz8oaYBgLA5ScZWCDEYgXo1iY9aOo21Haq0iZ0Z2OLGYGUJUPIsmwCRjoErWzGsUgZKECinEXkg5roFgsDugF-HULQnLLOJvxILuIoHl+R4RKe57UsqYzXhqWq6vqslPqaoWWu+qWSihEEet6foBsGcbJeB0axqGWGwU1sDyXmMFdiWZaPLo8k5vIJLsRWhFV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IVZgAyAiSYdA6MYUWoOHhizBIBsFsFwQU3FxTrbYFuY4TebzrFkvMfQ+5VKWnuO+QcdO+wgA)*

## The password grant: don't

The resource owner password credentials grant lets an app collect your username and password and trade them for a token. It teaches users to type passwords into apps and breaks multi-factor sign-in and single sign-on. RFC 9700 says it must not be used.

## Refresh tokens

Access tokens should be short-lived, often minutes. A **refresh token** lets the client get a new access token from the token endpoint without sending you back through the login. Because it lives longer, it is the more valuable thing to steal. For public clients the refresh token must either be sender-constrained (bound to a key the client holds, as DPoP does) or rotated, so each use returns a new one and replaying an old one is detected.

## Which flow should I use?

- **Web app with a backend:** authorization code with PKCE and a client secret or private key.
- **Single-page app:** authorization code with PKCE. Better still, a small backend-for-frontend that keeps the tokens off the browser.
- **Mobile or desktop app:** authorization code with PKCE, through the system browser, never an embedded web view.
- **Service to service:** client credentials.
- **TV, console or CLI:** device authorization.
- **Signing users in:** any of the user flows plus OpenID Connect, covered in [OpenID Connect explained](https://archhy.com/blog/openid-connect-explained).

## Draw your own

Every diagram here is an archhy diagram you can open, change and export: swap the generic authorization server for your own identity provider and label the scopes your API really uses. The colours carry the same meaning in each one: blue through the browser, purple server to server, green to the API.

## Frequently asked questions

### Is OAuth 2.0 authentication or authorization?

Authorization. It gives an app limited access to an API for you. To learn who the user is, add OpenID Connect, which adds an ID token on top of OAuth.

### Is the client ID a secret?

No. The client ID is a public identifier that appears in redirect URLs. Only confidential clients, which run on a server, also hold a client secret or private key.

### Do I still need PKCE if my app has a client secret?

It is recommended. RFC 9700 requires PKCE for public clients and recommends it for confidential ones, because it also protects against authorization code injection.

### Can I still use the implicit flow?

You should not for new apps. RFC 9700 says clients should not use it, and the OAuth 2.1 draft removes it. Use the authorization code flow with PKCE instead.

*Based on:* Original archhy explainer and diagrams, checked against [RFC 9700, OAuth 2.0 Security Best Current Practice](https://www.rfc-editor.org/rfc/rfc9700), [RFC 7636 (PKCE)](https://www.rfc-editor.org/rfc/rfc7636), [RFC 8628 (Device Authorization Grant)](https://www.rfc-editor.org/rfc/rfc8628) and the [OAuth 2.1 draft](https://datatracker.ietf.org/doc/draft-ietf-oauth-v2-1/) (work in progress).
