validateApiKey(); if ($apiKeyErrors !== []) { return $apiKeyErrors; } $body = $this->request->getRequest(); $errors = []; $ciphertext = $body->get('ciphertext'); if (! StringHelper::isString($ciphertext)) { $errors['ciphertext'] = 'Must be a string'; } else { $ciphertextError = SecretsValidators::validateBase64($ciphertext, 1, SecretService::MAX_CIPHERTEXT_BYTES); if ($ciphertextError !== null) { $errors['ciphertext'] = $ciphertextError; } } $nonce = $body->get('nonce'); if (! StringHelper::isString($nonce)) { $errors['nonce'] = 'Must be a string'; } else { $nonceError = SecretsValidators::validateBase64Exact($nonce, SecretService::NONCE_LENGTH); if ($nonceError !== null) { $errors['nonce'] = $nonceError; } } $ttl = $body->get('ttl_seconds'); $ttlError = SecretsValidators::validateTtl($ttl, $this->getMaxTtlSeconds()); if ($ttlError !== null) { $errors['ttl_seconds'] = $ttlError; } $errors = array_merge($errors, SecretsValidators::validateAttachmentFields($body, $this->getMaxAttachmentBytes())); return $errors; } private function getMaxTtlSeconds(): int { if ($this->projectConfig instanceof SecretsLimitsInterface) { return $this->projectConfig->getMaxTtlSeconds(); } return SecretService::DEFAULT_MAX_TTL_SECONDS; } private function getMaxAttachmentBytes(): int { if ($this->projectConfig instanceof SecretsLimitsInterface) { return $this->projectConfig->getMaxAttachmentBytes(); } return SecretService::DEFAULT_MAX_ATTACHMENT_BYTES; } public function getData(): array { try { $quotaDenial = $this->reserveApiKeyQuota(); if ($quotaDenial !== null) { return $quotaDenial; } $body = $this->request->getRequest(); $ciphertext = Base64Url::decode((string) $body->get('ciphertext')) ?? ''; $nonce = Base64Url::decode((string) $body->get('nonce')) ?? ''; $ttl = (int) $body->get('ttl_seconds'); $expiresAt = new DateTime('now', new DateTimeZone('UTC')); $expiresAt->add(new DateInterval('PT' . $ttl . 'S')); $attachmentCiphertextRaw = $body->get('attachment_ciphertext'); $attachmentNonceRaw = $body->get('attachment_nonce'); $attachmentMetaRaw = $body->get('attachment_meta'); $attachmentCiphertext = StringHelper::isString($attachmentCiphertextRaw) ? Base64Url::decode($attachmentCiphertextRaw) : null; $attachmentNonce = StringHelper::isString($attachmentNonceRaw) ? Base64Url::decode($attachmentNonceRaw) : null; $attachmentMeta = StringHelper::isString($attachmentMetaRaw) ? Base64Url::decode($attachmentMetaRaw) : null; $secret = $this->secretService->createSecret( $ciphertext, $nonce, $expiresAt, $attachmentCiphertext, $attachmentNonce, $attachmentMeta, $this->getMaxAttachmentBytes(), ); $this->notifySecretCreated($secret->getToken()); return [ 'token' => $secret->getToken(), 'expires_at' => $secret->getExpiresAt()->format(DATE_ATOM), ]; } catch (InvalidArgumentException $e) { $this->httpStatusCode = Http::BAD_REQUEST; $this->secretLogger->logException($e); return $this->showErrorMessage('Invalid request'); } catch (Throwable $e) { $this->secretLogger->logException($e); return $this->showErrorMessage('Unable to create secret'); } } }