Class: EmailCredential

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

Overview

メール認証情報

Constant Summary collapse

EmailMaximumLength =
200
TokenLength =
20
TokenPattern =
TokenUtil.create_token_regexp(TokenLength)
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(email, password) ⇒ Object



63
64
65
66
67
68
# File 'app/models/email_credential.rb', line 63

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

.compare_hashed_password(password, hashed_password) ⇒ Object



57
58
59
60
61
# File 'app/models/email_credential.rb', line 57

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



52
53
54
55
# File 'app/models/email_credential.rb', line 52

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

.create_unique_activation_tokenObject



48
49
50
# File 'app/models/email_credential.rb', line 48

def self.create_unique_activation_token
  return TokenUtil.create_unique_token(self, :activation_token, TokenLength)
end

Instance Method Details

#activate!Object



80
81
82
83
84
# File 'app/models/email_credential.rb', line 80

def activate!
  return false if self.activated?
  self.update_attributes!(:activated_at => Time.now)
  return true
end

#activated?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'app/models/email_credential.rb', line 76

def activated?
  return !self.activated_at.nil?
end

#authenticated?(password) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
73
74
# File 'app/models/email_credential.rb', line 70

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



86
87
88
# File 'app/models/email_credential.rb', line 86

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