liveBook · Manning

401 unauthorized

As explained in Secure APIs

The HTTP 401 status code indicates that a request lacks valid authentication credentials.

The HTTP 401 status code means a request does not include valid authentication credentials. It differs from 403 Forbidden: 403 means the credentials are valid, but access is not allowed. section 12.4

Testing an unauthenticated request

A test sends an unauthenticated GET request to /courses/{course_id}/lessons/{lesson_id}. The correct expectation is 401. If the API returns 403 instead, the test has exposed an authentication-response problem and potentially a security flaw. section 12.4

Use these checks when testing authentication failures:

  • Send the request without credentials and assert a 401 response. section 12.4
  • Use pytest to automate the check and reveal unauthorized access to protected content. 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

Inspect the focused test structure: create a test client, obtain resource identifiers, send an unauthenticated request, and verify the response status. section 12.4

How Secure APIs covers it

How deep it goes
Defines the status distinction and applies it in an automated endpoint test, without detailing authentication internals.
Examples
Tests an unauthenticated lesson request and runs it with pytest against a FastAPI service.
What it assumes
Assumes familiarity with HTTP endpoints and tests while establishing the authentication-versus-authorization distinction explicitly.
What you can do after
Diagnose/debug with it, identifying authentication failures and separating them from authorization failures in automated API tests.
How it connects
Connects HTTP authentication, authorization, 403 Forbidden, API security testing, FastAPI, and pytest.

Also explained in

Compare how 2 books explain 401 unauthorized →

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.