Created
March 5, 2009 09:57
-
-
Save ArtemGr/74278 to your computer and use it in GitHub Desktop.
memcache openid store
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Store implementation for the openidenabled.com python library, using the GAE's memcache.""" | |
from openid.store.interface import OpenIDStore | |
from openid.store import nonce | |
import string, logging | |
import google.appengine.api.memcache | |
memcache = google.appengine.api.memcache.Client() | |
class MemcacheStore(OpenIDStore): | |
"""Implements http://openidenabled.com/files/python-openid/docs/2.2.1/openid.store.interface.OpenIDStore-class.html""" | |
def storeAssociation(self, server_url, assoc): | |
"""This method puts a Association object into storage, retrievable by server URL and handle. """ | |
key = "openid.assoc." + server_url | |
logging.info ("storeAssociation %s" % server_url) | |
associations = memcache.get (key) | |
if (not associations): associations = {} | |
associations[assoc.handle] = assoc | |
memcache.set (key, associations, 60) | |
def getAssociation(self, server_url, handle=None): | |
"""This method returns an Association object from storage that matches the server URL and, if specified, handle. | |
It returns None if no such association is found or if the matching association is expired.""" | |
logging.info ("getAssociation %s" % server_url) | |
associations = memcache.get ("openid.assoc." + server_url) | |
if (not associations): return None | |
if (handle): | |
assoc = associations.get (handle) | |
if (assoc and assoc.getExpiresIn() > 0): return assoc | |
else: return None | |
match = [assoc for assoc in associations.itervalues() if assoc.getExpiresIn() > 0] | |
if (len (match) == 0): return None | |
else: return match[0] | |
def removeAssociation(self, server_url, handle): | |
"""This method removes the matching association if it's found, | |
and returns whether the association was removed or not.""" | |
key = "openid.assoc." + server_url | |
logging.info ("removeAssociation %s" % server_url) | |
if (not handle): return memcache.delete (key) == 2 | |
associations = memcache.get (key) | |
if (not associations): return False | |
assoc = associations.pop (handle, None) | |
if (not assoc): return False | |
memcache.set (key, associations, 60) | |
return True | |
def useNonce(self, server_url, timestamp, salt): | |
"""This method should return True if the nonce has not been used before, | |
and store it for a while to make sure nobody tries to use the same value again.""" | |
nonce = string.join (["openid.nonce", server_url, str (timestamp), salt]) | |
logging.info ("useNonce %s" % nonce) | |
return memcache.add (nonce, "-", 600) | |
def cleanup(self): | |
None | |
def cleanupAssociations(self): | |
None | |
def cleanupNonces(self): | |
None |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment