diff --git a/src/platform/darwin.sh b/src/platform/darwin.sh index 9a1fda8..b1a1333 100644 --- a/src/platform/darwin.sh +++ b/src/platform/darwin.sh @@ -2,15 +2,35 @@ # This file is licensed under the GPLv2+. Please see COPYING for more information. clip() { + copy_concealed() { + if command -v osascript &>/dev/null; then + osascript -l JavaScript \ + -e 'ObjC.import("AppKit")' \ + -e 'ObjC.import("Foundation")' \ + -e 'const input = $.NSFileHandle.fileHandleWithStandardInput.readDataToEndOfFile' \ + -e 'const text = $.NSString.alloc.initWithDataEncoding(input, $.NSUTF8StringEncoding)' \ + -e 'const pasteboard = $.NSPasteboard.generalPasteboard' \ + -e 'pasteboard.clearContents' \ + -e 'pasteboard.setStringForType(text, $.NSPasteboardTypeString)' \ + -e 'pasteboard.setDataForType($.NSData.data, "org.nspasteboard.ConcealedType")' \ + -e 'pasteboard.setDataForType($.NSData.data, "org.nspasteboard.TransientType")' \ + -e 'pasteboard.setDataForType($.NSData.data, "org.nspasteboard.AutoGeneratedType")' \ + -e 'undefined' + else + pbcopy + fi + } + local sleep_argv0="password store sleep for user $(id -u)" pkill -f "^$sleep_argv0" 2>/dev/null && sleep 0.5 local before="$(pbpaste | $BASE64)" - echo -n "$1" | pbcopy + local secret="$(printf "%s" "$1" | $BASE64)" + printf "%s" "$1" | copy_concealed || die "Error: could not copy password to clipboard." ( ( exec -a "$sleep_argv0" sleep "$CLIP_TIME" ) local now="$(pbpaste | $BASE64)" - [[ $now != $(echo -n "$1" | $BASE64) ]] && before="$now" - echo "$before" | $BASE64 -d | pbcopy + [[ $now != "$secret" ]] && exit + printf "%s\n" "$before" | $BASE64 -d | copy_concealed ) >/dev/null 2>&1 & disown echo "Copied $2 to clipboard. Will clear in $CLIP_TIME seconds." }