Web / API Series

🔑 Understanding How OAuth 2.0 Works From a Top 1% Perspective — the Authorization Code Flow, and Why an App Never Needs Your Password

Introduction

This article is part of the Top 1% Series Complete Article Guide, and the fourth article in the Web/API Series.

Prerequisite Knowledge

Getting the Big Picture

”Authentication” and “Authorization” Are Entirely Different Concepts

The first distinction to draw, in understanding OAuth 2.0, is between Authentication and Authorization.

TermMeaningExample
AuthenticationConfirming “who you are”Confirming you’re really you, with a password or SMS code
AuthorizationDeciding “what you’re allowed to do""Allow this app to only read your Google Calendar”

OAuth 2.0 isn’t an authentication mechanism. It’s an authorization mechanism. What a “Log in with Google” button actually does is “authorize this app to read some of your Google account information (name, email, and similar)” — the actual authentication, confirming who you are, happens on Google’s own side, behind the scenes.

sequenceDiagram
    participant User as User
    participant App as External App
    participant Google as Google (Authorization Server)

    User->>App: Clicks "Log in with Google"
    App->>Google: Requests authorization (redirect)
    Google->>User: Shows a login screen + a permission confirmation screen
    User->>Google: Logs in and grants permission
    Google->>App: Issues an authorization code
    App->>Google: Exchanges the code for an access token
    Google->>App: Issues an access token
    App->>Google: Calls the API using the access token

Deep Dive Into the Fundamentals

The Authorization Code Flow: Why Is It Split Into Two Stages, “Code” and “Token”?

The flow shown in the diagram above is OAuth 2.0’s most widely used variant, the authorization code flow. What stands out is its two-stage structure: it never passes the access token directly through the browser — it routes through a disposable, one-time “authorization code” first.

The reason for this two-stage structure is that the browser redirect path is a relatively low-security route — information sticks around in the URL and browser history. If the access token itself (a piece of information carrying real, strong power to call the API) were passed directly down this low-security path, the token itself would risk sticking around in browser history or in logs on equipment along the way. The authorization code carries no power on its own — it’s a single-use voucher, exchanged for the access token through a safer, direct back-channel connection between the app’s own server and the authorization server. This mechanism means the access token, which carries real power, never has to travel down the low-security path at all.

Why Is a "Client Secret" Needed to Exchange the Authorization Code?

When exchanging the authorization code for an access token, the external app also has to present a client secret — a secret string registered with the authorization server ahead of time. This acts as a password proving “the one trying to use this authorization code is genuinely the legitimate app the user actually authorized.” The authorization code alone could, if stolen, be reused from a different (fake) app by a third party — but combining it with the client secret, something only the app’s own server knows, prevents this kind of misuse.

Access Tokens and Refresh Tokens: Why Are There Two Kinds?

OAuth 2.0 ultimately issues two kinds of tokens: the access token and the refresh token.

Keeping the access token’s lifespan short limits the damage, in time, if it ever gets leaked. But if the lifespan stayed that short across the board, users would be forced to log in again constantly, hurting usability. To satisfy both demands — security and usability — at once, the design splits the roles into a “short-lived but freely usable access token,” and a “long-lived refresh token that’s only ever good for reissuing a new access token.”

What a Pro Sees Here (Top 1% Understanding)

OAuth 2.0 Isn’t “a Way to Authenticate” — Authentication Gets Layered on Top, Separately (OpenID Connect)

A very common real-world mix-up is believing “OAuth 2.0 can be used for user authentication.” OAuth 2.0 itself is purely an authorization mechanism — deciding what to allow. A standardized way of conveying “who you are” is a separate thing entirely. That’s OpenID Connect (OIDC). OIDC achieves authentication by layering an additional token, the ID Token, carrying the user’s identity information, on top of OAuth 2.0’s own mechanism. “Log in with Google” actually works as a login (authentication) because OIDC is used alongside OAuth 2.0 behind the scenes — OAuth 2.0 alone, strictly speaking, never achieves a “login.”

Common Misconceptions and Pitfalls

Troubleshooting Perspective

  1. An error like “this app is invalid” occurs when exchanging the authorization code: Check whether the client secret’s value is correct, and whether the registered redirect URL exactly matches the actual URL.
  2. The access token expired, and API calls suddenly started failing: Check whether the access-token reissue process, using the refresh token, is implemented correctly.
  3. Users are unexpectedly forced to log in again frequently: Check whether the refresh token itself has expired, or whether there’s a problem in how the refresh token is being stored and reused.

Summary

Takeaways to Apply Today

  1. Whenever you see the word “OAuth,” build the habit of first distinguishing whether it’s being used for authentication or authorization.
  2. When implementing an integration with an external service, confirm you’re handling the access token and refresh token correctly, matched to each one’s own role.

References

Comments

Feel free to leave thoughts, questions, or corrections below (sign in with a GitHub account to comment or react).