s3_policy 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2c5d0222981fa5af54e2f07e1a30e3fdad43b784
4
+ data.tar.gz: 7c938b25040d556549b972df4db20cb434df9c01
5
+ SHA512:
6
+ metadata.gz: d0ab17188060124eb1fc3213a7ef2b840d9f0eb53678f1c3e8292ec2474dcb77ee86ef0c6310f282a0af8e4e106c1ea938fa9a5d5b51cc35144727cf26227291
7
+ data.tar.gz: b31f09b45b19f311a223aea5f9d898987cf200abfe93174c80b809345c2d0a372d0ac81c2618eaf124ee8fa2e436fdcc8eaac425c0258e4b561cc2aa717f3d38
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in s3_policy.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Daniel X Moore
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,45 @@
1
+ # S3Policy
2
+
3
+ Generate a signed S3 policy document for namespaced clientside uploads.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 's3_policy'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install s3_policy
18
+
19
+ ## Usage
20
+
21
+ ```ruby
22
+ require 's3_policy'
23
+
24
+ policy = S3Policy.generate_policy_document(
25
+ bucket: "my_awesome_bucket",
26
+ namespace: "users/#{user_id}/"
27
+ )
28
+
29
+ signature = S3Policy.sign_document(policy, aws_secret_key)
30
+
31
+ # Give this to the client
32
+ {
33
+ aws_access_key_id: aws_access_key_id,
34
+ policy: policy,
35
+ signature: signature
36
+ }
37
+ ```
38
+
39
+ ## Contributing
40
+
41
+ 1. Fork it
42
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
43
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
44
+ 4. Push to the branch (`git push origin my-new-feature`)
45
+ 5. Create new Pull Request
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << 'test'
6
+ end
7
+
8
+ desc "Run tests"
9
+ task :default => :test
@@ -0,0 +1,60 @@
1
+ require "s3_policy/version"
2
+
3
+ require "base64"
4
+ require "date"
5
+ require "digest/sha1"
6
+ require "json"
7
+ require "openssl"
8
+ require "time"
9
+
10
+ module S3Policy
11
+ class << self
12
+ def generate_policy_document(options={})
13
+ # Required
14
+ bucket = options[:bucket]
15
+
16
+ # Optional
17
+ acl = options[:acl] || "public-read"
18
+ content_type = options[:content_type] || ""
19
+ expiration = options[:expiration] || one_week_from_now
20
+ max_size = options[:max_size] || 1024 * 1024 * 10
21
+ namespace = options[:namespace] || ""
22
+
23
+ # TODO: ACL Option
24
+
25
+ policy_document = {
26
+ expiration: expiration,
27
+ conditions: [
28
+ { bucket: bucket},
29
+ ["starts-with", "$key", namespace],
30
+ { acl: acl},
31
+ ["starts-with", "$Cache-Control", ""],
32
+ ["starts-with", "$Content-Type", content_type],
33
+ ["content-length-range", 0, max_size]
34
+ ]
35
+ }
36
+ end
37
+
38
+ def one_week_from_now
39
+ (DateTime.now + 7).to_time.utc.iso8601
40
+ end
41
+
42
+ def encode_document(policy_document)
43
+ Base64.strict_encode64(policy_document.to_json)
44
+ end
45
+
46
+ def sign_document(policy_document, secret_key)
47
+ sign_encoded_document(encode_document(policy_document), secret_key)
48
+ end
49
+
50
+ def sign_encoded_document(encoded_policy_document, secret_key)
51
+ Base64.strict_encode64(
52
+ OpenSSL::HMAC.digest(
53
+ OpenSSL::Digest::Digest.new('sha1'),
54
+ secret_key,
55
+ encoded_policy_document
56
+ )
57
+ )
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,3 @@
1
+ module S3Policy
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 's3_policy/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "s3_policy"
8
+ spec.version = S3Policy::VERSION
9
+ spec.authors = ["Daniel X Moore"]
10
+ spec.email = ["yahivin@gmail.com"]
11
+ spec.description = %q{S3 Policy Document Generator}
12
+ spec.summary = %q{Generate a signed S3 policy document for namespaced clientside uploads.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ end
@@ -0,0 +1,40 @@
1
+ require 'test/unit'
2
+ require 's3_policy'
3
+
4
+ class S3PolicyTest < Test::Unit::TestCase
5
+ def test_signing_document
6
+ aws_access_key_id = "AKIAIOSFODNN7EXAMPLE"
7
+
8
+ policy_document = {
9
+ "expiration"=> "2013-08-06T12:00:00.000Z",
10
+ "conditions"=> [
11
+ {"bucket"=> "examplebucket"},
12
+ ["starts-with", "$key", "user/user1/"],
13
+ {"acl"=> "public-read"},
14
+ {"success_action_redirect"=> "http://acl6.s3.amazonaws.com/successful_upload.html"},
15
+ ["starts-with", "$Content-Type", "image/"],
16
+ {"x-amz-meta-uuid"=> "14365123651274"},
17
+ ["starts-with", "$x-amz-meta-tag", ""],
18
+ {"x-amz-credential"=> "AKIAIOSFODNN7EXAMPLE/20130806/us-east-1/s3/aws4_request"},
19
+ {"x-amz-algorithm"=> "AWS4-HMAC-SHA256"},
20
+ {"x-amz-date"=> "20130806T000000Z" }
21
+ ]
22
+ }
23
+
24
+ assert_equal "eyJleHBpcmF0aW9uIjoiMjAxMy0wOC0wNlQxMjowMDowMC4wMDBaIiwiY29uZGl0aW9ucyI6W3siYnVja2V0IjoiZXhhbXBsZWJ1Y2tldCJ9LFsic3RhcnRzLXdpdGgiLCIka2V5IiwidXNlci91c2VyMS8iXSx7ImFjbCI6InB1YmxpYy1yZWFkIn0seyJzdWNjZXNzX2FjdGlvbl9yZWRpcmVjdCI6Imh0dHA6Ly9hY2w2LnMzLmFtYXpvbmF3cy5jb20vc3VjY2Vzc2Z1bF91cGxvYWQuaHRtbCJ9LFsic3RhcnRzLXdpdGgiLCIkQ29udGVudC1UeXBlIiwiaW1hZ2UvIl0seyJ4LWFtei1tZXRhLXV1aWQiOiIxNDM2NTEyMzY1MTI3NCJ9LFsic3RhcnRzLXdpdGgiLCIkeC1hbXotbWV0YS10YWciLCIiXSx7IngtYW16LWNyZWRlbnRpYWwiOiJBS0lBSU9TRk9ETk43RVhBTVBMRS8yMDEzMDgwNi91cy1lYXN0LTEvczMvYXdzNF9yZXF1ZXN0In0seyJ4LWFtei1hbGdvcml0aG0iOiJBV1M0LUhNQUMtU0hBMjU2In0seyJ4LWFtei1kYXRlIjoiMjAxMzA4MDZUMDAwMDAwWiJ9XX0=",
25
+ S3Policy.encode_document(policy_document)
26
+
27
+ end
28
+
29
+ def test_signing_encoded_document
30
+ aws_secret_key = "uV3F3YluFJax1cknvbcGwgjvx4QpvB+leU8dUj2o"
31
+ encoded_document = "eyAiZXhwaXJhdGlvbiI6ICIyMDA3LTEyLTAxVDEyOjAwOjAwLjAwMFoiLAogICJjb25kaXRpb25zIjogWwogICAgeyJidWNrZXQiOiAiam9obnNtaXRoIiB9LAogICAgWyJzdGFydHMtd2l0aCIsICIka2V5IiwgInVzZXIvZXJpYy8iXSwKICAgIHsiYWNsIjogInB1YmxpYy1yZWFkIiB9LAogICAgeyJyZWRpcmVjdCI6ICJodHRwOi8vam9obnNtaXRoLnMzLmFtYXpvbmF3cy5jb20vc3VjY2Vzc2Z1bF91cGxvYWQuaHRtbCIgfSwKICAgIFsic3RhcnRzLXdpdGgiLCAiJENvbnRlbnQtVHlwZSIsICJpbWFnZS8iXSwKICAgIHsieC1hbXotbWV0YS11dWlkIjogIjE0MzY1MTIzNjUxMjc0In0sCiAgICBbInN0YXJ0cy13aXRoIiwgIiR4LWFtei1tZXRhLXRhZyIsICIiXSwKICBdCn0K"
32
+
33
+ assert_equal "2qCp0odXe7A9IYyUVqn0w2adtCA=",
34
+ S3Policy.sign_encoded_document(encoded_document, aws_secret_key)
35
+ end
36
+
37
+ def test_generate_policy_document
38
+ assert S3Policy.generate_policy_document(bucket: 'yolo')
39
+ end
40
+ end
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: s3_policy
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Daniel X Moore
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: S3 Policy Document Generator
42
+ email:
43
+ - yahivin@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - .gitignore
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - lib/s3_policy.rb
54
+ - lib/s3_policy/version.rb
55
+ - s3_policy.gemspec
56
+ - test/test_s3_policy.rb
57
+ homepage: ''
58
+ licenses:
59
+ - MIT
60
+ metadata: {}
61
+ post_install_message:
62
+ rdoc_options: []
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - '>='
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ requirements: []
76
+ rubyforge_project:
77
+ rubygems_version: 2.0.3
78
+ signing_key:
79
+ specification_version: 4
80
+ summary: Generate a signed S3 policy document for namespaced clientside uploads.
81
+ test_files:
82
+ - test/test_s3_policy.rb