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
