Module: RailsJwtAuth::Lockable

Defined in:
app/models/concerns/rails_jwt_auth/lockable.rb

Constant Summary collapse

BOTH_UNLOCK_STRATEGIES =
%i[time email].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



5
6
7
8
9
10
11
12
13
14
# File 'app/models/concerns/rails_jwt_auth/lockable.rb', line 5

def self.included(base)
  base.class_eval do
    if defined?(Mongoid) && ancestors.include?(Mongoid::Document)
      field :failed_attempts,         type: Integer
      field :unlock_token,            type: String
      field :first_failed_attempt_at, type: Time
      field :locked_at,               type: Time
    end
  end
end

Instance Method Details

#access_locked?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'app/models/concerns/rails_jwt_auth/lockable.rb', line 36

def access_locked?
  locked_at && !lock_expired?
end

#clean_lockObject



24
25
26
27
28
# File 'app/models/concerns/rails_jwt_auth/lockable.rb', line 24

def clean_lock
  self.locked_at = nil
  self.unlock_token = nil
  reset_attempts
end

#failed_attemptObject



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/models/concerns/rails_jwt_auth/lockable.rb', line 40

def failed_attempt
  return if access_locked?

  reset_attempts if attempts_expired?

  self.failed_attempts ||= 0
  self.failed_attempts += 1
  self.first_failed_attempt_at = Time.current if failed_attempts == 1

  save(validate: false).tap do |result|
    lock_access if result && attempts_exceeded?
  end
end

#lock_accessObject



16
17
18
19
20
21
22
# File 'app/models/concerns/rails_jwt_auth/lockable.rb', line 16

def lock_access
  self.locked_at = Time.current

  save(validate: false).tap do |result|
    send_unlock_instructions if result && unlock_strategy_enabled?(:email)
  end
end

#unlock_accessObject



30
31
32
33
34
# File 'app/models/concerns/rails_jwt_auth/lockable.rb', line 30

def unlock_access
  clean_lock

  save(validate: false) if changed?
end