push
This commit is contained in:
1 parent
bcf05e8063
commit
f37985b4c8
109 files changed
+22810
-6143
No files matched your search
+34
@@ -0,0 +1,34 @@
|
||||
// Compact signed tokens: base64url(json) + '.' + hmac. Used to hand the client
|
||||
// opaque URLs for proxy/remux/subs without exposing an open proxy.
|
||||
const crypto = require('crypto');
|
||||
|
||||
let secret = '';
|
||||
|
||||
function init(s) {
|
||||
secret = s;
|
||||
}
|
||||
|
||||
function mac(body) {
|
||||
return crypto.createHmac('sha256', secret).update(body).digest('base64url').slice(0, 22);
|
||||
}
|
||||
|
||||
function sign(obj) {
|
||||
const body = Buffer.from(JSON.stringify(obj)).toString('base64url');
|
||||
return `${body}.${mac(body)}`;
|
||||
}
|
||||
|
||||
function verify(token) {
|
||||
const i = String(token || '').lastIndexOf('.');
|
||||
if (i <= 0) return null;
|
||||
const body = token.slice(0, i);
|
||||
const sig = Buffer.from(token.slice(i + 1));
|
||||
const want = Buffer.from(mac(body));
|
||||
if (sig.length !== want.length || !crypto.timingSafeEqual(sig, want)) return null;
|
||||
try {
|
||||
return JSON.parse(Buffer.from(body, 'base64url').toString('utf8'));
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { init, sign, verify };
|
||||
Reference in new issue
Block a user