Class: CORS::Policy::S3Post

Inherits:
Object
  • Object
show all
Includes:
CORS::Policy
Defined in:
lib/cors/policy/s3_post.rb

Overview

POST form upload policy for Amazon S3.

Instance Attribute Summary

Attributes included from CORS::Policy

#attributes, #errors

Instance Method Summary collapse

Methods included from CORS::Policy

included, #initialize, #rules, #sign, #valid?

Instance Method Details

#policy(expiration) ⇒ Hash

Generate the policy used to sign the request.

Parameters:

  • expiration (Time)

Returns:

  • (Hash)

    policy as a hash



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/cors/policy/s3_post.rb', line 16

def policy(expiration)
  conditions = []
  properties = attributes.dup

  if properties.has_key?("content-length")
    length = escape(properties.delete("content-length"))
    conditions << [ "content-length-range", length, length ]
  end

  {
    "expiration" => expiration.gmtime.iso8601,
    "conditions" => conditions + properties.map do |(name, value)|
      [ "eq", "$#{escape(name)}", escape(value) ]
    end
  }
end

#policy_base64(expiration) ⇒ String

Generate a policy and encode it in URL-safe Base64 encoding.

Parameters:

  • expiration (Time)

Returns:

  • (String)

    the policy, in JSON, in URL-safe base64 encoding.



37
38
39
40
# File 'lib/cors/policy/s3_post.rb', line 37

def policy_base64(expiration)
  json = MultiJson.dump(policy(expiration))
  Base64.urlsafe_encode64(json)
end

#sign!(secret_access_key, expiration) ⇒ String

Sign the #policy for the given expiration.

Parameters:

  • secret_access_key (String)
  • expiration (Time)

Returns:

  • (String)

    signed policy in Base64-encoding.



47
48
49
50
# File 'lib/cors/policy/s3_post.rb', line 47

def sign!(secret_access_key, expiration)
  digest = OpenSSL::HMAC.digest("sha1", secret_access_key, policy_base64(expiration))
  Base64.strict_encode64(digest)
end