Skip to content

Instantly share code, notes, and snippets.

@samuelralak
Created April 13, 2016 06:01
Show Gist options
  • Save samuelralak/c3cd17fd3bcfc1a5cf38c6d031e820e5 to your computer and use it in GitHub Desktop.
Save samuelralak/c3cd17fd3bcfc1a5cf38c6d031e820e5 to your computer and use it in GitHub Desktop.
module AmazonSignature
extend self
def signature
Base64.encode64(
OpenSSL::HMAC.digest(
OpenSSL::Digest.new('sha1'),
AWS_CONFIG['secret_access_key'], self.policy
)
).gsub("\n", "")
end
def policy
Base64.encode64(self.policy_data.to_json).gsub("\n", "")
end
def policy_data
{
expiration: 10.hours.from_now.utc.iso8601,
conditions: [
["starts-with", "$key", AWS_CONFIG['key_start']],
["starts-with", "$x-requested-with", "xhr"],
["content-length-range", 0, 20.megabytes],
["starts-with", "$content-type", ""],
{bucket: AWS_CONFIG['bucket']},
{acl: AWS_CONFIG['acl']},
{success_action_status: "201"}
]
}
end
def data_hash
{:signature => self.signature, :policy => self.policy, :bucket => AWS_CONFIG['bucket'], :acl => AWS_CONFIG['acl'], :key_start => AWS_CONFIG['key_start'], :access_key => AWS_CONFIG['access_key_id']}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment