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
*.gem
.yardoc
doc

View file

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

View file

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

View file

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