SiteTidy
HomeGuidesImplementing OAuth 2.0 (Part 1): The Modern Authorization Model
Security

Implementing OAuth 2.0 (Part 1): The Modern Authorization Model

Understand OAuth 2.0 before implementing it: roles, authorization vs authentication, authorization code + PKCE, tokens, redirect URIs and current security guidance.

Published on August 15, 2026Updated August 31, 2026
Implementing OAuth 2.0 (Part 1): The Modern Authorization Model

OAuth is often introduced with a diagram containing arrows, tokens and a reassuringly large “Authorize” button. Then somebody implements the diagram, calls it login, stores a bearer token in the wrong place and discovers that understanding the protocol matters rather more than drawing it.

So Part 1 starts with the model.

OAuth 2.0 is an authorization framework. Its core job is to let a client obtain limited access to a protected resource without requiring the resource owner’s credentials to be handed to that client.

If your actual requirement is “sign this person into my application and tell me who they are,” OAuth alone is not the complete protocol. That is the territory of OpenID Connect (OIDC), an identity layer built on top of OAuth 2.0.

That distinction is the first thing to get right.

A concrete example

Imagine a photo-printing application that wants permission to read selected photos from a user’s cloud-photo account.

The bad design is obvious: ask the user for their cloud-photo username and password, then impersonate them.

OAuth changes the relationship:

  1. the printing app sends the user’s browser to the photo provider’s authorization server;
  2. the provider authenticates the user itself;
  3. the user approves a defined level of access;
  4. the printing app receives an authorization result;
  5. it exchanges that result for an access token;
  6. it presents the access token to the photo API.

The printing app never needs the user’s photo-provider password.

That is delegated authorization.

The four roles you need to recognize

OAuth terminology becomes much easier when you map the names onto real components.

Resource owner

The entity capable of granting access to the protected resource. In user-facing flows, this is commonly the end user.

Client

The application requesting access.

“Client” does not necessarily mean browser. It could be a server-side web application, a single-page application, a native mobile application, a CLI or a backend service.

The type matters because different clients have different abilities to keep credentials secret.

Authorization server

The system that authenticates the resource owner as necessary, obtains authorization and issues tokens to the client.

Resource server

The API or service that hosts protected resources and accepts appropriate access tokens.

In a small system the authorization server and resource server may belong to the same organisation. Conceptually they still perform different jobs.

OAuth is not authentication

Suppose an application receives an access token that can call:

GET /calendar/events

What has it learned?

It has learned that the token grants some level of access to a calendar resource. That does not, by itself, define a standardized authentication ceremony for the client or give the client a standardized identity assertion about the user.

Developers historically filled that gap by calling a provider-specific “profile” endpoint and treating the result as login. That can work inside a proprietary ecosystem, but it is not what OAuth itself standardizes.

OpenID Connect adds the identity concepts needed for interoperable login, including the ID Token and UserInfo endpoint.

A useful rule is:

  • use OAuth when the question is “may this client access this resource?”;
  • use OIDC when the question includes “who signed in to this client?”

Real applications frequently use both at the same time.

The modern default: Authorization Code + PKCE

If you learned OAuth years ago, you may remember several browser-oriented flows being presented as roughly equivalent choices. Current security guidance is much more opinionated.

For interactive authorization, the Authorization Code flow with Proof Key for Code Exchange (PKCE) is the modern baseline.

PKCE was originally designed to protect public clients such as native applications, but current OAuth security guidance applies it much more broadly. Public clients must use it, and confidential clients should use it as well.

The important practical point: do not treat PKCE as a mobile-app-only extension.

What PKCE actually protects

Without PKCE, stealing an authorization code can be extremely valuable because the attacker may be able to redeem it at the token endpoint.

PKCE binds the authorization request to a secret, temporary value created by the client for that transaction.

The client generates a high-entropy code verifier:

code_verifier = cryptographically_random_value

It derives a challenge, normally using SHA-256:

code_challenge = BASE64URL(SHA256(code_verifier))

The authorization request includes the challenge:

response_type=code
client_id=client-123
redirect_uri=https://app.example.com/oauth/callback
code_challenge=...
code_challenge_method=S256

The verifier stays with the client.

Later, when the client exchanges the returned authorization code, it sends the original verifier:

POST /token

grant_type=authorization_code
code=...
redirect_uri=https://app.example.com/oauth/callback
client_id=client-123
code_verifier=...

The authorization server calculates the challenge from that verifier and compares it with the challenge bound to the authorization transaction.

An attacker who merely steals the authorization code should therefore be unable to redeem it without the verifier.

Use S256. Do not deliberately fall back to the plain challenge method in a new implementation.

The browser redirect is part of the security boundary

Interactive OAuth depends heavily on redirects, which means redirect handling deserves the same care as token validation.

A typical authorization request looks conceptually like:

