1
0
Fork 0
mirror of https://github.com/nishiki/manage-password.git synced 2024-11-22 21:37:52 +00:00

feat: comment the code with yard

This commit is contained in:
Adrien Waksberg 2017-05-03 22:53:56 +02:00
parent 96380d3d93
commit a7a165bca9
5 changed files with 81 additions and 80 deletions

2
.gitignore vendored
View file

@ -1,2 +1,4 @@
Gemfile.lock Gemfile.lock
*.gem *.gem
.yardoc
doc

View file

@ -28,14 +28,13 @@ require 'mpw/mpw'
module MPW module MPW
class Cli class Cli
# Constructor # @param config [Config]
# @args: config -> the config
def initialize(config) def initialize(config)
@config = config @config = config
end end
# Change a parameter int the config after init # Change a parameter int the config after init
# @args: options -> param to change # @param options [Hash] param to change
def set_config(options) def set_config(options)
@config.setup(options) @config.setup(options)
@ -46,7 +45,7 @@ module MPW
end end
# Change the wallet path # Change the wallet path
# @args: path -> the new path # @param path [String] new path
def set_wallet_path(path) def set_wallet_path(path)
@config.set_wallet_path(path, @wallet) @config.set_wallet_path(path, @wallet)
@ -57,7 +56,7 @@ module MPW
end end
# Create a new config file # Create a new config file
# @args: options -> set param # @param options [Hash]
def setup(options) def setup(options)
options[:lang] = options[:lang] || Locale::Tag.parse(ENV['LANG']).to_simple.to_s[0..1] options[:lang] = options[:lang] || Locale::Tag.parse(ENV['LANG']).to_simple.to_s[0..1]
@ -74,7 +73,7 @@ module MPW
end end
# Setup a new GPG key # Setup a new GPG key
# @args: gpg_key -> the key name # @param gpg_key [String] gpg key name
def setup_gpg_key(gpg_key) def setup_gpg_key(gpg_key)
return if @config.check_gpg_key? return if @config.check_gpg_key?
@ -139,8 +138,8 @@ module MPW
end end
# Format list on a table # Format list on a table
# @args: title -> the name of table # @param title [String] name of table
# list -> array or hash # @param list an array or hash
def table_list(title, list) def table_list(title, list)
length = { k: 0, v: 0 } length = { k: 0, v: 0 }
@ -172,7 +171,7 @@ module MPW
end end
# Format items on a table # Format items on a table
# @args: items -> an aray items # @param items [Array]
def table_items(items = []) def table_items(items = [])
group = '.' group = '.'
i = 1 i = 1
@ -252,7 +251,7 @@ module MPW
end end
# Display the query's result # Display the query's result
# @args: options -> the option to search # @param options [Hash] the options to search
def list(**options) def list(**options)
result = @mpw.list(options) result = @mpw.list(options)
@ -264,8 +263,8 @@ module MPW
end end
# Get an item when multiple choice # Get an item when multiple choice
# @args: items -> array of items # @param items [Array] list of items
# @rtrn: item # @return item [Item]
def get_item(items) def get_item(items)
return items[0] if items.length == 1 return items[0] if items.length == 1
@ -276,8 +275,8 @@ module MPW
end end
# Copy in clipboard the login and password # Copy in clipboard the login and password
# @args: item -> the item # @param item [Item]
# clipboard -> enable clipboard # @param clipboard [Boolean] enable clipboard
def clipboard(item, clipboard = true) def clipboard(item, clipboard = true)
# Security: force quit after 90s # Security: force quit after 90s
Thread.new do Thread.new do
@ -350,7 +349,7 @@ module MPW
end end
# Display the wallet # Display the wallet
# @args: wallet -> the wallet name # @param wallet [String] wallet name
def get_wallet(wallet = nil) def get_wallet(wallet = nil)
@wallet = @wallet =
if wallet.to_s.empty? if wallet.to_s.empty?
@ -375,7 +374,7 @@ module MPW
end end
# Add a new public key # Add a new public key
# args: key -> the key name or key file to add # @param key [String] key name or key file to add
def add_key(key) def add_key(key)
@mpw.add_key(key) @mpw.add_key(key)
@mpw.write_data @mpw.write_data
@ -386,7 +385,7 @@ module MPW
end end
# Add new public key # Add new public key
# args: key -> the key name to delete # @param key [String] key name to delete
def delete_key(key) def delete_key(key)
@mpw.delete_key(key) @mpw.delete_key(key)
@mpw.write_data @mpw.write_data
@ -397,10 +396,10 @@ module MPW
end end
# Text editor interface # Text editor interface
# @args: template -> template name # @param template [String] template name
# item -> the item to edit # @param item [Item] the item to edit
# password -> disable field password # @param password [Boolean] disable field password
# @rtrn: a hash with the value for an item # @return [Hash] the values for an item
def text_editor(template_name, password = false, item = nil, **options) def text_editor(template_name, password = false, item = nil, **options)
editor = ENV['EDITOR'] || 'nano' editor = ENV['EDITOR'] || 'nano'
opts = {} opts = {}
@ -429,9 +428,9 @@ module MPW
end end
# Form to add a new item # Form to add a new item
# @args: password -> generate a random password # @param password [Boolean] generate a random password
# text_editor -> enable text editor mode # @param text_editor [Boolean] enable text editor mode
# values -> hash with multiples value to set the item # @param values [Hash] multiples value to set the item
def add(password = false, text_editor = false, **values) def add(password = false, text_editor = false, **values)
options = text_editor('add_form', password, nil, values) if text_editor options = text_editor('add_form', password, nil, values) if text_editor
item = Item.new(options) item = Item.new(options)
@ -448,10 +447,10 @@ module MPW
end end
# Update an item # Update an item
# @args: password -> generate a random password # @param password [Boolean] generate a random password
# text_editor -> enable text editor mode # @param text_editor [Boolean] enable text editor mode
# options -> the option to search # @param options [Hash] the options to search
# values -> hash with multiples value to set the item # @param values [Hash] multiples value to set the item
def update(password = false, text_editor = false, options = {}, **values) def update(password = false, text_editor = false, options = {}, **values)
items = @mpw.list(options) items = @mpw.list(options)
@ -476,7 +475,7 @@ module MPW
end end
# Remove an item # Remove an item
# @args: options -> the option to search # @param options [Hash] the options to search
def delete(**options) def delete(**options)
items = @mpw.list(options) items = @mpw.list(options)
@ -500,8 +499,8 @@ module MPW
end end
# Copy a password, otp, login # Copy a password, otp, login
# @args: clipboard -> enable clipboard # @param clipboard [Boolean] enable clipboard
# options -> the option to search # @param options [Hash] the options to search
def copy(clipboard = true, **options) def copy(clipboard = true, **options)
items = @mpw.list(options) items = @mpw.list(options)
@ -517,9 +516,9 @@ module MPW
puts "#{I18n.t('display.error')} #14: #{e}".red puts "#{I18n.t('display.error')} #14: #{e}".red
end end
# Export the items in a CSV file # Export the items in an yaml file
# @args: file -> the destination file # @param file [String] the path of destination file
# options -> option to search # @param options [Hash] options to search
def export(file, options) def export(file, options)
file = 'export-mpw.yml' if file.to_s.empty? file = 'export-mpw.yml' if file.to_s.empty?
items = @mpw.list(options) items = @mpw.list(options)
@ -549,8 +548,8 @@ module MPW
puts "#{I18n.t('display.error')} #17: #{e}".red puts "#{I18n.t('display.error')} #17: #{e}".red
end end
# Import items from a YAML file # Import items from an yaml file
# @args: file -> the import file # @param file [String] path of import file
def import(file) def import(file)
raise I18n.t('form.import.file_empty') if file.to_s.empty? raise I18n.t('form.import.file_empty') if file.to_s.empty?
raise I18n.t('form.import.file_not_exist') unless File.exist?(file) raise I18n.t('form.import.file_not_exist') unless File.exist?(file)

