Muffinresearch Labs by Stuart Colville

Python: Keychain.py access to the Mac OSX keychain | 4 Comments

Posted in Code on 5th February 2008, 11:53 pm by

The code in the project has changed somewhat since this post – see the project for more info

keychain.py

I’ve been busy hacking away on a simple little class that makes it really easy to get access to the keychain on mac osx. I’d seen already a way to acces the keychain in this post by Mark Rowe however I wanted to avoid the dependency on objc.

To solve the problem I wrapped the security command which provides fairly comprehensive access to the keychain.

Where this comes in handy is any time that you want to create a Python CLI app that could do with keychain access you can use keychain.py to access credentials stored in the keychain. In addition keychain.py allows keychain settings to be altered and read and you can also create new keychains and keychain items.

Here’s a couple of simple examples of how you can use keychain.py in a script.

Instantiation

>>> import keychain
>>> k=keychain.Keychain();

Create a keychain

>>> k.createkeychain('TopSecret','Shhhssh')
(True, 'Keychain created successfully')

List keychains

>>> k.listkeychains()
{'System': '/Library/Keychains/System.keychain', 'TopSecret': '/Users/muffinman/Library/Keychains/TopSecret.keychain', 'login': '/Users/muffinman/Library/Keychains/login.keychain'}

Set and get a keychain item

>>> k.setgenericpassword('TopSecret', 'muffin','test')
(True, 'Password added to TopSecret.keychain successfully')
>>> k.getgenericpassword('TopSecret', 'muffin')
{'account': 'muffin', 'password': 'test'}

The code is on launchpad.net so if you would like to try it out you can simply checkout the code or download it

As always all feedback and suggestions welcome.

Post Tools

  • Pingback: Pages tagged "osx"

  • http://cyril.doussin.name Cyril Doussin

    Ok, that’s a really cool icon !

  • Larry

    Very nice. Thank you!

  • Banana Rama

    Thanks a lot!

GNU screen: open tab in current working directory|(1)

A nice trick for having screen open a new tab in the same directory as the one you’re currently in. To use it add it to your .screenrc

# Open new window in current dir.
bind c stuff "screen -X chdir \$PWD;screen^M"
bind ^c stuff "screen -X chdir \$PWD;screen^M"

Hat tip: mteckert on SuperUser.com

Ubuntu: add-apt-repository: command not found|(3)

When you’re using a minimal Ubuntu install if you find the ‘add-apt-repository’ command is missing (it’s useful for adding PPAs and other repositories), then simply run:

sudo apt-get install python-software-properties

Photos on Flickr

© Copyright 2004-12 Stuart Colville, all rights reserved. May contain traces of Muffin. Powered by WordPress. Hosting by Slicehost.com This page was baked in 1.550s.