Skip to content

Instantly share code, notes, and snippets.

@bryanjhv
Last active May 17, 2020 20:44
Show Gist options
  • Save bryanjhv/806b9fd70f1704fe7361b2292a54835f to your computer and use it in GitHub Desktop.
Save bryanjhv/806b9fd70f1704fe7361b2292a54835f to your computer and use it in GitHub Desktop.
TURN REST API for the browser (INSECURE!).
/*
USAGE:
TURN('mysecret', 24 * 60 * 60)
.then(({ username, credential }) => {
console.log({ username, credential })
})
*/
const TURN = ((crypto, encoder) => {
return (secret, duration = 3600) => {
const username = Math.floor(Date.now() / 1000) + duration + ''
return crypto
.importKey(
'raw',
encoder.encode(secret),
{ name: 'HMAC', hash: 'SHA-1' },
false,
['sign']
)
.then(key => crypto.sign('HMAC', key, encoder.encode(username)))
.then(hmac => ({
username,
credential: btoa(String.fromCharCode(...new Uint8Array(hmac))),
}))
}
})(crypto.subtle, new TextEncoder())
var TURN=function(n,e){return function(r,t){void 0===t&&(t=3600);var o=Math.floor(Date.now()/1e3)+t+"";return n.importKey("raw",e.encode(r),{name:"HMAC",hash:"SHA-1"},!1,["sign"]).then(function(r){return n.sign("HMAC",r,e.encode(o))}).then(function(n){return{username:o,credential:btoa(String.fromCharCode.apply(String,new Uint8Array(n)))}})}}(crypto.subtle,new TextEncoder);
const TURN=((e,n)=>(t,r=3600)=>{const o=Math.floor(Date.now()/1e3)+r+"";return e.importKey("raw",n.encode(t),{name:"HMAC",hash:"SHA-1"},!1,["sign"]).then(t=>e.sign("HMAC",t,n.encode(o))).then(e=>({username:o,credential:btoa(String.fromCharCode(...new Uint8Array(e)))}))})(crypto.subtle,new TextEncoder);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment