diff --git a/alfred_workflow.rb b/alfred_workflow.rb new file mode 100644 index 0000000..2402d2c --- /dev/null +++ b/alfred_workflow.rb @@ -0,0 +1,66 @@ +# encoding: utf-8 + +class AlfredXmlFormatter + def initialize() + @keepass_items = [] + end + + def add_kp(kp) + @keepass_items << kp + end + + def to_string() + kp_item_template = + ' + %s + %s %s + %s + ~/ + ~/ + ' + + string_buffer = '' + @keepass_items.each { |kp| + string_buffer += kp_item_template % [kp.title, kp.username, kp.password, kp.password] + } + + xml_template = + ' + + %s + ' + return xml_template % [string_buffer] + end +end + + +class KeepassItem + def initialize(title, username, password) + @title = title + @username = username + @password = password + end + + def title + @title + end + + def username + @username + end + + def password + @password + end +end + +# kp_i_1 = KeepassItem.new("小木虫", "samueldeng", "222222") +# kp_i_2 = KeepassItem.new("小木虫2", "samueldeng2", "33333") +# kp_i_3 = KeepassItem.new("小木虫3", "samueldeng3", "44444") + +# alfred_xml = AlfredXmlFormatter.new +# alfred_xml.add_kp(kp_i_1) +# alfred_xml.add_kp(kp_i_2) +# alfred_xml.add_kp(kp_i_3) + +# puts alfred_xml.to_string \ No newline at end of file diff --git a/crypt.rb b/crypt.rb new file mode 100644 index 0000000..f617319 --- /dev/null +++ b/crypt.rb @@ -0,0 +1,20 @@ +def decrypt(data, key, iv) + decode_cipher = OpenSSL::Cipher::Cipher.new("AES-256-CBC") + decode_cipher.decrypt + decode_cipher.key = Base64.decode64(key) + decode_cipher.iv = Base64.decode64(iv) + decode_cipher.update(Base64.decode64(data)) + decode_cipher.final +end + +def encrypt(data, key, iv) + cipher_nonce = OpenSSL::Cipher::Cipher.new("AES-256-CBC") + cipher_nonce.encrypt + cipher_nonce.key = Base64.decode64(key) + cipher_nonce.iv = iv + b64enc(cipher_nonce.update(data) + cipher_nonce.final) + +end + +def b64enc(data) + Base64.encode64(data).gsub(/\n/, '') +end \ No newline at end of file diff --git a/main.rb b/main.rb new file mode 100644 index 0000000..fc65e92 --- /dev/null +++ b/main.rb @@ -0,0 +1,63 @@ +# encoding: utf-8 +query = "{query}" + +require "openssl" +require "base64" +require "net/http" +require "json" +load "alfred_workflow.rb" +load "crypt.rb" + +keepass_server = "http://localhost:19455" +key_name = "MBP-Chrome" +aes_key = "DBn3Wrqd7rFHu8cgPXCnEzIWgaUXcRXzOplkskcy9fo=" +# url = "http://" + query +url = ARGV[0] + + +iv = OpenSSL::Cipher::Cipher.new("AES-256-CBC").random_iv +nonce = b64enc(iv) + +veri = encrypt(nonce, aes_key, iv) +url = encrypt(url, aes_key, iv) + +query_body = { + RequestType: "get-logins", + Id: key_name, + Nonce: nonce, + Verifier: veri, + Url: url, +}.to_json + +# puts query_body + +uri = URI(keepass_server) +req = Net::HTTP::Post.new(uri, 'Content-Type' => 'application/json') +req.body = query_body +res = Net::HTTP.start(uri.hostname, uri.port) do |http| + http.request(req) +end +# puts res.body + +body = JSON.parse(res.body) +resp_nonce = body["Nonce"] +# puts body["Entries"] + + +alfred_xml = AlfredXmlFormatter.new +entries = body["Entries"] +entries.each do |entry| + title = entry["Name"] + username = entry["Login"] + password = entry["Password"] + + title_plain = decrypt(title, aes_key, resp_nonce) + username_plain = decrypt(username, aes_key, resp_nonce) + password_plain = decrypt(password, aes_key, resp_nonce) + + kp = KeepassItem.new(title_plain, username_plain, password_plain) + alfred_xml.add_kp(kp) + +end + +puts alfred_xml.to_string diff --git a/screenshot/alfred_keepass_1.png b/screenshot/alfred_keepass_1.png new file mode 100644 index 0000000..4457b39 Binary files /dev/null and b/screenshot/alfred_keepass_1.png differ diff --git a/screenshot/alfred_keepass_2.png b/screenshot/alfred_keepass_2.png new file mode 100644 index 0000000..84c2937 Binary files /dev/null and b/screenshot/alfred_keepass_2.png differ diff --git a/screenshot/alfred_keepass_3.png b/screenshot/alfred_keepass_3.png new file mode 100644 index 0000000..3d167a6 Binary files /dev/null and b/screenshot/alfred_keepass_3.png differ diff --git a/screenshot/alfred_keepass_4.png b/screenshot/alfred_keepass_4.png new file mode 100644 index 0000000..59dacdf Binary files /dev/null and b/screenshot/alfred_keepass_4.png differ diff --git a/test.kdbx b/test.kdbx new file mode 100644 index 0000000..939c30c Binary files /dev/null and b/test.kdbx differ