liveBook · Manning

Broken authentication

As explained in Secure APIs

Broken authentication is a vulnerability in which flaws in identity verification or session handling allow an unauthorized actor to access protected functionality or data.

Broken authentication occurs when weaknesses in identity verification, login flows, credentials, or token handling let an unauthorized actor take over an account or reach protected API functionality. Because authorization decisions often rely on authenticated identity and access-token claims, one authentication failure can undermine the wider API security strategy. section 4.4

Common failure modes connect directly to attacker behavior:

  • Weak password policies and missing brute-force protections allow repeated guessing attempts. section 4.4
  • Credential stuffing reuses credentials leaked from another website to take over accounts. section 4.4
  • Multistep login flows can reveal which usernames or email addresses are registered. section 4.4
  • Accepting unsigned, expired, tampered, or incorrectly targeted tokens can expose protected operations. section 4.4

An audience claim exposes the wrong API

A Python API validates a JWT signature with JWCrypto but does not require the expected aud claim. A customer token issued for one API could therefore reach administrator functionality in another API. Configure validation with the expected administrative audience, such as https://apithreats.com/admin, and reject tokens whose claims do not match. section 4.5

Reduce exposure by using an identity provider and standard protocols or specifications such as OAuth and JWT, validating token claims, protecting credentials, and adding threat detection where login flows can be abused. Test every protected endpoint with missing, malformed, expired, and tampered credentials. section 4.11 section 12.4

Listing 12.2 Running a broken authentication test
# file: ch12/tests.py
from sqlalchemy import select
from starlette_testclient import TestClient

from db_session import session_maker
from models import Course
from server import server


client = TestClient(server)     #1


def test_broken_authentication():
    with session_maker() as session:     #2
        course = session.scalar(select(Course))     #3
        lessons = course.lessons
    response = client.get(
        f"/courses/{course.id}/lessons/{lessons[0].id}"
    )     #4
    assert response.status_code == 403    #5

A focused test requests protected course content without credentials and expects HTTP 403; the same approach can be repeated across authenticated endpoints. section 12.4

How Secure APIs covers it

How deep it goes
Explains credential and token failure modes, demonstrates JWT audience validation, and specifies endpoint tests without detailing complete authentication-system design.
Examples
Uses credential stuffing, multistep login enumeration, cross-API JWT audiences, and unauthenticated requests to protected course endpoints.
What it assumes
Assumes understanding of the API’s design, implementation, and threat model while teaching authentication flows and access tokens from basic principles.
What you can do after
Diagnose broken authentication with JWCrypto claim validation and pytest checks for missing, malformed, expired, or tampered credentials.
How it connects
Connects authentication with authorization, access tokens, JWT, OAuth, credential stuffing, brute-force attacks, and user enumeration.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Secure APIs. The section links point to the parts of the book it draws on.