2022-12-21 19:24:54 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# Ensure the passwords are secure enough
|
2022-07-29 17:37:42 +02:00
|
|
|
class SecurePassword
|
|
|
|
LOWER_LETTERS = ('a'..'z').to_a
|
|
|
|
UPPER_LETTERS = ('A'..'Z').to_a
|
|
|
|
DIGITS = ('0'..'9').to_a
|
2022-12-21 19:24:54 +01:00
|
|
|
SPECIAL_CHARS = ['!', '#', '$', '%', '&', '(', ')', '*', '+', ',', '-', '.', '/', ':', ';', '<', '=', '>', '?', '@', '[', ']', '^', '_', '{',
|
|
|
|
'|', '}', '~', "'", '`', '"'].freeze
|
2022-07-29 17:37:42 +02:00
|
|
|
|
|
|
|
def self.generate
|
2022-12-21 19:24:54 +01:00
|
|
|
(LOWER_LETTERS.sample(4) + UPPER_LETTERS.sample(4) + DIGITS.sample(4) + SPECIAL_CHARS.sample(4)).shuffle.join
|
2022-07-29 17:37:42 +02:00
|
|
|
end
|
|
|
|
|
2022-12-21 19:24:54 +01:00
|
|
|
def self.secured?(password)
|
|
|
|
password_as_array = password.chars
|
|
|
|
password_as_array.any? { |c| c.in? LOWER_LETTERS } &&
|
|
|
|
password_as_array.any? { |c| c.in? UPPER_LETTERS } &&
|
|
|
|
password_as_array.any? { |c| c.in? DIGITS } &&
|
|
|
|
password_as_array.any? { |c| c.in? SPECIAL_CHARS }
|
2022-07-29 17:37:42 +02:00
|
|
|
end
|
2022-12-21 19:24:54 +01:00
|
|
|
end
|