https://auth.example.com/authorize?
  response_type=code&
  client_id=client-123&
  redirect_uri=https%3A%2F%2Fapp.example.com%2Foauth%2Fcallback&
  scope=photos.read&
  state=...&
  code_challenge=...&
  code_challenge_method=S256

After authorization, the browser returns to the registered callback:

https://app.example.com/oauth/callback?code=...&state=...

Redirect URIs should be exact

Authorization servers should compare registered redirect URIs using exact string matching, with the specific localhost port exception defined for native apps.

Do not build production clients around patterns such as:

https://*.example.com/callback

or accept arbitrary redirect_uri values because they happen to share part of a domain name.

Loose redirect validation creates opportunities to leak authorization codes or tokens to attacker-controlled destinations.

The same principle applies to open redirectors. An endpoint such as:

https://app.example.com/redirect?to=https://anything.example/

can become part of an OAuth attack chain if it blindly forwards browsers to arbitrary locations.

state, PKCE and CSRF

OAuth callback endpoints need protection against forged or confused authorization responses.

Historically, clients generated a random state value, bound it to the browser session, sent it in the authorization request and verified that the returned value matched.

That remains a common and useful pattern.

Current OAuth security guidance also recognizes PKCE as providing CSRF protection when the client has ensured the authorization server supports PKCE correctly. OpenID Connect has additional transaction-binding mechanisms such as nonce.

In practice, do not simplify this to “state is obsolete” or “PKCE replaces every other check.” Your library and protocol profile may use several values for distinct protections. Follow the authorization server’s supported standards and validate every value your chosen flow requires.

Most importantly, these values must be transaction-specific, unpredictable where required, and securely bound to the browser session that initiated the flow.

Access tokens and ID tokens are not interchangeable

This causes a remarkable number of broken implementations.

Access token

An access token is presented to a resource server to authorize access to protected resources.

Conceptually:

Authorization: Bearer eyJ...

The client should treat an access token according to the contract of the authorization server and API. It may be a JWT, but OAuth does not require every access token to be one. Some access tokens are deliberately opaque to the client.

Do not write client code that assumes it can decode every access token and use arbitrary claims inside it as application identity.

ID token

An ID Token belongs to OpenID Connect. It carries claims about an authentication event and the authenticated end user for the client.

It is not a replacement access token for calling arbitrary APIs.

If your application sends an ID Token to an API merely because “it is also a JWT,” the architecture is confused.

Bearer tokens deserve the name

Most OAuth access tokens are bearer credentials: possession is sufficient to use them unless additional sender-constraining mechanisms are in place.

That makes token leakage a serious event.

Do not put bearer access tokens in URLs:

https://api.example.com/data?access_token=SECRET

URLs leak into logs, history, monitoring systems and sometimes referrer information.

Use the HTTP Authorization header when the API profile expects bearer-token authentication:

Authorization: Bearer ACCESS_TOKEN

Always use TLS end-to-end for OAuth endpoints and protected API traffic.

Scopes are delegated permissions, not application roles

A scope expresses the access being requested or granted in an OAuth context.

For example:

photos.read
photos.write
profile.read

Good scopes describe useful capabilities without forcing every client to request enormous bundles of access.

Do not assume that an OAuth scope and an internal application role are the same thing.

admin, for example, is often too vague to be a useful public OAuth scope. The resource server still owns authorization decisions and may combine token permissions with tenant membership, resource ownership, policy, subscription level and server-side data.

A token saying invoices.read does not necessarily mean its holder may read every invoice in the database.

OAuth diagrams often emphasize a user-facing consent screen. Consent can be important, but it is not what makes OAuth secure.

Some first-party applications do not show consent on every authorization. Machine-to-machine flows have no end user to click “Allow.” Enterprise policy may pre-authorize clients.

Security comes from the protocol relationships: registered clients, validated redirect URIs, protected authorization transactions, appropriate token issuance, audience and permission checks, secure token handling and resource-server authorization.

A beautiful consent screen cannot compensate for accepting a token intended for another API.

Public and confidential clients

OAuth distinguishes clients partly by whether they can securely authenticate themselves to the authorization server.

Confidential client

A server-side application can often keep credentials away from end users, for example in a protected backend environment.

That does not mean embedding this in shipped JavaScript is safe:

const clientSecret = "super-secret";

Anything delivered to a user’s browser is available to that user.

Public client

Browser-based JavaScript applications, installed native apps and similar clients generally cannot rely on a static shared client secret remaining confidential.

A “secret” compiled into a SPA bundle or distributed mobile binary is not equivalent to a server-held credential.

PKCE exists precisely because public clients need strong authorization-code protection without pretending they can keep a universal secret.

Browser applications have an additional architectural choice

