Class: DragonsKeep::Account

Inherits:
Object
  • Object
show all
Includes:
DataMapper::Resource
Defined in:
lib/dragons_keep/account.rb

Overview

Account class to store accounts and passwords into a keeper system

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#password_confirmationObject

Returns the value of attribute password_confirmation.



23
24
25
# File 'lib/dragons_keep/account.rb', line 23

def password_confirmation
  @password_confirmation
end

#unencrypted_passwordObject

Transient storage of unencrypted password and confirmation field



22
23
24
# File 'lib/dragons_keep/account.rb', line 22

def unencrypted_password
  @unencrypted_password
end

Instance Method Details

#after_initializeObject

loading data validate password filed and check status



32
33
34
# File 'lib/dragons_keep/account.rb', line 32

def after_initialize   
  @unencrypted = self.password.nil?
end

#before_createObject

Before create data set salt and encrypt password

Raises:



37
38
39
# File 'lib/dragons_keep/account.rb', line 37

def before_create
  raise PasswordException, "Password is not Encrypted"  if self.new_password? && self.unencrypted?
end

#create_saltObject

Create the salt



91
92
93
# File 'lib/dragons_keep/account.rb', line 91

def create_salt
  self.salt = UUID.generate(:compact)
end

#decrpyt_password(encrypt_pass) ⇒ Object

Decrypt the password encrypt_pass = Given password when user created the account information



58
59
60
61
62
63
64
# File 'lib/dragons_keep/account.rb', line 58

def decrpyt_password(encrypt_pass)
  if !(self.unencrypted?)
    self.unencrypted_password = EzCrypto::Key.decrypt_with_password(encrypt_pass, self.salt, Base64.decode64( self.password))
    @unencrypted = true
    self.password_confirmation=""
  end
end

#encrypt_password(encrypt_pass) ⇒ Object

Encrypt the password encrypt_pass = Given password when user created the account information



48
49
50
51
52
53
54
# File 'lib/dragons_keep/account.rb', line 48

def encrypt_password(encrypt_pass)
  if self.unencrypted? && self.unencrypted_password == self.password_confirmation
    self.create_salt
    self.password = Base64.encode64(EzCrypto::Key.encrypt_with_password(encrypt_pass, self.salt, self.unencrypted_password))
    @unencrypted = false
  end
end

#generate_password(length_of_pass, special_char) ⇒ Object

Generate a random password length_of_password = Length of the password to generate special_char = Can this password need to contain Special Characters



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/dragons_keep/account.rb', line 74

def generate_password( length_of_pass, special_char )
  chars = []
  ("a".."z").each {|ele| chars << ele}
  ("A".."Z").each {|ele| chars << ele}
  ("0".."9").each {|ele| chars << ele}
  if(special_char)
    ["@", "!", "_",].each {|ele| chars << ele}
  end
  newpass = ""
  1.upto(length_of_pass) { |i| newpass << chars[rand(chars.size-1)] }
  #self.password
  self.unencrypted_password = newpass
  self.password_confirmation = newpass
  @unencrypted = true
end

#new_password?Boolean

are we re defining the account password

Returns:

  • (Boolean)


42
43
44
# File 'lib/dragons_keep/account.rb', line 42

def new_password?()
  return !(self.unencrypted_password.blank? and self.password_confirmation.blank?)
end

#unencrypted?Boolean

Is the password encrypted?

Returns:

  • (Boolean)


67
68
69
# File 'lib/dragons_keep/account.rb', line 67

def unencrypted?      
  return (@unencrypted.nil?)? self.password.nil?: @unencrypted
end