shipping_easy 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 05f04872e2e47ff54f0ae7e2b0784421d81aee43
4
+ data.tar.gz: c6978b8998f1e1cf99ee9394dab5627d4564ccff
5
+ SHA512:
6
+ metadata.gz: 71f47f2cbe3696252d9fbc11fe3e954cb99d059bf7b08ac37214e455b7868c59f5c20178b1b7bd6061956d8cb5e753d784a0e848ad75fb48521e357462633796
7
+ data.tar.gz: 986bf7dd0aceb8028709c448a06e71751b55fe5a690ff4f81a81d60df56749d480241ccf32fdb90911189b738cdc4506b63329bb29652ad70cdacb9c7df1c909
data/.gitignore ADDED
@@ -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 shipping_easy-ruby.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 ShippingEasy
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.
data/README.md ADDED
@@ -0,0 +1,25 @@
1
+ # ShippingEasy
2
+
3
+ Still in development. Please hold.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'shipping_easy'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install shipping_easy
18
+
19
+ ## Contributing
20
+
21
+ 1. Fork it
22
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
23
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
24
+ 4. Push to the branch (`git push origin my-new-feature`)
25
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,25 @@
1
+ require 'rack'
2
+ require "shipping_easy/version"
3
+
4
+ module ShippingEasy
5
+
6
+ class Error < StandardError; end
7
+
8
+ class RequestExpiredError < Error
9
+ def initialize(msg = "The request has expired.")
10
+ super(msg)
11
+ end
12
+ end
13
+
14
+ class AccessDeniedError < Error
15
+ def initialize(msg = "Access denied.")
16
+ super(msg)
17
+ end
18
+ end
19
+
20
+ class TimestampFormatError < Error
21
+ def initialize(msg = "The API timestamp could not be parsed.")
22
+ super(msg)
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,100 @@
1
+ module ShippingEasy
2
+
3
+ # Authenticates a signed ShippingEasy API request by matching the supplied signature with a freshly calculated one using the API
4
+ # shared secret. Requests may not be more than 10 minutes old in order to prevent playback attacks.
5
+ class Authenticator
6
+
7
+ EXPIRATION_INTERVAL = 10 * 60
8
+
9
+ attr_reader :api_secret,
10
+ :api_signature,
11
+ :api_timestamp,
12
+ :method,
13
+ :path,
14
+ :params,
15
+ :body,
16
+ :expected_signature
17
+
18
+ # Creates a new API authenticator object.
19
+ #
20
+ # options - The Hash options used to authenticate the request:
21
+ # :api_secret - A ShippingEasy-supplied API secret
22
+ # :method - The HTTP method used in the request. Either :get or :post. Default is :get.
23
+ # :path - The URI path of the request. E.g. "/api/orders"
24
+ # :params - The query params passed in as part of the request.
25
+ # :body - The body of the request which should normally be a JSON payload.
26
+ def initialize(options = {})
27
+ @api_secret = options.fetch(:api_secret)
28
+ @method = options.fetch(:method, :get)
29
+ @path = options.fetch(:path)
30
+ @body = options.fetch(:body, nil)
31
+ @params = options.fetch(:params, {})
32
+ @api_signature = params.delete(:api_signature)
33
+ @api_timestamp = params.fetch(:api_timestamp, nil).to_i
34
+ @expected_signature = ShippingEasy::Signature.new(api_secret: @api_secret, method: @method, path: @path, params: @params, body: @body)
35
+ end
36
+
37
+ # Convenience method to instantiate an authenticator and authenticate a signed request.
38
+ #
39
+ # options - The Hash options used to authenticate the request:
40
+ # :api_secret - A ShippingEasy-supplied API secret
41
+ # :method - The HTTP method used in the request. Either :get or :post. Default is :get.
42
+ # :path - The URI path of the request. E.g. "/api/orders"
43
+ # :params - The query params passed in as part of the request.
44
+ # :body - The body of the request which should normally be a JSON payload.
45
+ #
46
+ # See #authenticate for more detail.
47
+ def self.authenticate(options = {})
48
+ new(options).authenticate
49
+ end
50
+
51
+ # Authenticates the signed request.
52
+ #
53
+ # Example:
54
+ #
55
+ # authenticator = ShippingEasyShippingEasy::Authenticator.new(api_secret: "XXX",
56
+ # method: :post,
57
+ # path: "/api/orders",
58
+ # params: { test_param: "ABCDE", api_key: "123", api_timestamp: "2014-01-03 10:41:21 -0600" },
59
+ # body: "{\"orders\":{\"name\":\"Flip flops\",\"cost\":\"10.00\",\"shipping_cost\":\"2.00\"}}")
60
+ #
61
+ # Throws ShippingEasyShippingEasy::RequestExpiredError if the API timestamp is expired.
62
+ # Throws ShippingEasyShippingEasy::AccessDeniedError if the signature cannot be verified.
63
+ # Throws ShippingEasyShippingEasy::TimestampFormatError if the timestamp format is invalid
64
+ #
65
+ # Returns true if authentication passes.
66
+ def authenticate
67
+ raise ShippingEasy::RequestExpiredError if request_expired?
68
+ raise ShippingEasy::AccessDeniedError unless signatures_match?
69
+ true
70
+ end
71
+
72
+ # Returns true if the signature included in the request matches our calculated signature.
73
+ def signatures_match?
74
+ expected_signature == api_signature
75
+ end
76
+
77
+ # Returns true if the supplied API timestamp has expired.
78
+ def request_expired?
79
+ parsed_timestamp < request_expires_at
80
+ end
81
+
82
+ # Returns the time that the request expires, given the supplied API timestamp.
83
+ #
84
+ # Returns a Time object.
85
+ def request_expires_at
86
+ Time.now - EXPIRATION_INTERVAL
87
+ end
88
+
89
+ # Parses the supplied API timestamp string into a Time object.
90
+ #
91
+ # Raises ShippingEasyShippingEasy::TimestampFormatError if the string cannot be converted into a Time object.
92
+ # Returns a Time object.
93
+ def parsed_timestamp
94
+ raise ArgumentError if api_timestamp == 0
95
+ Time.at(api_timestamp)
96
+ rescue ArgumentError, TypeError
97
+ raise ShippingEasy::TimestampFormatError
98
+ end
99
+ end
100
+ end
File without changes
File without changes
File without changes
@@ -0,0 +1,82 @@
1
+ module ShippingEasy
2
+
3
+ # Used to generate ShippingEasy API signatures or to compare signature with one another.
4
+ class Signature
5
+
6
+ attr_reader :api_secret,
7
+ :method,
8
+ :path,
9
+ :params,
10
+ :body
11
+
12
+ # Creates a new API signature object.
13
+ #
14
+ # options - The Hash options used to create a signature:
15
+ # :api_secret - A ShippingEasy-supplied API secret
16
+ # :method - The HTTP method used in the request. Either :get or :post. Default is :get.
17
+ # :path - The URI path of the request. E.g. "/api/orders"
18
+ # :params - The query params passed in as part of the request.
19
+ # :body - The body of the request which should normally be a JSON payload.
20
+ #
21
+ def initialize(options = {})
22
+ @api_secret = options.delete(:api_secret) || ""
23
+ @method = options.fetch(:method, :get).to_s.upcase
24
+ @path = options.delete(:path) || ""
25
+ @body = options.delete(:body) || ""
26
+ @params = options.delete(:params) || {}
27
+ @params.delete(:api_signature) # remove for convenience
28
+ end
29
+
30
+ # Concatenates the parts of the base signature into a plaintext string using the following order:
31
+ #
32
+ # 1. Capitilized method of the request. E.g. "POST"
33
+ # 2. The URI path
34
+ # 3. The query parameters sorted alphabetically and concatenated together into a URL friendly format: param1=ABC&param2=XYZ
35
+ # 4. The request body as a string if one exists
36
+ #
37
+ # All parts are then concatenated together with an ampersand. The result resembles something like this:
38
+ #
39
+ # "POST&/api/orders&param1=ABC&param2=XYZ&{\"orders\":{\"name\":\"Flip flops\",\"cost\":\"10.00\",\"shipping_cost\":\"2.00\"}}"
40
+ #
41
+ # Returns a correctly contenated plaintext API signature.
42
+ def plaintext
43
+ parts = []
44
+ parts << method
45
+ parts << path
46
+ parts << Rack::Utils.build_query(params.sort)
47
+ parts << body.to_s unless body.nil? || body == ""
48
+ parts.join("&")
49
+ end
50
+
51
+ # Encrypts the plaintext signature with the supplied API secret. This signature should be included
52
+ # when making a ShippingEasy API call.
53
+ #
54
+ # Returns an encrypted signature.
55
+ def encrypted
56
+ OpenSSL::HMAC::hexdigest("sha256", api_secret, plaintext)
57
+ end
58
+
59
+ # Equality operator to determine if another signature object, or string, matches the current signature. If a string is passed in, it
60
+ # should represent the encrypted form of the API signature, not the plaintext version.
61
+ #
62
+ # It uses a constant time string comparison function to limit the vulnerability of timing attacks.
63
+ #
64
+ # Returns true if the supplied string or signature object matches the current object.
65
+ def ==(other_signature)
66
+ expected_signature, supplied_signature = self.to_s, other_signature.to_s
67
+ return false if expected_signature.blank? || supplied_signature.blank? || expected_signature.bytesize != supplied_signature.bytesize
68
+ l = expected_signature.unpack "C#{expected_signature.bytesize}"
69
+ res = 0
70
+ supplied_signature.each_byte { |byte| res |= byte ^ l.shift }
71
+ res == 0
72
+ end
73
+
74
+ # Returns the encrypted form of the signature.
75
+ #
76
+ # Returns an encrypted signature.
77
+ def to_s
78
+ encrypted
79
+ end
80
+
81
+ end
82
+ end
@@ -0,0 +1,3 @@
1
+ module ShippingEasy
2
+ VERSION = "0.0.1"
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 'shipping_easy/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "shipping_easy"
8
+ spec.version = ShippingEasy::VERSION
9
+ spec.authors = ["ShippingEasy"]
10
+ spec.email = ["dev@shippingeasy.com"]
11
+ spec.description = "The official ShippingEasy API client for Ruby."
12
+ spec.summary = "The official ShippingEasy API client for Ruby."
13
+ spec.homepage = "https://github.com/ShippingEasy/shipping_easy-ruby"
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
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: shipping_easy
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - ShippingEasy
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-27 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: The official ShippingEasy API client for Ruby.
42
+ email:
43
+ - dev@shippingeasy.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/shipping_easy.rb
54
+ - lib/shipping_easy/authenticator.rb
55
+ - lib/shipping_easy/resources/cancellations.rb
56
+ - lib/shipping_easy/resources/order.rb
57
+ - lib/shipping_easy/resources/store.rb
58
+ - lib/shipping_easy/signature.rb
59
+ - lib/shipping_easy/version.rb
60
+ - shipping_easy.gemspec
61
+ homepage: https://github.com/ShippingEasy/shipping_easy-ruby
62
+ licenses:
63
+ - MIT
64
+ metadata: {}
65
+ post_install_message:
66
+ rdoc_options: []
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - '>='
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - '>='
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ requirements: []
80
+ rubyforge_project:
81
+ rubygems_version: 2.0.0
82
+ signing_key:
83
+ specification_version: 4
84
+ summary: The official ShippingEasy API client for Ruby.
85
+ test_files: []