Created
November 6, 2017 21:22
-
-
Save brandonaaskov/dcea2c9bf334adddf9c027d847c9ee84 to your computer and use it in GitHub Desktop.
Filestack's docs are woefully lame for how to create security policies. Hopefully this helps.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// -------------------------------------------- SERVER SIDE | |
const base64 = require('base64-url') | |
const crypto = require('crypto') | |
const moment = require('moment') | |
function calculatePolicy(policy, secret) { | |
const policyString = JSON.stringify(policy) | |
const encodedPolicy = base64.encode(policyString) | |
const signature = crypto.createHmac('sha256', secret).update(secret).digest('hex') | |
return { | |
policy: encodedPolicy, | |
signature | |
} | |
} | |
// this policy can be created dynamically either on the server or client | |
const basicSamplePolicy = { | |
call: [ 'pick', 'list' ], | |
expiry: moment().add(1, 'hour').valueOf() | |
} | |
const FILESTACK_SECRET = 'MY_FILESTACK_SECRET' | |
console.log(calculatePolicy(basicSamplePolicy, FILESTACK_SECRET)) | |
// To use, you would put this `calculatePolicy` stuff behind an API route, so you could | |
// say something like `/policy` and it would return the encoded policy and signature | |
// which you would use to instantiate the filestack client. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment