Skip to content

Instantly share code, notes, and snippets.

@squadette
Last active November 3, 2015 01:25
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 squadette/7f9fb7c68f01cd6231f9 to your computer and use it in GitHub Desktop.
Save squadette/7f9fb7c68f01cd6231f9 to your computer and use it in GitHub Desktop.
@@ -10,8 +10,31 @@ class ApplicationController < ActionController::Base
before_filter :initiate_handover
before_filter -> { request.format = "html" unless params[:format] }
+ skip_before_action :verify_authenticity_token, if: :check_api_token
+
protected
+ def check_api_token
+ return false if prevent_api_token_access
+
+ return false if request.format.to_sym != :json
+
+ uuid = request.headers["X-API-Token"]
+
+ return false if uuid.blank?
+
+ api_token = ApiToken.find_by(uuid: uuid, active: true)
+ raise ActionController::NotAuthorized if !api_token
+
+ sign_in(api_token.user)
+
+ return true
+ end
+
+ def prevent_api_token_access
+ false
+ end
+
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment