Tuesday, March 31, 2015

Update of Keepassx Autotyping on Mac OS X

Back in 2009 I published the article on this blog about doing autotyping in Keepassx for Mac OS X using AppleScript and some other helper application, MoreInternet. That article is available at:
It is still a popular reading on my blog, however MoreInternet is no longer available for recent releases of Mac OS X. But that's for a better option as now Mac OS X can auto-register URL handlers on first run of an application that advertises the capability.

Lately I am using mainly iTerm2 instead of the classic Terminal.app, therefore I spent a bit of time in upgrading the AppleScript to fit better with my current environment, Mac OS X 10.10 (Yosemite) and iTerm2 (as main option).

The AppleScript is available on GitHub, feel free to fork and make pull requests with enhancements:
With the new version comes few alternatives of specifying the URL schema in order to make it nicer looking inside the Keepassx. The old format with 'kpx' is still available, allowing the variants:
  kpx://proto?username:password@address:port/path
  kpx://proto?username:password:address:port/path
The proto field can be ssh, http or https.

The new variants use kpx-proto for URL schema, getting rid of the strange url with 'proto?' inside it, resulting in something more closer to actual URLs. The new URL format is:
kpx-proto://username:password@address:port/path
Again, the proto can be ssh, http or https. For both formats, old and new, the port and path are optional. For ssh, the path must not be provided. Next are some examples:
kpx-ssh://alice:secret@10.0.0.10 
kpx-https://alice:secret@mywebsite.com/login

It is possible to use the KeePassX self expanding variables such as {USERNAME} or {PASSWORD}.
kpx-https://{USERNAME}:{PASSWORD}@mywebsite.com/login

Installation

Download the kpx.as file from GitHub repository.

Open Script Editor from Applications => Utilities, paste the content of kpx.as into it and export it as 'Application', save it as kpx.app somewhere on your disk.

With a text editor like 'vim', edit kpx.app/Contents/PkgInfo and set the content to "APPLokpx" (no double quotes). Edit kpx.app/Contents/Info.plist and set the bundle signature to the last 4 letters of the value in PkgInfo file and add details about 'kpx' URL handling, you should get to something like this:
    <key>CFBundleSignature</key> <string>okpx</string> <key>CFBundleURLTypes</key> <array> <dict> <key>CFBundleURLName</key> <string>KeePassX</string> <key>CFBundleURLSchemes</key> <array> <string>kpx</string> <string>kpx-ssh</string> <string>kpx-http</string> <string>kpx-https</string> </array> </dict> </array>
Note: CFBundleSignature should be there already, just update the string value. CFBundleURLTypes (and the array value) must be added.

Save the files you edited and the execute kpx.app from Finder. This operation is registering the kpx URL handlers. The execution is practically exiting immediately, but afterwards Keepassx will be able to launch it for its registered URL schemes.

Again, if you have an older version of Mac OS X, you may need to install More Internet application to register new URL handlers, for more details see the blog for older version of this script. Read that article if you didn't do it in the past, because it provides other useful hints for testing and using as well as screenshots.

Mentioned before that iTerm2 is preferred terminal, if you prefer the Terminal.all, edit the downloaded kpx.as and replace the line:
set myTerm to "iTerm2"
with:
set myTerm to "Terminal"
Then do the same steps as above for installation.

The terminal application is used for ssh handling. For http/https, the Safari browser is used.

An important note is to re-install and run the application every time you change something in the AppleScript file kpx.as, before attempting to use Keepassx with the modifications done in kpx.as.

Hopefully this article will be useful for some people out there!

No effective time to work on at this moment, but it in the future I am thinking to add the option to start mosh instead of ssh and work with other web browsers Chromium/Chrome, Firefox or Opera -- I haven't checked which browsers have support for AppleScript commands. Of course, these or other contributions are welcome!


2 comments:

  1. Hi Daniel,

    Nice article. I cannot get the self expanding variables work with kpx-ssh, tried with new and old formats. The substitution does not happen. Do you know any KeepasX settings needed to be activated in order to get these variables work?

    kpx-ssh://{USERNAME}:{PASSWORD}@xxx.xxx.xxx.xxx:22

    Keepassx version: 2.0 alpha 6
    OSx Yosemite 10.10.2

    Many Thanks,

    AK

    ReplyDelete
  2. Hello,

    I haven't set any special setting, but I am using keepassx 0.4.3, never tried the 2.0 alpha. The 0.4.3 is stable, with packages on Linux distros as well, doing all I need for now.

    Daniel

    ReplyDelete