-
Notifications
You must be signed in to change notification settings - Fork 147
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add server signed response #163
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,40 @@ def api_authenticated?(secret_key) | |
end | ||
end | ||
|
||
module ClassMethods | ||
def validation_with_api_auth(api_auth_options = nil) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we call this something like api_auth_sign_response There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
ActionController.add_renderer(:json) do |json, options| | ||
api_auth_options ||= options[:api_auth] | ||
options.delete(:api_auth) | ||
|
||
json = json.to_json(options) unless json.is_a?(String) | ||
|
||
if options[:callback].present? | ||
self.content_type = Mime[:js] if content_type.nil? || content_type == Mime[:json] | ||
|
||
"/**/#{options[:callback]}(#{json})" | ||
else | ||
self.content_type ||= Mime[:json] | ||
|
||
# API AUTH addition headers | ||
if api_auth_options | ||
response.headers['CONTENT-MD5'] ||= Digest::MD5.base64digest(json) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this needed because the response object won't have a body for ApiAuth to inspect yet? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In fact, the body is the json variable. I keep the same code as the classic JSON renderer. It's just a .to_json of the variable passed in the controller. The source code is here : https://github.com/rails/rails/blob/master/actionpack/lib/action_controller/metal/renderers.rb#L156 I add a comment in the code for memory. |
||
response.headers['Authorization'] ||= ApiAuth.sign!( | ||
request, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We'd want to pass in the response object here right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know that is strange, but we need to pass the request object because we need to access to the request.fullpath. |
||
api_auth_options[:access_id], | ||
api_auth_options[:secret_key], | ||
api_auth_options[:options] || {} | ||
).env['Authorization'] | ||
end | ||
|
||
json | ||
end | ||
end | ||
end | ||
end | ||
|
||
ActionController::Base.send(:include, ControllerMethods::InstanceMethods) if defined?(ActionController::Base) | ||
ActionController::Base.send(:extend, ControllerMethods::ClassMethods) if defined?(ActionController::Base) | ||
end # ControllerMethods | ||
|
||
module ActiveResourceExtension # :nodoc: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you're trying to say the server can sign the content of the response, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done