Skip to content

Commit

Permalink
v0.1 release
Browse files Browse the repository at this point in the history
  • Loading branch information
samueldeng committed Oct 29, 2016
1 parent 8dd3d0f commit ae7fe4c
Show file tree
Hide file tree
Showing 8 changed files with 149 additions and 0 deletions.
66 changes: 66 additions & 0 deletions alfred_workflow.rb
Original file line number Diff line number Diff line change
@@ -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 =
'<item uid="home" valid="YES" autocomplete="Home Folder" type="file">
<title>%s</title>
<subtitle>%s %s</subtitle>
<text type="copy">%s</text>
<icon type="fileicon">~/</icon>
<arg>~/</arg>
</item>'

string_buffer = ''
@keepass_items.each { |kp|
string_buffer += kp_item_template % [kp.title, kp.username, kp.password, kp.password]
}

xml_template =
'<?xml version="1.0" encoding="UTF-8"?>
<items>
%s
</items>'
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
20 changes: 20 additions & 0 deletions crypt.rb
Original file line number Diff line number Diff line change
@@ -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
63 changes: 63 additions & 0 deletions main.rb
Original file line number Diff line number Diff line change
@@ -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
Binary file added screenshot/alfred_keepass_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added screenshot/alfred_keepass_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added screenshot/alfred_keepass_3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added screenshot/alfred_keepass_4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test.kdbx
Binary file not shown.

0 comments on commit ae7fe4c

Please # to comment.