1. Home
  2. Blog
  3. Security

Security

What Is OAuth 2.0 and OpenID Connect? Authorization Code Flow, Scopes, Token Lifetime and PKCE Guide

What is OAuth 2.0 and OpenID Connect? We explain the authorization code flow, PKCE, scope design and token lifetime with a dealer portal example.

Contents 15 sections
  1. What Are OAuth 2.0 and OpenID Connect?
  2. What Is the Difference Between Authentication and Authorization?
  3. What Are the Four Roles in OAuth 2.0?
  4. How Does the Authorization Code Flow Work?
  5. What Does PKCE Do?
  6. Which Flow Suits Which Client?
  7. How Should Scopes Be Designed?
  8. Why Do the Redirect URI, State and Nonce Matter So Much?
  9. How Should Access and Refresh Token Lifetimes Be Chosen?
  10. What Is the Difference Between an ID Token and an Access Token?
  11. How Are Tokens Stored and Revoked?
  12. What Is the Pre-Launch OAuth Checklist?
  13. What Mistakes Are Common with OAuth and OpenID Connect?
  14. Secure Authorization and Integration Development with İnovabil
  15. Frequently Asked Questions

One of your dealers wants its own accounting software to read the orders and invoices stored in your dealer portal. You do not want to hand over a username and password, and you do not want the dealer's password saved inside someone else's application either. OAuth 2.0 and OpenID Connect are open protocols built for exactly this problem: they let you grant an application access without sharing a password, within a limited scope and for a limited time.

This guide explains what the two protocols are, how authentication differs from authorization, the four roles, the steps of the authorization code flow, what PKCE does, how to design scopes, the rules for redirect addresses and state, token lifetime and revocation, the difference between an ID token and an access token, and a pre-launch checklist. The approach is defensive; no attack steps are described. All example figures are representative, not measurements of a real system.

Key takeawayOAuth 2.0 does not answer "who are you?" but "what may this application do on my behalf?"; OpenID Connect adds the "who just signed in?" layer on top. Security comes down to three settings: how narrow the scope is, how short-lived the token is, and how strictly the redirect address is matched. Even a correctly implemented protocol leaves the door open if those three are loose.

What Are OAuth 2.0 and OpenID Connect?

OAuth 2.0 is the protocol that defines delegated access: how an application reaches a resource in another system on a user's behalf. Its output is an access token, a short-lived document that says "this application may call this API for this user, within this scope, until this time." OpenID Connect (OIDC) is the identity layer built on top of OAuth 2.0; it adds an ID token that states who the user is and when they signed in.

The two belong together, but they do different jobs. The authorization server handles the sign-in screen and the consent step, and your application never sees the user's password. We covered multi-factor authentication and single sign-on in our MFA and SSO guide; this article focuses on the delegation of access that follows that sign-in.

We recommend reading: Our integration service

What Is the Difference Between Authentication and Authorization?

Authentication answers "who are you?"; authorization answers "what are you allowed to do?". OAuth 2.0 solves the second question and OpenID Connect takes care of the first.

TopicAuthentication (OIDC)Authorization (OAuth 2.0)
Question answeredWho is the user?What can the application access?
OutputID tokenAccess token
Intended forThe client applicationThe resource server (API)
Example"Alice signed in""This application may read orders"

Inside a business application you still need to design roles and permissions separately; we covered that in our role-based access control guide. An OAuth scope does not replace the role model; it places an outer limit above it.

What Are the Four Roles in OAuth 2.0?

There are four roles: the resource owner, the client, the authorization server and the resource server. Every rule in the protocol is about what these four parties may say to each other.

RoleDealer portal exampleResponsibility
Resource ownerDealer employeeGrants permission
ClientThe dealer's accounting applicationAsks to reach data with permission
Authorization serverThe portal's sign-in and consent serviceVerifies identity, issues tokens
Resource serverOrder and invoice APIValidates the token, serves data

The resource server looks at who the token was issued for, its scope and its expiry; it never sees the password. We described the general security layers of an API in our API security guide.

How Does the Authorization Code Flow Work?

The authorization code flow is the standard route for applications that interact with a user. The token never appears in the browser's address bar; the browser only receives a short-lived, single-use code, and the token is exchanged server to server.

The six steps of the authorization code flow: redirect, sign-in, consent, code, exchange and API call
Authorization code flow: the token travels server to server, not through the address bar.
  1. Redirect: The client sends the user to the authorization server and attaches the requested scope, the registered redirect address, a random state value and the PKCE challenge.
  2. Sign-in: The user proves their identity at the authorization server. The password or the second factor is entered only there.
  3. Consent: The user sees the requested scopes and approves or rejects them.
  4. Code: The server sends the user back to the registered address with a single-use code. In our example the code lives for 60 seconds.
  5. Exchange: The client sends the code and the PKCE verifier to the server and receives the access token (and a refresh token if needed).
  6. API call: The client calls the resource server with the access token; the resource server checks the scope and the expiry.

You can find the general logic of data flow between systems in our API integration guide.

What Does PKCE Do?