View file

@ -35,8 +35,7 @@ module MPW
attr_accessor :password attr_accessor :password
attr_accessor :pinmode attr_accessor :pinmode
# Constructor # @param config_file [String] path of config file
# @args: config_file -> the specify config file
def initialize(config_file = nil) def initialize(config_file = nil)
@config_file = config_file @config_file = config_file
@config_dir = @config_dir =
@ -52,8 +51,7 @@ module MPW
end end
# Create a new config file # Create a new config file
# @args: options -> hash with values # @param options [Hash] the value to set the config file
# @rtrn: true if le config file is create
def setup(**options) def setup(**options)
gpg_key = options[:gpg_key] || @gpg_key gpg_key = options[:gpg_key] || @gpg_key
lang = options[:lang] || @lang lang = options[:lang] || @lang
@ -99,11 +97,10 @@ module MPW
end end
# Setup a new gpg key # Setup a new gpg key
# @args: password -> the GPG key password # @param password [String] gpg key password
# name -> the name of user # @param name [String] the name of user
# length -> length of the GPG key # @param length [Integer] length of the gpg key
# expire -> the time of expire to GPG key # @param expire [Integer] time of expire to gpg key
# @rtrn: true if the GPG key is create, else false
def setup_gpg_key(password, name, length = 4096, expire = 0) def setup_gpg_key(password, name, length = 4096, expire = 0)
raise I18n.t('error.config.genkey_gpg.name') if name.to_s.empty? raise I18n.t('error.config.genkey_gpg.name') if name.to_s.empty?
raise I18n.t('error.config.genkey_gpg.password') if password.to_s.empty? raise I18n.t('error.config.genkey_gpg.password') if password.to_s.empty?
@ -147,7 +144,7 @@ module MPW
end end
# Check if private key exist # Check if private key exist
# @rtrn: true if the key exist, else false # @return [Boolean] true if the key exist, else false
def check_gpg_key? def check_gpg_key?
ctx = GPGME::Ctx.new ctx = GPGME::Ctx.new
ctx.each_key(@gpg_key, true) do ctx.each_key(@gpg_key, true) do
@ -158,8 +155,8 @@ module MPW
end end
# Change the path of one wallet # Change the path of one wallet
# @args: path -> the new directory path # @param path [String]new directory path
# wallet -> the wallet name # @param wallet [String] wallet name
def set_wallet_path(path, wallet) def set_wallet_path(path, wallet)
path = @wallet_dir if path == 'default' path = @wallet_dir if path == 'default'

