Class: NameCredential

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/name_credential.rb

Constant Summary collapse

NameMaximumLength =
200
HashedPasswordPattern =
/\A([0-9a-f]{8}):([0-9a-f]{64})\z/
MaximumRecordsPerUser =
10

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.authenticate(name, password) ⇒ Object



34
35
36
37
38
39
# File 'app/models/name_credential.rb', line 34

def self.authenticate(name, password)
  credential = self.find_by_name(name)
  return nil unless credential
  return nil unless credential.authenticated?(password)
  return credential
end

.compare_hashed_password(password, hashed_password) ⇒ Object



28
29
30
31
32
# File 'app/models/name_credential.rb', line 28

def self.compare_hashed_password(password, hashed_password)
  return false unless HashedPasswordPattern =~ hashed_password
  salt, digest = $1, $2
  return (Digest::SHA256.hexdigest(salt + ":" + password) == digest)
end

.create_hashed_password(password) ⇒ Object



23
24
25
26
# File 'app/models/name_credential.rb', line 23

def self.create_hashed_password(password)
  salt = 8.times.map { rand(16).to_s(16) }.join
  return salt + ":" + Digest::SHA256.hexdigest(salt + ":" + password)
end

Instance Method Details

#activate!Object



51
52
53
# File 'app/models/name_credential.rb', line 51

def activate!
  true
end

#activated?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'app/models/name_credential.rb', line 47

def activated?
  true
end

#authenticated?(password) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
44
45
# File 'app/models/name_credential.rb', line 41

def authenticated?(password)
  return false unless self.class.compare_hashed_password(password, self.hashed_password)
  return false unless self.activated?
  return true
end

#login!Object



55
56
57
# File 'app/models/name_credential.rb', line 55

def login!
  self.update_attributes!(:loggedin_at => Time.now)
end

#to_labelObject



59
60
61
# File 'app/models/name_credential.rb', line 59

def to_label
  name
end