Module: OneTimePassword::OneTimeAuthenticationModel

Extended by:
ActiveSupport::Concern
Included in:
OneTimeAuthentication
Defined in:
lib/one_time_password/one_time_authentication_model.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#authenticate_one_time_client_token!(client_token) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/one_time_password/one_time_authentication_model.rb', line 111

def authenticate_one_time_client_token!(client_token)
  if (self.client_token.present? &&
    self.client_token == client_token)
    # Refresh client_token, and return this token
    new_client_token = self.set_client_token
    self.save!
    new_client_token
  else
    # Put invalid token(nil) in client_token, and return nil
    self.client_token = nil
    self.save!
    nil
  end
end

#authenticate_one_time_password!(password) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/one_time_password/one_time_authentication_model.rb', line 126

def authenticate_one_time_password!(password)
  result =
    if !self.expired? && self.under_valid_failed_count?
      !!self.authenticate(password)
    else
      false
    end

  if result
    self.authenticated_at = Time.zone.now
    # Put invalid token(nil) in client_token, and return nil
    self.client_token = nil
  else
    self.failed_count += 1
  end
  self.save!

  result
end

#expired?Boolean

Returns:

  • (Boolean)


102
103
104
105
# File 'lib/one_time_password/one_time_authentication_model.rb', line 102

def expired?
  !(self.created_at.to_f <= Time.zone.now.to_f &&
    Time.zone.now.to_f <= self.created_at.to_f + self.expires_seconds.to_f)
end

#set_client_tokenObject



146
147
148
# File 'lib/one_time_password/one_time_authentication_model.rb', line 146

def set_client_token
  self.client_token = SecureRandom.urlsafe_base64
end

#set_password_and_password_length(length = 6) ⇒ Object



150
151
152
# File 'lib/one_time_password/one_time_authentication_model.rb', line 150

def set_password_and_password_length(length=6)
  self.password = self.password_confirmation = OneTimeAuthentication.generate_random_password(length)
end

#under_valid_failed_count?Boolean

Returns:

  • (Boolean)


107
108
109
# File 'lib/one_time_password/one_time_authentication_model.rb', line 107

def under_valid_failed_count?
  self.failed_count < self.max_authenticate_password_count
end