class PasswordStore: def __init__(self, chain="login", account=None, service=None): if account is None: account = keyinfo.account if service is None: service = keyinfo.realm self.chain = chain self.account = account self.service = service self.keychain = Keychain() def unlock(self): self.keychain.unlockkeychain(self.chain) def lock(self): self.keychain.lockkeychain(chain) def password(self, account=None): if account is None: account = self.account _, password = self.keychain.getgenericpassword(self.chain, account, self.service) return password def save_password(self, password, account=None): if account is None: account = keyinfo.account self.keychain.setgenericpassword(self.chain, account, password, self.service)
class Preferences(NSWindowController): # IBOutlets email = IBOutlet() password = IBOutlet() def init(self): NSLog('Preferences: init') self = super(Preferences, self).init() self.initWithWindowNibName_('Preferences') return self def awakeFromNib(self): NSLog('Preferences: awakeFromNib') self.keychain = Keychain() # We need to get the account email so we can do the keychain lookup try: f = open('account') except IOError: pass else: account = f.readline() # If there is no value then the details haven't been set yet if account != '': account = account.replace('\n', '') key = self.keychain.getgenericpassword('login', account) # If the keychain exists then we can set the default value for the fields if key != False: NSLog('Preferences: awakeFromNib - keychain exists, setting fields') self.email.setStringValue_(key['account']) self.password.setStringValue_(key['password']) @IBAction def problem_(self, sender): NSLog('Preferences: problem_') ws = NSWorkspace.sharedWorkspace() url = NSURL.URLWithString_('http://www.google.com/support/accounts/bin/answer.py?answer=48598') ws.openURL_(url) @IBAction def showWindow_(self, sender): NSLog('Preferences: showWindow') super(Preferences, self).showWindow_(sender) @IBAction def save_(self, sender): NSLog('Preferences: save_') self.keychain.setgenericpassword('login', self.email.stringValue(), self.password.stringValue(), 'GFGain') # Save the account email so we can use it later f = open('account', 'w') f.write(self.email.stringValue()) f.close() self.close()