simple-hmac 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 +7 -0
- data/LICENSE.txt +19 -0
- data/Rakefile +6 -0
- data/lib/action-dispatch/request.rb +42 -0
- data/lib/rest-client/request.rb +28 -0
- data/lib/simple-hmac/helper.rb +8 -0
- data/lib/simple-hmac/version.rb +4 -0
- data/lib/simple-hmac.rb +3 -0
- metadata +150 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 260c97832e4ffccf5c3ab6c473e7a2bbbc84902d
|
4
|
+
data.tar.gz: 49cca9442b1f363ab3f735c91dd3131cbc78f2b9
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 88150e79ee11de58025fe408f358658c8d1438717a3093691468ac64139201423e848149009b090f5bada28c7ccce306e53765376982cabbf3c751ed3d63b50c
|
7
|
+
data.tar.gz: 9925feeac7c80e69668ebd634f1a28eca8aa800b4352461ef8d8bebbf3e66244ebbcac11172ac4c219e3482f6173ef865ebea97d6eec0b0eed2f9e15d4c05ef0
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright (c) 2015 Wizypay - Chaker Nakhli
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
5
|
+
in the Software without restriction, including without limitation the rights
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
8
|
+
furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
11
|
+
all copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
module ActionDispatch
|
2
|
+
class Request
|
3
|
+
include SimpleHmac::Helper
|
4
|
+
|
5
|
+
def hmac_api_id(auth_prefix='\w+')
|
6
|
+
parse_hmac(auth_prefix)[1]
|
7
|
+
end
|
8
|
+
|
9
|
+
def hmac_valid?(api_secret, timeout_seconds=900, auth_prefix='\w+')
|
10
|
+
((Time.now.utc -Time.httpdate(timestamp).utc >= timeout_seconds) rescue false) &&
|
11
|
+
parse_hmac(auth_prefix)[2] == hmac_token(content_type, calculate_content_md5, url, timestamp, api_secret)
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def parse_hmac(auth_prefix)
|
17
|
+
Regexp.new("\\A#{auth_prefix} ([^:]+):(.+)\\Z").match(authorization_header)
|
18
|
+
end
|
19
|
+
|
20
|
+
def calculate_content_md5
|
21
|
+
(post? || put? || patch?) ? Digest::MD5.base64digest(raw_post) : ''
|
22
|
+
end
|
23
|
+
|
24
|
+
def content_type
|
25
|
+
find_header(%w(CONTENT-TYPE CONTENT_TYPE HTTP_CONTENT_TYPE))
|
26
|
+
end
|
27
|
+
|
28
|
+
def timestamp
|
29
|
+
find_header(%w(DATE HTTP_DATE))
|
30
|
+
end
|
31
|
+
|
32
|
+
def authorization_header
|
33
|
+
find_header %w(AUTHORIZATION HTTP_AUTHORIZATION)
|
34
|
+
end
|
35
|
+
|
36
|
+
def find_header(keys)
|
37
|
+
cap_env = Hash[env.each_pair { |k, v| [k.to_s.upcase, v] }]
|
38
|
+
keys.each { |k| return cap_env[k] unless cap_env[k].blank? }
|
39
|
+
''
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module RestClient
|
2
|
+
class Request
|
3
|
+
include SimpleHmac::Helper
|
4
|
+
|
5
|
+
def sign!(api_id, api_secret, auth_prefix='APIAuth')
|
6
|
+
processed_headers['Content-Type'] ||= 'text/plain'
|
7
|
+
date = Time.now.utc.httpdate
|
8
|
+
processed_headers.merge! 'Date' => date
|
9
|
+
hmac_token = hmac_token(processed_headers['Content-Type'], set_md5_header, url, date, api_secret)
|
10
|
+
processed_headers.merge! 'Authorization' => "#{auth_prefix} #{api_id}:#{hmac_token}"
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
def set_md5_header
|
16
|
+
return '' unless [:post, :put, :patch].include?(@method)
|
17
|
+
if payload
|
18
|
+
body = payload.read
|
19
|
+
payload.instance_variable_get(:@stream).seek(0)
|
20
|
+
else
|
21
|
+
body = ''
|
22
|
+
end
|
23
|
+
content_md5 = Digest::MD5.base64digest(body)
|
24
|
+
processed_headers.merge! 'Content-MD5' => content_md5
|
25
|
+
content_md5
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,8 @@
|
|
1
|
+
module SimpleHmac
|
2
|
+
module Helper
|
3
|
+
def hmac_token(content_type, md5, url, date, api_secret)
|
4
|
+
data = [content_type, md5, url.gsub(/https?:\/\/[^(,|\?|\/)]*/, ''), date].join(',')
|
5
|
+
Base64.strict_encode64(OpenSSL::HMAC.digest('sha1', api_secret, data))
|
6
|
+
end
|
7
|
+
end
|
8
|
+
end
|
data/lib/simple-hmac.rb
ADDED
metadata
ADDED
@@ -0,0 +1,150 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: simple-hmac
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Chaker Nakhli
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-02-09 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rest-client
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.7'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activesupport
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '4.2'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '4.2'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: activemodel
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '4.2'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '4.2'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: bundler
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.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.7'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rake
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '10.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '10.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: '3.1'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '3.1'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: webmock
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '1.20'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '1.20'
|
111
|
+
description:
|
112
|
+
email:
|
113
|
+
- chaker@wizypay.com
|
114
|
+
executables: []
|
115
|
+
extensions: []
|
116
|
+
extra_rdoc_files: []
|
117
|
+
files:
|
118
|
+
- LICENSE.txt
|
119
|
+
- Rakefile
|
120
|
+
- lib/action-dispatch/request.rb
|
121
|
+
- lib/rest-client/request.rb
|
122
|
+
- lib/simple-hmac.rb
|
123
|
+
- lib/simple-hmac/helper.rb
|
124
|
+
- lib/simple-hmac/version.rb
|
125
|
+
homepage: http://www.wizypay.com
|
126
|
+
licenses:
|
127
|
+
- MIT
|
128
|
+
metadata: {}
|
129
|
+
post_install_message:
|
130
|
+
rdoc_options:
|
131
|
+
- "--charset=UTF-8"
|
132
|
+
require_paths:
|
133
|
+
- lib
|
134
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: 2.1.5
|
139
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
140
|
+
requirements:
|
141
|
+
- - ">="
|
142
|
+
- !ruby/object:Gem::Version
|
143
|
+
version: '0'
|
144
|
+
requirements: []
|
145
|
+
rubyforge_project:
|
146
|
+
rubygems_version: 2.4.3
|
147
|
+
signing_key:
|
148
|
+
specification_version: 4
|
149
|
+
summary: Lightweight HMAC implementation for Rails + Restclient.
|
150
|
+
test_files: []
|