PKCE is a verification layer that stops a third party who intercepts the authorization code from exchanging it for a token. For every flow the client generates a random verifier and sends only its digest in the first request; at the exchange step it presents the original verifier. The server issues no token until the two match.

Worked example: 32 random bytes are 32 × 8 = 256 bits; with an encoding that carries 6 bits per character, 256 ÷ 6 ≈ 42.7, so the verifier is 43 characters long. Common practice is to treat PKCE as mandatory for clients that cannot keep a secret safely, such as mobile and single-page applications, and to enable it for server-side clients as well. Confirm the current recommendation in the official documentation.

Which Flow Suits Which Client?

The choice depends on whether the client can keep a secret and whether a user is present. With a user, use the authorization code flow with PKCE; without a user, use the client credentials flow.

FlowWhen?Note
Authorization code + PKCEWeb, mobile, single-page application; a user is presentDefault choice
Client credentialsServer to server, no user (nightly sync)Secret only on the server; keep permissions narrow
Device flowInput-limited device (terminal, display)User approves a code on another device
Implicit and password flowsNot recommended for new projectsCurrent security guidance advises staying away from them

Where a server-to-server secret is stored is a separate question; we described its lifecycle in our secrets management guide.

How Should Scopes Be Designed?

A scope is the smallest unit of permission that defines what a token may do. The rule is simple: every client gets only the scopes its job requires. Broad scopes look convenient, but they decide how big the damage is when a client is compromised.

In the representative example the portal has eight scopes: orders.read, orders.write, invoices.read, stock.read, prices.read, accounts.read, profile.read, reports.read. Granting all of them to 20 clients would mean 20 × 8 = 160 permission assignments. Granted narrowly by job, the table looks like this.

Client groupClientsScopes / clientTotal assignments
Reporting6212
Order entry5315
Accounting integration4312
Price and stock sync326
Mobile application248
Total20—53

Assignments drop from 160 to 53: (160 − 53) ÷ 160 = 107 ÷ 160 ≈ 66.9% fewer. We covered dealer and sub-user structures in our B2B company account guide.

Why Do the Redirect URI, State and Nonce Matter So Much?

These three values make sure the code and the token come back to the right place and belong to the right request. However solid the rest of the protocol is, a bad setup of these values can send the flow to the wrong party.

  • Redirect URI: The client declares its addresses at registration; the server compares the address in the request with the registered one using an exact match. Accepting wildcards or matching only the domain opens the chance that a code is sent to an unintended address.
  • State: A random value the client sends with the request and expects back unchanged; it confirms that the response belongs to a request the client itself started. Example: a 128-bit value has 2128 ≈ 3.4 × 1038 possibilities, so it cannot be guessed.
  • Nonce: A value that ties an ID token to a specific sign-in request and shows it was not replayed; it is used in the OpenID Connect flow.

Review example: if 20 clients have 3 registered addresses each, there are 60 registered addresses; if 4 of them contain a wildcard, the rate is 4 ÷ 60 ≈ 6.7%, and the target is 0%. Reviewing registered addresses regularly belongs with user account lifecycle management; see our account lifecycle guide.

How Should Access and Refresh Token Lifetimes Be Chosen?

The access token is short-lived and the refresh token lives longer; a short lifetime narrows the damage window of a stolen token. The price is more refresh requests.

Damage window of a stolen token: a 24-hour token allows 57,600 requests, 1 hour allows 2,400, 15 minutes allows 600; 96 times narrower
The shorter the token lifetime, the narrower the damage window.

Representative assumptions: a client makes 40 requests per minute and there are 600 active sessions. The damage window equals the token lifetime; the number of requests possible inside it is 40 × minutes.

Access token lifetimeWindowRequests in windowRefreshes / day / sessionRefreshes / s across 600 sessions
24 hours1,440 min57,6001≈ 0.007
1 hour60 min2,40024≈ 0.17
15 minutes15 min60096≈ 0.67

Going from 24 hours to 15 minutes narrows the window by 1,440 ÷ 15 = 96 times. Refreshing happens 24 × 60 ÷ 15 = 96 times a day; across 600 sessions that is 600 × 96 = 57,600 refreshes, or 57,600 ÷ 86,400 ≈ 0.67 requests per second.

If a refresh token is valid for 30 days, the window when it is stolen is 30 × 1,440 = 43,200 minutes. With refresh token rotation (a new token on every use, the old one invalidated), presenting an old token a second time is detected immediately and the whole chain is revoked; because the legitimate client refreshes at least every 15 minutes, the leak signal also shows up within that interval. Compared with the full window: 43,200 ÷ 15 = 2,880 times shorter detection interval. An absolute lifetime (30 days) and an idle limit (for example 7 days) are applied together.

For the general secure development framework, see our secure software development guide.

What Is the Difference Between an ID Token and an Access Token?

The ID token tells the client "who the user is and when they signed in"; the access token tells the API "this call is permitted". Mixing them up, by sending the ID token to the API or treating the access token as proof of identity, is a common mistake.

