Class: Gloo::Objs::Password

Inherits:
Core::Obj show all
Defined in:
lib/gloo/objs/security/password.rb

Constant Summary collapse

KEYWORD =
'password'.freeze
KEYWORD_SHORT =
'hash'.freeze
SALT =
'salt'.freeze
PASSWORD =
'password'.freeze
HASH =
'hash'.freeze

Constants inherited from Core::Baseo

Core::Baseo::NOT_IMPLEMENTED_ERR

Instance Attribute Summary

Attributes inherited from Core::Obj

#children, #parent, #value

Attributes inherited from Core::Baseo

#name

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Core::Obj

#add_child, can_create?, #can_receive_message?, #child_count, #child_index, #contains_child?, #delete_children, #dispatch, #display_value, #find_add_child, #find_child, #find_child_resolve_alias, #find_child_value, help, inherited, #initialize, #is_alias?, #is_function?, #msg_blank?, #msg_contains?, #msg_reload, #msg_unload, #multiline_value?, #pn, #remove_child, #render, #root?, #send_message, #set_parent, #set_value, #type_display, #value_display, #value_is_array?, #value_is_blank?, #value_string?

Methods inherited from Core::Baseo

#initialize, #type_display

Constructor Details

This class inherits a constructor from Gloo::Core::Obj

Class Method Details

.messagesObject

Get a list of message names that this object receives.



120
121
122
# File 'lib/gloo/objs/security/password.rb', line 120

def self.messages
  return super + %w[hash check generate]
end

.short_typenameObject

The short name of the object type.



33
34
35
# File 'lib/gloo/objs/security/password.rb', line 33

def self.short_typename
  return KEYWORD_SHORT
end

.typenameObject

The name of the object type.



26
27
28
# File 'lib/gloo/objs/security/password.rb', line 26

def self.typename
  return KEYWORD
end

Instance Method Details

#add_children_on_create?Boolean

Does this object have children to add when an object is created in interactive mode? This does not apply during obj load, etc.

Returns:



97
98
99
# File 'lib/gloo/objs/security/password.rb', line 97

def add_children_on_create?
  return true
end

#add_default_childrenObject

Add children to this object. This is used by containers to add children needed for default configurations.



106
107
108
109
110
111
# File 'lib/gloo/objs/security/password.rb', line 106

def add_default_children
  fac = @engine.factory
  fac.create_string SALT, '', self
  fac.create_string PASSWORD, '', self
  fac.create_string HASH, '', self
end

#hashObject

Get the hashed password value. Returns nil if there is none.



74
75
76
# File 'lib/gloo/objs/security/password.rb', line 74

def hash
  return find_child_value HASH
end

#msg_checkObject

Check the password against the hash. Uses the salt and the hash to check the password.



156
157
158
159
160
# File 'lib/gloo/objs/security/password.rb', line 156

def msg_check
  hashed_pwd = BCrypt::Password.new( hash )
  result = ( hashed_pwd == salt_pwd )
  @engine.heap.it.set_to result
end

#msg_generateObject

Generate a random alphanumeric password. By default the length is 7 characters. Set the length with an optional parameter.



129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/gloo/objs/security/password.rb', line 129

def msg_generate
  len = 7
  if @params&.token_count&.positive?
    expr = Gloo::Expr::Expression.new( @engine, @params.tokens )
    data = expr.evaluate
    len = data.to_i
  end

  s = StringGenerator.alphanumeric( len )
  update_password s
  @engine.heap.it.set_to s
  return s
end

#msg_hashObject

Hash the password with the salt. Uses the salt and the password to create a hash.



147
148
149
150
# File 'lib/gloo/objs/security/password.rb', line 147

def msg_hash
  hashed_pwd = BCrypt::Password.create( salt_pwd )
  update_hash hashed_pwd
end

#passwordObject

Get the password value. Returns nil if there is none.



49
50
51
# File 'lib/gloo/objs/security/password.rb', line 49

def password
  return find_child_value PASSWORD
end

#saltObject

Get the password salt. Returns nil if there is none.



41
42
43
# File 'lib/gloo/objs/security/password.rb', line 41

def salt
  return find_child_value SALT
end

#salt_pwdObject

Get the salted password.



66
67
68
# File 'lib/gloo/objs/security/password.rb', line 66

def salt_pwd
  return "#{salt}#{password}"
end

#update_hash(new_hash) ⇒ Object

Update the hashed password value.



81
82
83
84
85
86
# File 'lib/gloo/objs/security/password.rb', line 81

def update_hash( new_hash )
  o = find_child_resolve_alias HASH
  return unless o

  o.set_value new_hash
end

#update_password(new_pwd) ⇒ Object

Update the password value.



56
57
58
59
60
61
# File 'lib/gloo/objs/security/password.rb', line 56

def update_password( new_pwd )
  o = find_child_resolve_alias PASSWORD
  return unless o

  o.set_value new_pwd
end