Skip to content

Commit 8406260

Browse files
committed
Add server signed response
Signed-off-by: Florian Wininger <fw.centrale@gmail.com>
1 parent 7491633 commit 8406260

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,30 @@ Rails app:
226226
end
227227
```
228228

229+
### Server signing response
230+
231+
The server can perform a validation of the response.
232+
233+
You can add the validation in the controller :
234+
235+
```ruby
236+
class ApplicationController < ActiveController::Base
237+
validation_with_api_auth(access_id: 'test', secret_key: 'test', options: { digest: 'sha256' } )
238+
end
239+
```
240+
241+
or specified at every render
242+
243+
```ruby
244+
class ApplicationController < ActiveController::Base
245+
validation_with_api_auth()
246+
247+
def index
248+
render json: @users, api_auth: { access_id: 'test', secret_key: 'test', options: { digest: 'sha256' }}
249+
end
250+
end
251+
```
252+
229253
## Development
230254

231255
ApiAuth uses bundler for gem dependencies and RSpec for testing. Developing the

lib/api_auth/headers.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ def initialize_request_driver(request)
2828
end
2929
when /ActionDispatch::Request/
3030
ActionDispatchRequest.new(request)
31+
when /ActionDispatch::Response/
32+
ActionDispatchRequest.new(request)
3133
when /ActionController::CgiRequest/
3234
ActionControllerRequest.new(request)
3335
when /HTTPI::Request/

lib/api_auth/railtie.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,40 @@ def api_authenticated?(secret_key)
1313
end
1414
end
1515

16+
module ClassMethods
17+
def validation_with_api_auth(api_auth_options = nil)
18+
ActionController.add_renderer(:json) do |json, options|
19+
api_auth_options ||= options[:api_auth]
20+
options.delete(:api_auth)
21+
22+
json = json.to_json(options) unless json.is_a?(String)
23+
24+
if options[:callback].present?
25+
self.content_type = Mime[:js] if content_type.nil? || content_type == Mime[:json]
26+
27+
"/**/#{options[:callback]}(#{json})"
28+
else
29+
self.content_type ||= Mime[:json]
30+
31+
# API AUTH addition headers
32+
if api_auth_options
33+
response.headers['CONTENT-MD5'] ||= Digest::MD5.base64digest(json)
34+
response.headers['Authorization'] ||= ApiAuth.sign!(
35+
request,
36+
api_auth_options[:access_id],
37+
api_auth_options[:secret_key],
38+
api_auth_options[:options] || {}
39+
).env['Authorization']
40+
end
41+
42+
json
43+
end
44+
end
45+
end
46+
end
47+
1648
ActionController::Base.send(:include, ControllerMethods::InstanceMethods) if defined?(ActionController::Base)
49+
ActionController::Base.send(:extend, ControllerMethods::ClassMethods) if defined?(ActionController::Base)
1750
end # ControllerMethods
1851

1952
module ActiveResourceExtension # :nodoc:

0 commit comments

Comments
 (0)