Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227<?php
declare(strict_types=1);
namespace Wotnak\Atproto\Tests\Unit;
use GuzzleHttp\Psr7\HttpFactory;use GuzzleHttp\Psr7\Response;use PHPUnit\Framework\Attributes\DataProvider;use PHPUnit\Framework\TestCase;use Wotnak\Atproto\Client\AuthenticatedXrpcRequestSender;use Wotnak\Atproto\Client\WwwAuthenticateParser;use Wotnak\Atproto\Tests\Support\TestRefreshableDpopSession;use Wotnak\Atproto\Tests\Support\TestXrpcRequestSender;
final class WwwAuthenticateParserTest extends TestCase {
/** * @param array<string, array<string, string>|string>|null $expected */ #[DataProvider('headerProvider')] public function testParsesWwwAuthenticateHeader(string $header, ?array $expected): void { static::assertSame($expected, WwwAuthenticateParser::parse($header)); }
/** * @return array<string, array{string, array<string, array<string, string>|string>|null}> */ public static function headerProvider(): array { return [ 'single unquoted auth param' => [ 'Bearer realm=example.com', ['Bearer' => ['realm' => 'example.com']], ], 'single quoted auth param' => [ 'Bearer realm="example.com"', ['Bearer' => ['realm' => 'example.com']], ], 'quoted value with spaces' => [ 'Bearer realm="my realm"', ['Bearer' => ['realm' => 'my realm']], ], 'quoted value with escaped double quotes' => [ 'Bearer realm="example\\"quoted\\""', ['Bearer' => ['realm' => 'example"quoted"']], ], 'quoted value with escaped backslashes' => [ 'Bearer realm="path\\\\to\\\\file"', ['Bearer' => ['realm' => 'path\to\file']], ], 'param name with hyphen' => [ 'Bearer error-uri="https://example.com"', ['Bearer' => ['error-uri' => 'https://example.com']], ], 'param name with underscore' => [ 'Bearer error_description="test"', ['Bearer' => ['error_description' => 'test']], ], 'numeric param value' => [ 'Bearer max-age=3600', ['Bearer' => ['max-age' => '3600']], ], 'empty quoted value' => [ 'Bearer realm=""', ['Bearer' => ['realm' => '']], ], 'Basic challenge' => [ 'Basic realm="Access to staging site"', ['Basic' => ['realm' => 'Access to staging site']], ], 'Bearer challenge with realm' => [ 'Bearer realm="https://auth.example.com"', ['Bearer' => ['realm' => 'https://auth.example.com']], ], 'DPoP challenge with algs' => [ 'DPoP algs="ES256 RS256"', ['DPoP' => ['algs' => 'ES256 RS256']], ], 'Digest challenge' => [ 'Digest realm="digest-realm", nonce="abc123"', ['Digest' => ['realm' => 'digest-realm', 'nonce' => 'abc123']], ], 'empty unquoted param' => [ 'Bearer realm=', ['Bearer' => ['realm' => '']], ], 'empty param' => [ 'Bearer realm=""', ['Bearer' => ['realm' => '']], ], 'scheme-only header' => [ 'Basic', ['Basic' => []], ], 'multiple challenges with commas and escaped quotes' => [ "Newauth realm=\"apps\", type=1,\n\t title=\"Login to \\\"apps\\\"\", Basic realm=\"simple\"", [ 'Newauth' => [ 'realm' => 'apps', 'type' => '1', 'title' => 'Login to "apps"', ], 'Basic' => ['realm' => 'simple'], ], ], 'first challenge before comma' => [ 'Basic realm="foo", Bearer realm="bar"', [ 'Basic' => ['realm' => 'foo'], 'Bearer' => ['realm' => 'bar'], ], ], 'empty string' => ['', []], 'whitespace-only string' => [' ', []], 'outer whitespace' => [ ' Bearer realm="test" ', ['Bearer' => ['realm' => 'test']], ], 'commas in quoted param value' => [ 'Bearer realm="example, with, commas"', ['Bearer' => ['realm' => 'example, with, commas']], ], 'multiple challenges with varying whitespace' => [ ' Bearer realm="test" , Basic rr= ', [ 'Bearer' => ['realm' => 'test'], 'Basic' => ['rr' => ''], ], ], 'invalid single challenge without comma' => [ 'Bearer realm="oauth" error="invalid"', NULL, ], 'unclosed quoted value' => [ 'Bearer realm="unclosed', NULL, ], 'token68 value' => [ 'Bearer sometoken', ['Bearer' => 'sometoken'], ], 'trailing whitespace after scheme' => [ 'Bearer ', ['Bearer' => []], ], 'duplicate params use last value' => [ 'Bearer realm="first", realm="second"', ['Bearer' => ['realm' => 'second']], ], 'param name with otherwise invalid characters' => [ 'Bearer realm@foo="bar"', ['Bearer' => ['realm@foo' => 'bar']], ], 'empty param name' => [ 'Bearer ="value"', NULL, ], 'completely malformed input' => [ '!@#$%', ['!@#$%' => []], ], 'duplicate schemes' => [ 'Basic realm="first", Basic realm="second"', NULL, ], 'params without scheme' => [ 'Bearer, realm="foo"', NULL, ], ]; }
public function testInvalidTokenErrorStaysAssociatedWithItsChallenge(): void { $inner = new TestXrpcRequestSender([ new Response(401, [ 'WWW-Authenticate' => 'Bearer realm="api", Basic error="invalid_token"', 'DPoP-Nonce' => 'nonce-value', ]), new Response(200, ['DPoP-Nonce' => 'rotated-nonce'], '{}'), ]); $session = new TestRefreshableDpopSession(); $request = new HttpFactory()->createRequest('GET', 'https://pds.example/xrpc/com.example.get');
$response = new AuthenticatedXrpcRequestSender($inner, $session)->send($request);
static::assertSame(401, $response->getStatusCode()); static::assertSame(1, $inner->requestCount); static::assertSame(0, $session->refreshCount); }
public function testDpopNonceErrorStaysAssociatedWithItsChallenge(): void { $inner = new TestXrpcRequestSender([ new Response(401, [ 'WWW-Authenticate' => 'DPoP algs="ES256", Basic error="use_dpop_nonce"', 'DPoP-Nonce' => 'nonce-value', ]), new Response(200, ['DPoP-Nonce' => 'rotated-nonce'], '{}'), ]); $session = new TestRefreshableDpopSession(); $request = new HttpFactory()->createRequest('GET', 'https://pds.example/xrpc/com.example.get');
$response = new AuthenticatedXrpcRequestSender($inner, $session)->send($request);
static::assertSame(401, $response->getStatusCode()); static::assertSame(1, $inner->requestCount); }
public function testMalformedChallengeDoesNotTriggerRefresh(): void { $inner = new TestXrpcRequestSender([ new Response(401, [ 'WWW-Authenticate' => 'Bearer realm="oauth" error="invalid_token"', 'DPoP-Nonce' => 'nonce-value', ]), new Response(200, ['DPoP-Nonce' => 'rotated-nonce'], '{}'), ]); $session = new TestRefreshableDpopSession(); $request = new HttpFactory()->createRequest('GET', 'https://pds.example/xrpc/com.example.get');
$response = new AuthenticatedXrpcRequestSender($inner, $session)->send($request);
static::assertSame(401, $response->getStatusCode()); static::assertSame(1, $inner->requestCount); static::assertSame(0, $session->refreshCount); }
}