Module: Rack::CouchdbOAuth2::Model::Account

Defined in:
lib/couchdb_oauth2/model/account.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/couchdb_oauth2/model/account.rb', line 7

def self.included(klass)
  klass.class_eval do

    property  :email,   String
    property  :encrypted_password,  String, :protected => true
    property  :pepper,  String, :protected => true

    view_by   :email
    
    validates_presence_of :email
    validates_uniqueness_of :email
    validates_confirmation_of :password
    validates_presence_of :encrypted_password, :message => 'password should not be empty'
    validates_presence_of :password_confirmation, :if => :password_changed?
    
    attr_reader :password

    def self.stretches
      5
    end

    def self.secure_compare(a, b)
      return false if a.blank? || b.blank? || a.bytesize != b.bytesize
      l = a.unpack "C#{a.bytesize}"

      res = 0
      b.each_byte { |byte| res |= byte ^ l.shift }
      res == 0
    end

    def self.(identity)
      raise 'implement me'
    end
  end
end

Instance Method Details

#access_tokensObject



69
70
71
# File 'lib/couchdb_oauth2/model/account.rb', line 69

def access_tokens
  AccessToken.view(:by_account_id, :key => self['_id'])
end

#authenticatable_saltObject

A reliable way to expose the salt regardless of the implementation.



65
66
67
# File 'lib/couchdb_oauth2/model/account.rb', line 65

def authenticatable_salt
  self.encrypted_password[0,29] if self.encrypted_password
end

#clean_up_passwordsObject



60
61
62
# File 'lib/couchdb_oauth2/model/account.rb', line 60

def clean_up_passwords
  self.password = self.password_confirmation = ""
end

#password=(new_password) ⇒ Object



43
44
45
46
47
# File 'lib/couchdb_oauth2/model/account.rb', line 43

def password=(new_password)
  @password = new_password
  self.pepper = BCrypt::Engine.generate_salt
  self.encrypted_password = password_digest(@password) if @password.present?
end

#password_changed?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/couchdb_oauth2/model/account.rb', line 49

def password_changed?
  self.encrypted_password_changed?
end

#refresh_tokensObject



73
74
75
# File 'lib/couchdb_oauth2/model/account.rb', line 73

def refresh_tokens
  RefreshToken.view(:by_account_id, :key => self['_id'])
end

#valid_password?(password) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
56
57
58
# File 'lib/couchdb_oauth2/model/account.rb', line 53

def valid_password?(password)
  return false if encrypted_password.blank?
  bcrypt   = ::BCrypt::Password.new(self.encrypted_password)
  password = ::BCrypt::Engine.hash_secret("#{password}#{self.pepper}", bcrypt.salt)
  self.class.secure_compare(password, self.encrypted_password)
end