PropertyID tokenAccess token
Audience (aud)Client applicationResource server (API)
PurposeReport the user who signed inAuthorize an API call
Sent to the API?NoYes
Typical contentUser identifier, sign-in time, nonceScope, expiry, target API
LifetimeShort; for the sign-in momentShort (15 min in the example)

The signature, issuer, audience and expiry of both tokens must be validated by the recipient.

How Are Tokens Stored and Revoked?

Where a token is stored and how it is revoked determine the outcome of a leak. The principle: keep the token in the narrowest place possible, never write it to an address or a log, and build the revocation path in advance.

  • Storage: On mobile, the operating system's secure store; on the web, a protected place suited to the environment, such as a server-side session or a cookie that scripts cannot read. A token is never written to a URL, a log or an error message.
  • Revocation: A token is revoked when the user signs out, when the dealer relationship ends or when a leak is suspected; the client registration can also be disabled.
  • The validation method sets the revocation speed: validating a signed token locally is fast but revocation only takes effect when the lifetime ends; asking the authorization server reflects revocation at once but adds load.
Validation methodRevocation delayQueries to the authorization server
Local signature checkUp to 15 min (token lifetime)0
Query cached for 60 sUp to 60 sUp to 10 queries/s
Query on every requestImmediate≈ 138.9 queries/s

Calculation: at 12,000,000 requests a day, 12,000,000 ÷ 86,400 ≈ 138.9 requests per second; a query per request adds that much load. Querying 600 active tokens with a 60-second cache leaves 600 ÷ 60 = 10 queries per second, a load reduction of 1 − 10 ÷ 138.9 ≈ 92.8%. For general key and quota management, see our API key and quota guide.

What Is the Pre-Launch OAuth Checklist?

Answer the eight items below before you go live; if any answer is "no", that one gets closed first.

  1. Does every client that involves a user use the authorization code flow and PKCE?
  2. Are redirect addresses checked with an exact match, with no wildcards accepted?
  3. Is state generated randomly for every request and verified on return?
  4. Is each client's scope limited to its job, with no full-access client?
  5. Is the access token lifetime short (15 min in the example) and is the refresh token rotated?
  6. Are the signature, issuer, audience and expiry of tokens validated at the API?
  7. Are client secrets in a vault, tokens kept out of logs, and the revocation path tested?
  8. Are the authorization server's sign-in, consent and revocation events written to an audit log?

What Mistakes Are Common with OAuth and OpenID Connect?

  • Treating the access token as proof of identity: Delegated access and sign-in information are different tokens.
  • Wildcard redirect addresses: Easy to set up, but they make it unclear who will receive the code.
  • Full scope for every client: One leak then covers all the data.
  • Long-lived access tokens: A 24-hour token leaves a window 96 times wider than a 15-minute one.
  • Writing tokens to URLs or logs: Logs turn into token stores.
We recommend reading: What is an API gateway? Shared rules, caching and added latency guide
We recommend reading: What Is Zero Trust Architecture and How to Adopt It? Identity, Devices, Micro-Segmentation and a Phased Plan

Secure Authorization and Integration Development with İnovabil

At İnovabil our focus is building custom software from scratch for your company: custom software, ERP, CRM, e-commerce, web, mobile and integration. If you have a portal or an external integration that needs delegated access, we suggest writing down the scope, the token lifetime and the revocation path during requirements analysis. Role-based access, audit logs and secure hosting are the registered capabilities of our development process; we do not claim a ready-made identity product or a certification.

We built CRM, purchasing and mobile for Avcılar İnşaat; a purchasing system for İZBAN; logistics and shipment planning with wholesale order management for Karyak; and a prepaid cargo system for Kredili Kargo. Send us a short summary of which parties your portal will share which data with, and we will outline the scope and flow together.

We recommend reading: Our software development process: from analysis to launch, step by step

Frequently Asked Questions

What is OAuth 2.0 and what is it for?

It is an open protocol that lets an application reach a resource within a limited scope and for a limited time without the user sharing a password. The application ends up with a short-lived access token.

What is the difference between OAuth 2.0 and OpenID Connect?

OAuth 2.0 solves authorization, the question of what the application may access. OpenID Connect adds authentication on top, who the user is and whether they signed in, through the ID token.

What is PKCE and is it needed for every client?

PKCE is a verification layer that stops someone who intercepts the authorization code from exchanging it for a token. It is essential for clients that cannot keep a secret, such as mobile and single-page applications; current guidance recommends it for server-side clients too.

How long should an access token live?

Keeping it short narrows the damage window. In the representative example, 15 minutes gives a window 96 times narrower than 24 hours while the refresh load is only about 0.67 requests per second. The exact value depends on your risk and usage pattern.

What should I do if an OAuth token is stolen?

Revoke the token and its linked refresh token, disable the client if needed, review the affected calls in the audit log and narrow the scopes. If personal data is affected, consult your legal counsel about notification obligations.

Let's talk about your project

Tell us briefly about the software you have in mind; we'll listen and prepare a tailored roadmap and proposal.

Get a quote