Skip to content

Commit

Permalink
Support for condition matching (all types)
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrieloczkowski committed Dec 7, 2015
1 parent a9a0713 commit 061ce69
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 8 deletions.
13 changes: 10 additions & 3 deletions lib/index.js

Large diffs are not rendered by default.

11 changes: 8 additions & 3 deletions src/lib/index.coffee
Expand Up @@ -22,7 +22,7 @@ class S3Client
# Browser form post params for uploading
uploadPostForm: (options = {}, cb) ->
throw new Error 'Callback is required' unless cb
{ extension, key, bucket, expires, acl, contentLength, algorithm, region } = options
{ extension, key, bucket, expires, acl, contentLength, algorithm, region, conditionMatching } = options
key = options.key
bucket = options.bucket
extension = options.extension ? null
Expand All @@ -31,6 +31,7 @@ class S3Client
contentLength = options.contentLength ? null
algorithm = options.algorithm ? 'AWS4-HMAC-SHA256'
region = options.region ? @region
conditionMatching = options.conditionMatching ? null

# @TODO options type check
unless key and bucket
Expand Down Expand Up @@ -65,6 +66,9 @@ class S3Client
policyDoc.conditions.push { "x-amz-credential": "#{@accessKeyId}/#{dateShortPolicy}/#{region}/s3/aws4_request" }
policyDoc.conditions.push { "x-amz-date": dateLongPolicy}

if conditionMatching and _.isArray conditionMatching
policyDoc.conditions = _.union conditionMatching, policyDoc.conditions

dateKey = crypto.createHmac(hashalg, "#{sigver}#{@secretAccessKey}").update(dateShortPolicy).digest()
dateRegionKey = crypto.createHmac(hashalg, dateKey).update(region).digest()
dateRegionServiceKey = crypto.createHmac(hashalg, dateRegionKey).update('s3').digest()
Expand All @@ -82,8 +86,9 @@ class S3Client
"policy": policy
"x-amz-signature": signature
stream.params['content-type'] = contentType if contentType
stream['public_url'] = "https://#{bucket}.s3.amazonaws.com/#{key}"
stream['form_url'] = "https://#{bucket}.s3.amazonaws.com/"
stream['conditions'] = conditionMatching if conditionMatching
stream['public_url'] = "https://#{bucket}.s3.amazonaws.com/#{key}"
stream['form_url'] = "https://#{bucket}.s3.amazonaws.com/"

cb null, stream

Expand Down
26 changes: 26 additions & 0 deletions src/test/index.coffee
Expand Up @@ -47,6 +47,32 @@ describe 's3-uploadPostForm tests', () ->
expect(params).to.have.deep.property 'params.x-amz-signature'
expect(params).to.have.deep.property 'public_url'
expect(params).to.have.deep.property 'form_url'
expect(params).to.not.have.deep.property 'conditions'

done()

it 'should return json with all parameters required to build a form if custom conditionMatching used', (done) ->
uploadPostFormOptions =
key: "testKey.jpg"
bucket: 'testBucket'
expires: moment().add(60, 'minutes').toDate()
extension: 'jpg'
conditionMatching: [
{"success_action_redirect": "http://google.com"}
]

s3client.uploadPostForm uploadPostFormOptions, (err, params) ->
expect(params).to.have.deep.property 'params.key'
expect(params).to.have.deep.property 'params.acl'
expect(params).to.have.deep.property 'params.content-type'
expect(params).to.have.deep.property 'params.x-amz-algorithm'
expect(params).to.have.deep.property 'params.x-amz-credential'
expect(params).to.have.deep.property 'params.x-amz-date'
expect(params).to.have.deep.property 'params.policy'
expect(params).to.have.deep.property 'params.x-amz-signature'
expect(params).to.have.deep.property 'public_url'
expect(params).to.have.deep.property 'form_url'
expect(params).to.have.deep.property 'conditions'

done()

Expand Down

0 comments on commit 061ce69

Please sign in to comment.