atalanda-signature 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -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 atalanda-signature.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Dominik Goltermann
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,57 @@
1
+
2
+ AtalandaSignature-ruby
3
+ ==================
4
+
5
+ AtalandaSignature-ruby provides a simple Ruby class that lets you sign requests to the [atalogics API](http://atalogics.com) and verify our callbacks.
6
+
7
+ Installation
8
+ ============
9
+
10
+ The best way to install the library is by using bundler. Add the following to `Gemfile` in the root of your project:
11
+
12
+ ```
13
+ gem "atalanda-signature"
14
+ ```
15
+
16
+ Then, on the command line:
17
+
18
+ ``` bash
19
+ bundle install
20
+ ```
21
+
22
+ Usage
23
+ =====
24
+
25
+ Signing API calls
26
+ -----------------
27
+ Use this to add an auth_hash containing a valid signature to the parameter hash that you send to our API.
28
+ ``` ruby
29
+ parameters = {
30
+ "atalogics" => {}
31
+ }
32
+ token = Atalanda::Signature::Token.new(KEY, SECRET)
33
+ request = Atalanda::Signature::Request.new("POST", "api/order", parameters)
34
+ signed_parameters = request.sign(token)
35
+ =>
36
+ {
37
+ "atalogics" => {},
38
+ "auth_timestamp" => 1391167211,
39
+ "auth_key" => "[Your API key]",
40
+ "auth_signature" => "552beac4b99949a556b120b7e5f7e22def46f663992a08f0f132ad4afee68b9f"
41
+ }
42
+ ```
43
+
44
+ Verifying the signature of our callbacks
45
+ --------------
46
+ Use this to verify the signature of our callbacks.
47
+ ``` ruby
48
+ data = JSON.parse(body) // convert json from post body into ruby hash
49
+ token = Atalanda::Signature::Token.new(KEY, SECRET)
50
+ request = Atalanda::Signature::Request.new("POST", "api/order", data)
51
+ result = request.authenticate(token)
52
+
53
+ if result["authenticated"] == true
54
+ // signature is valid
55
+ end
56
+ ```
57
+
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'atalanda/signature/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "atalanda-signature"
8
+ spec.version = Atalanda::Signature::VERSION
9
+ spec.authors = ["Dominik Goltermann"]
10
+ spec.email = ["dominik@goltermann.cc"]
11
+ spec.description = %q{Gem for signing atalogics api calls}
12
+ spec.summary = %q{Gem for signing atalogics api calls}
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
+ spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "timecop"
25
+ spec.add_development_dependency "debugger"
26
+ end
@@ -0,0 +1,104 @@
1
+ require "atalanda/signature/version"
2
+ require "digest"
3
+
4
+ module Atalanda
5
+ module Signature
6
+ class Token
7
+ attr_accessor :api_key, :api_secret
8
+
9
+ def initialize(app_api_key, app_api_secret)
10
+ self.api_key=app_api_key
11
+ self.api_secret=app_api_secret
12
+ end
13
+ end
14
+
15
+ class Request
16
+ def initialize(method, path, parameters, time=Time.now)
17
+ @method = method.upcase
18
+ @path = path
19
+ @parameters = parameters
20
+ @time = time.to_i
21
+ end
22
+
23
+ def sign(token)
24
+ param_string = buildParameterString()
25
+ signature = calculateSignature(token, param_string, @time)
26
+ @parameters.merge!({
27
+ "auth_timestamp" => @time,
28
+ "auth_key" => token.api_key,
29
+ "auth_signature" => signature
30
+ })
31
+ end
32
+
33
+ def authenticate token, timestamp_grace=600
34
+ if get_auth_hash.nil?
35
+ return {
36
+ "authenticated" => false,
37
+ "reason" => "Auth hash is missing"
38
+ }
39
+ end
40
+
41
+ if @time - get_auth_hash["auth_timestamp"].to_i > timestamp_grace
42
+ return {
43
+ "authenticated" => false,
44
+ "reason" => "Auth timestamp is older than #{timestamp_grace} seconds"
45
+ }
46
+ end
47
+
48
+ recalculated_signature = calculateSignature(token, buildParameterString(), get_auth_hash["auth_timestamp"])
49
+ if recalculated_signature != get_auth_hash["auth_signature"]
50
+ return {
51
+ "authenticated" => false,
52
+ "reason" => "Signature does not match"
53
+ }
54
+ end
55
+
56
+ {
57
+ "authenticated" => true
58
+ }
59
+ end
60
+
61
+ private
62
+
63
+ def get_auth_hash
64
+ if @parameters.has_key?("auth_key") && @parameters.has_key?("auth_signature") && @parameters.has_key?("auth_timestamp")
65
+ return {
66
+ "auth_timestamp" => @parameters["auth_timestamp"],
67
+ "auth_key" => @parameters["auth_key"],
68
+ "auth_signature" => @parameters["auth_signature"]
69
+ }
70
+ else
71
+ return nil
72
+ end
73
+ end
74
+
75
+ def calculateSignature token, string, time
76
+ Digest::SHA256.hexdigest("#{string}#{token.api_key}#{token.api_secret}#{time}")
77
+ end
78
+
79
+ def buildParameterString
80
+ "#{@method}#{@path}#{canonical_string_from_hash(@parameters)}"
81
+ end
82
+
83
+ def canonical_string_from_hash value, key=nil
84
+ str = ""
85
+ return "" if key == "auth_key" || key == "auth_timestamp" || key == "auth_signature"
86
+
87
+ if value.is_a? Hash
88
+ str += key.to_s
89
+ value.keys.sort.each do |k|
90
+ str += canonical_string_from_hash(value[k], k)
91
+ end
92
+ elsif value.is_a? Array
93
+ str += key.to_s
94
+ value.each do |v|
95
+ str += canonical_string_from_hash(v)
96
+ end
97
+ else
98
+ str += key ? "#{key}#{value}" : value.to_s
99
+ end
100
+ return str
101
+ end
102
+ end
103
+ end
104
+ end
@@ -0,0 +1,5 @@
1
+ module Atalanda
2
+ module Signature
3
+ VERSION = "1.0.2"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,135 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: atalanda-signature
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Dominik Goltermann
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-02-01 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: timecop
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: debugger
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ description: Gem for signing atalogics api calls
95
+ email:
96
+ - dominik@goltermann.cc
97
+ executables: []
98
+ extensions: []
99
+ extra_rdoc_files: []
100
+ files:
101
+ - .gitignore
102
+ - Gemfile
103
+ - LICENSE.txt
104
+ - README.md
105
+ - Rakefile
106
+ - atalanda-signature.gemspec
107
+ - lib/atalanda/signature.rb
108
+ - lib/atalanda/signature/version.rb
109
+ homepage: ''
110
+ licenses:
111
+ - MIT
112
+ post_install_message:
113
+ rdoc_options: []
114
+ require_paths:
115
+ - lib
116
+ required_ruby_version: !ruby/object:Gem::Requirement
117
+ none: false
118
+ requirements:
119
+ - - ! '>='
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ required_rubygems_version: !ruby/object:Gem::Requirement
123
+ none: false
124
+ requirements:
125
+ - - ! '>='
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ requirements: []
129
+ rubyforge_project:
130
+ rubygems_version: 1.8.23
131
+ signing_key:
132
+ specification_version: 3
133
+ summary: Gem for signing atalogics api calls
134
+ test_files: []
135
+ has_rdoc: