const crypto = require("crypto");
const API_PUBLIC_KEY = "your_public_key";
const API_PRIVATE_KEY = "your_private_key";
const CHANNEL_AUTH_ENDPOINT = "https://api.futuur.com/v2.0/pusher/auth/";
async function authorizeChannel(socketId, channelName) {
const timestamp = String(Math.floor(Date.now() / 1000));
const paramsToSign = {
Key: API_PUBLIC_KEY,
Timestamp: timestamp,
channel_name: channelName,
socket_id: socketId,
};
const rawToSign = new URLSearchParams(
Object.entries(paramsToSign).sort(([a], [b]) => a.localeCompare(b)),
).toString();
const signature = crypto
.createHmac("sha512", API_PRIVATE_KEY)
.update(rawToSign)
.digest("hex");
const response = await fetch(CHANNEL_AUTH_ENDPOINT, {
method: "POST",
headers: {
Key: API_PUBLIC_KEY,
Timestamp: timestamp,
HMAC: signature,
"Content-Type": "application/x-www-form-urlencoded",
},
body: new URLSearchParams({
socket_id: socketId,
channel_name: channelName,
}),
});
if (!response.ok) {
throw new Error("Forbidden");
}
return response.json(); // { auth: "..." }
}