Facebook
From Commodious Bee, 3 Years ago, written in JavaScript.
Embed
Download Paste or View Raw
Hits: 285
  1.  
  2. // Global variables
  3. const jwkKey = '???'
  4. const keyID = '???'
  5. const videoID = '???'
  6. // expiresTimeInS is the expired time in second of the video
  7. const expiresTimeInS = 3600
  8.  
  9. // Main function
  10. async function streamSignedUrl () {
  11.   const encoder = new TextEncoder()
  12.   const expiresIn = Math.floor(Date.now() / 1000) + expiresTimeInS
  13.   const headers = {
  14.     "alg": "RS256",
  15.     "kid": keyID
  16.   }
  17.   const data = {
  18.     "sub": videoID,
  19.     "kid": keyID,
  20.     "exp": expiresIn,
  21.     "accessRules": [
  22.       {
  23.         "type": "ip.geoip.country",
  24.         "action": "allow",
  25.         "country": [
  26.           "US"
  27.         ]
  28.       },
  29.       {
  30.         "type": "any",
  31.         "action": "block"
  32.       }
  33.     ]
  34.   }
  35.  
  36.   const token = `${objectToBase64url(headers)}.${objectToBase64url(data)}`
  37.  
  38.   const jwk = JSON.parse(atob(jwkKey))
  39.  
  40.   const key = await crypto.subtle.importKey(
  41.     "jwk", jwk,
  42.     {
  43.       name: 'RSASSA-PKCS1-v1_5',
  44.       hash: 'SHA-256',
  45.     },
  46.     false, [ "sign" ]
  47.   )
  48.  
  49.   const signature = await crypto.subtle.sign(
  50.     { name: 'RSASSA-PKCS1-v1_5' }, key,
  51.     encoder.encode(token)
  52.   )
  53.  
  54.   const signedToken = `${token}.${arrayBufferToBase64Url(signature)}`
  55.  
  56.   return signedToken
  57. }
  58.  
  59. // Utilities functions
  60. function arrayBufferToBase64Url(buffer) {
  61.   return btoa(String.fromCharCode(...new Uint8Array(buffer)))
  62.     .replace(/=/g, '')
  63.     .replace(/\+/g, '-')
  64.     .replace(/\//g, '_')
  65. }
  66.  
  67. function objectToBase64url(payload) {
  68.   return arrayBufferToBase64Url(
  69.     new TextEncoder().encode(JSON.stringify(payload)),
  70.   )
  71. }
  72.  
  73. addEventListener('fetch', event => {
  74.   event.respondWith(handleRequest(event.request))
  75. })
  76.  
  77. /**
  78.  * Respond to the request
  79.  * @param {Request} request
  80.  */
  81. async function handleRequest(request) {
  82.   const signedURL = await streamSignedUrl()
  83.   return new Response(signedURL, {status: 200})
  84. }
  85.  
  86.