Skip to content

OAuth 2.0 and OpenID Connect demystified

B2Bnetwork team · · 6 min

Security

In brief

OAuth 2.0 is an authorization protocol: it lets an application obtain limited access to a resource on a user's behalf, without learning the user's password. OpenID Connect adds an identity layer on top, meaning information about who signed in, in the form of an ID Token. For applications with a user, choose the authorization code flow with PKCE; for server-to-server communication, client credentials. The implicit and password flows are discouraged today.

Authorization vs. authentication

The most common misunderstanding goes: “we sign in with OAuth”. OAuth 2.0, described in RFC 6749, answers the question of what an application may do, for example read the list of accounts on a customer's behalf. It does not say who the user is. An access token is like a pass to specific doors, not an ID card.

OpenID Connect (OIDC) is a layer on top of OAuth 2.0, developed by the OpenID Foundation. It adds the ID Token, a signed document in JWT format that says who authenticated, with whom, and when. This lets an application sign a user in with a corporate or bank account without storing their password.

In practice: if an application needs to know who the user is, it uses OIDC. If it only needs API access, OAuth 2.0 is enough. A typical business system uses both at once: a customer portal signs the user in through OIDC, and then calls service APIs with an access token from the same authorization server.

Roles and flows

OAuth 2.0 defines four roles:

  • resource owner: usually the user, for example a bank customer,
  • client: the application that wants access, for example an accounting app,
  • authorization server: issues tokens after the user consents, for example Keycloak, Microsoft Entra ID, Okta,
  • resource server: the API that accepts the token and returns data.
FlowWhenNotes
authorization code with PKCEweb applications, mobile apps, SPAsthe default choice, PKCE is described in RFC 7636
client credentialsserver-to-server, batch jobsno user, the client authenticates itself
device authorizationTVs, terminals without a keyboardthe user confirms on a second device
implicitformerly SPAsdiscouraged, token in the URL
resource owner passwordformerly first-party appsdiscouraged, the application sees the password

Recommendations on flows are collected in RFC 9700, the OAuth 2.0 security best practices published by the IETF. The document recommends PKCE for all clients using authorization code and discourages implicit and password.

Tokens: access, refresh, and ID

  • The access token goes to the API. It should be short-lived, have a narrow scope of permissions (scope), and name its intended recipient (audience).
  • The refresh token lets you obtain a new access token without signing in again. It is valuable, so it is stored on the server side or in the mobile system's secure storage, and the authorization server can rotate it on every use.
  • The ID Token is for the client application and says who signed in. Do not send it to an API in place of an access token.

A resource server that accepts a token in JWT format checks every time: the signature using a key from the authorization server's published key set, the issuer (`iss`), the audience (`aud`), the expiry (`exp`), and the required scope. Skipping the audience check is a common mistake: a token issued for one service is then accepted by another.

The most common deployment mistakes

  1. Loose redirect URI matching. The authorization server compares `redirect_uri` exactly against the registered value, with no wildcards.
  2. Missing `state` and `nonce`. The `state` parameter protects against request forgery, and in OIDC `nonce` binds the ID Token to a specific sign-in.
  3. Tokens in the browser's localStorage. Any script on the page can read them. For web applications, a safer pattern is one where the backend holds the tokens and the browser has a session cookie with the `HttpOnly`, `Secure`, and `SameSite` flags.
  4. Scopes that are too broad. The application asks for full access because “it was simpler that way”. The scope should match the function.
  5. Long-lived access tokens. A token valid for a week is hard to invalidate after a leak.
  6. A custom implementation. Ready-made libraries and authorization servers have years of fixes behind them. Writing your own authorization server from scratch rarely pays off.

How to verify a deployment

Most of these points can be checked with automated tests, without offensive tools:

  • the authorization server rejects a request with a redirect address other than the registered one,
  • the API rejects a token with a different audience, an expired token, and a token without the required scope,
  • the API rejects an ID Token sent in place of an access token,
  • signing out invalidates the session and the refresh token,
  • a refresh token used a second time after rotation is rejected.

Tests for these points belong in the automated API suite, alongside the permission tests described in the article API security: the most common mistakes.

OAuth in banking and at telecom operators

In open banking, OAuth 2.0 is the basis of third-party providers' access to a bank's API: the customer grants consent at the bank, and the provider receives a token for a specific scope and time. For such uses the OpenID Foundation developed the FAPI profiles, which tighten the requirements, for example by binding the token to the client's certificate or key. We describe the regulatory context in the article PSD2 and open banking from an IT team's perspective.

At a telecom operator, the same mechanism organizes partner access to the product catalog and service activation APIs: each partner has its own client, scopes, and limits, and cutting off one partner does not require changing the passwords of the others. How B2Bnetwork engineers test such APIs is shown in the scenario penetration tests of an API for third-party providers.

Questions and answers

Is JWT the same as OAuth?

No. JWT (RFC 7519) is a token format: a signed set of information in JSON. OAuth 2.0 is a protocol for issuing tokens that does not require a specific format. An access token can be a JWT or an opaque string that the API checks with the authorization server. The ID Token in OpenID Connect is always a JWT.

Can a SPA use OAuth safely?

Yes, if it uses the authorization code flow with PKCE, not implicit. It is safer still when a lightweight backend for the application holds the tokens and the browser receives only a session cookie. This pattern limits the impact of a possible XSS, because a script on the page has no access to the tokens.

How do you invalidate a token after a leak?

A short-lived access token usually just expires, which is why lifetimes are measured in minutes. A refresh token is revoked at the authorization server, and services can check a token's status through introspection (RFC 7662). In a serious incident, the authorization server's signing keys are rotated as well.

Is client credentials safe for integrations between systems?

Yes, provided the client secret sits in a vault rather than in configuration and has a narrow scope. It is even better when the client authenticates with a certificate or a signed JWT instead of a password. More on storing such data in the article Secrets in applications.

What it looks like in a project

Want to check your application's security? Talk to an engineer.

Let's talk

Read more: Security