Skip to content

An open-source, self-hostable PaaS alternative to Vercel, Heroku & Netlify that lets you easily deploy static sites, databases, full-stack applications and 360+ one-click services on your own servers.

2.0 KB
Raw Blame
<?php

namespace App\Services;

use App\Models\IntegrationToken;

/**
 * Validates an integration token against its provider API before it is saved.
 */
class IntegrationTokenValidator
{
    public function validate(string $provider, string $token, array $capabilities, array $metadata = []): bool
    {
        return match ($provider) {
            'cloudflare' => app(CloudflareTokenValidator::class)->validate($token, $capabilities),
            'doppler' => (new DopplerService($token))->validate(),
            'infisical' => (new InfisicalService(
                (string) data_get($metadata, 'base_url', 'https://app.infisical.com'),
                (string) data_get($metadata, 'client_id'),
                $token,
                IntegrationToken::requestTimeout($metadata),
            ))->validate(),
            'vault' => (new VaultService(
                (string) data_get($metadata, 'base_url'),
                $token,
                data_get($metadata, 'namespace'),
                IntegrationToken::requestTimeout($metadata),
            ))->validate(),
            default => false,
        };
    }

    public function errorMessage(string $provider): string
    {
        return match ($provider) {
            'cloudflare' => 'The token could not access the selected Cloudflare capabilities. Check its permissions and zone resources.',
            'doppler' => 'The Doppler token could not be verified. Check the token and its access.',
            'infisical' => 'Infisical login failed. Check the base URL, the client ID, and the client secret.',
            'vault' => 'The Vault token could not be verified. Check the base URL, the namespace, and the token.',
            default => 'The token could not be verified.',
        };
    }

    public function connectionErrorMessage(string $provider): string
    {
        $name = IntegrationToken::PROVIDER_NAMES[$provider] ?? ucfirst($provider);

        return "Could not connect to the {$name} API. Check the network connection of Coolify and try again.";
    }
}