Skip to content

Commit

Permalink
Merge pull request #124 from javierav/sudoable
Browse files Browse the repository at this point in the history
Remove dependency on redis / kredis for sudoable
  • Loading branch information
lazaronixon authored Oct 24, 2024
2 parents c934cfc + cf425db commit 110f5fd
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## New version

* Remove dependency on redis / kredis for sudoable

## Authentication Zero 4.0.1 ##

* Remove rate limit from api generator
Expand Down
3 changes: 1 addition & 2 deletions lib/generators/authentication/authentication_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ def add_gems

if redis?
gem "redis", "~> 4.0", comment: "Use Redis adapter to run additional authentication features"
gem "kredis", comment: "Use Kredis to get higher-level data types in Redis [https://github.com/rails/kredis]"
end

if options.pwned?
Expand Down Expand Up @@ -259,7 +258,7 @@ def sudoable?
end

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

def importmaps?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Sessions::SudosController < ApplicationController
session_record = Current.session

if session_record.user.authenticate(params[:password])
session_record.sudo.mark; redirect_to(params[:proceed_to_url])
session_record.touch(:sudo_at); redirect_to(params[:proceed_to_url])
else
redirect_to new_sessions_sudo_path(proceed_to_url: params[:proceed_to_url]), alert: "The password you entered is incorrect"
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ class <%= migration_class_name %> < ActiveRecord::Migration[<%= ActiveRecord::Mi
t.references :user, null: false, foreign_key: true
t.string :user_agent
t.string :ip_address
<%- if sudoable? %>
t.datetime :sudo_at, null: false
<%- end -%>

t.timestamps
end
Expand Down
15 changes: 9 additions & 6 deletions lib/generators/authentication/templates/models/session.rb.tt
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
class Session < ApplicationRecord
belongs_to :user
<%- if sudoable? %>
kredis_flag :sudo, expires_in: 30.minutes
<%- end -%>

before_create do
self.user_agent = Current.user_agent
self.ip_address = Current.ip_address
<%- if sudoable? %>
self.sudo_at = Time.current
<%- end -%>
end
<%- if sudoable? %>
after_create { sudo.mark }
<%- end -%>
<%- if options.trackable? %>
after_create { user.events.create! action: "signed_in" }
after_destroy { user.events.create! action: "signed_out" }
<%- end -%>
<%- if sudoable? %>

def sudo?
sudo_at > 30.minutes.ago
end
<%- end -%>
end

0 comments on commit 110f5fd

Please sign in to comment.