Class: Gloo::Objs::Password

Inherits:
Core::Obj show all
Defined in:
lib/gloo/objs/str_utils/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_container?, #is_function?, #msg_blank?, #msg_contains?, #msg_reload, #msg_responds_to?, #msg_unload, #multiline_value?, #pn, #remove_child, #render, #root?, #send_message, #set_parent, #set_value, #sql_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

.doc_dataObject

Get the object's documentation data.



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/gloo/objs/str_utils/password.rb', line 169

def self.doc_data
  {
    :name => KEYWORD,
    :shortcut => KEYWORD_SHORT,
    :description => 'A password object that can be generated, hashed, and compared.',
    :children => [
      'salt (string) — Optional salt to use when hashing and checking passwords.',
      'password (string) — The password. generate puts a new random password here. Used in the hash and check messages.',
      'hash (string) — The hashed value of the password. Created when the hash message is called, and compared when the check message is called.'
    ],
    :messages => [
      'generate ({len}) — Generate a new password, put in the password child object. The {len} parameter is optional; the length is 7 if not specified.',
      'hash — Hash the password in the child object. Put it in the hash child object.',
      'check — Check the password in the child object. Is it the same as the hash?'
    ],
    :examples => <<~EXAMPLES.strip
      #
      # Password Hashing and checking.
      #
      password [can] :

        pwd [password] :
          salt [string] : MySalt
          password [string] :
          hash [string] :

        on_load [script] :
          show
          show 'Generating a new password' (white)
          tell ^.pwd to generate
          show 'Password: ' + ^.pwd.password
          show
          show 'hashing the password' (white)
          tell ^.pwd to hash
          show ^.pwd.hash (red)
          show
          show 'checking with the correct password' (white)
          tell ^.pwd to check
          show 'Result of check: ' + it (green)
          show
          show 'checking with the wrong password' (white)
          tell ^.pwd to generate(23)
          tell ^.pwd to check
          show 'Result of check: ' + it (red)
          show
    EXAMPLES
  }
end

.messagesObject

Get a list of message names that this object receives.



120
121
122
# File 'lib/gloo/objs/str_utils/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/str_utils/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/str_utils/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/str_utils/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/str_utils/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/str_utils/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/str_utils/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/str_utils/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/str_utils/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/str_utils/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/str_utils/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/str_utils/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/str_utils/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/str_utils/password.rb', line 56

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

  o.set_value new_pwd
end