Commit 193ca7b3 by Carlos Andrés Rocha

Fix error when creating empty associations

Also renamed some variables to make them more distinguishable
parent e62968d5
...@@ -8,7 +8,7 @@ from django.core.cache import cache ...@@ -8,7 +8,7 @@ from django.core.cache import cache
import logging import logging
import time import time
DEFAULT_ASSOCIATION_TIMEOUT = 60 DEFAULT_ASSOCIATIONS_TIMEOUT = 60
DEFAULT_NONCE_TIMEOUT = 600 DEFAULT_NONCE_TIMEOUT = 600
ASSOCIATIONS_KEY_PREFIX = 'openid.provider.associations.' ASSOCIATIONS_KEY_PREFIX = 'openid.provider.associations.'
...@@ -34,51 +34,51 @@ class DjangoOpenIDStore(OpenIDStore): ...@@ -34,51 +34,51 @@ class DjangoOpenIDStore(OpenIDStore):
def __init__(self): def __init__(self):
log.info('DjangoStore cache:' + str(cache.__class__)) log.info('DjangoStore cache:' + str(cache.__class__))
def storeAssociation(self, server_url, association): def storeAssociation(self, server_url, assoc):
key = get_url_key(server_url) key = get_url_key(server_url)
log.info('storeAssociation {0}'.format(key)) log.info('storeAssociation {0}'.format(key))
associations = cache.get(key, {}) associations = cache.get(key, {})
associations[association.handle] = association associations[assoc.handle] = assoc
cache.set(key, associations, DEFAULT_ASSOCIATION_TIMEOUT) cache.set(key, associations, DEFAULT_ASSOCIATIONS_TIMEOUT)
def getAssociation(self, server_url, handle=None): def getAssociation(self, server_url, handle=None):
key = get_url_key(server_url) key = get_url_key(server_url)
log.info('getAssociation {0}'.format(key)) log.info('getAssociation {0}'.format(key))
associations = cache.get(key) associations = cache.get(key, {})
association = None assoc = None
if associations: if handle is None:
if handle is None: # get best association
# get best association valid_assocs = [a for a in associations if a.getExpiresIn() > 0]
valid = [a for a in associations if a.getExpiresIn() > 0] if valid_assocs:
if valid: valid_assocs.sort(lambda a: a.getExpiresIn(), reverse=True)
association = valid[0] assoc = valid_assocs.sort[0]
else: else:
association = associations.get(handle) assoc = associations.get(handle)
# check expiration and remove if it has expired # check expiration and remove if it has expired
if association and association.getExpiresIn() <= 0: if assoc and assoc.getExpiresIn() <= 0:
if handle is None: if handle is None:
cache.delete(key) cache.delete(key)
else: else:
associations.pop(handle) associations.pop(handle)
cache.set(key, association, DEFAULT_ASSOCIATION_TIMEOUT) cache.set(key, associations, DEFAULT_ASSOCIATIONS_TIMEOUT)
association = None assoc = None
return association return assoc
def removeAssociation(self, server_url, handle): def removeAssociation(self, server_url, handle):
key = get_url_key(server_url) key = get_url_key(server_url)
log.info('removeAssociation {0}'.format(key)) log.info('removeAssociation {0}'.format(key))
associations = cache.get(key) associations = cache.get(key, {})
removed = False removed = False
...@@ -87,9 +87,9 @@ class DjangoOpenIDStore(OpenIDStore): ...@@ -87,9 +87,9 @@ class DjangoOpenIDStore(OpenIDStore):
cache.delete(key) cache.delete(key)
removed = True removed = True
else: else:
association = associations.pop(handle) assoc = associations.pop(handle)
if association: if assoc:
cache.set(key, association, DEFAULT_ASSOCIATION_TIMEOUT) cache.set(key, associations, DEFAULT_ASSOCIATIONS_TIMEOUT)
removed = True removed = True
return removed return removed
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment