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

update to use AWS Signature Version 4 #174

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 33 additions & 13 deletions lib/s3_direct_upload/form_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def initialize(options)
aws_access_key_id: S3DirectUpload.config.access_key_id,
aws_secret_access_key: S3DirectUpload.config.secret_access_key,
bucket: options[:bucket] || S3DirectUpload.config.bucket,
region: S3DirectUpload.config.region || "s3",
region: S3DirectUpload.config.region || "us-east-1",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should us-east-1 be the default?

url: S3DirectUpload.config.url,
ssl: true,
acl: "public-read",
Expand All @@ -25,10 +25,20 @@ def initialize(options)
callback_method: "POST",
callback_param: "file",
key_starts_with: @key_starts_with,
key: key
key: key,
date: Time.now.utc.strftime("%Y%m%d"),
timestamp: Time.now.utc.strftime("%Y%m%dT%H%M%SZ")
)
end

def hostname
if @options[:region] == "us-east-1"
"#{@options[:bucket]}.s3.amazonaws.com"
else
"#{@options[:bucket]}.s3-#{@options[:region]}.amazonaws.com"
end
end

def form_options
{
id: @options[:id],
Expand All @@ -46,12 +56,14 @@ def form_options

def fields
{
:key => @options[:key] || key,
:acl => @options[:acl],
"AWSAccessKeyId" => @options[:aws_access_key_id],
:key => @options[:key] || key,
:policy => policy,
:signature => signature,
:success_action_status => "201",
'X-Amz-Algorithm' => 'AWS4-HMAC-SHA256',
'X-Amz-Credential' => "#{@options[:aws_access_key_id]}/#{@options[:date]}/#{@options[:region]}/s3/aws4_request",
'X-Amz-Date' => @options[:timestamp],
'X-Amz-Signature' => signature,
'X-Requested-With' => 'xhr'
}
end
Expand All @@ -61,7 +73,7 @@ def key
end

def url
@options[:url] || "http#{@options[:ssl] ? 's' : ''}://#{@options[:region]}.amazonaws.com/#{@options[:bucket]}/"
@options[:url] || "http#{@options[:ssl] ? 's' : ''}://#{hostname}/"
end

def policy
Expand All @@ -79,18 +91,26 @@ def policy_data
["starts-with","$content-type", @options[:content_type_starts_with] ||""],
{bucket: @options[:bucket]},
{acl: @options[:acl]},
{success_action_status: "201"}
{success_action_status: "201"},
{'X-Amz-Algorithm' => 'AWS4-HMAC-SHA256'},
{'X-Amz-Credential' => "#{@options[:aws_access_key_id]}/#{@options[:date]}/#{@options[:region]}/s3/aws4_request"},
{'X-Amz-Date' => @options[:timestamp]}
] + (@options[:conditions] || [])
}
end

def signing_key
#AWS Signature Version 4
kDate = OpenSSL::HMAC.digest('sha256', "AWS4" + @options[:aws_secret_access_key], @options[:date])
kRegion = OpenSSL::HMAC.digest('sha256', kDate, @options[:region])
kService = OpenSSL::HMAC.digest('sha256', kRegion, 's3')
kSigning = OpenSSL::HMAC.digest('sha256', kService, "aws4_request")

kSigning
end

def signature
Base64.encode64(
OpenSSL::HMAC.digest(
OpenSSL::Digest.new('sha1'),
@options[:aws_secret_access_key], policy
)
).gsub("\n", "")
OpenSSL::HMAC.hexdigest('sha256', signing_key, policy)
end
end
end
Expand Down