mirror of
https://github.com/nishiki/manage-password.git
synced 2024-11-23 13:57:52 +00:00
add generate gpg key
This commit is contained in:
parent
a0720672bd
commit
f9f06f245c
5 changed files with 94 additions and 5 deletions
|
@ -6,6 +6,7 @@
|
||||||
module MPW
|
module MPW
|
||||||
|
|
||||||
require 'rubygems'
|
require 'rubygems'
|
||||||
|
require 'gpgme'
|
||||||
require 'yaml'
|
require 'yaml'
|
||||||
require 'i18n'
|
require 'i18n'
|
||||||
|
|
||||||
|
@ -96,6 +97,38 @@ module MPW
|
||||||
@error_msg = "#{I18n.t('error.config.write')}\n#{e}"
|
@error_msg = "#{I18n.t('error.config.write')}\n#{e}"
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Setup a new gpg key
|
||||||
|
def setup_gpg_key(password, name, length = 2048, expire = 0)
|
||||||
|
if name.nil? || name.empty?
|
||||||
|
@error_msg = "#{I18n.t('error.config.genkey_gpg.name')}"
|
||||||
|
return false
|
||||||
|
elsif password.nil? || password.empty?
|
||||||
|
@error_msg = "#{I18n.t('error.config.genkey_gpg.password')}"
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
param = ''
|
||||||
|
param << '<GnupgKeyParms format="internal">' + "\n"
|
||||||
|
param << "Key-Type: DSA\n"
|
||||||
|
param << "Key-Length: #{length}\n"
|
||||||
|
param << "Subkey-Type: ELG-E\n"
|
||||||
|
param << "Subkey-Length: #{length}\n"
|
||||||
|
param << "Name-Real: #{name}\n"
|
||||||
|
param << "Name-Comment: #{name}\n"
|
||||||
|
param << "Name-Email: #{@key}\n"
|
||||||
|
param << "Expire-Date: #{expire}\n"
|
||||||
|
param << "Passphrase: apc\n"
|
||||||
|
param << "</GnupgKeyParms>\n"
|
||||||
|
|
||||||
|
ctx = GPGME::Ctx.new
|
||||||
|
ctx.genkey(param, nil, nil)
|
||||||
|
|
||||||
|
return true
|
||||||
|
rescue Exception => e
|
||||||
|
@error_msg = "#{I18n.t('error.config.genkey_gpg.exception')}\n#{e}"
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
# Check the config file
|
# Check the config file
|
||||||
# @rtrn: true if the config file is correct
|
# @rtrn: true if the config file is correct
|
||||||
|
@ -118,14 +151,25 @@ module MPW
|
||||||
@error_msg = I18n.t('error.config.check')
|
@error_msg = I18n.t('error.config.check')
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
I18n.locale = @lang.to_sym
|
I18n.locale = @lang.to_sym
|
||||||
|
|
||||||
return true
|
return true
|
||||||
rescue Exception => e
|
rescue Exception => e
|
||||||
|
puts e
|
||||||
@error_msg = "#{I18n.t('error.config.check')}\n#{e}"
|
@error_msg = "#{I18n.t('error.config.check')}\n#{e}"
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Check if private key exist
|
||||||
|
# @rtrn: true if the key exist, else false
|
||||||
|
def check_gpg_key?
|
||||||
|
ctx = GPGME::Ctx.new
|
||||||
|
ctx.each_key(@key, true) do |key|
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
# Set the last update when there is a sync
|
# Set the last update when there is a sync
|
||||||
# @rtrn: true is the file has been updated
|
# @rtrn: true is the file has been updated
|
||||||
|
|
|
@ -89,11 +89,10 @@ class Cli
|
||||||
sync_path = ask(I18n.t('form.setup.sync_path')).to_s
|
sync_path = ask(I18n.t('form.setup.sync_path')).to_s
|
||||||
end
|
end
|
||||||
|
|
||||||
if !language.nil? && !language.empty?
|
if language.nil? || language.empty?
|
||||||
I18n.locale = language.to_sym
|
language = lang
|
||||||
else
|
|
||||||
language = nil
|
|
||||||
end
|
end
|
||||||
|
I18n.locale = language.to_sym
|
||||||
|
|
||||||
sync_type = sync_type.nil? || sync_type.empty? ? nil : sync_type
|
sync_type = sync_type.nil? || sync_type.empty? ? nil : sync_type
|
||||||
sync_host = sync_host.nil? || sync_host.empty? ? nil : sync_host
|
sync_host = sync_host.nil? || sync_host.empty? ? nil : sync_host
|
||||||
|
@ -115,6 +114,28 @@ class Cli
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Setup a new GPG key
|
||||||
|
def setup_gpg_key
|
||||||
|
puts I18n.t('form.setup_gpg_key.title')
|
||||||
|
puts '--------------------'
|
||||||
|
name = ask(I18n.t('form.setup_gpg_key.name')).to_s
|
||||||
|
password = ask(I18n.t('form.setup_gpg_key.password')).to_s
|
||||||
|
length = ask(I18n.t('form.setup_gpg_key.length')).to_s
|
||||||
|
expire = ask(I18n.t('form.setup_gpg_key.expire')).to_s
|
||||||
|
|
||||||
|
length = length.nil? || length.empty? ? 2048 : length.to_i
|
||||||
|
expire = expire.nil? || expire.empty? ? 0 : expire.to_i
|
||||||
|
|
||||||
|
puts I18n.t('form.setup_gpg_key.wait')
|
||||||
|
|
||||||
|
if @config.setup_gpg_key(password, name, length, expire)
|
||||||
|
puts I18n.t('form.setup_gpg_key.valid')
|
||||||
|
else
|
||||||
|
puts "#{I18n.t('display.error')}: #{@config.error_msg}"
|
||||||
|
exit 2
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
# Request the GPG password and decrypt the file
|
# Request the GPG password and decrypt the file
|
||||||
def decrypt
|
def decrypt
|
||||||
if !defined?(@mpw)
|
if !defined?(@mpw)
|
||||||
|
|
|
@ -5,6 +5,10 @@ en:
|
||||||
write: "Can't write the config file!"
|
write: "Can't write the config file!"
|
||||||
check: "Checkconfig failed!"
|
check: "Checkconfig failed!"
|
||||||
key_bad_format: "The key string isn't in good format!"
|
key_bad_format: "The key string isn't in good format!"
|
||||||
|
genkey_gpg:
|
||||||
|
exception: "Can't create the GPG key!"
|
||||||
|
name: "You must define a name for your GPG key!"
|
||||||
|
password: "You must define a password for your GPG key!"
|
||||||
delete:
|
delete:
|
||||||
id_no_exist: "Can't delete the item %{id}, it doesn't exist!"
|
id_no_exist: "Can't delete the item %{id}, it doesn't exist!"
|
||||||
export:
|
export:
|
||||||
|
@ -77,6 +81,13 @@ en:
|
||||||
sync_pwd: "Password for the synchronization: "
|
sync_pwd: "Password for the synchronization: "
|
||||||
sync_path: "File path for the synchronization : "
|
sync_path: "File path for the synchronization : "
|
||||||
valid: "The config file has been created!"
|
valid: "The config file has been created!"
|
||||||
|
setup_gpg_key:
|
||||||
|
title: "Setup a GPG key"
|
||||||
|
name: "Your name and lastname: "
|
||||||
|
password: "A password for the GPG key: "
|
||||||
|
length: "Size of the GPG key [default=2048]: "
|
||||||
|
expire: "Expire time of the GPG key [default=0 (unlimited)]: "
|
||||||
|
wait: "Please waiting during the GPG key generate, this process can take few minutes."
|
||||||
update:
|
update:
|
||||||
title: "Update an item"
|
title: "Update an item"
|
||||||
name: "Enter the name [%{name}]: "
|
name: "Enter the name [%{name}]: "
|
||||||
|
|
|
@ -5,6 +5,10 @@ fr:
|
||||||
write: "Impossible d'écrire le fichier de configuration!"
|
write: "Impossible d'écrire le fichier de configuration!"
|
||||||
check: "Le fichier de configuration est invalide!"
|
check: "Le fichier de configuration est invalide!"
|
||||||
key_bad_format: "La clé GPG est invalide!"
|
key_bad_format: "La clé GPG est invalide!"
|
||||||
|
genkey_gpg:
|
||||||
|
exception: "La création de la clé GPG n'a pas pu aboutir!"
|
||||||
|
name: "Vous devez définir un nom pour votre clé GPG!"
|
||||||
|
password: "Vous devez définir un mot de passe pour votre clé GPG!"
|
||||||
delete:
|
delete:
|
||||||
id_no_exist: "Impossible de supprimer l'élément %{id}, car il n'existe pas!"
|
id_no_exist: "Impossible de supprimer l'élément %{id}, car il n'existe pas!"
|
||||||
export:
|
export:
|
||||||
|
@ -77,6 +81,13 @@ fr:
|
||||||
sync_pwd: "Mot de passe pour la synchronisation: "
|
sync_pwd: "Mot de passe pour la synchronisation: "
|
||||||
sync_path: "Chemin du fichier pour la synchronisation: "
|
sync_path: "Chemin du fichier pour la synchronisation: "
|
||||||
valid: "Le fichier de configuration a bien été créé!"
|
valid: "Le fichier de configuration a bien été créé!"
|
||||||
|
setup_gpg_key:
|
||||||
|
title: "Configuration d'une clé GPG"
|
||||||
|
name: "Votre nom et prénom: "
|
||||||
|
password: "Mot de passe de la clé GPG: "
|
||||||
|
length: "Taille de la clé GPG [défaut=2048]: "
|
||||||
|
expire: "Expiration de la clé GPG [défaut=0 (illimité)]: "
|
||||||
|
wait: "Veuillez patienter durant la génération de votre clé GPG, ce processus peut prendre quelques minutes."
|
||||||
update:
|
update:
|
||||||
title: "Mis à jour d'un élément"
|
title: "Mis à jour d'un élément"
|
||||||
name: "Entrez le nom [%{name}]: "
|
name: "Entrez le nom [%{name}]: "
|
||||||
|
|
2
mpw
2
mpw
|
@ -117,6 +117,8 @@ cli = Cli.new(lang, config)
|
||||||
# Setup a new config
|
# Setup a new config
|
||||||
if !check_error || !options[:setup].nil?
|
if !check_error || !options[:setup].nil?
|
||||||
cli.setup(lang)
|
cli.setup(lang)
|
||||||
|
elsif !config.check_gpg_key?
|
||||||
|
cli.setup_gpg_key
|
||||||
end
|
end
|
||||||
|
|
||||||
cli.decrypt
|
cli.decrypt
|
||||||
|
|
Loading…
Reference in a new issue