QA Interview Hub · Knowledge base · Quizzes · Українська

API › Authentication and authorization

OAuth 2.0 and OpenID Connect: roles, flows and what to test

OAuth 2.0 is a protocol of delegated access: an app gets limited access to a user's resources without their password. OpenID Connect adds authentication, the answer to who this is. Web and mobile use Authorization Code with PKCE, service-to-service uses Client Credentials. Tests cover single-use codes, the verifier, an exact redirect URI and state; a valid token lacking the scope gets 403, an invalid one 401.

API · Authorization

"Sign in with Google" in an app. Who gives what to whom, and what is there to test?

An app has a "Sign in with Google" button. The user types the password on the Google page, the app never sees it and receives only a token. A tester gets the task "check authorization" and does not know where to start: whom to look at, how the tokens differ, why one error is returned as 401 and another as 403. The answer is in the roles and flows of OAuth 2.0.

Below: the roles, the Authorization Code flow with PKCE, what the access, refresh and ID tokens are, and a set of cases with example requests.

What it is and why

OAuth 2.0 (RFC 6749) is a protocol of delegated access: an app gets limited access to a user's resources without the user's password. OpenID Connect (OIDC) is a layer on top of OAuth 2.0 for authentication: it answers "who is this user". There are four roles: the resource owner (the user), the client (the app), the authorization server (issues tokens) and the resource server (the API that accepts tokens).

How Authorization Code with PKCE works

This is the standard flow for web, mobile and SPA. The user signs in at the authorization server, the client gets a one-time code and exchanges it for tokens. To make a stolen code useless, the client adds PKCE (RFC 7636): a random code_verifier from which a code_challenge is computed.

The Authorization Code flow with PKCE

1 client: verifier and challenge 2 browser: /authorize + state 3 user signs in and consents 4 redirect: ?code=...&state=... 5 client checks state 6 POST /token: code + verifier 7 access + refresh (+ ID token)
Figure 1. The code travels through the browser, while tokens are obtained by a direct request from the client to the authorization server. The verifier stays with the client, so an intercepted code is useless without it.

Why it is designed this way. The code goes through the address bar and the browser, a place where it can be intercepted (redirects, logs, a malicious app on a phone). So the code is one-time and short-lived, and tokens are issued only for a direct request where the client proves it started the sign-in: it sends the verifier whose hash was in the first request. The state parameter binds the response to the original request and protects against CSRF. An exact match of the redirect URI with the registered one prevents sending the code to a foreign address. This binding is exactly what the tests check.

The code-for-tokens exchange looks like this:

POST /oauth/token HTTP/1.1
Host: auth.shop.test
Content-Type: application/x-www-form-urlencoded

grant_type=authorization_code&code=SplxlOBeZQQYbYS6WxSbIA
&redirect_uri=https%3A%2F%2Fapp.shop.test%2Fcb&client_id=web
&code_verifier=dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk

Other flows and tokens

  • Client Credentials: service-to-service with no user, the client authenticates with its own secret.
  • Refresh Token: exchanging a long-lived token for a new short access token.
  • Implicit and Password (ROPC) are not recommended by current guidance (RFC 9700), and the OAuth 2.1 draft drops them.

Access token and ID token

Access token

For the API (resource server). Has the aud of that API and a scope. It is sent in Authorization: Bearer.

ID token (OIDC)

For the client: a JWT about the fact of sign-in, with sub, iss, nonce. An API does not accept it as access.

Figure 2. The two tokens have different audiences. It is a defect when an API accepts an ID token instead of an access token.

Scope is a list of what is allowed (orders:read, orders:write). A valid token without the required scope gets 403, while a missing, expired or broken one gets 401. RFC 6750 describes this in the WWW-Authenticate header:

HTTP/1.1 401 Unauthorized
WWW-Authenticate: Bearer error="invalid_token", error_description="expired"

HTTP/1.1 403 Forbidden
WWW-Authenticate: Bearer error="insufficient_scope", scope="orders:write"

Example: what to check in the flow

Cases for the authorization code: the code works once (RFC 6749 requires the server to reject reuse), and a wrong verifier must be rejected. An exchange error comes back as 400 with error: invalid_grant.

test('an authorization code works once', async ({ request }) => {
  const form = {
    grant_type: 'authorization_code',
    code: process.env.AUTH_CODE!,       // obtained in the beforeEach step
    redirect_uri: 'https://app.shop.test/cb',
    client_id: 'web',
    code_verifier: process.env.CODE_VERIFIER!,
  };

  const first = await request.post('/oauth/token', { form });
  expect(first.status()).toBe(200);

  const second = await request.post('/oauth/token', { form });
  expect(second.status()).toBe(400);
  expect((await second.json()).error).toBe('invalid_grant');
});

test('a wrong code_verifier is rejected (PKCE)', async ({ request }) => {
  const res = await request.post('/oauth/token', {
    form: { ...validForm(), code_verifier: 'x'.repeat(43) },
  });
  expect(res.status()).toBe(400);
  expect((await res.json()).error).toBe('invalid_grant');
});

In the first test the second exchange of the same code must fail. Additionally the server should revoke the tokens already issued from this code at this step, but this is only a recommendation of the standard, so it depends on the contract. In the second test the verifier does not match the challenge, so no tokens are issued. The helper validForm() returns a correct form from the same preparation.

The remaining cases:

  • A token with the scope read does not call an endpoint that requires write (403).
  • Expired, forged and revoked tokens give 401.
  • A token issued for one API (aud) is not accepted by another.
  • Refresh: the old refresh token does not work after rotation, and the new one yields a new pair.
  • The redirect URI must match exactly, not by prefix or subdomain.
  • The client rejects a response with a foreign or missing state.

Limits and alternatives

  • A UI test of a flow with a third-party provider (Google) is unstable. Tests use their own test authorization server or pre-issued tokens, and the full flow is checked separately.
  • Not every server rotates the refresh token. The policy (one-time, sliding, with reuse detection) is set by the contract, and the test checks it, not one template.
  • For service-to-service, an API Key or mTLS is sometimes used instead of Client Credentials. That is a different trade-off, not a mistake.
  • OAuth does not say how to store a token in a browser. That is a separate client security topic.

Common mistakes

  • Mixing up OAuth and OIDC. OAuth is about access to a resource, OIDC about identity. Signing in by an ID token without checking the signature, aud and nonce is a defect.
  • Sending the ID token to the API. It is not meant for an API, and a server that accepts it has a defect.
  • Not checking state. Without it the sign-in is open to a CSRF attack.
  • Allowing a redirect URI by a mask. An attacker registers a similar address and receives the code.

Check yourself

A token with the scope orders:read calls POST /orders. Which status do we expect, and which header?

Answer

403 and WWW-Authenticate: Bearer error="insufficient_scope" (optionally with the list of required scopes). The token is valid, so it is not 401, but the rights are insufficient.

Why is the authorization code one-time, and what do we check?

Answer

The code passes through the browser, where it can be intercepted, so reuse means theft or an error. The test exchanges the same code twice and expects 400 invalid_grant the second time.

In an interview. Junior: OAuth 2.0 gives an app limited access without the user's password, and OpenID Connect adds the answer "who is this". For a mobile sign-in with Google I take Authorization Code with PKCE, for a call between backends Client Credentials. Middle/Senior: I check that the code is one-time, that the verifier matches, the exact redirect URI and state, refresh token rotation, aud and scope: a valid token without the scope gives 403 with insufficient_scope, an invalid one gives 401. I do not accept an ID token as an access token. I do not use Implicit and ROPC, because current guidance advises against them.

More articles in this chapter