Last active
July 25, 2017 06:07
-
-
Save t11a/06965f6aa87569f78b3a to your computer and use it in GitHub Desktop.
CloudFront - Signed Cookies Using a Custom Policy
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
#!/usr/bin/env ruby | |
require 'json' | |
### CloudFront Key Pair | |
KEY_PAIR_ID = "XXXXX" | |
PRIVATE_KEY = "pk-XXXXX.pem" | |
### Destination URL and RESOURCE for Policy | |
DST_URL = "https://xxxx.cloudfront.net/index.html" | |
RESOURCE = "http*://xxxx.cloudfront.net/index.html" | |
start_time = (Time.now - 60).to_i | |
expire_time = (Time.now + 60*60*24*10).to_i | |
condition = { "DateLessThan" => {"AWS:EpochTime" => expire_time }, "DateGreaterThan" => {"AWS:EpochTime" => start_time } } | |
policy = { "Statement" => ["Resource" => RESOURCE, "Condition" => condition] } | |
puts "------- policy -------" | |
p policy = policy.to_json | |
puts "------- encoded_policy -------" | |
encoded_policy = `printf %s '#{policy}' | base64 | tr '+=/' '-_~'` | |
p encoded_policy.gsub!(/(\r\n|\r|\n)/, "") | |
# cat policy.json | openssl sha1 -sign pk.pem | openssl base64 | tr '+=/' '-_~' | |
signature = `printf %s '#{policy}' | openssl sha1 -sign #{PRIVATE_KEY} | openssl base64 | tr '+=/' '-_~'` | |
puts "------ signature --------" | |
p signature.gsub!(/(\r\n|\r|\n)/, "") | |
header = "Cookie:CloudFront-Expires=#{expire_time}; CloudFront-Policy=#{encoded_policy}; CloudFront-Signature=#{signature}; CloudFront-Key-Pair-Id=#{KEY_PAIR_ID}" | |
puts "-------- header -------" | |
p header | |
puts "---------------" | |
puts `curl -vH '#{header}' #{DST_URL}` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://dev.classmethod.jp/cloud/aws/cf-s3-private-content-signed-cookies/