1
0
Fork 0
mirror of https://github.com/nishiki/manage-password.git synced 2024-11-23 13:57:52 +00:00

rename function

This commit is contained in:
nishiki 2014-01-26 15:09:48 +01:00
parent 174a028238
commit 1cabe202e7
6 changed files with 27 additions and 32 deletions

View file

@ -23,8 +23,8 @@ class Cli
@config = config @config = config
end end
# Destructor # Close sync
def finalize() def sync_close()
@sync.close() @sync.close()
end end
@ -47,7 +47,7 @@ class Cli
puts "#{I18n.t('display.error')}: #{@mpw.error_msg}" puts "#{I18n.t('display.error')}: #{@mpw.error_msg}"
elsif !@sync.update(File.open(@config.file_gpg).read) elsif !@sync.update(File.open(@config.file_gpg).read)
puts "#{I18n.t('display.error')}: #{@sync.error_msg}" puts "#{I18n.t('display.error')}: #{@sync.error_msg}"
elsif !@config.setLastUpdate() elsif !@config.set_last_update()
puts "#{I18n.t('display.error')}: #{@config.error_msg}" puts "#{I18n.t('display.error')}: #{@config.error_msg}"
else else
return true return true
@ -188,7 +188,7 @@ class Cli
# Update an item # Update an item
# @args: id -> the item's id # @args: id -> the item's id
def update(id) def update(id)
row = @mpw.searchById(id) row = @mpw.search_by_id(id)
if not row.empty? if not row.empty?
puts I18n.t('form.update.title') puts I18n.t('form.update.title')
@ -222,7 +222,7 @@ class Cli
# force -> no resquest a validation # force -> no resquest a validation
def remove(id, force=false) def remove(id, force=false)
if not force if not force
result = @mpw.searchById(id) result = @mpw.search_by_id(id)
if result.length > 0 if result.length > 0
displayFormat(result) displayFormat(result)
@ -265,7 +265,7 @@ class Cli
# @args: file -> the import file # @args: file -> the import file
# force -> no resquest a validation # force -> no resquest a validation
def import(file, force=false) def import(file, force=false)
result = @mpw.importPreview(file) result = @mpw.import_preview(file)
if not force if not force
if result.is_a?(Array) && !result.empty? if result.is_a?(Array) && !result.empty?

View file

@ -105,7 +105,7 @@ class MPW
# Search in some csv data # Search in some csv data
# @args: id -> the id item # @args: id -> the id item
# @rtrn: a row with the resultat of the search # @rtrn: a row with the resultat of the search
def searchById(id) def search_by_id(id)
@data.each do |row| @data.each do |row|
if row[ID] == id if row[ID] == id
return row return row
@ -243,7 +243,7 @@ class MPW
# Return a preview import # Return a preview import
# @args: file -> path to file import # @args: file -> path to file import
# @rtrn: an array with the items to import, if there is an error return false # @rtrn: an array with the items to import, if there is an error return false
def importPreview(file) def import_preview(file)
begin begin
result = Array.new() result = Array.new()
id = 0 id = 0

View file

