UNPKG

matterbridge-shelly

Version:
45 lines (44 loc) 1.75 kB
import crypto from 'node:crypto'; export function parseBasicAuthenticateHeader(authHeader) { authHeader = authHeader.replace('Basic ', ''); const authParams = {}; authHeader.split(', ').forEach((param) => { const [key, value] = param.split('='); authParams[key.trim()] = value.replace(/"/g, ''); }); return authParams; } export function parseDigestAuthenticateHeader(authHeader) { authHeader = authHeader.replace('Digest ', ''); const authParams = {}; authHeader.split(', ').forEach((param) => { const [key, value] = param.split('='); authParams[key.trim()] = value.replace(/"/g, ''); }); return authParams; } export function createBasicShellyAuth(username, password) { return Buffer.from(`${username}:${password}`).toString('base64'); } export function createDigestShellyAuth(username, password, nonce, cnonce, realm, nc = 1) { const auth = { realm, username, nonce, cnonce, response: '', algorithm: 'SHA-256' }; const ha1 = crypto.createHash('sha256').update(`admin:${auth.realm}:${password}`).digest('hex'); const ha2 = crypto.createHash('sha256').update('dummy_method:dummy_uri').digest('hex'); auth.response = crypto.createHash('sha256').update(`${ha1}:${auth.nonce}:${nc}:${cnonce}:auth:${ha2}`).digest('hex'); return auth; } export function getGen1BodyOptions(params) { return new URLSearchParams(params).toString(); } export function getGen2BodyOptions(jsonrpc, id, src, method, params, auth) { const body = {}; body.jsonrpc = '2.0'; body.id = 10; body.src = 'Matterbridge'; body.method = method; if (params) body.params = params; if (auth) body.auth = auth; return JSON.stringify(body); }