A browser-based application can call OAuth-protected APIs directly, but that places OAuth tokens in a hostile execution environment: JavaScript running in the page shares risk with XSS, malicious dependencies and compromised third-party scripts.

A Backend for Frontend (BFF) pattern moves OAuth token handling to a server component and gives the browser an application session instead. This can substantially reduce direct exposure of OAuth tokens to browser JavaScript, although it introduces server-side session and CSRF considerations of its own.

As of August 2026, the IETF has published dedicated Best Current Practice guidance for browser-based OAuth applications. We will examine SPA and BFF architecture properly later in this series rather than pretending there is one universal browser pattern.

Do not implement OAuth from memory

The original OAuth 2.0 RFC is foundational, but modern deployments also need the current OAuth Security Best Current Practice. Years of real-world attacks changed what is considered safe.

Flows you should not choose for a new application

Implicit grant

The implicit grant historically returned an access token through the browser authorization response. Current OAuth security guidance says clients should not use it. Authorization Code + PKCE gives the client a safer pattern without exposing access tokens in the authorization response in the same way.

Resource Owner Password Credentials grant

The password grant asked the user to give their credentials directly to the client so it could exchange them for tokens.

That undermines one of OAuth’s central architectural benefits and is explicitly excluded from modern recommended practice. Do not choose it for a new application.

If you control both the UI and identity system and think “but this is first party,” use a modern authentication architecture rather than reviving the password grant.

Client Credentials is different

Not every OAuth interaction represents a user.

The Client Credentials grant is used when a confidential client accesses resources based on its own authorization rather than acting on behalf of a resource owner.

For example:

nightly-reporting-service -> reporting API

There is no browser redirect and no user consent step.

That makes Client Credentials appropriate for some service-to-service scenarios, but do not use it as a shortcut for user delegation. A service token representing billing-worker is not a substitute for knowing which user authorized an operation.

Refresh tokens extend the security problem

Access tokens are often deliberately short-lived. A refresh token can allow the client to obtain new access tokens without sending the user through the full authorization flow again.

That makes refresh tokens valuable credentials.

Public clients need protections against replay. Current security guidance requires authorization servers issuing refresh tokens to public clients to use mechanisms that detect replay, such as refresh-token rotation, or sender-constrain the tokens.

Rotation typically works like this:

refresh token A -> access token + refresh token B

Token A is invalidated. If an attacker later attempts to reuse A, the authorization server can recognize a replay condition and respond according to its security policy.

Do not implement refresh tokens as immortal database strings with no rotation, revocation or lifecycle design.

Authorization servers should publish metadata

Modern clients should not need hard-coded folklore for every endpoint.

OAuth Authorization Server Metadata can publish information such as authorization and token endpoints, supported PKCE methods and other capabilities.

Using standardized metadata reduces configuration mistakes and helps libraries adopt new protocol features safely.

OpenID Connect providers have their own discovery metadata based on the same general idea.

Where a mature library supports discovery, prefer it to manually assembling endpoint URLs from strings.

Use a proven protocol library

OAuth and OIDC are poor candidates for “we can write the 200 lines ourselves.”

The visible happy path is easy. The difficult parts are validation, state binding, PKCE, issuer handling, key rotation, token validation, redirect edge cases, error responses, replay, mix-up attacks, logout behaviour and provider interoperability.

Use a well-maintained OAuth/OIDC library for your platform and understand its configuration.

That last clause matters. A library cannot save you if you disable issuer validation, accept arbitrary redirect URIs or persist tokens insecurely around it.

A useful mental model for the rest of the series

When reviewing an OAuth design, ask five questions:

  1. Who is the client? Browser, native app, backend or machine?
  2. What authority is being delegated? Which API, resource and scopes?
  3. How is the authorization transaction bound to the client instance? PKCE, state/nonce and redirect validation matter here.
  4. Where do the tokens live? Browser memory, backend session, native secure storage or server secret store?
  5. What does the resource server validate before acting? Token validity is only the start; audience, permissions and resource-level authorization matter too.

If those answers are vague, adding another JWT library will not fix the architecture.

Where the series goes next

Part 1 establishes the model. The remaining guides will turn it into an implementation:

  • Part 2: Authorization Code + PKCE end to end, including request construction, callback handling and token exchange;
  • Part 3: access tokens, refresh tokens, JWT validation, scopes, audiences and resource-server authorization;
  • Part 4: browser, SPA, BFF, native and server-side client architecture, with safe token storage;
  • Part 5: production hardening — OIDC, discovery, key rotation, logout, sender-constrained tokens, attack cases and operational troubleshooting.

The biggest mistake you can avoid before Part 2 is simple: do not begin by asking which OAuth flow is easiest to code. Begin by identifying the actors, the trust boundaries and what the client is actually being authorized to do.

Ready to audit your site?

Put this guide into practice immediately using our free tools.

Browse 180+ Free Tools