Skip to content

Commit

Permalink
Use verify_authenticity_token directly
Browse files Browse the repository at this point in the history
By adding a logger and setting the protection_strategy to raise an
exception, we can use verify_authenticity_token directly.  The main
benefit of this is that we will get a more helpful error message
attached to the exception.
  • Loading branch information
nevans committed Sep 24, 2024
1 parent ca0b33e commit 238f4f0
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions lib/omniauth/rails_csrf_protection/token_verifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,29 @@ def self.config
# our configuration delegate to `ActionController::Base`.
config.each_key do |key| config.delete(key) end

# OmniAuth expects us to raise an exception on auth failure.
self.forgery_protection_strategy = protection_method_class(:exception)

# Logging from ActionController::RequestForgeryProtection is redundant.
# OmniAuth logs basically the same message (from the exception).
self.log_warning_on_csrf_failure = false

def call(env)
dup._call(env)
end

def _call(env)
@request = ActionDispatch::Request.new(env.dup)

unless verified_request?
raise ActionController::InvalidAuthenticityToken
end
verify_authenticity_token
end

private

attr_reader :request
delegate :params, :session, to: :request

delegate :logger, to: OmniAuth
end
end
end

0 comments on commit 238f4f0

Please sign in to comment.