rack-simple_auth 1.0.3 → 1.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 185f4e18284df3e3d9a0a54d91ab5d6f44efa43d
4
- data.tar.gz: 170edd2a83ad1b31c9f9b84fd554592bc5695e91
3
+ metadata.gz: 0166a62aa16634f85f815db10c9856bf39e9e11c
4
+ data.tar.gz: 7a4043bcbe808edfb1eeb580d737914b47637f90
5
5
  SHA512:
6
- metadata.gz: 625d216c440cbb2d84448d7652f23e4f4b45b00ffdd427ae8571a5e974f1873fcca41fc67dae4496c82950c3e82aeb9496f70cbc0255f8f94a01e53a6dcab2f2
7
- data.tar.gz: 2fa968ad1e8e9489a9fc636da5f4a0f249c76f1166cda18e8070ad5454165c0b6d7f76e72cbee1db8e6f4bc04c48d96412bb4b3955cf11af19eda675a67769d9
6
+ metadata.gz: f32b803d35245a9a37e665fff4b39fde60bc4c3431318bc8c7480b8d6a7fb1cae99a9aee3214634e059cd45f7674e01a2f074d201945e3a839b3001b1fb3365f
7
+ data.tar.gz: a257ea5f3f9f234c74e308932239280b118dda9f6475b0ffbffc91627fe08835803ea901af3e91f848089ef93f8f1c69786815c5cfdbf30ac5e9b80a65762638
data/MANIFEST CHANGED
@@ -8,9 +8,13 @@ checksum/rack-simple_auth-0.1.2.gem.sha512
8
8
  checksum/rack-simple_auth-1.0.0.gem.sha512
9
9
  checksum/rack-simple_auth-1.0.0rc.gem.sha512
10
10
  checksum/rack-simple_auth-1.0.1.gem.sha512
11
+ checksum/rack-simple_auth-1.0.3.gem.sha512
12
+ checksum/rack-simple_auth-1.0.4.gem.sha512
11
13
  lib/rack/simple_auth.rb
12
14
  lib/rack/simple_auth/hmac/config.rb
13
15
  lib/rack/simple_auth/hmac/middleware.rb
16
+ lib/rack/simple_auth/hmac/request.rb
17
+ lib/rack/simple_auth/hmac/response.rb
14
18
  lib/rack/simple_auth/logger.rb
15
19
  lib/rack/simple_auth/version.rb
16
20
  rack-simple_auth.gemspec
@@ -0,0 +1 @@
1
+ 3c74fc621b80b95b3dcde055142e6900778c67c8e5ed3b550195c4d934c5aff5b7ab427b936dd725afc1aa5c118e7e7646a25e991be9f12f0c45e7621902f537
@@ -0,0 +1 @@
1
+ 4dc93569f637e6d1e4e4f564efab90c192cba3f84d4368de83ff80b88d14dd642c8a234e64ee3a1167ab765651963d4a37873e34353cff7701cd5e5eef06bf2d
@@ -3,6 +3,8 @@ require 'rack/simple_auth/logger'
3
3
 
4
4
  # HMAC utilities
5
5
  require 'rack/simple_auth/hmac/config'
6
+ require 'rack/simple_auth/hmac/request'
7
+ require 'rack/simple_auth/hmac/response'
6
8
  require 'rack/simple_auth/hmac/middleware'
7
9
 
8
10
  require 'json'
@@ -23,7 +23,7 @@ module Rack
23
23
  # @!attribute [rw] verbose
24
24
  # @return [TrueClass|NilClass]
25
25
  #
26
- class Config < Hash
26
+ class Config
27
27
  attr_writer :tolerance
28
28
  attr_writer :secret, :signature
29
29
 
@@ -63,163 +63,15 @@ module Rack
63
63
  #
64
64
  def call!(env)
65
65
  env = env.dup
66
- @request = Rack::Request.new(env)
66
+ @request = Request.new(env, @config)
67
67
 
68
- # This STATE is needed
69
- # logging & authorizing have to use the exact same messages, so don't call allowed_messages 2 times
70
- # Call it 1 time and save this state
71
- @allowed_messages = allowed_messages
72
-
73
- if valid_request?
68
+ if @request.valid?
74
69
  @app.call(env)
75
70
  else
76
- response = Rack::Response.new('Unauthorized', 401, 'Content-Type' => 'text/html')
71
+ response = Response.new('Unauthorized', 401, 'Content-Type' => 'text/html')
77
72
  response.finish
78
73
  end
79
74
  end
80
-
81
- private
82
-
83
- ##
84
- # Checks for valid HMAC Request
85
- #
86
- # @return [TrueClass] if request is authorized
87
- # @return [FalseClass] if request is not authorized or HTTP_AUTHORIZATION Header is not set
88
- #
89
- def valid_request?
90
- log
91
-
92
- return false if empty_header? || !authorized?
93
-
94
- true
95
- end
96
-
97
- ##
98
- # Check if HTTP_AUTHORIZATION Header is set
99
- #
100
- # @return [TrueClass] if header is set
101
- # @return [FalseClass] if header is not set
102
- #
103
- def empty_header?
104
- @request.env['HTTP_AUTHORIZATION'].nil?
105
- end
106
-
107
- ##
108
- # Check if request is authorized
109
- #
110
- # @return [TrueClass] if request is authorized -> {#request_signature} is correct & {#request_message} is included
111
- # in {#allowed_messages}
112
- # @return [FalseClass] if request is not authorized
113
- #
114
- def authorized?
115
- request_signature.eql?(@config.signature) && @allowed_messages.include?(request_message)
116
- end
117
-
118
- ##
119
- # Get request signature
120
- #
121
- # @return [String] signature of current request
122
- #
123
- def request_signature
124
- @request.env['HTTP_AUTHORIZATION'].split(':').last
125
- end
126
-
127
- ##
128
- # Get encrypted request message
129
- #
130
- # @return [String] message of current request
131
- #
132
- def request_message
133
- @request.env['HTTP_AUTHORIZATION'].split(':').first
134
- end
135
-
136
- ##
137
- # Builds Array of allowed message hashs between @tolerance via {#message}
138
- #
139
- # @return [Array]
140
- def allowed_messages
141
- messages = []
142
-
143
- # Timestamp with milliseconds as Fixnum
144
- date = (Time.now.to_f.freeze * 1000).to_i
145
- (-(@config.tolerance)..0).step(1) do |i|
146
- messages << OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha256'), @config.secret, message(date, i))
147
- end
148
-
149
- messages
150
- end
151
-
152
- ##
153
- # Get Message for current Request and delay
154
- #
155
- # @param [Fixnum] date [current date in timestamp format]
156
- # @param [Fixnum] delay [delay in timestamp format]
157
- #
158
- # @return [String] message
159
- def message(date, delay = 0)
160
- date += delay
161
-
162
- { 'method' => @request.request_method, 'date' => date, 'data' => request_data }.to_json
163
- end
164
-
165
- ##
166
- # Get Request Data specified by @config.request_config
167
- #
168
- # @return [String|Hash] data
169
- #
170
- # Note: REFACTOR this shit..
171
- def request_data
172
- return @request.send(@config.request_config[method].to_sym) if valid_message_type?
173
-
174
- fail "Not a valid option #{@config.request_config[method]} - Use either params or path"
175
- end
176
-
177
- ##
178
- # Request method for current request
179
- #
180
- # @return [String] Request Method [GET|POST|PUT|DELETE|PATCH]
181
- #
182
- def method
183
- @request.request_method
184
- end
185
-
186
- ##
187
- # Check if message type for current request is valid
188
- #
189
- # @return [TrueClass] if message type for current request is path or params
190
- # @return [FalseClass] if message type is invalid
191
- #
192
- def valid_message_type?
193
- @config.request_config[method] == 'path' || @config.request_config[method] == 'params'
194
- end
195
-
196
- ##
197
- # Log to @config.logpath
198
- # Contains:
199
- # - allowed messages and received message
200
- # - time when request was made
201
- # - type of request
202
- # - requested path
203
- #
204
- # Note: This is kinda slow under Rubinius
205
- # (Rack::SimpleAuth::Logger.log has IO action, i think there are some performance issues)
206
- #
207
- def log
208
- msg = "#{Time.new} - #{@request.request_method} #{@request.path} - 400 Unauthorized\n"
209
- msg << "HTTP_AUTHORIZATION: #{@request.env['HTTP_AUTHORIZATION']}\n"
210
- msg << "Auth Message Config: #{@config.request_config[@request.request_method]}\n"
211
-
212
- if @allowed_messages
213
- msg << "Allowed Encrypted Messages:\n"
214
- @allowed_messages.each do |hash|
215
- msg << "#{hash}\n"
216
- end
217
- end
218
-
219
- msg << "Auth Signature: #{@config.signature}"
220
-
221
- Rack::SimpleAuth::Logger.log(@config.logpath, @config.verbose, ENV['RACK_ENV'], msg)
222
- end
223
75
  end # Middleware
224
76
  end # HMAC
225
77
  end # SimpleAuth
@@ -0,0 +1,156 @@
1
+ module Rack
2
+ module SimpleAuth
3
+ module HMAC
4
+ class Request < Rack::Request
5
+ def initialize(env, config)
6
+ @env = env
7
+ @config = config
8
+ @allowed_messages = allowed_messages
9
+ end
10
+
11
+ ##
12
+ # Checks for valid HMAC Request
13
+ #
14
+ # @return [TrueClass] if request is authorized
15
+ # @return [FalseClass] if request is not authorized or HTTP_AUTHORIZATION Header is not set
16
+ #
17
+ def valid?
18
+ log
19
+
20
+ return false if empty_header? || !authorized?
21
+
22
+ true
23
+ end
24
+
25
+ private
26
+
27
+ ##
28
+ # Builds Array of allowed message hashs between @tolerance via {#message}
29
+ #
30
+ # @return [Array]
31
+ def allowed_messages
32
+ messages = []
33
+
34
+ # Timestamp with milliseconds as Fixnum
35
+ date = (Time.now.to_f.freeze * 1000).to_i
36
+ (-(@config.tolerance)..0).step(1) do |i|
37
+ messages << OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha256'), @config.secret, build_message(date, i))
38
+ end
39
+
40
+ messages
41
+ end
42
+
43
+ ##
44
+ # Build Message for current Request and delay
45
+ #
46
+ # @param [Fixnum] date [current date in timestamp format]
47
+ # @param [Fixnum] delay [delay in timestamp format]
48
+ #
49
+ # @return [String] message
50
+ def build_message(date, delay = 0)
51
+ date += delay
52
+
53
+ { 'method' => self.request_method, 'date' => date, 'data' => data }.to_json
54
+ end
55
+
56
+ ##
57
+ # Get Request Data specified by @config.request_config
58
+ #
59
+ # @return [String|Hash] data
60
+ #
61
+ # Note: REFACTOR this shit..
62
+ def data
63
+ return self.send(@config.request_config[method].to_sym) if valid_message_type?
64
+
65
+ fail "Not a valid option #{@config.request_config[method]} - Use either params or path"
66
+ end
67
+
68
+ ##
69
+ # Check if HTTP_AUTHORIZATION Header is set
70
+ #
71
+ # @return [TrueClass] if header is set
72
+ # @return [FalseClass] if header is not set
73
+ #
74
+ def empty_header?
75
+ self.env['HTTP_AUTHORIZATION'].nil?
76
+ end
77
+
78
+ ##
79
+ # Check if request is authorized
80
+ #
81
+ # @return [TrueClass] if request is authorized -> {#signature} is correct & {#message} is included
82
+ # in {#allowed_messages}
83
+ # @return [FalseClass] if request is not authorized
84
+ #
85
+ def authorized?
86
+ signature.eql?(@config.signature) && @allowed_messages.include?(message)
87
+ end
88
+
89
+ ##
90
+ # Get request signature
91
+ #
92
+ # @return [String] signature of current request
93
+ #
94
+ def signature
95
+ self.env['HTTP_AUTHORIZATION'].split(':').last
96
+ end
97
+
98
+ ##
99
+ # Get encrypted request message
100
+ #
101
+ # @return [String] message of current request
102
+ #
103
+ def message
104
+ self.env['HTTP_AUTHORIZATION'].split(':').first
105
+ end
106
+
107
+ ##
108
+ # Request method for current request
109
+ #
110
+ # @return [String] Request Method [GET|POST|PUT|DELETE|PATCH]
111
+ #
112
+ def method
113
+ self.request_method
114
+ end
115
+
116
+ ##
117
+ # Check if message type for current request is valid
118
+ #
119
+ # @return [TrueClass] if message type for current request is path or params
120
+ # @return [FalseClass] if message type is invalid
121
+ #
122
+ def valid_message_type?
123
+ @config.request_config[method] == 'path' || @config.request_config[method] == 'params'
124
+ end
125
+
126
+ ##
127
+ # Log to @config.logpath
128
+ # Contains:
129
+ # - allowed messages and received message
130
+ # - time when request was made
131
+ # - type of request
132
+ # - requested path
133
+ #
134
+ # Note: This is kinda slow under Rubinius
135
+ # (Rack::SimpleAuth::Logger.log has IO action, i think there are some performance issues)
136
+ #
137
+ def log
138
+ msg = "#{Time.new} - #{self.request_method} #{self.path} - 400 Unauthorized\n"
139
+ msg << "HTTP_AUTHORIZATION: #{self.env['HTTP_AUTHORIZATION']}\n"
140
+ msg << "Auth Message Config: #{@config.request_config[self.request_method]}\n"
141
+
142
+ if @allowed_messages
143
+ msg << "Allowed Encrypted Messages:\n"
144
+ @allowed_messages.each do |hash|
145
+ msg << "#{hash}\n"
146
+ end
147
+ end
148
+
149
+ msg << "Auth Signature: #{@config.signature}"
150
+
151
+ Rack::SimpleAuth::Logger.log(@config.logpath, @config.verbose, ENV['RACK_ENV'], msg)
152
+ end
153
+ end
154
+ end
155
+ end
156
+ end
@@ -0,0 +1,8 @@
1
+ module Rack
2
+ module SimpleAuth
3
+ module HMAC
4
+ class Response < Rack::Response
5
+ end
6
+ end
7
+ end
8
+ end
@@ -2,6 +2,6 @@ module Rack
2
2
  # Module which Contains different Authorization / Authentication Classes (HMAC, ..)
3
3
  module SimpleAuth
4
4
  # Current Gem Version
5
- VERSION = '1.0.3'
5
+ VERSION = '1.0.5'
6
6
  end
7
7
  end
@@ -28,8 +28,7 @@ Gem::Specification.new do |spec|
28
28
  spec.add_development_dependency "coveralls", '~> 0.7'
29
29
  spec.add_development_dependency 'codeclimate-test-reporter'
30
30
  spec.add_development_dependency "rack-test", '~> 0.6'
31
- spec.add_development_dependency 'rspec', '~> 2.14.1'
31
+ spec.add_development_dependency 'rspec', '~> 3'
32
32
 
33
33
  spec.add_development_dependency 'minitest', '~> 5.3'
34
- spec.add_development_dependency 'minitest-reporters'
35
34
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-simple_auth
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Benny1992
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-14 00:00:00.000000000 Z
11
+ date: 2014-06-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack
@@ -100,14 +100,14 @@ dependencies:
100
100
  requirements:
101
101
  - - "~>"
102
102
  - !ruby/object:Gem::Version
103
- version: 2.14.1
103
+ version: '3'
104
104
  type: :development
105
105
  prerelease: false
106
106
  version_requirements: !ruby/object:Gem::Requirement
107
107
  requirements:
108
108
  - - "~>"
109
109
  - !ruby/object:Gem::Version
110
- version: 2.14.1
110
+ version: '3'
111
111
  - !ruby/object:Gem::Dependency
112
112
  name: minitest
113
113
  requirement: !ruby/object:Gem::Requirement
@@ -122,20 +122,6 @@ dependencies:
122
122
  - - "~>"
123
123
  - !ruby/object:Gem::Version
124
124
  version: '5.3'
125
- - !ruby/object:Gem::Dependency
126
- name: minitest-reporters
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
125
  description: SimpleAuth HMAC authentication
140
126
  email:
141
127
  - r3qnbenni@gmail.com
@@ -153,9 +139,13 @@ files:
153
139
  - checksum/rack-simple_auth-1.0.0.gem.sha512
154
140
  - checksum/rack-simple_auth-1.0.0rc.gem.sha512
155
141
  - checksum/rack-simple_auth-1.0.1.gem.sha512
142
+ - checksum/rack-simple_auth-1.0.3.gem.sha512
143
+ - checksum/rack-simple_auth-1.0.4.gem.sha512
156
144
  - lib/rack/simple_auth.rb
157
145
  - lib/rack/simple_auth/hmac/config.rb
158
146
  - lib/rack/simple_auth/hmac/middleware.rb
147
+ - lib/rack/simple_auth/hmac/request.rb
148
+ - lib/rack/simple_auth/hmac/response.rb
159
149
  - lib/rack/simple_auth/logger.rb
160
150
  - lib/rack/simple_auth/version.rb
161
151
  - rack-simple_auth.gemspec
@@ -179,7 +169,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
179
169
  version: '0'
180
170
  requirements: []
181
171
  rubyforge_project:
182
- rubygems_version: 2.2.2
172
+ rubygems_version: 2.3.0
183
173
  signing_key:
184
174
  specification_version: 4
185
175
  summary: SimpleAuth HMAC authentication