Kimlik doğrulama

Reseller API

Zorunlu istek başlıkları

Her istek (/v1/health hariç) bu dört başlığı taşımalıdır. Biri eksik veya geçersizse sunucu HTTP 401 ile reddeder.

HeaderFormatDescription
KH-Keykh_live_[A-Z0-9]{32}Genel anahtar tanımlayıcısı. Gizli değildir, loglarda görünebilir.
KH-TimestampUnix saniye (10 basamak)Geçerli zaman damgası. Tolerans penceresi +-300s.
KH-Nonce22-44 base64url karakterİstek başına tek kullanımlık değer. 600s saklanır, ardından yeniden kullanılabilir.
KH-Signature64 hexHMAC-SHA256(secret, signing_string), hex kodlu.

İmzalama dizesini oluşturma

İmzalanacak dize, yeni satır (\n) ile ayrılmış beş bileşenden oluşur.

signing_string = METHOD + "\n"
               + PATH    + "\n"
               + TIMESTAMP + "\n"
               + NONCE   + "\n"
               + SHA256_HEX(BODY)

signature = HEX( HMAC_SHA256(secret, signing_string) )

PATH, sorgu dizesi dahil eksiksiz istek yolunu içerir, host ve fragment hariç. BODY, ham gövde baytlarıdır; gövdesi olmayan GET/DELETE isteklerinde BODY boş bir dizedir, SHA256 değeri bilinen sabit e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 değeridir.

Referans uygulamalar

PHP:

$method = 'POST';
$path   = '/v1/orders';
$body   = json_encode(['product_id' => 42, 'billing_cycle' => 'monthly']);
$ts     = (string) time();
$nonce  = bin2hex(random_bytes(16));

$signing = "{$method}\n{$path}\n{$ts}\n{$nonce}\n" . hash('sha256', $body);
$sig = hash_hmac('sha256', $signing, $KH_SECRET);

$ch = curl_init('https://www.kernelhost.com/cp/kh_reseller_api/v1/orders');
curl_setopt_array($ch, [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_CUSTOMREQUEST  => 'POST',
    CURLOPT_POSTFIELDS     => $body,
    CURLOPT_HTTPHEADER     => [
        'Content-Type: application/json',
        "KH-Key: {$KH_KEY}",
        "KH-Timestamp: {$ts}",
        "KH-Nonce: {$nonce}",
        "KH-Signature: {$sig}",
        'Idempotency-Key: ' . bin2hex(random_bytes(16)),
    ],
]);
$res = curl_exec($ch);

Node.js:

import crypto from 'crypto';

const method = 'POST';
const path   = '/v1/orders';
const body   = JSON.stringify({ product_id: 42, billing_cycle: 'monthly' });
const ts     = Math.floor(Date.now() / 1000).toString();
const nonce  = crypto.randomBytes(16).toString('hex');

const bodyHash = crypto.createHash('sha256').update(body).digest('hex');
const signing  = `${method}\n${path}\n${ts}\n${nonce}\n${bodyHash}`;
const sig      = crypto.createHmac('sha256', KH_SECRET).update(signing).digest('hex');

const res = await fetch('https://www.kernelhost.com/cp/kh_reseller_api/v1/orders', {
    method: 'POST',
    headers: {
        'Content-Type': 'application/json',
        'KH-Key': KH_KEY,
        'KH-Timestamp': ts,
        'KH-Nonce': nonce,
        'KH-Signature': sig,
        'Idempotency-Key': crypto.randomBytes(16).toString('hex'),
    },
    body,
});

Replay koruması

Başarıyla imzalanmış bir istek tekrar gönderilemez. Sunucu her nonce değerini 600s boyunca veritabanında saklar, aynı nonce ile gelen ikinci istek replay_detected ile reddedilir. Aynı şekilde zaman damgası sunucu zamanından 300s'den fazla sapan istekler de reddedilir.

Kapsam/izin modeli

Her anahtarın açık bir kapsam/izin listesi vardır. Rotalar gerekli kapsam/izni kontrol eder, eksikse HTTP 403 forbidden_scope döner. Yazma yetkili ve hassas kapsam/izinler anahtar oluşturulurken açıkça etkinleştirilmek zorundadır.

  • read:products, read:orders, read:services, read:billing, read:webhooks
  • read:credentials (Servis erişim bilgilerini okuma (root parolaları, FTP, VNC). Her çağrı başına ek bir credentials.read denetim kaydı oluşturur.)
  • write:orders (Sipariş verme ve ödeme.)
  • write:services (Servis aksiyonlarını yürütme (start/stop/reboot/reinstall/terminate).)
  • write:webhooks (Webhook URL'sini ayarlama.)