2017-03-09 11:44:51 +00:00
|
|
|
module ApplicationController::PreventsCsrf
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
|
|
|
before_action :verify_csrf_token
|
|
|
|
after_action :set_csrf_token_headers
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def set_csrf_token_headers
|
|
|
|
return true if @_auth_type.present? && @_auth_type != 'session'
|
|
|
|
headers['CSRF-TOKEN'] = form_authenticity_token
|
|
|
|
end
|
|
|
|
|
|
|
|
def verify_csrf_token
|
2018-07-09 06:47:03 +00:00
|
|
|
return true if !protect_against_forgery?
|
|
|
|
return true if request.get?
|
|
|
|
return true if request.head?
|
|
|
|
return true if %w[token_auth basic_auth].include?(@_auth_type)
|
2017-03-09 11:44:51 +00:00
|
|
|
return true if valid_authenticity_token?(session, params[:authenticity_token] || request.headers['X-CSRF-Token'])
|
|
|
|
logger.info 'CSRF token verification failed'
|
|
|
|
raise Exceptions::NotAuthorized, 'CSRF token verification failed!'
|
|
|
|
end
|
|
|
|
end
|