Muffinresearch Labs by Stuart Colville

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

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

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!







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>



Standalone mac battery charger|(0)

Got a spare mac battery? I’ve often wondered why up until now no-one’s produced a standalone charger so that you can charge batteries without having to plug them into the mac. Fortunately Fastmac.com have produced a standalone charger that allows you to do just that. and it’s compatible with iBooks, Powerbooks, macbooks and Macbook Pros. It’s also 110/200v. Exactly what I was looking for!

Django Admin Ominigraffle Stencil|(0)

Colleague Alex Lee has created a nice stencil for omingraffle with the Django Admin UI components, perfect for wireframing customised admin screens. For more details and to download the stencil see Alex’s Blog csensedesign.co.uk

Photos on Flickr

© Copyright 2004-08 Stuart Colville, all rights reserved. May contain traces of Muffin. Powered by WordPress. Hosting by 1&1 This page was baked in 1.042s.