Module: Passbox
- Defined in:
- lib/passbox/aes.rb,
lib/passbox/auth.rb,
lib/passbox/init.rb,
lib/passbox/accounts.rb,
lib/passbox/crud/read.rb,
lib/passbox/crud/create.rb,
lib/passbox/crud/delete.rb,
lib/passbox/crud/update.rb,
lib/passbox/helpers/options.rb,
lib/passbox/helpers/strings.rb
Instance Method Summary collapse
- #account_already_exists ⇒ Object
- #account_name_blank ⇒ Object
- #account_name_invalid ⇒ Object
- #account_not_found ⇒ Object
-
#account_update_success ⇒ Object
update.rb.
- #auth_failed ⇒ Object
- #auth_success ⇒ Object
- #cc(acc, key, action = :create) ⇒ Object
- #cc_cvv_blank ⇒ Object
- #cc_exp_blank ⇒ Object
- #cc_no_blank ⇒ Object
- #check_passbox ⇒ Object
- #create_account(option, key) ⇒ Object
- #create_master_password ⇒ Object
- #create_pass ⇒ Object
- #decrypt(datafile, key) ⇒ Object
- #delete_pass ⇒ Object
- #display_add_options ⇒ Object
- #does_account_exists(acc, type) ⇒ Object
- #encrypt(data, key, file) ⇒ Object
- #enter_account_name ⇒ Object
-
#enter_account_password ⇒ Object
auth.rb.
- #enter_account_pin ⇒ Object
- #enter_cc_cvv ⇒ Object
- #enter_cc_exp ⇒ Object
- #enter_cc_no ⇒ Object
- #enter_cc_pin ⇒ Object
- #enter_master_password ⇒ Object
- #enter_note ⇒ Object
- #enter_url ⇒ Object
- #enter_username ⇒ Object
- #fill_manadatory_field(enter_message, blank_message, action = :create, type = nil) ⇒ Object
- #get_password_from_user(action = :account) ⇒ Object
- #init ⇒ Object
- #invalid_password ⇒ Object
- #invalid_selection ⇒ Object
- #list_of_accounts ⇒ Object
- #login(acc, key, action = :create) ⇒ Object
- #multiple_accounts ⇒ Object
-
#no_accounts ⇒ Object
accounts.rb.
- #passbox_auth ⇒ Object
- #password_blank ⇒ Object
- #password_input(action) ⇒ Object
- #password_validation ⇒ Object
- #passwords_mismatch ⇒ Object
-
#pb_already_setup ⇒ Object
init.rb.
- #pb_not_setup ⇒ Object
- #pb_setup_complete ⇒ Object
- #pin(acc, key, action = :create) ⇒ Object
- #pin_blank ⇒ Object
- #re_enter_master_password ⇒ Object
- #read_pass(action = :display) ⇒ Object
- #select_option ⇒ Object
- #thank_you ⇒ Object
-
#too_many_attempts ⇒ Object
create.rb.
- #update_details(existing, filename, key) ⇒ Object
- #update_instructions ⇒ Object
- #update_pass ⇒ Object
- #user_input ⇒ Object
- #username_blank ⇒ Object
- #verify_account ⇒ Object
Instance Method Details
#account_already_exists ⇒ Object
25 |
# File 'lib/passbox/helpers/strings.rb', line 25 def account_already_exists; return "Account Name already exists, try different name!!\n"; end |
#account_name_blank ⇒ Object
3 |
# File 'lib/passbox/helpers/strings.rb', line 3 def account_name_blank; return "Account Name cannot be empty, try again!!\n"; end |
#account_name_invalid ⇒ Object
26 |
# File 'lib/passbox/helpers/strings.rb', line 26 def account_name_invalid; return "Alphabets, Numbers, Underscore and Dashes only, try again please!!\n"; end |
#account_not_found ⇒ Object
18 |
# File 'lib/passbox/helpers/strings.rb', line 18 def account_not_found; return "Account not found, Use 'passbox list' to see all your existing accounts.\n"; end |
#account_update_success ⇒ Object
update.rb
34 |
# File 'lib/passbox/helpers/strings.rb', line 34 def account_update_success; return "\nAccount details has been successfully updated!! \n"; end |
#auth_failed ⇒ Object
51 |
# File 'lib/passbox/helpers/strings.rb', line 51 def auth_failed; return "Authentication Failed!!\n"; end |
#auth_success ⇒ Object
50 |
# File 'lib/passbox/helpers/strings.rb', line 50 def auth_success; return "Authentication Successful!!\n"; end |
#cc(acc, key, action = :create) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/passbox/crud/create.rb', line 80 def cc(acc, key, action = :create) attempts = 0 hash = {} hash["card_number"] = fill_manadatory_field(enter_cc_no, cc_no_blank, action) hash["card_expiry"] = fill_manadatory_field(enter_cc_exp, cc_exp_blank, action) hash["card_cvv"] = fill_manadatory_field("", cc_cvv_blank, action, :cvv) cc_pin = get_password_from_user(:card_pin) hash["card_pin"] = cc_pin unless cc_pin.empty? print enter_note note = user_input hash["note"] = note unless note.empty? return hash if action == :update json = hash.to_json encrypt(json, key, "#{$pbdir}/#{acc}.cc") print "Account #{acc} has been successfully created!! \n\n".green end |
#cc_cvv_blank ⇒ Object
9 |
# File 'lib/passbox/helpers/strings.rb', line 9 def cc_cvv_blank; return "Card CVV cannot be empty, try again!!\n"; end |
#cc_exp_blank ⇒ Object
8 |
# File 'lib/passbox/helpers/strings.rb', line 8 def cc_exp_blank; return "Card Expiry cannot be empty, try again!!\n"; end |
#cc_no_blank ⇒ Object
7 |
# File 'lib/passbox/helpers/strings.rb', line 7 def cc_no_blank; return "Credit/Debit Card cannot be empty, try again!!\n"; end |
#check_passbox ⇒ Object
25 26 27 28 29 30 |
# File 'lib/passbox/init.rb', line 25 def check_passbox if !File.exists?($passfile) print pb_not_setup exit(0) end end |
#create_account(option, key) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/passbox/crud/create.rb', line 20 def create_account(option, key) attempts = 0 while(true) if attempts == 3 print too_many_attempts.bold.red exit(0) end print enter_account_name acc = user_input.downcase if acc.empty? print account_name_blank.red attempts = attempts + 1 next; end if (acc.count("a-z0-9_-") == acc.length) account_exists = does_account_exists(acc, option) if account_exists print account_already_exists.red attempts = attempts + 1 next; end break else print account_name_invalid attempts = attempts + 1 end end return acc end |
#create_master_password ⇒ Object
42 |
# File 'lib/passbox/helpers/strings.rb', line 42 def create_master_password; return "Please create your master password (min 8 chars): "; end |
#create_pass ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/passbox/crud/create.rb', line 3 def create_pass check_passbox option = select_option key = passbox_auth acc = create_account(option, key) case option when 1 login(acc, key) when 2 pin(acc, key) when 3 cc(acc, key) else exit(0) end end |
#decrypt(datafile, key) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/passbox/aes.rb', line 14 def decrypt(datafile, key) file = File.open(datafile, 'rb') data = file.read file.close decipher = OpenSSL::Cipher::AES256.new(:CTR) decipher.decrypt decipher.iv = data[0..15] data = data[16..] decipher.key = key[0..31] decrypted_data = decipher.update(data) + decipher.final return decrypted_data end |
#delete_pass ⇒ Object
3 4 5 6 7 8 9 10 11 |
# File 'lib/passbox/crud/delete.rb', line 3 def delete_pass check_passbox filename = verify_account key = passbox_auth if key File.delete(filename) print "\nYour account #{filename.split("/").last.split(".").first} has been deleted!!\n\n".bold.yellow end end |
#display_add_options ⇒ Object
3 4 5 6 7 8 9 |
# File 'lib/passbox/helpers/options.rb', line 3 def print "\n1. Password" print "\n2. Pin" print "\n3. Debit/Credit Card" print "\nPlease select one of the above options: " return user_input.to_i end |
#does_account_exists(acc, type) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/passbox/accounts.rb', line 63 def does_account_exists(acc, type) files = Dir.glob("#{$pbdir}/#{acc}.{pb,pn,cc}") if files.size == 0 return false else files.each do |file| return true if (file.split(".").last == [type]) end return false end end |
#encrypt(data, key, file) ⇒ Object
3 4 5 6 7 8 9 10 11 12 |
# File 'lib/passbox/aes.rb', line 3 def encrypt(data, key, file) cipher = OpenSSL::Cipher::AES256.new(:CTR) cipher.encrypt $iv = cipher.random_iv cipher.key = key[0..31] encrypted_data = $iv + cipher.update(data) + cipher.final file = File.open(file, 'wb') file.write(encrypted_data) file.close end |
#enter_account_name ⇒ Object
17 |
# File 'lib/passbox/helpers/strings.rb', line 17 def enter_account_name; return "Please enter you account name: "; end |
#enter_account_password ⇒ Object
auth.rb
38 |
# File 'lib/passbox/helpers/strings.rb', line 38 def enter_account_password; return "Please enter your Account Password: "; end |
#enter_account_pin ⇒ Object
39 |
# File 'lib/passbox/helpers/strings.rb', line 39 def enter_account_pin; return "Please enter you Account Pin: "; end |
#enter_cc_cvv ⇒ Object
40 |
# File 'lib/passbox/helpers/strings.rb', line 40 def enter_cc_cvv; return "Please enter you Card CVV: "; end |
#enter_cc_exp ⇒ Object
31 |
# File 'lib/passbox/helpers/strings.rb', line 31 def enter_cc_exp; return "Please enter your card expiry: "; end |
#enter_cc_no ⇒ Object
30 |
# File 'lib/passbox/helpers/strings.rb', line 30 def enter_cc_no; return "Please enter in your credit/debit card number: "; end |
#enter_cc_pin ⇒ Object
41 |
# File 'lib/passbox/helpers/strings.rb', line 41 def enter_cc_pin; return "Please enter you Card Pin: "; end |
#enter_master_password ⇒ Object
47 |
# File 'lib/passbox/helpers/strings.rb', line 47 def enter_master_password; return "Please enter your Master Password: "; end |
#enter_note ⇒ Object
29 |
# File 'lib/passbox/helpers/strings.rb', line 29 def enter_note; return "Enter note to self (optional): "; end |
#enter_url ⇒ Object
28 |
# File 'lib/passbox/helpers/strings.rb', line 28 def enter_url; return "Please enter in the login url: "; end |
#enter_username ⇒ Object
27 |
# File 'lib/passbox/helpers/strings.rb', line 27 def enter_username; return "Please enter in your username: "; end |
#fill_manadatory_field(enter_message, blank_message, action = :create, type = nil) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/passbox/crud/create.rb', line 97 def fill_manadatory_field(, , action=:create, type=nil) attempts = 0 while(true) if attempts == 3 print too_many_attempts.bold.red exit(0) end if (type.nil?) print field = user_input else field = get_password_from_user(type) end if field.empty? if (action == :create) print .red attempts = attempts + 1 next; end end return field end end |
#get_password_from_user(action = :account) ⇒ Object
5 6 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 42 43 44 45 46 47 |
# File 'lib/passbox/auth.rb', line 5 def get_password_from_user(action=:account) case action when :account print enter_account_password return password_input(action) when :pin print enter_account_pin return password_input(action) when :cvv print enter_cc_cvv return password_input(action) when :card_pin print enter_cc_pin return password_input(action) when :master attempt = 0 while(true) attempt = attempt + 1; print create_master_password pass256 = password_input(action) if pass256 print re_enter_master_password re_pass256 = password_input(action) if re_pass256 == pass256 print pb_setup_complete.bold.green return pass256 else print passwords_mismatch.bold.red exit(0); end else print password_validation.red end if attempt == 3 print too_many_attempts.bold.red exit(0) end end when :auth print enter_master_password return password_input(action) end end |
#init ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/passbox/init.rb', line 8 def init pass256="" if (Dir.exists?($pbdir)) if(File.exists?($passfile)) print pb_already_setup return else pass256 = get_password_from_user(:master) end else pass256 = get_password_from_user(:master) Dir.mkdir($pbdir) end encrypt(pass256, pass256, $passfile) end |
#invalid_password ⇒ Object
49 |
# File 'lib/passbox/helpers/strings.rb', line 49 def invalid_password; return "\nInvalid Password!!\n"; end |
#invalid_selection ⇒ Object
20 |
# File 'lib/passbox/helpers/strings.rb', line 20 def invalid_selection; return "\nInvalid selection. Try again. Bye!!\n\n"; end |
#list_of_accounts ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/passbox/accounts.rb', line 4 def list_of_accounts check_passbox empty = true files_ext = Dir["#{$pbdir}/*.pb"] print "\nLogin & Passwords\n".bold.cyan if files_ext.size != 0 files_ext.each_with_index do |file,i| print "#{i+1}. #{file.split('/').last.split('.').first}\n".cyan empty = false end files_ext = Dir["#{$pbdir}/*.pn"] print "\nAccount Pins\n".bold.magenta if files_ext.size != 0 files_ext.each_with_index do |file,i| print "#{i+1}. #{file.split('/').last.split('.').first}\n".magenta empty = false end files_ext = Dir["#{$pbdir}/*.cc"] print "\nCredit & Debit Cards\n".bold.yellow if files_ext.size != 0 files_ext.each_with_index do |file,i| print "#{i+1}. #{file.split('/').last.split('.').first}\n".yellow empty = false end print no_accounts.yellow if empty end |
#login(acc, key, action = :create) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/passbox/crud/create.rb', line 50 def login(acc, key, action=:create) attempts = 0 hash = {} hash["username"] = fill_manadatory_field(enter_username, username_blank, action) hash["password"] = fill_manadatory_field("", password_blank, action, :account) print enter_url url = user_input hash["url"] = url unless url.empty? print enter_note note = user_input hash["note"] = note unless note.empty? return hash if action == :update json = hash.to_json encrypt(json, key, "#{$pbdir}/#{acc}.pb") print "Account #{acc} has been successfully created!! \n\n".green end |
#multiple_accounts ⇒ Object
19 |
# File 'lib/passbox/helpers/strings.rb', line 19 def multiple_accounts; return "\nMultiple accounts found, please chose one: "; end |
#no_accounts ⇒ Object
accounts.rb
16 |
# File 'lib/passbox/helpers/strings.rb', line 16 def no_accounts; return "\nIts all empty here!! Use 'passbox add' to create new account.\n\n"; end |
#passbox_auth ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/passbox/auth.rb', line 74 def passbox_auth pass256User = get_password_from_user(:auth) pass256File = decrypt($passfile, pass256User) if pass256File == pass256User print auth_success.bold.green return pass256File else print auth_failed.bold.red exit(0) end end |
#password_blank ⇒ Object
5 |
# File 'lib/passbox/helpers/strings.rb', line 5 def password_blank; return "Password cannot be empty, try again!!\n"; end |
#password_input(action) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/passbox/auth.rb', line 49 def password_input(action) begin pass = STDIN.noecho(&:gets).chomp rescue Interrupt puts thank_you.cyan exit(0) end case action when :master, :auth if (pass.length < 8) if (action == :master) return false elsif (action == :auth) print invalid_password.bold.red exit(0) end else return Digest::SHA256.hexdigest(pass) end else print("\n") return pass end end |
#password_validation ⇒ Object
46 |
# File 'lib/passbox/helpers/strings.rb', line 46 def password_validation; return "\nPassword should be minimum 8 characters, try again!!\n"; end |
#passwords_mismatch ⇒ Object
45 |
# File 'lib/passbox/helpers/strings.rb', line 45 def passwords_mismatch; return "\n\nPasswords don't match. Try again!!\n\n"; end |
#pb_already_setup ⇒ Object
init.rb
12 |
# File 'lib/passbox/helpers/strings.rb', line 12 def pb_already_setup; return "Your passbox is already setup. Please type 'passbox help' to see usage.\n"; end |
#pb_not_setup ⇒ Object
13 |
# File 'lib/passbox/helpers/strings.rb', line 13 def pb_not_setup; return "Passbox is not setup, use 'passbox init' command to start.\n"; end |
#pb_setup_complete ⇒ Object
44 |
# File 'lib/passbox/helpers/strings.rb', line 44 def pb_setup_complete; return "\n\nPassbox setup complete. Use 'passbox help' to explore.\n\n"; end |
#pin(acc, key, action = :create) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/passbox/crud/create.rb', line 67 def pin(acc, key, action = :create) attempts = 0 hash = {} hash["pin"] = fill_manadatory_field("", pin_blank, action, :pin) print enter_note note = user_input hash["note"] = note unless note.empty? return hash if action == :update json = hash.to_json encrypt(json, key, "#{$pbdir}/#{acc}.pn") print "Account #{acc} has been successfully created!! \n\n".green end |
#pin_blank ⇒ Object
6 |
# File 'lib/passbox/helpers/strings.rb', line 6 def pin_blank; return "Pin cannot be empty, try again!!\n"; end |
#re_enter_master_password ⇒ Object
43 |
# File 'lib/passbox/helpers/strings.rb', line 43 def re_enter_master_password; return "\nPlease re-enter your master password: "; end |
#read_pass(action = :display) ⇒ Object
3 4 5 6 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 |
# File 'lib/passbox/crud/read.rb', line 3 def read_pass(action=:display) check_passbox filename = verify_account key = passbox_auth if key data = JSON.parse(decrypt(filename, key)) return data, filename, key if (action == :update) case filename.split(".").last when "pb" print "\nusername : #{data['username']}\n" print "password : #{data['password']}\n" print "url : #{data['url']}\n" if data['url'] print "note : #{data['note']}\n" if data['note'] print "\n" when "pn" print "\npin : #{data['pin']}\n" print "note : #{data['note']}\n" if data['note'] print "\n" when "cc" print "\ncard number : #{data['card_number']}\n" print "expiry : #{data['card_expiry']}\n" print "cvv : #{data['card_cvv']}\n" if data['card_cvv'] print "card pin : #{data['card_pin']}\n" if data['card_pin'] print "note : #{data['note']}\n" if data['note'] print "\n" end end end |
#select_option ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/passbox/helpers/options.rb', line 11 def select_option attempt = 0 while(true) attempt = attempt + 1 option = if (1..3).include?(option) return option else print "\nInvalid selection. Please try again!!\n".red end if attempt==3 print "\nToo many invalid attempts. Bye!!\n\n".bold.red exit(0) end end end |
#thank_you ⇒ Object
48 |
# File 'lib/passbox/helpers/strings.rb', line 48 def thank_you; return "\n\nThank you for using passbox. Bye!!\n"; end |
#too_many_attempts ⇒ Object
create.rb
23 |
# File 'lib/passbox/helpers/strings.rb', line 23 def too_many_attempts; return "\nToo many attempts. Start again!!\n\n"; end |
#update_details(existing, filename, key) ⇒ Object
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 |
# File 'lib/passbox/crud/update.rb', line 9 def update_details(existing, filename, key) print update_instructions.yellow change_flag = false case filename.split(".").last when "pb" updated = login(nil, key, :update) existing["username"] = updated["username"] unless updated["username"].empty? existing["password"] = updated["password"] unless updated["password"].empty? existing["url"] = updated["url"] unless (updated["url"].nil? || updated["url"].empty?) existing["note"] = updated["note"] unless (updated["note"].nil? || updated["note"].empty?) when "pn" updated = pin(nil, key, :update) existing["pin"] = updated["pin"] unless updated["pin"].empty? existing["note"] = updated["note"] unless (updated["note"].nil? || updated["note"].empty?) when "cc" updated = cc(nil, key, :update) existing["card_number"] = updated["card_number"] unless updated["card_number"].empty? existing["card_expiry"] = updated["card_expiry"] unless updated["card_expiry"].empty? existing["card_cvv"] = updated["card_cvv"] unless updated["card_cvv"].empty? existing["card_pin"] = updated["card_pin"] unless (updated["card_pin"].nil? || updated["card_pin"].empty?) existing["note"] = updated["note"] unless (updated["note"].nil? || updated["note"].empty?) else # do nothing end encrypt(existing.to_json, key, filename) end |
#update_instructions ⇒ Object
35 |
# File 'lib/passbox/helpers/strings.rb', line 35 def update_instructions; return "\nHit enter for no change or input a new value. \n"; end |
#update_pass ⇒ Object
3 4 5 6 7 |
# File 'lib/passbox/crud/update.rb', line 3 def update_pass existing, filename, key = read_pass(:update) update_details(existing, filename, key) print account_update_success.green end |
#user_input ⇒ Object
28 29 30 31 32 33 34 35 |
# File 'lib/passbox/helpers/options.rb', line 28 def user_input begin return gets.chomp rescue Interrupt puts "\n\nThank you for using passbox. Bye!!\n".cyan exit(0) end end |
#username_blank ⇒ Object
4 |
# File 'lib/passbox/helpers/strings.rb', line 4 def username_blank; return "Username cannot be empty, try again!!\n"; end |
#verify_account ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/passbox/accounts.rb', line 28 def verify_account print enter_account_name acc = user_input files = Dir.glob("#{$pbdir}/#{acc}.{pb,pn,cc}") if files.size == 0 print account_not_found exit(0) elsif files.size == 1 return files.first else files.each_with_index do |file, i| filename = file.split("/").last.split(".").first fileext = file.split("/").last.split(".").last case fileext when "pb" print "\n#{i+1}: #{filename} - Login & Password Category" when "pn" print "\n#{i+1}: #{filename} - Account Pins Category" when "cc" print "\n#{i+1}: #{filename} - Credit/Debit Card Category" else # do nothing end end print multiple_accounts option = user_input.to_i if (1..files.size).include?(option) return files[option-1] else print invalid_selection.red exit(0) end end end |