Muffinresearch Labs by Stuart Colville

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

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

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

Comments: Add yours

1. On February 7th, 2008 at 5:01 pm Pages tagged "osx" said:

[...] bookmarks tagged osx Python: Keychain.py access to the Mac OSX keychain saved by 30 others     Hanna467 bookmarked on 02/07/08 | [...]

2. On February 14th, 2008 at 9:50 am Cyril Doussin said:

Ok, that’s a really cool icon !

3. On March 11th, 2008 at 7:44 am Larry said:

Very nice. Thank you!

4. On October 15th, 2008 at 8:56 am Banana Rama said:

Thanks a lot!







XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>



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|(2)

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 0.621s.