Ensure cors headers are also returned for other requests

This commit is contained in:
Maciej Szlosarczyk 2018-10-17 11:22:52 +03:00
parent e3c83c601c
commit 09e0a96b70
No known key found for this signature in database
GPG key ID: 41D62D42D3B0D765
3 changed files with 18 additions and 0 deletions

View file

@ -5,6 +5,7 @@ module Api
module V1 module V1
module Registrant module Registrant
class AuthController < ActionController::API class AuthController < ActionController::API
before_action :set_cors_header
before_action :check_ip_whitelist before_action :check_ip_whitelist
rescue_from(ActionController::ParameterMissing) do |parameter_missing_exception| rescue_from(ActionController::ParameterMissing) do |parameter_missing_exception|
@ -27,6 +28,10 @@ module Api
private private
def set_cors_header
response.headers['Access-Control-Allow-Origin'] = '*'
end
def eid_params def eid_params
required_params = %i[ident first_name last_name] required_params = %i[ident first_name last_name]
required_params.each_with_object(params) do |key, obj| required_params.each_with_object(params) do |key, obj|

View file

@ -5,6 +5,7 @@ module Api
module V1 module V1
module Registrant module Registrant
class BaseController < ActionController::API class BaseController < ActionController::API
before_action :set_cors_header
before_action :authenticate before_action :authenticate
before_action :set_paper_trail_whodunnit before_action :set_paper_trail_whodunnit
@ -17,6 +18,10 @@ module Api
private private
def set_cors_header
response.headers['Access-Control-Allow-Origin'] = '*'
end
def bearer_token def bearer_token
pattern = /^Bearer / pattern = /^Bearer /
header = request.headers['Authorization'] header = request.headers['Authorization']

View file

@ -24,4 +24,12 @@ class RegistrantApiCorsHeadersTest < ApplicationIntegrationTest
assert_equal('', response.body) assert_equal('', response.body)
end end
def test_it_returns_cors_headers_for_other_requests
post '/api/v1/registrant/auth/eid', {}
assert_equal('*', response.headers['Access-Control-Allow-Origin'])
get '/api/v1/registrant/contacts', {}
assert_equal('*', response.headers['Access-Control-Allow-Origin'])
end
end end