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



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

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



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

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



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

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



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

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

Instance Method Details

#activate!Object



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

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

#activated?Boolean

Returns:

  • (Boolean)


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

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

#authenticated?(password) ⇒ Boolean

Returns:

  • (Boolean)


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

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



91
92
93
# File 'app/models/email_credential.rb', line 91

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

#to_labelObject



95
96
97
# File 'app/models/email_credential.rb', line 95

def to_label
  email
end