The OmniAuth::RailsCsrfProtection::TokenVerifier class is a callable object used to verify Cross-Site Request Forgery (CSRF) protection tokens during the OmniAuth request phase.
It works by including ActionController::RequestForgeryProtection and utilizing the verified_request? method. This ensures that the token verification logic used by OmniAuth matches exactly how Rails performs token verification within its own controllers.
When the call(env) method is invoked, it performs the following steps:
- Wraps the Rack
env in an ActionDispatch::Request. - Checks if the request is verified using
verified_request?. - If the request is not verified, it raises an
ActionController::InvalidAuthenticityToken error.
To ensure configuration consistency, the verifier delegates its configuration settings to ActionController::Base.config, meaning any CSRF settings applied to your Rails application are automatically respected by the verifier.
# Conceptual usage within a middleware stack or similar context
verifier = OmniAuth::RailsCsrfProtection::TokenVerifier.new
verifier.call(env)
# Raises ActionController::InvalidAuthenticityToken if CSRF check fails