UNPKG

@adonis-agora/media

Version:

Media-library for AdonisJS — owner collections (MIME whitelist, single-file replace, ordering), image conversions (eager/lazy), pluggable content transformers (HLS video via mediabunny, metadata probe, image blur-up placeholder), and column attachments, a

82 lines • 3.34 kB
import { createHmac, timingSafeEqual } from 'node:crypto'; import { DeliverySignatureError } from './errors.js'; export const DEFAULT_DELIVERY_SIGNING_TTL_SECONDS = 3600; /** HMAC-SHA256 over the canonical form of the params, base64url. */ export function signDelivery(secret, params) { const canonical = [ params.mediaId, params.conversion ?? '', params.width ?? '', params.quality ?? '', params.expires, ].join('\n'); return createHmac('sha256', secret).update(canonical).digest('base64url'); } /** * Expiry for a URL signed now: `now + ttl`, rounded up to a multiple of `ttl` so the URL is stable * for the rest of the window. */ export function deliveryExpiry(nowMs, ttlSeconds) { const now = Math.floor(nowMs / 1000); return Math.ceil((now + ttlSeconds) / ttlSeconds) * ttlSeconds; } /** * Check a request's signature. Throws {@link DeliverySignatureError} when it is invalid or expired. * Returns `false` when the request carries no signature at all (the caller decides whether that is * allowed), `true` when it is signed and valid. */ export function verifyDelivery(secret, request, nowMs) { const { signature, expires } = request; if ((signature === undefined || signature === '') && (expires === undefined || expires === '')) { return false; } if (!signature || expires === undefined || expires === '') { throw new DeliverySignatureError('invalid'); } const exp = Number(expires); if (!Number.isInteger(exp)) throw new DeliverySignatureError('invalid'); const expected = signDelivery(secret, { mediaId: request.mediaId, conversion: emptyToUndefined(request.conversion), width: toOptionalInt(request.width), quality: toOptionalInt(request.quality), expires: exp, }); const given = Buffer.from(signature); const wanted = Buffer.from(expected); if (given.length !== wanted.length || !timingSafeEqual(given, wanted)) { throw new DeliverySignatureError('invalid'); } // Checked after the signature: an attacker learns nothing from "expired" on a forged URL. if (exp * 1000 < nowMs) throw new DeliverySignatureError('expired'); return true; } /** Build the query string of a delivery URL. */ export function deliveryQueryString(params) { const query = new URLSearchParams(); if (params.conversion !== undefined) query.set('conversion', params.conversion); if (params.width !== undefined) query.set('w', String(params.width)); if (params.quality !== undefined) query.set('q', String(params.quality)); if (params.expires !== undefined) query.set('exp', String(params.expires)); if (params.signature !== undefined) query.set('sig', params.signature); const text = query.toString(); return text ? `?${text}` : ''; } function emptyToUndefined(value) { return value === '' ? undefined : value; } function toOptionalInt(value) { if (value === undefined || value === '') return undefined; const parsed = typeof value === 'number' ? value : /^\d+$/.test(value) ? Number(value) : Number.NaN; // A non-numeric value cannot have been signed by us; NaN makes the HMAC mismatch. return parsed; } //# sourceMappingURL=delivery_signing.js.map