Issues with Cloudfront Signed Cookies in my Video Streaming App

0
5
Asked By TechWizard42 On

I'm developing a learning management system using Django and React where I want to restrict video content to only users enrolled in specific courses. I'm attempting to implement Cloudfront signed cookies, but I'm running into a problem. When I request video content from Cloudfront using React (with Video.js for adaptive bitrate streaming), it seems that the cookies aren't being sent at all. I keep getting this error:
`MissingKeyMissing Key-Pair-Id query parameter or cookie value`.

Here's how I'm setting the cookies on the Django backend:
```
response.set_cookie('CloudFront-Policy', cookie_dict['CloudFront-Policy'], path='/', samesite='None', httponly=True, secure=True)
response.set_cookie('CloudFront-Signature', cookie_dict['CloudFront-Signature'], path='/', samesite='None', httponly=True, secure=True)
response.set_cookie('CloudFront-Key-Pair-Id', cookie_dict['CloudFront-Key-Pair-Id'], path='/', samesite='None', httponly=True, secure=True)
```

And this is the React code that sends the request:
```
useEffect(() => {
if (!playerRef.current) {
playerRef.current = videojs(videoRef.current, {..., html5: {withCredentials: true}});
}
}, [src]);
```

The functionality works fine when there are no content restrictions. Any help would be appreciated!

2 Answers

Answered By ChemoSh_tz On

You’re likely facing a configuration issue. Make sure that your cookies are actually being set. When testing locally, keep an eye on your browser's cookie settings to ensure they're being stored properly. If they're not set on the cloudfront domain, they'll be discarded. Verify your cookie's SameSite attribute as well, since using `None` requires the `Secure` flag to be set, and your app needs to be served over HTTPS.

Answered By CodingNinja99 On

It sounds like your cookie domain settings might be causing the issue. If your application runs on `localhost`, setting the domain to `cloudfront.net` won't work, as browsers won't accept cookies for a different domain. Try using the domain of your application instead. For local development, you can typically set it to `localhost`, but just make sure it matches the origin of your requests.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.