'dpop+jwt', 'alg' => $this->privateKey->algorithm, 'jwk' => $this->privateKey->publicJwk(), ]; $payload = [ 'htu' => $this->normalizeUri($request), 'htm' => \strtoupper($request->getMethod()), 'iat' => $this->now(), 'jti' => $this->newJti(), ]; if ($accessToken !== NULL && $accessToken !== '') { $payload['ath'] = Base64Url::encode(\hash('sha256', $accessToken, TRUE)); } if ($nonce !== NULL && $nonce !== '') { $payload['nonce'] = $nonce; } $jws = $this ->jwsBuilder() ->create() ->withPayload(\json_encode($payload, JSON_THROW_ON_ERROR)) ->addSignature($this->privateKey->jwk, $header) ->build(); return $this->serializer()->serialize($jws); } private function jwsBuilder(): JWSBuilder { return ( $this->jwsBuilder ?? new JWSBuilder(new AlgorithmManager([ new ES256(), new RS256(), ])) ); } private function serializer(): CompactSerializer { return $this->serializer ?? new CompactSerializer(); } private static function normalizeUri(RequestInterface $request): string { $uri = $request->getUri()->withQuery('')->withFragment(''); return (string) $uri; } private function now(): int { $now = $this->timeFactory !== NULL ? ($this->timeFactory)() : \time(); if (!\is_int($now)) { throw new \UnexpectedValueException('The DPoP time factory must return an integer timestamp.'); } return $now; } private function newJti(): string { if ($this->jtiFactory !== NULL) { $jti = ($this->jtiFactory)(); if (!\is_string($jti)) { throw new \UnexpectedValueException('The DPoP JTI factory must return a string.'); } return $jti; } return \bin2hex(\random_bytes(16)); } }