Skip to content

How to configure OmniAuth::AuthenticityTokenProtection for Hanami #1074

Closed

Description

@masterT

How can I configure the OmniAuth::AuthenticityTokenProtection to pass options to Rack::Protection::AuthenticityToken. I'm using Hanami 1.3 which already handle CSRF protection, but I would like to configure OmniAuth to use the right session key (key: :_csrf_token) and the right HTTP param (authenticity_param: :_csrf_token).

Here is the error I have.

Screen Shot 2022-06-09 at 22 20 55

Activity

  1. masterT commented on Jun 10, 2022

    @masterT
    ContributorAuthor

    Got my answer from #1070.

    # Since Hanami does not encode/decode the CSRF token in base64 we need to set a custom allow/deny logic.
    OmniAuth::AuthenticityTokenProtection.default_options(
      allow_if: lambda do |env|
        token_key = Hanami::Action::CSRFProtection::CSRF_TOKEN.to_s
        request_token = Rack::Request.new(env).params[token_key]
        session_token = env['rack.session'][token_key]
    
        !Hanami::Utils::Blank.blank?(request_token) && Rack::Utils.secure_compare(request_token, session_token)
      end
    )
  2. changed the title [-]How to configure OmniAuth::AuthenticityTokenProtection[/-] [+]How to configure OmniAuth::AuthenticityTokenProtection for Hanami[/+] on Jun 10, 2022
  3. added theissue type on Oct 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions