Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115<?php
declare(strict_types=1);
namespace Wotnak\Atproto\Auth\OAuth;
use Jose\Component\Core\JWK;use Jose\Component\KeyManagement\JWKFactory;use Wotnak\Atproto\Exception\OAuthException;
/** * Private key used to sign DPoP proofs. * * @internal */final readonly class DpopPrivateKey {
private const SUPPORTED_ALGORITHMS = ['RS256', 'ES256'];
public function __construct( #[\SensitiveParameter] public JWK $jwk, public string $algorithm, ) { if (!\in_array($this->algorithm, self::SUPPORTED_ALGORITHMS, TRUE)) { throw new \InvalidArgumentException(\sprintf('Unsupported DPoP signing algorithm "%s".', $this->algorithm)); } }
public static function generate(string $algorithm = 'ES256'): self { $key = match ($algorithm) { 'ES256' => \openssl_pkey_new([ 'private_key_type' => OPENSSL_KEYTYPE_EC, 'curve_name' => 'prime256v1', ]), 'RS256' => \openssl_pkey_new([ 'private_key_bits' => 2048, 'private_key_type' => OPENSSL_KEYTYPE_RSA, ]), default => throw new \InvalidArgumentException(\sprintf('Unsupported DPoP signing algorithm "%s".', $algorithm)), };
$pem = ''; if ($key === FALSE || !\openssl_pkey_export($key, $pem)) { throw new OAuthException('Unable to generate DPoP private key.'); } // OpenSSL writes through a by-reference output parameter. Keep the // defensive runtime check even though Mago retains the initialized type. // @mago-expect analysis:impossible-condition,redundant-type-comparison if (!\is_string($pem)) { throw new OAuthException('Unable to export DPoP private key.'); }
return self::fromPem($pem, $algorithm); }
public static function fromPem( #[\SensitiveParameter] string $privateKeyPem, string $algorithm = 'ES256', #[\SensitiveParameter] ?string $passphrase = NULL, ): self { if (!\in_array($algorithm, self::SUPPORTED_ALGORITHMS, TRUE)) { throw new \InvalidArgumentException(\sprintf('Unsupported DPoP signing algorithm "%s".', $algorithm)); }
return new self(JWKFactory::createFromKey($privateKeyPem, $passphrase, [ 'alg' => $algorithm, 'use' => 'sig', ]), $algorithm); }
/** * @param array<string, mixed> $values */ public static function fromJwk(#[\SensitiveParameter] array $values, ?string $algorithm = NULL): self { $algorithm ??= \is_string($values['alg'] ?? NULL) ? $values['alg'] : 'ES256'; if (!\in_array($algorithm, self::SUPPORTED_ALGORITHMS, TRUE)) { throw new \InvalidArgumentException(\sprintf('Unsupported DPoP signing algorithm "%s".', $algorithm)); }
$values['alg'] = $algorithm; $values['use'] ??= 'sig'; $jwk = JWKFactory::createFromValues($values); if (!$jwk instanceof JWK) { throw new \InvalidArgumentException('DPoP private key must be a single JWK, not a JWK set.'); } return new self($jwk, $algorithm); }
/** * @return array<string, mixed> */ public function publicJwk(): array { // This assignment carries the PHPDoc refinement for the vendor return type. // @mago-expect lint:inline-variable-return /** @var array<string, mixed> $values */ $values = $this->jwk->toPublic()->all(); return $values; }
/** * @return array<string, mixed> */ public function privateJwk(): array { // This assignment carries the PHPDoc refinement for the vendor return type. // @mago-expect lint:inline-variable-return /** @var array<string, mixed> $values */ $values = $this->jwk->all(); return $values; }
}