Skip to content

Composite::challenge() overwrites WWW-Authenticate headers instead of accumulating them #124

Closed

Description

@Sakanweb

Summary

Yiisoft\Auth\Method\Composite::challenge() uses withHeader() for each method. When multiple authentication methods are configured, only the last WWW-Authenticate value remains.

Expected

Multiple challenges should be returned, e.g.:

WWW-Authenticate: Bearer realm="api"
WWW-Authenticate: Basic realm="api"

Actual
Only the last method's challenge survives:

WWW-Authenticate: Basic realm="api"
Reproduce
$composite = new Composite([
    new HttpBearer($repository),
    new HttpBasic($repository),
]);
$response = $composite->challenge($responseFactory->createResponse(401));
// count($response->getHeader('WWW-Authenticate')) === 1
// value: "Basic realm=\"api\"" (or "Authorization realm=\"api\"" on 3.2.1, see #116)
HTTP check without credentials against a route protected by this Composite:

curl -sI https://example.test/protected | grep -i www-authenticate
# WWW-Authenticate: Basic realm="api"
Cause
foreach ($this->methods as $method) {
    $response = $method->challenge($response); // uses withHeader() → overwrites
}

Suggested fix
Accumulate challenges with withAddedHeader() (or equivalent), so each method can add its own WWW-Authenticate value.

Environment
Package: yiisoft/auth
Version: 3.2.1
Class: Yiisoft\Auth\Method\Composite

Activity

  1. samdark commented on Aug 6, 2026

    @samdark
    Member

    Thanks for reporting. The issue is valid.

    Using withAddedHeader() for these challenges, plus a regression test combining HttpBearer and HttpBasic, is an appropriate fix.

  2. Sakanweb commented on Aug 7, 2026

    @Sakanweb
    Author

    Hey, thanks for the quick response!

    I checked #125 — the withAddedHeader() change fixes the overwrite I hit with Composite,
    and the new test covers it. Looks good to me 👍

  3. added a commit that references this issue on Aug 11, 2026
    1addab2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions