Skip to content

Commit 047d805

Browse files
committed
Add server signed response
Signed-off-by: Florian Wininger <[email protected]>
1 parent 09c862c commit 047d805

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
@@ -248,6 +248,30 @@ def api_authenticate
248248
end
249249
```
250250

251+
### Server signing response
252+
253+
The server can perform a validation of the response.
254+
255+
You can add the validation in the controller :
256+
257+
```ruby
258+
class ApplicationController < ActiveController::Base
259+
validation_with_api_auth(access_id: 'test', secret_key: 'test', options: { digest: 'sha256' } )
260+
end
261+
```
262+
263+
or specified at every render
264+
265+
```ruby
266+
class ApplicationController < ActiveController::Base
267+
validation_with_api_auth()
268+
269+
def index
270+
render json: @users, api_auth: { access_id: 'test', secret_key: 'test', options: { digest: 'sha256' }}
271+
end
272+
end
273+
```
274+
251275
## Development
252276

253277
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
@@ -30,6 +30,8 @@ def initialize_request_driver(request)
3030
GrapeRequest.new(request)
3131
when /ActionDispatch::Request/
3232
ActionDispatchRequest.new(request)
33+
when /ActionDispatch::Response/
34+
ActionDispatchRequest.new(request)
3335
when /ActionController::CgiRequest/
3436
ActionControllerRequest.new(request)
3537
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)