Skip to content

Instantly share code, notes, and snippets.

@apal21
Created February 6, 2019 09:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save apal21/ff249671aebbb1890f06ccfa799d10e7 to your computer and use it in GitHub Desktop.
Save apal21/ff249671aebbb1890f06ccfa799d10e7 to your computer and use it in GitHub Desktop.
Example to generate Signed Cookies in CloudFront using Canned Policy.
const AWS = require('aws-sdk');
const cloudFront = new AWS.CloudFront.Signer(
process.env.PUBLIC_KEY,
process.env.PRIVATE_KEY
);
// Handle Login Route
router.post('/login-route', (req, res) => {
/* Code to Verify the credentials */
// Set Cookies after successful verification
const cookie = cloudFront.getSignedCookie({
url: 'https://cdn.your-domain.com/test/a.txt',
expires: Math.floor(new Date().getTime() / 1000) + 60 * 60 * 1, // Current Time in UTC + time in seconds, (60 * 60 * 1 = 1 hour)
});
res.cookie('CloudFront-Key-Pair-Id', cookie['CloudFront-Key-Pair-Id'], {
domain: '.your-domain.com',
path: '/',
httpOnly: true,
});
res.cookie('CloudFront-Expires', cookie['CloudFront-Expires'], {
domain: '.your-domain.com',
path: '/',
httpOnly: true,
});
res.cookie('CloudFront-Signature', cookie['CloudFront-Signature'], {
domain: '.your-domain.com',
path: '/',
httpOnly: true,
});
// Send some response
res.send({ some: 'response' });
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment