Skip to content

Instantly share code, notes, and snippets.

@czivko
Created January 8, 2019 21:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save czivko/ac1c2c94fe091ded6df09b73be4a8234 to your computer and use it in GitHub Desktop.
Save czivko/ac1c2c94fe091ded6df09b73be4a8234 to your computer and use it in GitHub Desktop.
Simple Auth Token security for RoR API Endpoint
class Api::V1::ApplicationController < ActionController::API
include ActionView::Rendering
include ActionController::HttpAuthentication::Token::ControllerMethods
before_action :restrict_access
before_action :allow_cross_domain_access
respond_to :json
def options
render plain: { ok: :ok }.to_json, status: :ok, content_type: 'application/json'
end
private
def restrict_access
return if request.method == "OPTIONS"
authenticate_or_request_with_http_token do |token, options|
token == "YOUR_SECRET_TOKEN_HERE"
end
end
def allow_cross_domain_access
response.headers["Access-Control-Allow-Origin"] = "*"
response.headers["Access-Control-Allow-Credentials"] = "true"
response.headers["Access-Control-Allow-Methods"] = "*"
response.headers['Access-Control-Request-Method'] = '*'
response.headers["Access-Control-Allow-Headers"] = "X-Zendesk-App-Installation-Id, X-CSRF-Token, X-Zendesk-App-Id, Origin, X-Requested-With, Content-Type, Accept, Authorization, Referer, DNT, User-Agent"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment