Skip to content

Commit

Permalink
Use native rate_limit for lockable
Browse files Browse the repository at this point in the history
  • Loading branch information
lazaronixon committed Oct 8, 2024
1 parent 576e86e commit b9887b6
Show file tree
Hide file tree
Showing 7 changed files with 5 additions and 24 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## Authentication Zero 4.0.0 ##

* Remove system tests
* Use native rate_limit for lockable

## Authentication Zero 3.0.2 ##

Expand Down
2 changes: 1 addition & 1 deletion lib/generators/authentication/authentication_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ def sudoable?
end

def redis?
options.lockable? || options.ratelimit? || sudoable?
options.ratelimit? || sudoable?
end

def importmaps?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,4 @@ class ApplicationController < ActionController::API
Current.user_agent = request.user_agent
Current.ip_address = request.ip
end
<%- if options.lockable? %>
def require_lock(wait: 1.hour, attempts: 10)
counter = Kredis.counter("require_lock:#{request.remote_ip}:#{controller_path}:#{action_name}", expires_in: wait)
counter.increment

if counter.value > attempts
render json: { error: "You've exceeded the maximum number of attempts" }, status: :too_many_requests
end
end
<%- end -%>
end
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class Identity::PasswordResetsController < ApplicationController
skip_before_action :authenticate

<%- if options.lockable? -%>
before_action :require_lock, only: :create
rate_limit to: 10, within: 1.hour, only: :create
<%- end -%>
before_action :set_user, only: :update

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,6 @@ class ApplicationController < ActionController::Base
Current.user_agent = request.user_agent
Current.ip_address = request.ip
end
<%- if options.lockable? %>
def require_lock(wait: 1.hour, attempts: 10)
counter = Kredis.counter("require_lock:#{request.remote_ip}:#{controller_path}:#{action_name}", expires_in: wait)
counter.increment

if counter.value > attempts
redirect_to root_path, alert: "You've exceeded the maximum number of attempts"
end
end
<%- end -%>
<%- if sudoable? %>
def require_sudo
unless Current.session.sudo?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class Identity::PasswordResetsController < ApplicationController
skip_before_action :authenticate

<%- if options.lockable? -%>
before_action :require_lock, only: :create
rate_limit to: 10, within: 1.hour, only: :create, with: -> { redirect_to root_path, alert: "Try again later" }
<%- end -%>
before_action :set_user, only: %i[ edit update ]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class Sessions::PasswordlessesController < ApplicationController
skip_before_action :authenticate

<%- if options.lockable? -%>
before_action :require_lock, only: :create
rate_limit to: 10, within: 1.hour, only: :create, with: -> { redirect_to root_path, alert: "Try again later" }
<%- end -%>
before_action :set_user, only: :edit

Expand Down

0 comments on commit b9887b6

Please sign in to comment.