Code Monkey home page Code Monkey logo

Comments (1)

6f6d6172 avatar 6f6d6172 commented on June 8, 2024

Through a series of monkeypatches, I've achieved the behavior I'm looking for. Hopefully this can do a decent job of explaining my usecase. This allows me to use it in a controller like like

  def show
    if limiter.at_limit?(request)
      @warning_message = "Attempts exceeded."
      @challenge = new_captcha_form
    end
  end

  def create
    if limiter.matched_by?(request)
      validate_captcha
    end

    rest_of_the_action
  end

  def limiter
    limiter = Rack::Attack.track("requests from bucket", limit: 5, period: 3600) do |req|
      bucket
    end
  end

monkeypatch:

Rack::Attack::Configuration.class_eval do
  # nop'd because otherwise all Rack::Attack::Throttles are incremented
  # even if the gated functionality is never exercised
  def tracked?(request)
  end
end

Rack::Attack::Cache.class_eval do
  def get_count(unprefixed_key, period)
    enforce_store_presence!
    enforce_store_method_presence!(:read)

    key, expires_in = key_and_expiry(unprefixed_key, period)
    result = store.read(key)
    result || 0
  end

end

Rack::Attack::Track.class_eval do
  def at_limit?(request)
    filter.at_limit?(request)
  end
end

Rack::Attack::Throttle.class_eval do
  def at_limit?(request)
    discriminator = discriminator_for(request)
    return false unless discriminator

    current_period  = period_for(request)
    current_limit   = limit_for(request)
    count           = cache.get_count("#{name}:#{discriminator}", current_period)
    
    count > current_limit
  end
end

from rack-attack.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.