Sha256: 6c02bc297c04013223fb68eb9e78cce15f2b374cd290672e8c4f342e3545bc41

Contents?: true

Size: 1.5 KB

Versions: 23

Compression:

Stored size: 1.5 KB

Contents

module S3Relay
  class UploadPresigner < S3Relay::Base

    attr_reader :expires, :uuid

    def initialize(options={})
      @expires = (options[:expires] || 1.minute.from_now).utc.xmlschema
      @uuid    = SecureRandom.uuid
    end

    def form_data
      fields.keys.inject({}) { |h,k| h[k.downcase.underscore] = fields[k]; h }
        .merge(
          "endpoint"  => endpoint,
          "policy"    => encoded_policy,
          "signature" => signature,
          "uuid"      => uuid
        )
    end

    private

    def fields
      {
        "AWSAccessKeyID"               => access_key_id,
        "x-amz-server-side-encryption" => "AES256",
        "key"                          => "#{uuid}/${filename}",
        "success_action_status"        => "201",
        "acl"                          => acl
      }
    end

    def hmac
      lambda { |data| OpenSSL::HMAC.digest(digest, secret_access_key, data) }
    end

    def policy_document
      {
        "expiration" => expires,
        "conditions" => [
          { "bucket" => bucket },
          { "acl" => acl },
          { "x-amz-server-side-encryption" => "AES256" },
          { "success_action_status" => "201" },
          ["starts-with", "$content-type", ""],
          ["starts-with", "$content-disposition", ""],
          ["starts-with", "$key", "#{uuid}/"]
        ]
      }
    end

    def encoded_policy
      Base64.strict_encode64(policy_document.to_json)
    end

    def signature
      Base64.strict_encode64(hmac[encoded_policy])
    end

  end
end

Version data entries

23 entries across 23 versions & 3 rubygems

Version Path
s3_relay-0.3.0 lib/s3_relay/upload_presigner.rb
s3_relay-0.2.0 lib/s3_relay/upload_presigner.rb
s3_relay-0.1.0 lib/s3_relay/upload_presigner.rb