Skip to content

Commit

Permalink
v2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Angoll authored and Angoll committed Oct 24, 2020
1 parent d4ce47d commit 5e66b39
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 23 deletions.
File renamed without changes.
Binary file added KPass.v2.0.alfredworkflow
Binary file not shown.
21 changes: 18 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,30 @@
- Alfred 4
- KeePassXC
- A created KeePassXC database
- jq installed ( https://stedolan.github.io/jq/download/ )

#### Configuration

Run **kpassinit** KPass Setup and follow the instructions:

1. Select the KeePassXC database file
2. Select which keychain will be used to store the database password
3. Enter the password for the KeePassXC database selected
4. Done!
2. [Optinally] If need it full path to the keychain file to use, leave it blank otherwise
3. Select which keychain will be used to store the database password
4. Enter the password for the KeePassXC database selected
5. Done!


## Known problems

#### Keepass file appears unselectable

Open the Alfred workflow from the Alfred App, double click the Run Script /usr/bin/osascript connected to the kpassinit. And remove the following line (on line 20)

```
ofType: ["dyn.ah62d4rv4ge8003dcta"] //.kdbx extension type identifier
```

This will make any file selectable.


## References
Expand Down
35 changes: 32 additions & 3 deletions scripts/configure.applescript
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ app.includeStandardAdditions = true;

var alfredApp = Application('com.runningwithcrayons.Alfred');
var bundleId = $.getenv('alfred_workflow_bundleid')
var keychainItem = $.getenv('keychainItem')

if (! keychainItem) {
keychainItem = "KPass_AlfredWorkflow"
}


try {
Expand All @@ -27,7 +32,6 @@ try {
});
keychain = keychain[0]


// Ask for KeepassXC Database password
var response = app.displayDialog("KeePassXC Database password", {
defaultAnswer: "",
Expand All @@ -42,15 +46,40 @@ try {
password = response.textReturned.replace(/\\/g, '\\\\').replace(/\"/g, '\\"');

// Create a new password
response = app.doShellScript("security add-generic-password -a $(id -un) -c 'kaiw' -C 'kaiw' -D 'KeepasXC Integration' -j 'Alfred KeepasXC Integration Database' -s 'KPass_AlfredWorkflow' -w \"" + password + "\" -U " + keychain);

response = app.doShellScript("security add-generic-password -a $(id -un) -c 'kaiw' -C 'kaiw' -D 'KeepasXC Integration' -j 'Alfred KeepasXC Integration Database' -s \""+ keychainItem + "\" -w \"" + password + "\" -U " + keychain);

// Ask for KeepassXC Keyfile
var response = app.displayDialog("KeePassXC Database Keyfile", {
defaultAnswer: "",
buttons: ["Cancel", "Continue"],
defaultButton: "Continue",
hiddenAnswer: false,
withTitle: "[Optionally]ÊPlease introduce the path of the keyfile",
withIcon: Path("/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/Lockedicon.icns")
});

// Get keyfile if any
keePassKeyFile = response.textReturned.replace(/\\/g, '\\\\').replace(/\"/g, '\\"');

// Configure Alfred env variables
alfredApp.setConfiguration('keychain', {
toValue: keychain.replace(/(^")|("$)/,""),
inWorkflow: bundleId,
exportable: false
});

alfredApp.setConfiguration('keychainItem', {
toValue: keychainItem.toString(),
inWorkflow: bundleId,
exportable: true
});

alfredApp.setConfiguration('keePassKeyFile', {
toValue: keePassKeyFile.toString(),
inWorkflow: bundleId,
exportable: false
});

alfredApp.setConfiguration('database', {
toValue: database.toString(),
inWorkflow: bundleId,
Expand Down
26 changes: 24 additions & 2 deletions scripts/getPassword.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,26 @@
export PATH='/usr/local/bin/:/usr/bin'

echo $(security find-generic-password -a $(id -un) -c 'kaiw' -C 'kaiw' -s "KPass_AlfredWorkflow" -w $keychain) |\
keepassxc-cli show -q -a Password "$database" "$1";
keePassKeyFile=""
if [[ ! -z ${keyfile} ]]; then
keePassKeyFile="--key-file \"${keyfile}\""
fi

function get_keys() {
security find-generic-password -a $(id -un) -c 'kpas' -C 'kpas' -s "${keychainItem}" -w "${keychain}" |\
keepassxc-cli show -q ${keePassKeyFile} -a Password "$database" "$1"
}

function get_errorInfo {
exec 3<&1
security find-generic-password -a $(id -un) -c 'kpas' -C 'kpas' -s "${keychainItem}" -w "${keychain}" 2>&3 |\
keepassxc-cli ls -R ${keePassKeyFile} -f "$database" 2>&3 | grep -Ev '(/|\[empty\]?)$'
exec 3>&-
}


data=$(get_keys $1)
if [ $? -ne 0 ]; then
echo $(get_errorInfo)
else
echo $data
fi
52 changes: 37 additions & 15 deletions scripts/listDatabase.sh
Original file line number Diff line number Diff line change
@@ -1,27 +1,49 @@
query="{query}"

export PATH='/usr/local/bin/:/usr/bin'

if [[ -z ${database} ]] || [[ -z ${keychain} ]];
then

echo "{\"items\": [{\"title\":\"Not configured, please run: kpassinit\"}]}";
useKeePassKeyFile=""
if [[ ! -z ${keePassKeyFile} ]]; then
useKeePassKeyFile="--key-file \"${keePassKeyFile}\""
fi

function get_keys(){
local keys
keys=$(security find-generic-password -a $(id -un) -c 'kpas' -C 'kpas' -s "AlfredWorkflow" -w "${keychain}" |\
keepassxc-cli locate -q "$database" "{query}" | grep -Ev '(/|\[empty\]?)$')
echo "${keys}"

function get_keys() {
security find-generic-password -a $(id -un) -c 'kpas' -C 'kpas' -s "${keychainItem}" -w "${keychain}" |\
keepassxc-cli ls -R -q ${useKeePassKeyFile} -f "$database" | grep -Ev '(/|\[empty\]?)$'
}

function get_errorInfo {
exec 3<&1
security find-generic-password -a $(id -un) -c 'kpas' -C 'kpas' -s "${keychainItem}" -w "${keychain}" 2>&3 |\
keepassxc-cli ls -R ${useKeePassKeyFile} -f "$database" 2>&3 | grep -Ev '(/|\[empty\]?)$'
exec 3>&-
}

keys=$(get_keys)
function build_entry() {
local entry=$1
#local title=${entry##*/}
local title=${entry}
jq -n --arg entry ${entry} --arg title ${title} --arg iconPath "${PWD}/icon.png" '{"uid": $entry, "title": $title, "subtitle": $entry , "arg": $entry, "autocomplete": $entry, "icon": {"type":"png", "path": $iconPath}}'
}

if [[ -z ${database} ]] || [[ -z ${keychain} ]];
then
echo "{\"items\": [{\"title\":\"Not configured, please run: kpassinit\"}]}";
exit
fi

keys=($(get_keys))
if [ $? -ne 0 ]; then
echo -n "{\"items\": [{\"title\":\"Error listing database, please check config\"}]}";
info=$(get_errorInfo | sed 's/"/\\"/g')
info=${info//$'\n'/}
echo "{\"items\": [{\"title\":\"Error listing database, please check config: Error: ${info}\"}]}";
exit
fi

echo "${keys}" | while read entry
do
jq -n --arg uid "${entry}" --arg title "${entry##*/}" --arg subtitle "${entry}" '{"uid": $uid, "title": $title, "subtitle": $subtitle }'
OIFS=$IFS
IFS=$'\n'
keys=($(get_keys))
for entry in ${keys[@]}; do
build_entry ${entry}
done | jq -c -s '{"items": .}'
IFS=$OIFS

0 comments on commit 5e66b39

Please sign in to comment.