Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CloudFront signed cookie to docs #1

Open
wparad opened this issue Jun 25, 2022 · 0 comments
Open

Add CloudFront signed cookie to docs #1

wparad opened this issue Jun 25, 2022 · 0 comments

Comments

@wparad
Copy link
Member

wparad commented Jun 25, 2022

const AWS = require('aws-sdk');

const cloudFront = new AWS.CloudFront.Signer(
  process.env.PUBLIC_KEY,
  process.env.PRIVATE_KEY
);

const policy = JSON.stringify({
  Statement: [
    {
      Resource: 'http*://cdn.your-domain.com/*', // http* => http and https
      Condition: {
        DateLessThan: {
          'AWS:EpochTime':
            Math.floor(new Date().getTime() / 1000) + 60 * 60 * 1, // Current Time in UTC + time in seconds, (60 * 60 * 1 = 1 hour)
        },
      },
    },
  ],
});

// Handle Login Route
router.post('/login-route', (req, res) => {
  /* Code to Verify the credentials */

  // Set Cookies after successful verification
  const cookie = cloudFront.getSignedCookie({
    policy,
  });

  res.cookie('CloudFront-Key-Pair-Id', cookie['CloudFront-Key-Pair-Id'], {
    domain: '.your-domain.com',
    path: '/',
    httpOnly: true,
  });

  res.cookie('CloudFront-Policy', cookie['CloudFront-Policy'], {
    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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant