← Back
Editing: Attestation.php
<?php declare(strict_types=1); namespace ThePhpFoundation\Attestation; use Webmozart\Assert\Assert; use function base64_decode; use function wordwrap; /** @internal This is not a public API, so should not be depended upon unless you accept the risk of BC breaks */ final class Attestation { /** @var non-empty-string */ public string $certificate; /** @var non-empty-string */ public string $dsseEnvelopePayload; /** @var non-empty-string */ public string $dsseEnvelopePayloadType; /** @var non-empty-string */ public string $dsseEnvelopeSignature; /** * @param non-empty-string $certificate * @param non-empty-string $dsseEnvelopePayload * @param non-empty-string $dsseEnvelopePayloadType * @param non-empty-string $dsseEnvelopeSignature */ private function __construct( string $certificate, string $dsseEnvelopePayload, string $dsseEnvelopePayloadType, string $dsseEnvelopeSignature ) { $this->dsseEnvelopeSignature = $dsseEnvelopeSignature; $this->dsseEnvelopePayloadType = $dsseEnvelopePayloadType; $this->dsseEnvelopePayload = $dsseEnvelopePayload; $this->certificate = $certificate; } /** @param array<array-key, mixed> $attestation */ public static function fromAttestationBundleWithDsseEnvelope(array $attestation): self { Assert::keyExists($attestation, 'bundle'); Assert::isArray($attestation['bundle']); Assert::keyExists($attestation['bundle'], 'verificationMaterial'); Assert::isArray($attestation['bundle']['verificationMaterial']); Assert::keyExists($attestation['bundle']['verificationMaterial'], 'certificate'); Assert::isArray($attestation['bundle']['verificationMaterial']['certificate']); Assert::keyExists($attestation['bundle']['verificationMaterial']['certificate'], 'rawBytes'); Assert::stringNotEmpty($attestation['bundle']['verificationMaterial']['certificate']['rawBytes']); Assert::keyExists($attestation['bundle'], 'dsseEnvelope'); Assert::isArray($attestation['bundle']['dsseEnvelope']); Assert::keyExists($attestation['bundle']['dsseEnvelope'], 'payload'); Assert::stringNotEmpty($attestation['bundle']['dsseEnvelope']['payload']); Assert::keyExists($attestation['bundle']['dsseEnvelope'], 'payloadType'); Assert::stringNotEmpty($attestation['bundle']['dsseEnvelope']['payloadType']); Assert::keyExists($attestation['bundle']['dsseEnvelope'], 'signatures'); Assert::isNonEmptyList($attestation['bundle']['dsseEnvelope']['signatures']); Assert::count($attestation['bundle']['dsseEnvelope']['signatures'], 1); Assert::keyExists($attestation['bundle']['dsseEnvelope']['signatures'], 0); Assert::isArray($attestation['bundle']['dsseEnvelope']['signatures'][0]); Assert::keyExists($attestation['bundle']['dsseEnvelope']['signatures'][0], 'sig'); Assert::stringNotEmpty($attestation['bundle']['dsseEnvelope']['signatures'][0]['sig']); $decoratedCertificate = "-----BEGIN CERTIFICATE-----\n" . wordwrap($attestation['bundle']['verificationMaterial']['certificate']['rawBytes'], 67, "\n", true) . "\n" . "-----END CERTIFICATE-----\n"; $decodedPayload = base64_decode($attestation['bundle']['dsseEnvelope']['payload']); Assert::stringNotEmpty($decodedPayload); $decodedSignature = base64_decode($attestation['bundle']['dsseEnvelope']['signatures'][0]['sig']); Assert::stringNotEmpty($decodedSignature); return new self( $decoratedCertificate, $decodedPayload, $attestation['bundle']['dsseEnvelope']['payloadType'], $decodedSignature, ); } }
Save File
Cancel