Navigation Menu

Skip to content

Commit

Permalink
Migrates the older views to work with couchdbkit views.
Browse files Browse the repository at this point in the history
  • Loading branch information
theju committed Jun 18, 2011
1 parent d56a0fa commit f1bfd8c
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 39 deletions.
@@ -1,5 +1,5 @@
function(doc) {
if (doc.doc_type == 'Nonce') {
if (doc.doc_type == 'Association') {
emit(doc.server_url, doc);
}
}
30 changes: 15 additions & 15 deletions django_couchdb_utils/openid_consumer/consumer.py
Expand Up @@ -27,10 +27,6 @@ class CookieConsumer(LoginConsumer, DjangoOpenidCookieConsumer):
pass

class AuthConsumer(SessionConsumer, DjangoOpenidAuthConsumer):
def __init__(self):
self.auth_db = User.get_db()
super(AuthConsumer, self).__init__()

def user_can_login(self, request, user):
"Over-ride for things like user bans or account e-mail validation"
return user.is_active
Expand All @@ -49,8 +45,8 @@ def do_associate(self, request):
except signed.BadSignature:
return self.show_error(request, self.csrf_failed_message)
# Associate openid with their account, if it isn't already
temp_db = UserOpenidAssociation.get_db()
if not len(temp_db.view('openid_view/all', key = openid)):
if not len(UserOpenidAssociation.view('%s/openid_view' % UserOpenidAssociation._meta.app_label,
key = openid), include_docs=True):
uoa = UserOpenidAssociation(user_id = request.user.id,
openid = openid,
created = datetime.datetime.now())
Expand All @@ -65,7 +61,6 @@ def do_associations(self, request):
if not request.user.is_authenticated():
return self.need_authenticated_user(request)
message = None
temp_db = UserOpenidAssociation.get_db()
if request.method == 'POST':
if 'todelete' in request.POST:
# Something needs deleting; find out what
Expand All @@ -78,17 +73,19 @@ def do_associations(self, request):
message = self.associate_tampering_message
else:
# It matches! Delete the OpenID relationship
row = temp_db.view('openid_view/all', key=todelete['openid']).first()
row = UserOpenidAssociation.view('%s/openid_view' % UserOpenidAssociation._meta.app_label,
key=todelete['openid'], include_docs=True).first()
if row.temp == True:
temp_db.delete(row)
row.delete()
message = self.association_deleted_message % (
todelete['openid']
)
except signed.BadSignature:
message = self.associate_tampering_message
# We construct a button to delete each existing association
openids = []
for association in temp_db.view('openid_view/all'):
for association in UserOpenidAssociation.view('%s/openid_view' % UserOpenidAssociation._meta.app_label,
include_docs=True):
openids.append({
'openid': association['openid'],
'button': signed.dumps({
Expand All @@ -108,25 +105,28 @@ def do_associations(self, request):


def lookup_openid(self, request, identity_url):
temp_db = UserOpenidAssociation.get_db()
try:
openid = temp_db.view('openid_view/all', key=identity_url).first()
openid = UserOpenidAssociation.view('%s/openid_view' % UserOpenidAssociation._meta.app_label,
key=identity_url, include_docs=True).first()
except (IndexError, ResourceNotFound):
return []
try:
return User.view('%s/users_by_username', key=openid['user_id'], include_docs=True).first()
return User.view('%s/users_by_username', key=openid['user_id'],
include_docs=True).first()
except ResourceNotFound:
return []

def lookup_users_by_email(self, email):
try:
return User.view('%s/users_by_email' % User._meta.app_label, key=email, include_docs=True).first()
return User.view('%s/users_by_email' % User._meta.app_label,
key=email, include_docs=True).first()
except ResourceNotFound:
return []

def lookup_user_by_username(self, username):
try:
return User.view('%s/users_by_username' % User._meta.app_label, key=username, include_docs=True).first()
return User.view('%s/users_by_username' % User._meta.app_label,
key=username, include_docs=True).first()
except (IndexError, ResourceNotFound):
return []

Expand Down
36 changes: 16 additions & 20 deletions django_couchdb_utils/openid_consumer/models.py
Expand Up @@ -34,14 +34,6 @@ class Meta:
app_label = "django_couchdb_utils_openid_consumer"

class DjangoCouchDBOpenIDStore(DjangoOpenIDStore):
def __init__(self):
# This constructor argument is specific only to
# the couchdb store. It accepts a couchdb db
# instance
self.nonce_db = Nonce.get_db()
self.assoc_db = Association.get_db()
self.user_openid_db = UserOpenidAssociation.get_db()

def storeAssociation(self, server_url, association):
assoc = Association(
server_url = server_url,
Expand All @@ -56,10 +48,11 @@ def storeAssociation(self, server_url, association):
def getAssociation(self, server_url, handle=None):
assocs = []
if handle is not None:
assocs = self.assoc_db.view('url_handle_view/all', key=[server_url, handle])
assocs = Association.view('%s/url_handle_view' % Association._meta.app_label,
key=[server_url, handle], include_docs=True).all()
else:
assocs = self.assoc_db.view('url_view/all', key=server_url)
assocs = assocs.iterator()
assocs = Association.view('%s/url_view' % Association._meta.app_label,
key=server_url, include_docs=True).all()
associations = []
try:
for assoc in assocs:
Expand All @@ -79,20 +72,21 @@ def getAssociation(self, server_url, handle=None):

def removeAssociation(self, server_url, handle):
try:
assocs = self.assoc_db.view('url_handle_view/all', key=[server_url, handle]).all()
assocs = Association.view('%s/url_handle_view' % Association._meta.app_label,
key=[server_url, handle], include_docs=True).all()
except ResourceNotFound:
assocs = []
for assoc in assocs:
self.assoc_db.delete(assoc)
assoc.delete()
return len(assocs)

def useNonce(self, server_url, timestamp, salt):
# Has nonce expired?
if abs(timestamp - time.time()) > openid.store.nonce.SKEW:
return False
try:
nonce = self.nonce_db.view('url_timestamp_salt_view/all',
key=[server_url, timestamp, salt]).first()
nonce = Nonce.view('%s/url_timestamp_salt_view' % Nonce._meta.app_label,
key=[server_url, timestamp, salt], include_docs=True).first()
except (IndexError, ResourceNotFound):
nonce = Nonce(
server_url = server_url,
Expand All @@ -101,16 +95,18 @@ def useNonce(self, server_url, timestamp, salt):
)
nonce.store()
return True
self.nonce_db.delete(nonce)
nonce.delete()
return False

def cleanupNonce(self):
max_key_val = time.time() - openid.store.nonce.SKEW
nonces = self.nonce_db.view('timestamp_view/all', endkey=max_key_val)
nonces = Nonce.view('%s/timestamp_view' % Nonce._meta.app_label,
endkey=max_key_val, include_docs=True)
for nonce in nonces:
self.nonce_db.delete(nonce)
nonce.delete()

def cleaupAssociations(self):
assocs = self.assoc_db.view('issued_lifetime_view/all', endkey=time.time())
assocs = Association.view('%s/issued_lifetime_view' % Association._meta.app_label,
endkey=time.time(), include_docs=True)
for assoc in assocs:
self.assoc_db.delete(assoc)
assoc.delete()
8 changes: 5 additions & 3 deletions django_couchdb_utils/openid_consumer/registration.py
Expand Up @@ -62,7 +62,8 @@ def suggest_nickname(self, nickname):
username_exists = True
while username_exists:
try:
username_exists = User.view('%s/users_by_username' % User._meta.app_label, key=nickname).count()
username_exists = User.view('%s/users_by_username' % User._meta.app_label,
key=nickname, include_docs=True).count()
except ResourceNotFound:
username_exists = False
if not username_exists:
Expand All @@ -89,7 +90,7 @@ def do_password(self, request):
if form.is_valid():
u = request.user
u.set_password(form.cleaned_data['password'])
u.store(self.auth_db)
u.store()
return self.show_password_has_been_set(request)
else:
form = ChangePasswordForm(request.user)
Expand Down Expand Up @@ -122,7 +123,8 @@ def do_c(self, request, token = ''):
if self.user_is_unconfirmed(user):
# Confirm them
try:
user = User.view('%s/users_by_username' % User._meta.app_label, key=user.username, include_docs=True).first()
user = User.view('%s/users_by_username' % User._meta.app_label,
key=user.username, include_docs=True).first()
except ResourceNotFound:
user = None
if user:
Expand Down

0 comments on commit f1bfd8c

Please sign in to comment.