Skip to main content
Be Bitwise

Authentication & Authorisation in APIs

API Security Fundamentals · API Security track

6 min read

In this module

The Authentication Challenge

Many APIs face a problem traditional web apps don't: statelessness. A mobile app, a script or another service has no browser session and no cookie jar quietly managed by a server-side framework. (Plenty of APIs behind a single-page web app do still use cookie sessions, and they inherit the web's CSRF concerns.) Either way, every single request has to prove who's making it. The authentication mechanism rides along on every call, and if it's flawed, every endpoint is wide open.

A clear picture of how API authentication works, and how it falls over, is the price of entry for testing one.

API Keys: Simple but Fragile

The simplest form of API authentication is an API key — a long random string tucked into a request header or query parameter.

http
GET /api/v1/data HTTP/1.1
Host: api.example.com
X-API-Key: ak_live_7f3a9c2e1b4d5f6a8e9c0d1b2a3f4e5d

An API key identifies the calling application, not the user. They get embedded in client-side code, accidentally committed to GitHub, leaked in logs when passed as URL parameters. Once one key is compromised, an attacker can blend in with legitimate traffic indefinitely — there's nothing to distinguish their requests from anyone else's.

API Keys Are Not Authentication

An API key identifies which application is calling, not which user is making the request. Treating API keys as user authentication is a common and dangerous mistake. They should be combined with a proper user authentication mechanism.

OAuth 2.0: Delegated Authorisation

OAuth 2.0 is the standard for delegated authorisation in APIs. Instead of handing over credentials, a user grants an application limited access via scoped tokens. OAuth on its own answers "what may this client do?", not "who is the user?": an access token isn't proof of identity. Login is the job of OpenID Connect, a layer on top, covered below.

Four parties dance the dance: the resource owner (user), the client application, the authorisation server, and the resource server (the API itself).

The diagram traces the authorisation code flow with PKCE, which is what a modern client should be running. The parenthesised checks are the ones that go missing in the misconfigurations listed below.

PKCE ties the code to the client that started the flow, so a code stolen from the redirect is useless without the verifier. If the client asked for the openid scope, OpenID Connect adds an ID token: a JWT that tells the client who logged in. It's meant for the client only. An API that accepts ID tokens in place of access tokens is skipping the audience check.

OAuth misconfigurations show up in a few familiar places: scopes that are far too broad, missing state parameter validation that opens the door to CSRF, open-redirect bugs in the callback URL, and the perennial failure to validate the token audience.

JWT: The Token That Carries Its Own Identity

JSON Web Tokens (JWTs) are the dominant token format for API authentication. A JWT is three Base64URL-encoded chunks joined by dots: a header, a payload, and a signature.

json
{
"header": {
"alg": "RS256",
"typ": "JWT"
},
"payload": {
"sub": "user_1842",
"role": "admin",
"iat": 1709251200,
"exp": 1709254800
}
}

The signature is what guarantees the token hasn't been tampered with. The server signs it with a secret or private key, then re-checks the signature on every incoming request. Several well-known attacks aim squarely at that mechanism.

The None Algorithm Attack

A handful of JWT libraries cheerfully accept "alg": "none" as a valid algorithm, meaning the token needs no signature at all. An attacker rewrites the payload (flipping their role from user to admin, say), strips the signature, sets the algorithm to none, and watches the server wave it through.

json
{
"alg": "none",
"typ": "JWT"
}

Algorithm Confusion: RS256 to HS256

This one rides on a subtle difference between asymmetric and symmetric signing. With RS256 (RSA), the server signs using a private key and verifies using the public key. Flip the algorithm header to HS256 (HMAC) and the server may pick up the public key — which is, helpfully, public — and treat it as the HMAC secret. The attacker has the same public key, so they can mint valid signatures at will.

Algorithm Confusion

In an algorithm confusion attack, the attacker changes the JWT header from RS256 to HS256 and signs the token using the server's public key as the HMAC secret. Because the public key is often publicly available, this allows complete token forgery.

Other JWT Attacks

Weak secret brute force: HMAC-signed JWTs fall to offline cracking if the secret is weak. Tools like hashcat and jwt_tool rip through millions of candidates per second against a captured token.

JWK header injection: a few implementations let the token nominate its own verification key through the jwk header parameter. An attacker drops in their own key pair and signs the token themselves.

kid parameter injection: the kid (Key ID) header tells the server which key to verify against. When that value gets piped into a file path lookup or a database query without sanitisation, it becomes a vector for path traversal or SQL injection.

Mutual TLS: Certificate-Based Trust

Mutual TLS (mTLS) asks both client and server to present certificates during the TLS handshake. It mostly shows up in service-to-service communication, particularly around gRPC. Each service proves its identity cryptographically, and the connection is authenticated and encrypted in one go.

mTLS is strong, but operationally it's a bear. Certificate rotation, revocation, and distribution across a fleet of microservices need careful management, usually through a service mesh.

Session Management in Stateless APIs

Traditional session management leans on server-side session stores. Stateless APIs push that job onto the token itself, and that creates specific headaches:

  • Token revocation is awkward because there's no server-side session to invalidate. The usual answers are short-lived access tokens paired with refresh tokens, or maintaining a token blocklist.
  • Token storage on the client side is a real risk. Browser apps end up choosing between localStorage (XSS-exposed) and httpOnly cookies (CSRF-exposed).
  • Refresh token rotation is non-negotiable. A stolen refresh token mints access tokens forever unless the server uses one-time-use refresh tokens.

These mechanisms and their failure modes are the launchpad for any serious API security testing. In the next lesson we'll look at the OWASP API Security Top 10 and see where these authentication weaknesses sit inside the broader vulnerability landscape.