@ -110,7 +110,7 @@ class MPWConfig
# 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
def setLastUpdate() def set_last_update()
config = {'config' => {'key' => @key, config = {'config' => {'key' => @key,
'lang' => @lang, 'lang' => @lang,
'file_gpg' => @file_gpg, 'file_gpg' => @file_gpg,

View file

@ -9,11 +9,6 @@ require 'logger'
require "#{APP_ROOT}/lib/MPW.rb" require "#{APP_ROOT}/lib/MPW.rb"
class Server class Server
INFO = 0
WARNING = 1
ERROR = 2
DEBUG = 3
attr_accessor :error_msg attr_accessor :error_msg
@ -37,37 +32,37 @@ class Server
@log.info("#{client.peeraddr[3]} is connected") @log.info("#{client.peeraddr[3]} is connected")
while true do while true do
msg = getClientMessage(client) msg = get_client_msg(client)
if !msg if !msg
next next
end end
if msg['gpg_key'].nil? || msg['gpg_key'].empty? || msg['password'].nil? || msg['password'].empty? if msg['gpg_key'].nil? || msg['gpg_key'].empty? || msg['password'].nil? || msg['password'].empty?
closeConnection(client) close_connection(client)
next next
end end
case msg['action'] case msg['action']
when 'get' when 'get'
@log.debug("#{client.peeraddr[3]} GET gpg_key=#{msg['gpg_key']} suffix=#{msg['suffix']}") @log.debug("#{client.peeraddr[3]} GET gpg_key=#{msg['gpg_key']} suffix=#{msg['suffix']}")
client.puts getFile(msg) client.puts get_file(msg)
when 'update' when 'update'
@log.debug("#{client.peeraddr[3]} UPDATE gpg_key=#{msg['gpg_key']} suffix=#{msg['suffix']}") @log.debug("#{client.peeraddr[3]} UPDATE gpg_key=#{msg['gpg_key']} suffix=#{msg['suffix']}")
client.puts updateFile(msg) client.puts update_file(msg)
when 'delete' when 'delete'
@log.debug("#{client.peeraddr[3]} DELETE gpg_key=#{msg['gpg_key']} suffix=#{msg['suffix']}") @log.debug("#{client.peeraddr[3]} DELETE gpg_key=#{msg['gpg_key']} suffix=#{msg['suffix']}")
client.puts deleteFile(msg) client.puts delete_file(msg)
when 'close' when 'close'
@log.info("#{client.peeraddr[3]} is disconnected") @log.info("#{client.peeraddr[3]} is disconnected")
closeConnection(client) close_connection(client)
else else
@log.warning("#{client.peeraddr[3]} is disconnected for unkwnow command") @log.warning("#{client.peeraddr[3]} is disconnected for unkwnow command")
send_msg = {:action => 'unknown', send_msg = {:action => 'unknown',
:gpg_key => msg['gpg_key'], :gpg_key => msg['gpg_key'],
:error => 'server.error.client.unknown'} :error => 'server.error.client.unknown'}
client.puts send_msg client.puts send_msg
closeConnection(client) close_connection(client)
end end
end end
end end
@ -77,7 +72,7 @@ class Server
# Get a gpg file # Get a gpg file
# @args: msg -> message puts by the client # @args: msg -> message puts by the client
# @rtrn: json message # @rtrn: json message
def getFile(msg) def get_file(msg)
gpg_key = msg['gpg_key'].sub('@', '_') gpg_key = msg['gpg_key'].sub('@', '_')
if msg['suffix'].nil? || msg['suffix'].empty? if msg['suffix'].nil? || msg['suffix'].empty?
@ -92,7 +87,7 @@ class Server
hash = gpg_data['gpg']['hash'] hash = gpg_data['gpg']['hash']
data = gpg_data['gpg']['data'] data = gpg_data['gpg']['data']
if isAuthorized?(msg['password'], salt, hash) if is_authorized?(msg['password'], salt, hash)
send_msg = {:action => 'get', send_msg = {:action => 'get',
:gpg_key => msg['gpg_key'], :gpg_key => msg['gpg_key'],
:data => data, :data => data,
@ -115,7 +110,7 @@ class Server
# Update a file # Update a file
# @args: msg -> message puts by the client # @args: msg -> message puts by the client
# @rtrn: json message # @rtrn: json message
def updateFile(msg) def update_file(msg)
gpg_key = msg['gpg_key'].sub('@', '_') gpg_key = msg['gpg_key'].sub('@', '_')
data = msg['data'] data = msg['data']
@ -143,7 +138,7 @@ class Server
hash = Digest::SHA256.hexdigest(salt + msg['password']) hash = Digest::SHA256.hexdigest(salt + msg['password'])
end end
if isAuthorized?(msg['password'], salt, hash) if is_authorized?(msg['password'], salt, hash)
begin begin
config = {'gpg' => {'salt' => salt, config = {'gpg' => {'salt' => salt,
'hash' => hash, 'hash' => hash,
@ -173,7 +168,7 @@ class Server
# Remove a gpg file # Remove a gpg file
# @args: msg -> message puts by the client # @args: msg -> message puts by the client
# @rtrn: json message # @rtrn: json message
def deleteFile(msg) def delete_file(msg)
gpg_key = msg['gpg_key'].sub('@', '_') gpg_key = msg['gpg_key'].sub('@', '_')
if msg['suffix'].nil? || msg['suffix'].empty? if msg['suffix'].nil? || msg['suffix'].empty?
@ -194,7 +189,7 @@ class Server
salt = gpg_data['gpg']['salt'] salt = gpg_data['gpg']['salt']
hash = gpg_data['gpg']['hash'] hash = gpg_data['gpg']['hash']
if isAuthorized?(msg['password'], salt, hash) if is_authorized?(msg['password'], salt, hash)
begin begin
File.unlink(file_gpg) File.unlink(file_gpg)
@ -220,7 +215,7 @@ class Server
# salt -> the salt # salt -> the salt
# hash -> the hash of the password with the salt # hash -> the hash of the password with the salt
# @rtrn: true is is good, else false # @rtrn: true is is good, else false
def isAuthorized?(password, salt, hash) def is_authorized?(password, salt, hash)
if hash == Digest::SHA256.hexdigest(salt + password) if hash == Digest::SHA256.hexdigest(salt + password)
return true return true
else else
@ -231,7 +226,7 @@ class Server
# Get message to client # Get message to client
# @args: client -> client connection # @args: client -> client connection
# @rtrn: array of the json string, or false if isn't json message # @rtrn: array of the json string, or false if isn't json message
def getClientMessage(client) def get_client_msg(client)
begin begin
msg = client.gets msg = client.gets
return JSON.parse(msg) return JSON.parse(msg)
@ -243,7 +238,7 @@ class Server
# Close the client connection # Close the client connection
# @args: client -> client connection # @args: client -> client connection
def closeConnection(client) def close_connection(client)
client.puts "Closing the connection. Bye!" client.puts "Closing the connection. Bye!"
client.close client.close
end end

2
mpw
View file

@ -140,6 +140,6 @@ else
end end
end end
cli = nil cli.sync_close()
exit 0 exit 0

View file

@ -66,6 +66,6 @@ else
cli.ssh(search) cli.ssh(search)
end end
cli = nil cli.sync_close()
exit 0 exit 0