def getAssociation(self, server_url, handle=None): if self.log_debug: log.debug("Association requested for server_url: %s, with handle: %s", server_url, handle) if server_url.find('://') == -1: raise ValueError('Bad server URL: %r' % server_url) if handle is None: associations = self.associations.find({ "server_url": server_url }) if associations.count(): associations = [Association.deserialize(a['association']) for a in associations] # Now use the one that was issued most recently associations.sort(cmp=lambda x, y: cmp(x.issued, y.issued)) log.debug("Most recent is %s", associations[-1].handle) return associations[-1] else: association = self.associations.find_one({ "_id": hash((server_url, handle)), "server_url": server_url, "handle": handle }) if association: return Association.deserialize(association['association'])
def getAssociation(self, server_url, handle=None): log_debug = self.log_debug if log_debug: log.debug('Association requested for server_url: %s, with handle: %s', server_url, handle) if handle is None: # Retrieve all the keys for this server connection key_name = self.getAssociationFilename(server_url, '') assocs = self._conn.keys('%s*' % key_name) if not assocs: if log_debug: log.debug('No association found for: %s', server_url) return None # Now use the one that was issued most recently associations = [] for assoc in self._conn.mget(assocs): associations.append(Association.deserialize(assoc)) associations.sort(cmp=lambda x,y: cmp(x.issued, y.issued)) if log_debug: log.debug('getAssociation found, returns most recently issued') return associations[-1] else: key_name = self.getAssociationFilename(server_url, handle) association_s = self._conn.get(key_name) if association_s: if log_debug: log.debug('getAssociation found, returning association') return Association.deserialize(association_s) else: if log_debug: log.debug('No association found for getAssociation') return None
def getAssociation(self, server_url, handle=None): log.debug("Association requested for server_url: %s, with handle: %s", server_url, handle) if server_url.find('://') == -1: raise ValueError('Bad server URL: %r' % server_url) if handle is None: associations = self.associations.find({"server_url": server_url}) if associations.count(): associations = [ Association.deserialize(a['association']) for a in associations ] # Now use the one that was issued most recently associations.sort(cmp=lambda x, y: cmp(x.issued, y.issued)) log.debug("Most recent is %s", associations[-1].handle) return associations[-1] else: association = self.associations.find_one({ "_id": hash((server_url, handle)), "server_url": server_url, "handle": handle }) if association: return Association.deserialize(association['association'])
def _getAssociation(self, filename): try: assoc_file = open(filename, 'rb') except IOError as why: if why.errno == ENOENT: # No association exists for that URL and handle return None else: raise try: assoc_s = assoc_file.read() finally: assoc_file.close() try: association = Association.deserialize(assoc_s) except ValueError: _removeIfPresent(filename) return None # Clean up expired associations if association.expiresIn == 0: _removeIfPresent(filename) return None else: return association
def test_storeAssociation(self): """Tests the NDBOpenIDStore.storeAssociation(server_url, association) method.""" # create association association = Association(handle='handle', secret='secret', issued=int(time.time()), lifetime=3600, assoc_type='HMAC-SHA1') server_url = 'server_url_abc' # store association NDBOpenIDStore.storeAssociation(server_url, association) # retrieve association key = ndb.Key('ServerUrl', server_url, NDBOpenIDStore, association.handle) entity = key.get() # check if entity exists assert entity is not None # check whether serialized match assert entity.serialized == association.serialize() # check whether expiration_date match issued = datetime.datetime.fromtimestamp(association.issued) lifetime = datetime.timedelta(0, association.lifetime) expiration_date = issued + lifetime assert entity.expiration_date == expiration_date # check whether the original and deserialized associations match assert association == Association.deserialize(entity.serialized)
def _allAssocs(self): all_associations = [] association_filenames = [os.path.join(self.association_dir, f) for f in os.listdir(self.association_dir)] for association_filename in association_filenames: try: association_file = open(association_filename, 'rb') except IOError as why: if why.errno == ENOENT: _LOGGER.exception("%s disappeared during %s._allAssocs", association_filename, self.__class__.__name__) else: raise else: try: assoc_s = association_file.read() finally: association_file.close() # Remove expired or corrupted associations try: association = Association.deserialize(assoc_s.decode('utf-8')) except ValueError: _removeIfPresent(association_filename) else: all_associations.append( (association_filename, association)) return all_associations
def _allAssocs(self): all_associations = [] association_filenames = map( lambda filename: os.path.join(self.association_dir, filename), os.listdir(self.association_dir)) for association_filename in association_filenames: try: association_file = file(association_filename, 'rb') except IOError, why: if why.errno == ENOENT: oidutil.log( "%s disappeared during %s._allAssocs" % (association_filename, self.__class__.__name__)) else: raise else: try: assoc_s = association_file.read() finally: association_file.close() # Remove expired or corrupted associations try: association = Association.deserialize(assoc_s) except ValueError: _removeIfPresent(association_filename) else: all_associations.append( (association_filename, association))
def getAssociation(self, server_url, handle=None): """ This method returns an C{L{Association <openid.association.Association>}} object from storage that matches the server URL and, if specified, handle. It returns C{None} if no such association is found or if the matching association is expired. If no handle is specified, the store may return any association which matches the server URL. If multiple associations are valid, the recommended return value for this method is the one that will remain valid for the longest duration. """ query = models.Association.all().filter('url', server_url) if handle: query.filter('handle', handle) results = query.fetch(1) assoc = None if len(results) > 0: assoc = xAssociation.deserialize(results[0].association) if assoc.getExpiresIn() <= 0: results[0].delete() #self.removeAssociation(server_url, handle) assoc = None return assoc
def getAssociation(self, server_url, handle=None): """ This method returns an C{L{Association <openid.association.Association>}} object from storage that matches the server URL and, if specified, handle. It returns C{None} if no such association is found or if the matching association is expired. If no handle is specified, the store may return any association which matches the server URL. If multiple associations are valid, the recommended return value for this method is the one that will remain valid for the longest duration. """ query = models.Association.all().filter('url', server_url) if handle: query.filter('handle', handle) results = query.fetch(1) assoc = None if len(results) > 0: assoc = xAssociation.deserialize(results[0].association) if assoc.getExpiresIn() <= 0: results[0].delete( ) #self.removeAssociation(server_url, handle) assoc = None return assoc
def _getAssociation(self, filename): try: assoc_file = open(filename, 'rb') except IOError as why: if why.errno == ENOENT: # No association exists for that URL and handle return None else: raise else: try: assoc_s = assoc_file.read() finally: assoc_file.close() try: association = Association.deserialize(assoc_s) except ValueError: _removeIfPresent(filename) return None # Clean up expired associations if association.expiresIn == 0: _removeIfPresent(filename) return None else: return association
def test_storeAssociation(self): """Tests the NDBOpenIDStore.storeAssociation(server_url, association) method.""" # create association association = Association(handle='handle', secret='secret', issued=int(time.time()), lifetime=3600, assoc_type='HMAC-SHA1') server_url = 'server_url_abc' # store association NDBOpenIDStore.storeAssociation(server_url, association) # retrieve association key = ndb.Key('ServerUrl', server_url, NDBOpenIDStore, association.handle) entity = key.get() # check if entity exists assert entity is not None # check whether serialized match assert entity.serialized == association.serialize() # check whether expiration_date match issued = datetime.datetime.fromtimestamp(association.issued) lifetime = datetime.timedelta(0, association.lifetime) expiration_date = issued + lifetime assert entity.expiration_date == expiration_date # check whether the original and deserialized associations match assert association == Association.deserialize(entity.serialized)
def _allAssocs(self): all_associations = [] association_filenames = [ os.path.join(self.association_dir, filename) for filename in os.listdir(self.association_dir) ] for association_filename in association_filenames: try: association_file = open(association_filename, 'rb') except IOError as why: if why.errno == ENOENT: logging.exception("%s disappeared during %s._allAssocs" % ( association_filename, self.__class__.__name__)) else: raise else: try: assoc_s = association_file.read() finally: association_file.close() # Remove expired or corrupted associations try: association = Association.deserialize(assoc_s) except ValueError: _removeIfPresent(association_filename) else: all_associations.append( (association_filename, association)) return all_associations
def getAssociation(self, server_url, handle=None): """ Get an association from ndb """ if handle == None: # find the newest association qry = OpenIDAssociation.query( OpenIDAssociation.server_url_hash == url_hash( server_url)).order(-OpenIDAssociation.issued) results = qry.fetch(1) if results == None or len(results) == 0: return None gae_assoc = results[0] if gae_assoc == None: return None association = None try: association = Association.deserialize( gae_assoc.association_bits) except: gae_assoc.key.delete() return None return association else: # find the specific association gae_assoc_key = ndb.Key( OpenIDAssociation, OpenIDAssociation.make_key_name(server_url, handle)) gae_assoc = gae_assoc_key.get() if gae_assoc == None: return None try: association = Association.deserialize( gae_assoc.association_bits) except: gae_assoc_key.delete() return None return association
def cleanup_assocs(self): old_len = len(self.assocs) self.assocs = [ a for a in self.assocs if Association.deserialize(a['value']).getExpiresIn() != 0 ] new_len = len(self.assocs) return (old_len - new_len), new_len
def getAssociation(self, server_url, handle=None): log_debug = self.log_debug if log_debug: log.debug( 'Association requested for server_url: %s, with handle: %s', server_url, handle) if handle is None: # Retrieve all the keys for this server connection key_name = self.getAssociationFilename(server_url, '') cursor = 0 assocs = set() while True: state = self._conn.scan(cursor, '%s*' % key_name, self._scan_count) cursor = state[0] for key in state[1]: assocs.add(key) if cursor == 0: break if not assocs: if log_debug: log.debug('No association found for: %s', server_url) return None # Now use the one that was issued most recently associations = [] for assoc in self._conn.mget(assocs): associations.append(Association.deserialize(assoc)) associations.sort(cmp=lambda x, y: cmp(x.issued, y.issued)) if log_debug: log.debug('getAssociation found, returns most recently issued') return associations[-1] else: key_name = self.getAssociationFilename(server_url, handle) association_s = self._conn.get(key_name) if association_s: if log_debug: log.debug('getAssociation found, returning association') return Association.deserialize(association_s) else: if log_debug: log.debug('No association found for getAssociation') return None
def getAssociation(self, server_url, handle=None): # Try to get association. assoc = self.session.get(self.ASSOCIATION_KEY) if assoc and assoc[0] == server_url: # If found deserialize and return it. self._log(logging.DEBUG, "SessionOpenIDStore: Association found.") return Association.deserialize(assoc[2].encode("latin-1")) else: self._log(logging.DEBUG, "SessionOpenIDStore: Association not found.")
def best_assoc(self): best = None for assoc in self.assocs: assoc = Association.deserialize(assoc['value']) if best is None or best.issued < assoc.issued: best = assoc if best: return best else: return None
def getAssociation(self, server_url, handle=None): # Try to get association. assoc = self.session.get(self.ASSOCIATION_KEY) if assoc and assoc[0] == server_url: # If found deserialize and return it. self._log(logging.DEBUG, u'SessionOpenIDStore: Association found.') return Association.deserialize(assoc[2].encode('latin-1')) else: self._log(logging.DEBUG, u'SessionOpenIDStore: Association not found.')
def best_assoc(self): best = None for assoc in self.assocs: assoc = Association.deserialize(assoc['value']) if best is None or best.issued < assoc.issued: best = assoc if best: return best else: return None
def getAssociation(self, server_url, handle=None): # Try to get association. assoc = self.session.get('oia') if assoc and assoc[0] == server_url: # If found deserialize and return it. self._log(logging.DEBUG, 'SessionOpenIDStore: Association found.') return Association.deserialize(assoc[2]) else: self._log(logging.DEBUG, 'SessionOpenIDStore: Association not found.')
def getAssociation(self, server_url, handle=None): key1, key2 = self.getAssociationKeys(server_url, handle) if handle: results = memcache.get_multi([key1, key2], namespace=MEMCACHE_NAMESPACE) else: results = {key1: memcache.get(key1, namespace=MEMCACHE_NAMESPACE)} data = results.get(key2) or results.get(key1) if data: return OpenIDAssociation.deserialize(data) else: return None
def getAssociation(self, server_url, handle=None, remove=True): try: key = self.getAssociationKey(server_url, handle) assoc = Association.deserialize(self.associations[key]) except KeyError: return None if remove and assoc.getExpiresIn() == 0: self.removeAssociation(server_url, handle) return None return assoc
def getAssociation(self, server_url, handle=None, remove=True): try: key=self.getAssociationKey(server_url, handle) assoc=Association.deserialize(self.associations[key]) except KeyError: return None if remove and assoc.getExpiresIn()==0: self.removeAssociation(server_url, handle) return None return assoc
def getAssociation(self, server_url, handle=None): key1, key2 = self.getAssociationKeys(server_url, handle) if handle: results = memcache.get_multi([key1, key2], namespace=MEMCACHE_NAMESPACE) else: results = {key1: memcache.get(key1, namespace=MEMCACHE_NAMESPACE)} data = results.get(key2) or results.get(key1) if data: return OpenIDAssociation.deserialize(data) else: return None
def cleanupAssociations(self): query = datastore.Query('Association') results = query.Get(100) numDeleted = 0 for result in results: assoc = Association.deserialize(result['association']) if assoc.getExpiresIn() == 0: numDeleted += 1 datastore.Delete(result.key()) return numDeleted
def removeAssociation(self, server_url, handle): key=self.getAssociationKey(server_url, handle) try: assoc=Association.deserialize(self.associations[key]) del self.associations[key] lst=self.handles[server_url] lst.remove(key) self.handles[server_url]=lst self.assoctimeline.remove((assoc.issued+assoc.lifetime, key)) return True except KeyError: return False
def removeAssociation(self, server_url, handle): key = self.getAssociationKey(server_url, handle) try: assoc = Association.deserialize(self.associations[key]) del self.associations[key] lst = self.handles[server_url] lst.remove(key) self.handles[server_url] = lst self.assoctimeline.remove((assoc.issued + assoc.lifetime, key)) return True except KeyError: return False
def getAssociation(self, server_url, handle=None): log_debug = self.log_debug if log_debug: log.debug( 'Association requested for server_url: %s, with handle: %s', server_url, handle) if handle is None: # Retrieve all the keys for this server connection key_name = self.getAssociationFilename(server_url, '') assocs = self._conn.keys('%s*' % key_name) if not assocs: if log_debug: log.debug('No association found for: %s', server_url) return None # Now use the one that was issued most recently associations = [] for assoc in self._conn.mget(assocs): associations.append(Association.deserialize(assoc)) associations.sort(key=lambda x: x.issued) if log_debug: log.debug('getAssociation found, returns most recently issued') return associations[-1] else: key_name = self.getAssociationFilename(server_url, handle) association_s = self._conn.get(key_name) if association_s: if log_debug: log.debug('getAssociation found, returning association') return Association.deserialize(association_s) else: if log_debug: log.debug('No association found for getAssociation') return None
def removeAssociation(self, server_url, handle): ce = caching.CacheEntry(self.request, 'openid', self.key(server_url), scope='wiki', use_pickle=True) if not ce.exists(): return assocs = ce.content() for idx in xrange(len(assocs)-1, -1, -1): assoc_str = assocs[idx] association = Association.deserialize(assoc_str) if association.handle == handle: del assocs[idx] if len(assocs): ce.update(assocs) else: ce.remove()
def removeAssociation(self, server_url, handle): ce = caching.CacheEntry(self.request, 'openid', self.key(server_url), scope='wiki', use_pickle=True) if not ce.exists(): return assocs = ce.content() for idx in xrange(len(assocs)-1, -1, -1): assoc_str = assocs[idx] association = Association.deserialize(assoc_str) if association.handle == handle: del assocs[idx] if len(assocs): ce.update(assocs) else: ce.remove()
def getAssociation(self, server_url, handle=None): ce = caching.CacheEntry(self.request, 'openid', self.key(server_url), scope='wiki', use_pickle=True) if not ce.exists(): return None assocs = ce.content() found = False for idx in xrange(len(assocs)-1, -1, -1): assoc_str = assocs[idx] association = Association.deserialize(assoc_str) if association.getExpiresIn() == 0: del assocs[idx] else: if handle is None or association.handle == handle: found = True break ce.update(assocs) if found: return association return None
def getAssociation(self, server_url, handle=None): ce = caching.CacheEntry(self.request, 'openid', self.key(server_url), scope='wiki', use_pickle=True) if not ce.exists(): return None assocs = ce.content() found = False for idx in xrange(len(assocs)-1, -1, -1): assoc_str = assocs[idx] association = Association.deserialize(assoc_str) if association.getExpiresIn() == 0: del assocs[idx] else: if handle is None or association.handle == handle: found = True break ce.update(assocs) if found: return association return None
def clean(self): """Remove expired entries from the database. This is potentially expensive, so only run when it is acceptable to take time. () -> NoneType """ nonces = os.listdir(self.nonce_dir) now = time.time() # Check all nonces for expiry for nonce in nonces: if not checkTimestamp(nonce, now=now): filename = os.path.join(self.nonce_dir, nonce) _removeIfPresent(filename) association_filenames = os.listdir(self.association_dir) for association_filename in association_filenames: try: association_file = file(association_filename, 'rb') except IOError, why: if why[0] == ENOENT: pass else: raise else: try: assoc_s = association_file.read() finally: association_file.close() # Remove expired or corrupted associations try: association = Association.deserialize(assoc_s) except ValueError: _removeIfPresent(association_filename) else: if association.getExpiresIn() == 0: _removeIfPresent(association_filename)
def getAssociation(self, server_url, handle=None): """ This method returns an C{L{Association <openid.association.Association>}} object from storage that matches the server URL and, if specified, handle. It returns C{None} if no such association is found or if the matching association is expired. If no handle is specified, the store may return any association which matches the server URL. If multiple associations are valid, the recommended return value for this method is the one that will remain valid for the longest duration. """ query = Association.objects.filter(url = server_url) if handle: query.filter(handle = handle) results = query[0] if results: association = OpenIDAssociation.deserialize(results[0].association) if association.getExpiresIn() > 0: # hasn't expired yet return association return None
def getAssociation(self, server_url, handle=None): """ This method returns an C{L{Association <openid.association.Association>}} object from storage that matches the server URL and, if specified, handle. It returns C{None} if no such association is found or if the matching association is expired. If no handle is specified, the store may return any association which matches the server URL. If multiple associations are valid, the recommended return value for this method is the one that will remain valid for the longest duration. """ query = datastore.Query('Association', {'url =': server_url}) if handle: query['handle ='] = handle results = query.Get(1) if results: association = Association.deserialize(results[0]['association']) if association.getExpiresIn() > 0: # hasn't expired yet return association return None
def get_assoc(self, handle): for a in self.assocs: if a['key'] == handle: return Association.deserialize(a['value']) return None
try: assoc_file = file(filename, 'rb') except IOError, why: if why.errno == ENOENT: # No association exists for that URL and handle return None else: raise else: try: assoc_s = assoc_file.read() finally: assoc_file.close() try: association = Association.deserialize(assoc_s) except ValueError: _removeIfPresent(filename) return None # Clean up expired associations if association.getExpiresIn() == 0: _removeIfPresent(filename) return None else: return association def removeAssociation(self, server_url, handle): """Remove an association if it exists. Do nothing if it does not. (str, str) -> bool
try: assoc_file = file(filename, 'rb') except IOError, why: if why.errno == ENOENT: # No association exists for that URL and handle return None else: raise else: try: assoc_s = assoc_file.read() finally: assoc_file.close() try: association = Association.deserialize(assoc_s) except ValueError: _removeIfPresent(filename) return None # Clean up expired associations if association.getExpiresIn() == 0: _removeIfPresent(filename) return None else: return association def removeAssociation(self, server_url, handle): """Remove an association if it exists. Do nothing if it does not. (str, str) -> bool
def get_assoc(self, handle): for a in self.assocs: if a['key'] == handle: return Association.deserialize(a['value']) return None
def cleanup_assocs(self): old_len = len(self.assocs) self.assocs = [ a for a in self.assocs if Association.deserialize(a['value']).getExpiresIn() != 0 ] new_len = len(self.assocs) return (old_len - new_len), new_len