aws_signer 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +18 -0
- data/.rubocop.yml +36 -0
- data/.ruby-version +1 -0
- data/.travis.yml +8 -0
- data/CHANGELOG.md +5 -0
- data/Gemfile +3 -0
- data/Guardfile +11 -0
- data/LICENSE +21 -0
- data/README.md +94 -0
- data/Rakefile +11 -0
- data/aws_signer.gemspec +35 -0
- data/lib/aws_signer.rb +42 -0
- data/lib/aws_signer/configuration.rb +47 -0
- data/lib/aws_signer/signature.rb +96 -0
- data/lib/aws_signer/version.rb +3 -0
- data/spec/lib/aws_signer/configuration_spec.rb +35 -0
- data/spec/lib/aws_signer/signature_spec.rb +98 -0
- data/spec/lib/aws_signer/version_spec.rb +7 -0
- data/spec/lib/aws_signer_spec.rb +11 -0
- data/spec/spec_helper.rb +24 -0
- metadata +238 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 5868c89d14d94d2a15d55ddc977c1a2d89784e31
|
4
|
+
data.tar.gz: 8ab25c7a52759c81dfdbd851463bc0b303f8fe6a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: fee5fc1abaebc5f53f141cae714cf2d89a7eb1cd9910fb250692d697cefc933a41dd6e4c46de92d2e7c706889cb5064c5263ffd4be121ac18a5510d661c576e0
|
7
|
+
data.tar.gz: 3b52987c7a2ec9a899fa223cf1226277696dc35ddc88877d84aff880bd950480b2b424efd40705d6d3cd208e437e1ec33d1bf8b60d98899913e014174d1e2b1a
|
data/.gitignore
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
AllCops:
|
2
|
+
RunRailsCops: false
|
3
|
+
|
4
|
+
LineLength:
|
5
|
+
Max: 120
|
6
|
+
|
7
|
+
ClassLength:
|
8
|
+
CountComments: false # count full line comments?
|
9
|
+
Max: 175
|
10
|
+
|
11
|
+
MethodLength:
|
12
|
+
CountComments: false # count full line comments?
|
13
|
+
Max: 10
|
14
|
+
|
15
|
+
HashSyntax:
|
16
|
+
EnforcedStyle: hash_rockets
|
17
|
+
|
18
|
+
Documentation:
|
19
|
+
Enabled: false
|
20
|
+
|
21
|
+
DefaultScope:
|
22
|
+
Enabled: false
|
23
|
+
|
24
|
+
CollectionMethods:
|
25
|
+
PreferredMethods:
|
26
|
+
map: 'collect'
|
27
|
+
map!: 'collect!'
|
28
|
+
reduce: 'inject'
|
29
|
+
find: 'detect'
|
30
|
+
find_all: 'select'
|
31
|
+
|
32
|
+
Output:
|
33
|
+
Enabled: false
|
34
|
+
|
35
|
+
HasAndBelongsToMany:
|
36
|
+
Enabled: false
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.0.0-p247
|
data/.travis.yml
ADDED
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/Guardfile
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
guard 'rspec', :cmd => 'rspec --colour', :all_on_start => false, :all_after_pass => false do
|
2
|
+
watch('spec/spec_helper.rb') { "spec" }
|
3
|
+
watch(%r{^spec/.+_spec\.rb})
|
4
|
+
watch(%r{^lib/(.+)\.rb}) { |m| "spec/lib/#{m[1]}_spec.rb" }
|
5
|
+
end
|
6
|
+
|
7
|
+
guard :rubocop do
|
8
|
+
watch(%r{.+\.gemspec$})
|
9
|
+
watch(%r{.+\.rb$})
|
10
|
+
watch(%r{(?:.+/)?\.rubocop\.yml$}) { |m| File.dirname(m[0]) }
|
11
|
+
end
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2014 PJ Kelly (Crush & Lovely)
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
# AwsSigner for Ruby
|
2
|
+
|
3
|
+
[![Build Status](http://img.shields.io/travis/crushlovely/aws_signer.svg?style=flat)](https://travis-ci.org/crushlovely/aws_signer)
|
4
|
+
[![Code Climate](http://img.shields.io/codeclimate/github/crushlovely/aws_signer.svg?style=flat)](https://codeclimate.com/github/crushlovely/aws_signer)
|
5
|
+
[![Code Coverage](http://img.shields.io/codeclimate/coverage/github/crushlovely/aws_signer.svg?style=flat)](https://codeclimate.com/github/crushlovely/aws_signer)
|
6
|
+
|
7
|
+
A gem to help with creating signed URLs for uploading files direct to S3.
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
`gem install aws_signer`
|
12
|
+
|
13
|
+
or in your `Gemfile`
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
gem 'aws_signer'
|
17
|
+
```
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
Make sure you require the library.
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
require 'aws_signer'
|
25
|
+
```
|
26
|
+
|
27
|
+
### Configuration
|
28
|
+
|
29
|
+
You will need to setup a few configuration options before using the library:
|
30
|
+
|
31
|
+
``` ruby
|
32
|
+
AwsSigner.configure do |configuration|
|
33
|
+
# The S3 bucket you want to upload to
|
34
|
+
configuration.upload_bucket = 'my-bucket'
|
35
|
+
# Your AWS Secret Access Key
|
36
|
+
configuration.secret_access_key = 'SECRETACCESSKEY'
|
37
|
+
# Your Access Key ID
|
38
|
+
configuration.access_key_id = 'ACCESSKEYID'
|
39
|
+
end
|
40
|
+
```
|
41
|
+
|
42
|
+
### Creating Signed URLs
|
43
|
+
|
44
|
+
Created a signed URL is as easy as passing in a file name and a mime type:
|
45
|
+
|
46
|
+
``` ruby
|
47
|
+
signer = AwsSigner.signature(:file_name => 'image.jpg', :mime_type => 'image/jpg')
|
48
|
+
# Return the public URL for the file.
|
49
|
+
signer.public_url
|
50
|
+
# Return the signed URL for the file.
|
51
|
+
signer.signed_url
|
52
|
+
```
|
53
|
+
|
54
|
+
The object returned responds to `#to_json`, so you can use it in a Rails controller like so:
|
55
|
+
|
56
|
+
``` ruby
|
57
|
+
class AwsSignaturesController < ApplicationController
|
58
|
+
def create
|
59
|
+
signer = AwsSigner.signature(signature_parameters)
|
60
|
+
render :json => signer, :status => 201
|
61
|
+
end
|
62
|
+
|
63
|
+
protected
|
64
|
+
|
65
|
+
def signature_parameters
|
66
|
+
params.require(:file_name)
|
67
|
+
params.require(:mime_type)
|
68
|
+
params
|
69
|
+
end
|
70
|
+
end
|
71
|
+
```
|
72
|
+
|
73
|
+
That controller action would return a JSON payload that looks something like this:
|
74
|
+
|
75
|
+
``` json
|
76
|
+
{
|
77
|
+
"public_url": "https://s3.amazonaws.com/my-bucket/2014/07/31/03/04/42c842de85749a1723c98ca2932a3b6a/image.jpg",
|
78
|
+
"signed_url": "https://s3.amazonaws.com/my-bucket/2014/07/31/03/04/42c842de85749a1723c98ca2932a3b6a/image.jpg?AWSAccessKeyId=ACCESSKEYID&Expires=1406776178&Signature=77DZuYVRYqZHjw9zqwipBiZrmUY%3D"
|
79
|
+
}
|
80
|
+
```
|
81
|
+
|
82
|
+
## Contributing to aws_signer
|
83
|
+
|
84
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
85
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
86
|
+
* Fork the project
|
87
|
+
* Start a feature/bugfix branch
|
88
|
+
* Commit and push until you are happy with your contribution
|
89
|
+
* Make sure to add tests for it. This is important so we don't break it in a future version unintentionally.
|
90
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so we can cherry-pick around it.
|
91
|
+
|
92
|
+
## Copyright
|
93
|
+
|
94
|
+
Copyright (c) 2014 PJ Kelly (Crush & Lovely). See LICENSE for further details.
|
data/Rakefile
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
2
|
+
require 'rake'
|
3
|
+
require 'rspec/core/rake_task'
|
4
|
+
|
5
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
6
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
7
|
+
spec.rspec_opts = ['--backtrace']
|
8
|
+
# spec.ruby_opts = ['-w']
|
9
|
+
end
|
10
|
+
|
11
|
+
task :default => :spec
|
data/aws_signer.gemspec
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
|
5
|
+
require 'aws_signer/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = 'aws_signer'
|
9
|
+
spec.version = AwsSigner::VERSION
|
10
|
+
spec.authors = ['PJ Kelly']
|
11
|
+
spec.email = ['pj@crushlovely.com']
|
12
|
+
spec.homepage = 'https://github.com/crushlovely/aws_signer'
|
13
|
+
spec.summary = 'Utility to create signed URLs for uploading files straight to Amazon S3.'
|
14
|
+
spec.description = 'Utility to create signed URLs for uploading files straight to Amazon S3.'
|
15
|
+
|
16
|
+
spec.rubyforge_project = 'aws_signer'
|
17
|
+
|
18
|
+
spec.files = `git ls-files`.split("\n")
|
19
|
+
spec.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
20
|
+
spec.executables = `git ls-files -- bin/*`.split("\n").collect { |f| File.basename(f) }
|
21
|
+
spec.require_paths = ['lib']
|
22
|
+
|
23
|
+
spec.add_dependency('activesupport', '>=2.1.0')
|
24
|
+
spec.add_dependency('hashie')
|
25
|
+
spec.add_development_dependency 'bundler', '~> 1.3'
|
26
|
+
spec.add_development_dependency('pry')
|
27
|
+
spec.add_development_dependency('rake')
|
28
|
+
spec.add_development_dependency('rspec')
|
29
|
+
spec.add_development_dependency('rubocop')
|
30
|
+
spec.add_development_dependency('timecop')
|
31
|
+
spec.add_development_dependency('guard')
|
32
|
+
spec.add_development_dependency('guard-rspec')
|
33
|
+
spec.add_development_dependency('guard-rubocop')
|
34
|
+
spec.add_development_dependency('codeclimate-test-reporter')
|
35
|
+
end
|
data/lib/aws_signer.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'aws_signer/configuration'
|
2
|
+
require 'aws_signer/signature'
|
3
|
+
require 'aws_signer/version'
|
4
|
+
|
5
|
+
require 'base64'
|
6
|
+
require 'openssl'
|
7
|
+
require 'active_support/core_ext/object/to_query'
|
8
|
+
require 'hashie'
|
9
|
+
|
10
|
+
module AwsSigner
|
11
|
+
extend Configuration
|
12
|
+
|
13
|
+
class << self
|
14
|
+
# Public: Create a signature using the options passed in.
|
15
|
+
#
|
16
|
+
# options - The Hash options used to create the signature (default: {}):
|
17
|
+
# :file_name - The String file name of the file being uploaded (required).
|
18
|
+
# :mime_type - The String mime type of the file being uploaded (required).
|
19
|
+
# :request_expiration - The Integer number of minute to expire the signature in (optional, default: 5).
|
20
|
+
# :headers - The String headers for the signature (optional, default: 'x-amz-acl:public-read').
|
21
|
+
# :base_file_path - The String base file path to upload the file to (optional, default: a path containing
|
22
|
+
# the current timestamp and a random string,
|
23
|
+
# e.g. 2014/07/31/02/12/debd6f2fcb1b5cb160fce9c9909aebf2).
|
24
|
+
#
|
25
|
+
# Examples:
|
26
|
+
#
|
27
|
+
# AwsSigner.signature(:file_name => 'image.gif', :mime_type => 'image/gif')
|
28
|
+
# # => <AwsSigner::Signature>
|
29
|
+
#
|
30
|
+
# Returns an AwsSigner::Signature object.
|
31
|
+
def signature(options = {})
|
32
|
+
Signature.new(options)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
class RequiredConfigurationError < StandardError
|
37
|
+
# Internal
|
38
|
+
def initialize(required_attribute)
|
39
|
+
super("Missing required configuration for '#{required_attribute}'")
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
module AwsSigner
|
2
|
+
module Configuration
|
3
|
+
REQUIRED_ATTRIBUTES = [:upload_bucket, :secret_access_key, :access_key_id]
|
4
|
+
# Default S3 Base URL
|
5
|
+
DEFAULT_S3_BASE_URL = 'https://s3.amazonaws.com'
|
6
|
+
|
7
|
+
# S3 Base URL
|
8
|
+
attr_writer :s3_base_url
|
9
|
+
|
10
|
+
# Bucket to upload to
|
11
|
+
attr_accessor :upload_bucket
|
12
|
+
|
13
|
+
# AWS Secret Access Key
|
14
|
+
attr_accessor :secret_access_key
|
15
|
+
|
16
|
+
# AWS Access Key ID
|
17
|
+
attr_accessor :access_key_id
|
18
|
+
|
19
|
+
# Public: Configuration object.
|
20
|
+
#
|
21
|
+
# Example:
|
22
|
+
#
|
23
|
+
# AwsSigner.configure do |configuration|
|
24
|
+
# configuration.upload_bucket = 'my-bucket'
|
25
|
+
# configuration.secret_access_key = 'SECRET'
|
26
|
+
# configuration.access_key_id = 'SECRET'
|
27
|
+
# end
|
28
|
+
#
|
29
|
+
# Yields self to be able to configure AwsSigner with block-style configuration.
|
30
|
+
def configure
|
31
|
+
yield self
|
32
|
+
end
|
33
|
+
|
34
|
+
# Public: The S3 base url.
|
35
|
+
def s3_base_url
|
36
|
+
@s3_base_url ||= DEFAULT_S3_BASE_URL
|
37
|
+
end
|
38
|
+
|
39
|
+
# Public: Validate that all required configuration is present.
|
40
|
+
def validate_configuration!
|
41
|
+
REQUIRED_ATTRIBUTES.any? do |required_attribute|
|
42
|
+
required = send(required_attribute)
|
43
|
+
fail AwsSigner::RequiredConfigurationError, required_attribute if required.nil? || required.empty?
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,96 @@
|
|
1
|
+
module AwsSigner
|
2
|
+
class Signature
|
3
|
+
REQUIRED_OPTIONS = [:file_name, :mime_type]
|
4
|
+
|
5
|
+
attr_reader :options
|
6
|
+
|
7
|
+
# Public: Initialize a Signature object.
|
8
|
+
def initialize(options = {})
|
9
|
+
AwsSigner.validate_configuration!
|
10
|
+
@options = Hashie::Mash.new(default_options.merge(options))
|
11
|
+
validate_options!
|
12
|
+
end
|
13
|
+
|
14
|
+
# Public: The public url for the file being uploaded
|
15
|
+
#
|
16
|
+
# Returns the url as a String.
|
17
|
+
def public_url
|
18
|
+
"#{AwsSigner.s3_base_url}/#{AwsSigner.upload_bucket}/#{file_path}"
|
19
|
+
end
|
20
|
+
|
21
|
+
# Public: The signed url for the file being uploaded.
|
22
|
+
# This includes the public url as well as all required
|
23
|
+
# signature parameters.
|
24
|
+
#
|
25
|
+
# Returns the signed url as a String.
|
26
|
+
def signed_url
|
27
|
+
[public_url, signed_url_params.to_query].join('?')
|
28
|
+
end
|
29
|
+
|
30
|
+
# Public: The signed url parameters.
|
31
|
+
#
|
32
|
+
# Returns a Hash of required parameters for signing
|
33
|
+
# the public url.
|
34
|
+
def signed_url_params
|
35
|
+
{
|
36
|
+
'AWSAccessKeyId' => AwsSigner.access_key_id,
|
37
|
+
'Expires' => expiration,
|
38
|
+
'Signature' => base64_encoded_signature
|
39
|
+
}
|
40
|
+
end
|
41
|
+
|
42
|
+
# Public: The expiration of the signing request.
|
43
|
+
#
|
44
|
+
# Returns a timestamp in Integer format (in seconds).
|
45
|
+
def expiration
|
46
|
+
@expiration ||= Time.now.to_i + (60 * options.request_expiration)
|
47
|
+
end
|
48
|
+
|
49
|
+
# Public: The JSON representation of this object.
|
50
|
+
#
|
51
|
+
# Returns a Hash containing the public and signed urls
|
52
|
+
# that can be serialized into JSON.
|
53
|
+
def to_json
|
54
|
+
{
|
55
|
+
:public_url => public_url,
|
56
|
+
:signed_url => signed_url
|
57
|
+
}
|
58
|
+
end
|
59
|
+
|
60
|
+
private
|
61
|
+
|
62
|
+
def default_options
|
63
|
+
{
|
64
|
+
:request_expiration => 5,
|
65
|
+
:base_file_path => [Time.now.utc.strftime('%Y/%m/%d/%H/%M'), SecureRandom.hex].join('/'),
|
66
|
+
:headers => 'x-amz-acl:public-read'
|
67
|
+
}
|
68
|
+
end
|
69
|
+
|
70
|
+
def validate_options!
|
71
|
+
REQUIRED_OPTIONS.any? do |required_option|
|
72
|
+
fail AwsSigner::RequiredConfigurationError, required_option unless options.send("#{required_option}?")
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def sanitized_file_name
|
77
|
+
options.file_name.gsub(/[^\w\.\-]/, '_')
|
78
|
+
end
|
79
|
+
|
80
|
+
def base64_encoded_signature
|
81
|
+
Base64.strict_encode64(hmac_digest_signature)
|
82
|
+
end
|
83
|
+
|
84
|
+
def hmac_digest_signature
|
85
|
+
OpenSSL::HMAC.digest('sha1', AwsSigner.secret_access_key, signing_string)
|
86
|
+
end
|
87
|
+
|
88
|
+
def signing_string
|
89
|
+
"PUT\n\n#{options.mime_type}\n#{expiration}\n#{options.headers}\n/#{AwsSigner.upload_bucket}/#{file_path}"
|
90
|
+
end
|
91
|
+
|
92
|
+
def file_path
|
93
|
+
@file_path ||= [options.base_file_path, sanitized_file_name].join('/')
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe AwsSigner::Configuration do
|
4
|
+
describe '.configure' do
|
5
|
+
it 'should have default attributes' do
|
6
|
+
AwsSigner.configure do |configuration|
|
7
|
+
expect(configuration.s3_base_url).to eq(AwsSigner::Configuration::DEFAULT_S3_BASE_URL)
|
8
|
+
expect(configuration.upload_bucket).to be_nil
|
9
|
+
expect(configuration.secret_access_key).to be_nil
|
10
|
+
expect(configuration.access_key_id).to be_nil
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
describe '.validate_configuration!' do
|
16
|
+
before do
|
17
|
+
AwsSigner.configure do |configuration|
|
18
|
+
configuration.upload_bucket = 'fake'
|
19
|
+
configuration.secret_access_key = 'fake'
|
20
|
+
configuration.access_key_id = 'fake'
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
AwsSigner::Configuration::REQUIRED_ATTRIBUTES.each do |required_attribute|
|
25
|
+
context "when '#{required_attribute}' is not set" do
|
26
|
+
before { AwsSigner.send("#{required_attribute}=", nil) }
|
27
|
+
|
28
|
+
it 'raises an exception' do
|
29
|
+
expect { AwsSigner.validate_configuration! }
|
30
|
+
.to raise_error { AwsSigner::RequiredConfigurationError.new(required_attribute) }
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,98 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe AwsSigner::Signature do
|
4
|
+
let(:file_name) { 'keyboard_cat.gif' }
|
5
|
+
let(:mime_type) { 'image/gif' }
|
6
|
+
let(:local_time) { Time.local(2008, 9, 1, 12, 0, 0).utc }
|
7
|
+
let(:secure_random_string) { 'c1c9d98871b8d9a6308ff0a5b6e69ff8' }
|
8
|
+
let(:base64_string) { 'faba3f38e1f19793e531c5c94ef6b399acb38890cea42c6448' }
|
9
|
+
let(:hmac_digest_string) { '73cbfa693a5960187625ca70e148a9526b54f92376befdffcc' }
|
10
|
+
|
11
|
+
before do
|
12
|
+
Timecop.freeze(local_time)
|
13
|
+
allow(SecureRandom).to receive(:hex).and_return(secure_random_string)
|
14
|
+
allow(Base64).to receive(:strict_encode64).and_return(base64_string)
|
15
|
+
allow(OpenSSL::HMAC).to receive(:digest).and_return(hmac_digest_string)
|
16
|
+
end
|
17
|
+
|
18
|
+
describe 'initialization' do
|
19
|
+
subject { described_class.new(:file_name => file_name, :mime_type => mime_type) }
|
20
|
+
|
21
|
+
context 'when the library has not been properly configured' do
|
22
|
+
it 'raises an exception' do
|
23
|
+
expect { subject }
|
24
|
+
.to raise_error { AwsSigner::RequiredConfigurationError }
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
context 'when the library has been properly configured' do
|
29
|
+
before { configure! }
|
30
|
+
|
31
|
+
it 'sets the file_name' do
|
32
|
+
expect(subject.options.file_name).to eq(file_name)
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'sets the mime_type' do
|
36
|
+
expect(subject.options.mime_type).to eq(mime_type)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
shared_examples 'a method returning the public url' do
|
42
|
+
it { should include(AwsSigner.s3_base_url) }
|
43
|
+
it { should include(AwsSigner.upload_bucket) }
|
44
|
+
it { should include(local_time.strftime('%Y/%m/%d/%H/%M')) }
|
45
|
+
it { should include(secure_random_string) }
|
46
|
+
it { should include(file_name) }
|
47
|
+
end
|
48
|
+
|
49
|
+
describe '#public_url' do
|
50
|
+
subject { described_class.new(:file_name => file_name, :mime_type => mime_type).public_url }
|
51
|
+
before { configure! }
|
52
|
+
|
53
|
+
it_behaves_like 'a method returning the public url'
|
54
|
+
end
|
55
|
+
|
56
|
+
describe '#signed_url' do
|
57
|
+
subject { described_class.new(:file_name => file_name, :mime_type => mime_type).signed_url }
|
58
|
+
before { configure! }
|
59
|
+
it_behaves_like 'a method returning the public url'
|
60
|
+
end
|
61
|
+
|
62
|
+
describe '#signed_url_params' do
|
63
|
+
let(:signer) { described_class.new(:file_name => file_name, :mime_type => mime_type) }
|
64
|
+
let(:mock_signing_string) { 'fake' }
|
65
|
+
|
66
|
+
subject { signer.signed_url_params }
|
67
|
+
|
68
|
+
before do
|
69
|
+
configure!
|
70
|
+
allow(signer).to receive(:signing_string).and_return(mock_signing_string)
|
71
|
+
end
|
72
|
+
|
73
|
+
it 'should digest the signing string' do
|
74
|
+
expect(OpenSSL::HMAC).to receive(:digest).with('sha1', AwsSigner.secret_access_key, mock_signing_string)
|
75
|
+
subject
|
76
|
+
end
|
77
|
+
|
78
|
+
it 'should base64 encode the digest' do
|
79
|
+
expect(Base64).to receive(:strict_encode64).with(hmac_digest_string)
|
80
|
+
subject
|
81
|
+
end
|
82
|
+
|
83
|
+
it 'contains the AWSAccessKeyId' do
|
84
|
+
expect(subject).to have_key('AWSAccessKeyId')
|
85
|
+
expect(subject['AWSAccessKeyId']).to eq(AwsSigner.access_key_id)
|
86
|
+
end
|
87
|
+
|
88
|
+
it 'contains the Expires' do
|
89
|
+
expect(subject).to have_key('Expires')
|
90
|
+
expect(subject['Expires']).to eq(signer.expiration)
|
91
|
+
end
|
92
|
+
|
93
|
+
it 'contains the Signature' do
|
94
|
+
expect(subject).to have_key('Signature')
|
95
|
+
expect(subject['Signature']).to eq(base64_string)
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe AwsSigner do
|
4
|
+
describe '.signature' do
|
5
|
+
before { configure! }
|
6
|
+
|
7
|
+
it 'returns an instance of AwsSigner::Signature' do
|
8
|
+
expect(AwsSigner.signature(:file_name => 'image.jpg', :mime_type => 'image/jpg')).to be_an(AwsSigner::Signature)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require "codeclimate-test-reporter"
|
2
|
+
CodeClimate::TestReporter.start
|
3
|
+
|
4
|
+
require 'rspec'
|
5
|
+
require 'aws_signer'
|
6
|
+
require 'timecop'
|
7
|
+
|
8
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
|
9
|
+
|
10
|
+
RSpec.configure do |config|
|
11
|
+
config.before(:each) do
|
12
|
+
AwsSigner.upload_bucket = nil
|
13
|
+
AwsSigner.secret_access_key = nil
|
14
|
+
AwsSigner.access_key_id = nil
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def configure!
|
19
|
+
AwsSigner.configure do |configuration|
|
20
|
+
configuration.upload_bucket = 'my-bucket'
|
21
|
+
configuration.secret_access_key = 'SECRETACCESSKEY'
|
22
|
+
configuration.access_key_id = 'ACCESSKEYID'
|
23
|
+
end
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,238 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: aws_signer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- PJ Kelly
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-07-31 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 2.1.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 2.1.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: hashie
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.3'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.3'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: pry
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rake
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rspec
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - '>='
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rubocop
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - '>='
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - '>='
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: timecop
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - '>='
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - '>='
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: guard
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - '>='
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - '>='
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
- !ruby/object:Gem::Dependency
|
140
|
+
name: guard-rspec
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - '>='
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '0'
|
146
|
+
type: :development
|
147
|
+
prerelease: false
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - '>='
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '0'
|
153
|
+
- !ruby/object:Gem::Dependency
|
154
|
+
name: guard-rubocop
|
155
|
+
requirement: !ruby/object:Gem::Requirement
|
156
|
+
requirements:
|
157
|
+
- - '>='
|
158
|
+
- !ruby/object:Gem::Version
|
159
|
+
version: '0'
|
160
|
+
type: :development
|
161
|
+
prerelease: false
|
162
|
+
version_requirements: !ruby/object:Gem::Requirement
|
163
|
+
requirements:
|
164
|
+
- - '>='
|
165
|
+
- !ruby/object:Gem::Version
|
166
|
+
version: '0'
|
167
|
+
- !ruby/object:Gem::Dependency
|
168
|
+
name: codeclimate-test-reporter
|
169
|
+
requirement: !ruby/object:Gem::Requirement
|
170
|
+
requirements:
|
171
|
+
- - '>='
|
172
|
+
- !ruby/object:Gem::Version
|
173
|
+
version: '0'
|
174
|
+
type: :development
|
175
|
+
prerelease: false
|
176
|
+
version_requirements: !ruby/object:Gem::Requirement
|
177
|
+
requirements:
|
178
|
+
- - '>='
|
179
|
+
- !ruby/object:Gem::Version
|
180
|
+
version: '0'
|
181
|
+
description: Utility to create signed URLs for uploading files straight to Amazon
|
182
|
+
S3.
|
183
|
+
email:
|
184
|
+
- pj@crushlovely.com
|
185
|
+
executables: []
|
186
|
+
extensions: []
|
187
|
+
extra_rdoc_files: []
|
188
|
+
files:
|
189
|
+
- .gitignore
|
190
|
+
- .rubocop.yml
|
191
|
+
- .ruby-version
|
192
|
+
- .travis.yml
|
193
|
+
- CHANGELOG.md
|
194
|
+
- Gemfile
|
195
|
+
- Guardfile
|
196
|
+
- LICENSE
|
197
|
+
- README.md
|
198
|
+
- Rakefile
|
199
|
+
- aws_signer.gemspec
|
200
|
+
- lib/aws_signer.rb
|
201
|
+
- lib/aws_signer/configuration.rb
|
202
|
+
- lib/aws_signer/signature.rb
|
203
|
+
- lib/aws_signer/version.rb
|
204
|
+
- spec/lib/aws_signer/configuration_spec.rb
|
205
|
+
- spec/lib/aws_signer/signature_spec.rb
|
206
|
+
- spec/lib/aws_signer/version_spec.rb
|
207
|
+
- spec/lib/aws_signer_spec.rb
|
208
|
+
- spec/spec_helper.rb
|
209
|
+
homepage: https://github.com/crushlovely/aws_signer
|
210
|
+
licenses: []
|
211
|
+
metadata: {}
|
212
|
+
post_install_message:
|
213
|
+
rdoc_options: []
|
214
|
+
require_paths:
|
215
|
+
- lib
|
216
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
217
|
+
requirements:
|
218
|
+
- - '>='
|
219
|
+
- !ruby/object:Gem::Version
|
220
|
+
version: '0'
|
221
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
222
|
+
requirements:
|
223
|
+
- - '>='
|
224
|
+
- !ruby/object:Gem::Version
|
225
|
+
version: '0'
|
226
|
+
requirements: []
|
227
|
+
rubyforge_project: aws_signer
|
228
|
+
rubygems_version: 2.0.3
|
229
|
+
signing_key:
|
230
|
+
specification_version: 4
|
231
|
+
summary: Utility to create signed URLs for uploading files straight to Amazon S3.
|
232
|
+
test_files:
|
233
|
+
- spec/lib/aws_signer/configuration_spec.rb
|
234
|
+
- spec/lib/aws_signer/signature_spec.rb
|
235
|
+
- spec/lib/aws_signer/version_spec.rb
|
236
|
+
- spec/lib/aws_signer_spec.rb
|
237
|
+
- spec/spec_helper.rb
|
238
|
+
has_rdoc:
|