dibs_hmac 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: c197eee0c75f8a65605d7812a0567090acf94e02
4
+ data.tar.gz: 826270bbeabc2d03ec0bc7b353c3b84984c4d7a6
5
+ SHA512:
6
+ metadata.gz: a1c03a2e4dc4d92c8fc6b4cf480de40cba0e832dc70d01d279a2b1e7b4e9dda45a146150b9b5ab5a00369122227127911dd68dce33b693087efc802bac3d06e6
7
+ data.tar.gz: 97a992cde9f5a3d75800b17097e142c4552179dbaa14ae234eaaa41a99282a2b26d8e034700b5fed11fec294a955677dbe84c542d7ae1b098a60f0f47b06dc8f
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "rspec", "~> 2.8.0"
10
+ gem "rdoc", "~> 3.12"
11
+ gem "bundler", "~> 1.0"
12
+ gem "jeweler", "~> 1.8.7"
13
+ end
@@ -0,0 +1,165 @@
1
+ GNU LESSER GENERAL PUBLIC LICENSE
2
+ Version 3, 29 June 2007
3
+
4
+ Copyright (C) 2012 Björn Blomqvist
5
+ Everyone is permitted to copy and distribute verbatim copies
6
+ of this license document, but changing it is not allowed.
7
+
8
+
9
+ This version of the GNU Lesser General Public License incorporates
10
+ the terms and conditions of version 3 of the GNU General Public
11
+ License, supplemented by the additional permissions listed below.
12
+
13
+ 0. Additional Definitions.
14
+
15
+ As used herein, "this License" refers to version 3 of the GNU Lesser
16
+ General Public License, and the "GNU GPL" refers to version 3 of the GNU
17
+ General Public License.
18
+
19
+ "The Library" refers to a covered work governed by this License,
20
+ other than an Application or a Combined Work as defined below.
21
+
22
+ An "Application" is any work that makes use of an interface provided
23
+ by the Library, but which is not otherwise based on the Library.
24
+ Defining a subclass of a class defined by the Library is deemed a mode
25
+ of using an interface provided by the Library.
26
+
27
+ A "Combined Work" is a work produced by combining or linking an
28
+ Application with the Library. The particular version of the Library
29
+ with which the Combined Work was made is also called the "Linked
30
+ Version".
31
+
32
+ The "Minimal Corresponding Source" for a Combined Work means the
33
+ Corresponding Source for the Combined Work, excluding any source code
34
+ for portions of the Combined Work that, considered in isolation, are
35
+ based on the Application, and not on the Linked Version.
36
+
37
+ The "Corresponding Application Code" for a Combined Work means the
38
+ object code and/or source code for the Application, including any data
39
+ and utility programs needed for reproducing the Combined Work from the
40
+ Application, but excluding the System Libraries of the Combined Work.
41
+
42
+ 1. Exception to Section 3 of the GNU GPL.
43
+
44
+ You may convey a covered work under sections 3 and 4 of this License
45
+ without being bound by section 3 of the GNU GPL.
46
+
47
+ 2. Conveying Modified Versions.
48
+
49
+ If you modify a copy of the Library, and, in your modifications, a
50
+ facility refers to a function or data to be supplied by an Application
51
+ that uses the facility (other than as an argument passed when the
52
+ facility is invoked), then you may convey a copy of the modified
53
+ version:
54
+
55
+ a) under this License, provided that you make a good faith effort to
56
+ ensure that, in the event an Application does not supply the
57
+ function or data, the facility still operates, and performs
58
+ whatever part of its purpose remains meaningful, or
59
+
60
+ b) under the GNU GPL, with none of the additional permissions of
61
+ this License applicable to that copy.
62
+
63
+ 3. Object Code Incorporating Material from Library Header Files.
64
+
65
+ The object code form of an Application may incorporate material from
66
+ a header file that is part of the Library. You may convey such object
67
+ code under terms of your choice, provided that, if the incorporated
68
+ material is not limited to numerical parameters, data structure
69
+ layouts and accessors, or small macros, inline functions and templates
70
+ (ten or fewer lines in length), you do both of the following:
71
+
72
+ a) Give prominent notice with each copy of the object code that the
73
+ Library is used in it and that the Library and its use are
74
+ covered by this License.
75
+
76
+ b) Accompany the object code with a copy of the GNU GPL and this license
77
+ document.
78
+
79
+ 4. Combined Works.
80
+
81
+ You may convey a Combined Work under terms of your choice that,
82
+ taken together, effectively do not restrict modification of the
83
+ portions of the Library contained in the Combined Work and reverse
84
+ engineering for debugging such modifications, if you also do each of
85
+ the following:
86
+
87
+ a) Give prominent notice with each copy of the Combined Work that
88
+ the Library is used in it and that the Library and its use are
89
+ covered by this License.
90
+
91
+ b) Accompany the Combined Work with a copy of the GNU GPL and this license
92
+ document.
93
+
94
+ c) For a Combined Work that displays copyright notices during
95
+ execution, include the copyright notice for the Library among
96
+ these notices, as well as a reference directing the user to the
97
+ copies of the GNU GPL and this license document.
98
+
99
+ d) Do one of the following:
100
+
101
+ 0) Convey the Minimal Corresponding Source under the terms of this
102
+ License, and the Corresponding Application Code in a form
103
+ suitable for, and under terms that permit, the user to
104
+ recombine or relink the Application with a modified version of
105
+ the Linked Version to produce a modified Combined Work, in the
106
+ manner specified by section 6 of the GNU GPL for conveying
107
+ Corresponding Source.
108
+
109
+ 1) Use a suitable shared library mechanism for linking with the
110
+ Library. A suitable mechanism is one that (a) uses at run time
111
+ a copy of the Library already present on the user's computer
112
+ system, and (b) will operate properly with a modified version
113
+ of the Library that is interface-compatible with the Linked
114
+ Version.
115
+
116
+ e) Provide Installation Information, but only if you would otherwise
117
+ be required to provide such information under section 6 of the
118
+ GNU GPL, and only to the extent that such information is
119
+ necessary to install and execute a modified version of the
120
+ Combined Work produced by recombining or relinking the
121
+ Application with a modified version of the Linked Version. (If
122
+ you use option 4d0, the Installation Information must accompany
123
+ the Minimal Corresponding Source and Corresponding Application
124
+ Code. If you use option 4d1, you must provide the Installation
125
+ Information in the manner specified by section 6 of the GNU GPL
126
+ for conveying Corresponding Source.)
127
+
128
+ 5. Combined Libraries.
129
+
130
+ You may place library facilities that are a work based on the
131
+ Library side by side in a single library together with other library
132
+ facilities that are not Applications and are not covered by this
133
+ License, and convey such a combined library under terms of your
134
+ choice, if you do both of the following:
135
+
136
+ a) Accompany the combined library with a copy of the same work based
137
+ on the Library, uncombined with any other library facilities,
138
+ conveyed under the terms of this License.
139
+
140
+ b) Give prominent notice with the combined library that part of it
141
+ is a work based on the Library, and explaining where to find the
142
+ accompanying uncombined form of the same work.
143
+
144
+ 6. Revised Versions of the GNU Lesser General Public License.
145
+
146
+ The Free Software Foundation may publish revised and/or new versions
147
+ of the GNU Lesser General Public License from time to time. Such new
148
+ versions will be similar in spirit to the present version, but may
149
+ differ in detail to address new problems or concerns.
150
+
151
+ Each version is given a distinguishing version number. If the
152
+ Library as you received it specifies that a certain numbered version
153
+ of the GNU Lesser General Public License "or any later version"
154
+ applies to it, you have the option of following the terms and
155
+ conditions either of that published version or of any later version
156
+ published by the Free Software Foundation. If the Library as you
157
+ received it does not specify a version number of the GNU Lesser
158
+ General Public License, you may choose any version of the GNU Lesser
159
+ General Public License ever published by the Free Software Foundation.
160
+
161
+ If the Library as you received it specifies that a proxy can decide
162
+ whether future versions of the GNU Lesser General Public License shall
163
+ apply, that proxy's public statement of acceptance of any version is
164
+ permanent authorization for you to choose that version for the
165
+ Library.
@@ -0,0 +1,36 @@
1
+ # dibs_hmac
2
+
3
+ An implementation of DIBS Message authentication code. Has correct sorting and rules for which params to include. So far it only includes the params for DIBS Payment Window. This is production ready. I am curently using it for a Webshop.
4
+
5
+ - http://tech.dibs.dk/integration_methods/dibs_payment_window/parameters/
6
+ - http://tech.dibs.dk/integration_methods/dibs_payment_window/return_pages/
7
+
8
+ If you find this useful please tell me in an email.
9
+
10
+ ## Code example
11
+
12
+ ### Calculate mac
13
+
14
+ dibs_params = {:amount => "100", :merchant => "2222", ....}
15
+
16
+ mac = DIBS::HMAC.calculate(dibs_params, Rails.application.config.dibs_hmac_key)
17
+
18
+ ### Validate response from DIBS
19
+
20
+ raise "INVALID REQUEST" unless DIBS::HMAC.valid?(params, Rails.application.config.dibs_hmac_key)
21
+
22
+
23
+ ## Contributing to dibs_hmac
24
+
25
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
26
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
27
+ * Fork the project.
28
+ * Start a feature/bugfix branch.
29
+ * Commit and push until you are happy with your contribution.
30
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
31
+
32
+ ## Copyright
33
+
34
+ Copyright (c) 2013 Björn Blomqvist. See LICENSE.txt for
35
+ further details.
36
+
@@ -0,0 +1,44 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "dibs_hmac"
18
+ gem.homepage = "http://github.com/bjornblomqvist/dibs_hmac"
19
+ gem.license = "LGPL"
20
+ gem.summary = %Q{An implementation of DIBS Message authentication code.}
21
+ gem.description = %Q{An implementation of DIBS Message authentication code. Has correct sorting and rules for which params to include. So far it only includes the params for DIBS Payment Window.}
22
+ gem.email = "bjorn@bjornblomqvist.se"
23
+ gem.authors = ["Björn Blomqvist"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
32
+ end
33
+
34
+ task :default => :spec
35
+
36
+ require 'rdoc/task'
37
+ Rake::RDocTask.new do |rdoc|
38
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
39
+
40
+ rdoc.rdoc_dir = 'rdoc'
41
+ rdoc.title = "dibs_hmac #{version}"
42
+ rdoc.rdoc_files.include('README*')
43
+ rdoc.rdoc_files.include('lib/**/*.rb')
44
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,92 @@
1
+ require 'digest/hmac'
2
+
3
+ module DIBS
4
+ class HMAC
5
+
6
+ # Params taken from the below urls. 2013-10-17
7
+ # http://tech.dibs.dk/integration_methods/dibs_payment_window/parameters/
8
+ # http://tech.dibs.dk/integration_methods/dibs_payment_window/return_pages/
9
+ #
10
+ DIBS_PARAM_NAMES = %{
11
+ currency
12
+ merchant
13
+ amount
14
+ orderId
15
+ acceptReturnUrl
16
+ oiTypes
17
+ oiNames
18
+ billingFirstName
19
+ billingLastName
20
+ billingAddress
21
+ billingAddress2
22
+ billingPostalCode
23
+ billingPostalPlace
24
+ billingEmail
25
+ billingMobile
26
+ cancelreturnurl
27
+ callbackUrl
28
+ language
29
+ addFee
30
+ payType
31
+ capturenow
32
+ shippingFirstName
33
+ shippingLastName
34
+ shippingAddress
35
+ shippingAddress2
36
+ shippingPostalCode
37
+ shippingPostalPlace
38
+ socialSecurityNumber
39
+ createTicket
40
+ createTicketAndAuth
41
+ test
42
+ account
43
+ status
44
+ amount
45
+ currency
46
+ transaction
47
+ actionCode
48
+ acquirer
49
+ cardNumberMasked
50
+ expMonth
51
+ expYear
52
+ cardTypeName
53
+ captureStatus
54
+ status3D
55
+ ECI
56
+ enrollStatus
57
+ xidPresent
58
+ ticket
59
+ ticketStatus
60
+ fee
61
+ verificationIdPresent
62
+ validationErrors
63
+ acquirerOrderId
64
+ acquirerCustomerId
65
+ acquirerFirstName
66
+ acquirerLastName
67
+ acquirerDeliveryAddress
68
+ acquirerDeliveryPostalCode
69
+ acquirerDeliveryPostalPlace
70
+ acquirerDeliveryCountryCode
71
+ }.strip.split("\n").map {|name| name.strip }
72
+
73
+
74
+ def self.calculate(params, key)
75
+ param_string = ""
76
+ params.keys.sort.each do |key|
77
+ if DIBS_PARAM_NAMES.include?(key.to_s) || key.to_s.start_with?("s_") || key.to_s.match(/oiRow[0-9]+/)
78
+ value = params[key]
79
+ param_string += "&" if param_string.length > 0
80
+ param_string += "#{key}=#{value}"
81
+ end
82
+ end
83
+
84
+ OpenSSL::HMAC.hexdigest(OpenSSL::Digest::Digest.new('sha256'),Array(key).pack('H*'), param_string)
85
+ end
86
+
87
+ def self.valid?(params, key)
88
+ params['MAC'] || params[:MAC] == calculate(params, key)
89
+ end
90
+
91
+ end
92
+ end
@@ -0,0 +1,2 @@
1
+
2
+ require_relative "./dibs/hmac"
@@ -0,0 +1,61 @@
1
+ require 'spec_helper'
2
+
3
+ describe DIBS::HMAC do
4
+
5
+ hmac_key = "366b2c24397034366e4175727e44527076617d6f41457c3525433b302d32504272486b623b485b7b5a466a32625e53324a404c40435a7578666c2a4726327872"
6
+
7
+ describe '.calculate' do
8
+
9
+ subject { DIBS::HMAC }
10
+
11
+ it 'should calculate a correct mac value' do
12
+
13
+ mac = subject.calculate({:amount => "100", :merchant => "2222"}, hmac_key)
14
+ mac.should == "9c4d30a74ade9f93dfeca46edbdac110c8ec9b250185aab25a4555b3575d3ddc"
15
+
16
+ mac = subject.calculate({:amount => "100", :merchant => "2222", :orderId => "3423432"}, hmac_key)
17
+ mac.should == "f78bebd663d45c67916b5a19690b65cb961dceb6c346dcb202fa591213e6f14f"
18
+ end
19
+
20
+ it 'should ignore unkown params' do
21
+ mac1 = subject.calculate({:amount => "100", :merchant => "2222"}, hmac_key)
22
+ mac2 = subject.calculate({:amount => "100", :merchant => "2222", :none => "this is not included"}, hmac_key)
23
+ mac1.should == mac2
24
+ end
25
+
26
+ it 'should include params prefixed with s_' do
27
+ mac1 = subject.calculate({:amount => "100", :merchant => "2222"}, hmac_key)
28
+ mac2 = subject.calculate({:amount => "100", :merchant => "2222", :s_none => "this is included"}, hmac_key)
29
+ mac1.should_not == mac2
30
+ mac2.should == '2e94c4b78a0c75101c769b0592960cdd8f44556feea5a4f0c334184a3beb1b2c'
31
+ end
32
+
33
+ it 'should include order rows' do
34
+ mac1 = subject.calculate({:amount => "100", :merchant => "2222"}, hmac_key)
35
+ mac2 = subject.calculate({:amount => "100", :merchant => "2222", :oiRow1 => "1;something,100"}, hmac_key)
36
+ mac1.should_not == mac2
37
+ mac2.should == "cab07080a501a38154482017505af601a5ab6823cbac450b2d48b15df38ceeb4"
38
+ end
39
+ end
40
+
41
+ describe ".valid?" do
42
+
43
+ subject { DIBS::HMAC }
44
+
45
+ it 'should be true' do
46
+
47
+ mac = "9c4d30a74ade9f93dfeca46edbdac110c8ec9b250185aab25a4555b3575d3ddc"
48
+ subject.valid?({:amount => "100", :merchant => "2222", :MAC => mac}, hmac_key).should be_true
49
+
50
+ end
51
+
52
+ it 'should be false' do
53
+
54
+ mac = "9c4d30a74ade9f93dfeca46edbdac110c8ec9b250185aab25a4555b3575d3ddc"
55
+ subject.valid?({:amount => "100", :MAC => mac}, hmac_key).should be_false
56
+
57
+ end
58
+
59
+ end
60
+
61
+ end
@@ -0,0 +1,12 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'dibs_hmac'
5
+
6
+ # Requires supporting files with custom matchers and macros, etc,
7
+ # in ./support/ and its subdirectories.
8
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
+
10
+ RSpec.configure do |config|
11
+
12
+ end
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dibs_hmac
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Björn Blomqvist
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-10-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 2.8.0
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 2.8.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: rdoc
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '3.12'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '3.12'
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.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: jeweler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 1.8.7
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: 1.8.7
69
+ description: An implementation of DIBS Message authentication code. Has correct sorting
70
+ and rules for which params to include. So far it only includes the params for DIBS
71
+ Payment Window.
72
+ email: bjorn@bjornblomqvist.se
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files:
76
+ - LICENSE.txt
77
+ - README.md
78
+ files:
79
+ - .document
80
+ - .rspec
81
+ - Gemfile
82
+ - LICENSE.txt
83
+ - README.md
84
+ - Rakefile
85
+ - VERSION
86
+ - lib/dibs/hmac.rb
87
+ - lib/dibs_hmac.rb
88
+ - spec/dibs/hmac_spec.rb
89
+ - spec/spec_helper.rb
90
+ homepage: http://github.com/bjornblomqvist/dibs_hmac
91
+ licenses:
92
+ - LGPL
93
+ metadata: {}
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - '>='
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - '>='
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ requirements: []
109
+ rubyforge_project:
110
+ rubygems_version: 2.0.6
111
+ signing_key:
112
+ specification_version: 4
113
+ summary: An implementation of DIBS Message authentication code.
114
+ test_files: []