View file

@ -31,10 +31,7 @@ module MPW
attr_accessor :last_edit attr_accessor :last_edit
attr_accessor :created attr_accessor :created
# Constructor # @param options [Hash] the option :host is required
# Create a new item
# @args: options -> a hash of parameter
# raise an error if the hash hasn't the key name
def initialize(**options) def initialize(**options)
if !options.key?(:host) || options[:host].to_s.empty? if !options.key?(:host) || options[:host].to_s.empty?
raise I18n.t('error.update.host_empty') raise I18n.t('error.update.host_empty')
@ -54,7 +51,7 @@ module MPW
end end
# Update the item # Update the item
# @args: options -> a hash of parameter # @param options [Hash]
def update(**options) def update(**options)
if options.key?(:host) && options[:host].to_s.empty? if options.key?(:host) && options[:host].to_s.empty?
raise I18n.t('error.update.host_empty') raise I18n.t('error.update.host_empty')
@ -95,6 +92,7 @@ module MPW
private private
# Generate an random id # Generate an random id
# @return [String] random string
def generate_id def generate_id
[*('A'..'Z'), *('a'..'z'), *('0'..'9')].sample(16).join [*('A'..'Z'), *('a'..'z'), *('0'..'9')].sample(16).join
end end

View file

@ -25,7 +25,11 @@ require 'mpw/item'
module MPW module MPW
class MPW class MPW
# Constructor # @param key [String] gpg key name
# @param wallet_file [String] path of the wallet file
# @param gpg_pass [String] password of the gpg key
# @param gpg_exe [String] path of the gpg executable
# @param pinmode [Boolean] enable the gpg pinmode
def initialize(key, wallet_file, gpg_pass = nil, gpg_exe = nil, pinmode = false) def initialize(key, wallet_file, gpg_pass = nil, gpg_exe = nil, pinmode = false)
@key = key @key = key
@gpg_pass = gpg_pass @gpg_pass = gpg_pass
@ -98,7 +102,7 @@ module MPW
raise "#{I18n.t('error.mpw_file.read_data')}\n#{e}" raise "#{I18n.t('error.mpw_file.read_data')}\n#{e}"
end end
# Encrypt a file # Encrypt all data in tarball
def write_data def write_data
data = {} data = {}
tmp_file = "#{@wallet_file}.tmp" tmp_file = "#{@wallet_file}.tmp"
@ -154,7 +158,7 @@ module MPW
end end
# Get a password # Get a password
# args: id -> the item id # @param id [String] the item id
def get_password(id) def get_password(id)
password = decrypt(@passwords[id]) password = decrypt(@passwords[id])
@ -165,9 +169,9 @@ module MPW
end end
end end
# Set a password # Set a new password for an item
# args: id -> the item id # @param id [String] the item id
# password -> the new password # @param password [String] the new password
def set_password(id, password) def set_password(id, password)
salt = MPW.password(length: Random.rand(4..9)) salt = MPW.password(length: Random.rand(4..9))
password = "$#{salt}::#{password}" password = "$#{salt}::#{password}"
@ -176,13 +180,13 @@ module MPW
end end
# Return the list of all gpg keys # Return the list of all gpg keys
# rtrn: an array with the gpg keys name # @return [Array] the gpg keys name
def list_keys def list_keys
@keys.keys @keys.keys
end end
# Add a public key # Add a public key
# args: key -> new public key file or name # @param key [String] new public key file or name
def add_key(key) def add_key(key)
if File.exist?(key) if File.exist?(key)
data = File.open(key).read data = File.open(key).read
@ -200,7 +204,7 @@ module MPW
end end
# Delete a public key # Delete a public key
# args: key -> public key to delete # @param key [String] public key to delete
def delete_key(key) def delete_key(key)
@keys.delete(key) @keys.delete(key)
@passwords.each_key { |id| set_password(id, get_password(id)) } @passwords.each_key { |id| set_password(id, get_password(id)) }
@ -208,7 +212,7 @@ module MPW
end end
# Add a new item # Add a new item
# @args: item -> Object MPW::Item # @param item [Item]
def add(item) def add(item)
raise I18n.t('error.bad_class') unless item.instance_of?(Item) raise I18n.t('error.bad_class') unless item.instance_of?(Item)
raise I18n.t('error.empty') if item.empty? raise I18n.t('error.empty') if item.empty?
@ -217,8 +221,8 @@ module MPW
end end
# Search in some csv data # Search in some csv data
# @args: options -> a hash with paramaters # @params options [Hash]
# @rtrn: a list with the resultat of the search # @return [Array] a list with the resultat of the search
def list(**options) def list(**options)
result = [] result = []
@ -240,9 +244,9 @@ module MPW
result result
end end
# Search in some csv data # Search an item with an id
# @args: id -> the id item # @param id [String]the id item
# @rtrn: a row with the result of the search # @return [Item] an item or nil
def search_by_id(id) def search_by_id(id)
@data.each do |item| @data.each do |item|
return item if item.id == id return item if item.id == id
@ -251,36 +255,35 @@ module MPW
nil nil
end end
# Set an opt key # Set a new opt key
# args: id -> the item id # @param id [String] the item id
# key -> the new key # @param key [String] the new key
def set_otp_key(id, key) def set_otp_key(id, key)
@otp_keys[id] = encrypt(key.to_s) unless key.to_s.empty? @otp_keys[id] = encrypt(key.to_s) unless key.to_s.empty?
end end
# Get an opt key # Get an opt key
# args: id -> the item id # @param id [String] the item id
# key -> the new key
def get_otp_key(id) def get_otp_key(id)
@otp_keys.key?(id) ? decrypt(@otp_keys[id]) : nil @otp_keys.key?(id) ? decrypt(@otp_keys[id]) : nil
end end
# Get an otp code # Get an otp code
# @args: id -> the item id # @param id [String] the item id
# @rtrn: an otp code # @return [String] an otp code
def get_otp_code(id) def get_otp_code(id)
@otp_keys.key?(id) ? 0 : ROTP::TOTP.new(decrypt(@otp_keys[id])).now @otp_keys.key?(id) ? 0 : ROTP::TOTP.new(decrypt(@otp_keys[id])).now
end end
# Get remaining time before expire otp code # Get remaining time before expire otp code
# @rtrn: return time in seconde # @return [Integer] time in seconde
def get_otp_remaining_time def get_otp_remaining_time
(Time.now.utc.to_i / 30 + 1) * 30 - Time.now.utc.to_i (Time.now.utc.to_i / 30 + 1) * 30 - Time.now.utc.to_i
end end
# Generate a random password # Generate a random password
# @args: options -> :length, :special, :alpha, :numeric # @param options [Hash] :length, :special, :alpha, :numeric
# @rtrn: a random string # @return [String] a random string
def self.password(**options) def self.password(**options)
length = length =
if !options.include?(:length) || options[:length].to_i <= 0 if !options.include?(:length) || options[:length].to_i <= 0
@ -310,7 +313,8 @@ module MPW
private private
# Decrypt a gpg file # Decrypt a gpg file
# @args: data -> string to decrypt # @param data [String] data to decrypt
# @return [String] data decrypted
def decrypt(data) def decrypt(data)
return nil if data.to_s.empty? return nil if data.to_s.empty?
@ -331,7 +335,8 @@ module MPW
end end
# Encrypt a file # Encrypt a file
# args: data -> string to encrypt # @param data [String] data to encrypt
# @return [String] data encrypted
def encrypt(data) def encrypt(data)
recipients = [] recipients = []
crypto = GPGME::Crypto.new(armor: true, always_trust: true) crypto = GPGME::Crypto.new(armor: true, always_trust: true)