Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051<?php
declare(strict_types=1);
namespace Wotnak\Atproto\Auth\OAuth;
use Psr\Http\Message\RequestInterface;use Wotnak\Atproto\Client\AccessTokenProviderInterface;use Wotnak\Atproto\Client\DpopNonceAwareAuthenticatedRequestSignerInterface;
/** * Signs outgoing requests with a DPoP proof. * * @internal */final readonly class DpopRequestSigner implements DpopNonceAwareAuthenticatedRequestSignerInterface {
public function __construct( private DpopProofFactory $proofFactory, private ?AccessTokenProviderInterface $accessTokenProvider = NULL, private ?string $nonce = NULL, ) {}
public function sign(RequestInterface $request): RequestInterface { $accessToken = $this->accessTokenProvider?->getAccessToken() ?? $this->extractBearerToken($request); $request = $request->withHeader('DPoP', $this->proofFactory->createProof($request, $accessToken, $this->nonce)); if ($accessToken !== NULL && $accessToken !== '') { $request = $request->withHeader('Authorization', 'DPoP ' . $accessToken); } return $request; }
public function withNonce(?string $nonce): self { return new self($this->proofFactory, $this->accessTokenProvider, $nonce); }
public function signWithDpopNonce(RequestInterface $request, ?string $nonce): RequestInterface { return $this->withNonce($nonce)->sign($request); }
private static function extractBearerToken(RequestInterface $request): ?string { $authorization = $request->getHeaderLine('Authorization'); $matches = []; if (\preg_match('/^Bearer\s+(.+)$/i', $authorization, $matches) !== 1) { return NULL; } return $matches[1]; }
}