summaryrefslogtreecommitdiff
path: root/src/leap/mx/couchdbhelper.py
diff options
context:
space:
mode:
authorKali Kaneko <kali@leap.se>2016-04-18 12:04:34 -0400
committerKali Kaneko <kali@leap.se>2016-04-18 12:04:34 -0400
commit3a25464e92acd3fb0192c2d438a2c2122ca526fd (patch)
tree531689dd646f174f3e9ce9acbdf0a621da827210 /src/leap/mx/couchdbhelper.py
parent881cab15bd331c4a8f58d13c0a4e35ebfb324442 (diff)
parente2a19eaf0ceca35acaedafb3796c66b562e825da (diff)
Merge tag '0.8.0'
Tag leap.mx version 0.8.0 # gpg: Signature made Mon 18 Apr 2016 12:03:24 PM BOT # gpg: using RSA key 1CAF6C5B9F720808 # gpg: Good signature from "Kaliyuga <kaliyuga@riseup.net>" [ultimate] # gpg: aka "Kali Kaneko (leap communications) <kali@leap.se>" [ultimate]
Diffstat (limited to 'src/leap/mx/couchdbhelper.py')
-rw-r--r--src/leap/mx/couchdbhelper.py70
1 files changed, 67 insertions, 3 deletions
diff --git a/src/leap/mx/couchdbhelper.py b/src/leap/mx/couchdbhelper.py
index 1752b4e..e9cf4a4 100644
--- a/src/leap/mx/couchdbhelper.py
+++ b/src/leap/mx/couchdbhelper.py
@@ -23,7 +23,9 @@ maps, user UUIDs, and GPG keyIDs.
from paisley import client
+from twisted.internet import defer
from twisted.python import log
+from leap.soledad.common.couch import CouchDatabase
class ConnectedCouchDB(client.CouchDB):
@@ -50,6 +52,10 @@ class ConnectedCouchDB(client.CouchDB):
:param str password: (optional) The password for authorization.
:type password: str
"""
+ self._mail_couch_url = "http://%s:%s@%s:%s" % (username,
+ password,
+ host,
+ port)
client.CouchDB.__init__(self,
host,
port=port,
@@ -94,9 +100,10 @@ class ConnectedCouchDB(client.CouchDB):
pubkey = None
if result["rows"]:
doc = result["rows"][0]["doc"]
- uuid = doc["user_id"]
- if "keys" in doc:
- pubkey = doc["keys"]["pgp"]
+ if doc["enabled"]:
+ uuid = doc["user_id"]
+ if "keys" in doc:
+ pubkey = doc["keys"]["pgp"]
return uuid, pubkey
d.addCallback(_get_uuid_and_pubkey_cbk)
@@ -131,3 +138,60 @@ class ConnectedCouchDB(client.CouchDB):
d.addCallbacks(_get_pubkey_cbk, log.err)
return d
+
+ def getCertExpiry(self, fingerprint):
+ """
+ Query couch and return a deferred that will fire with the expiration
+ date for the cert with the given fingerprint.
+
+ :param fingerprint: The cert fingerprint
+ :type fingerprint: str
+
+ :return: A deferred that will fire with the cert expiration date as a
+ str.
+ :rtype: Deferred
+ """
+ d = self.openView(docId="Identity",
+ viewId="cert_expiry_by_fingerprint/",
+ key=fingerprint,
+ reduce=False,
+ include_docs=True)
+
+ def _get_cert_expiry_cbk(result):
+ try:
+ expiry = result["rows"][0]["value"]
+ except (KeyError, IndexError):
+ expiry = None
+ return expiry
+
+ d.addCallback(_get_cert_expiry_cbk)
+ return d
+
+ def put_doc(self, uuid, doc):
+ """
+ Update a document.
+
+ If the document currently has conflicts, put will fail.
+ If the database specifies a maximum document size and the document
+ exceeds it, put will fail and raise a DocumentTooBig exception.
+
+ :param uuid: The uuid of a user
+ :type uuid: str
+ :param doc: A Document with new content.
+ :type doc: leap.soledad.common.couch.CouchDocument
+
+ :return: A deferred which fires with the new revision identifier for
+ the document if the Document object has being updated, or
+ which fails with CouchDBError if there was any error.
+ """
+ # TODO: that should be implemented with paisley
+ url = self._mail_couch_url + "/user-%s" % (uuid,)
+ try:
+ db = CouchDatabase.open_database(url, create=False)
+ return defer.succeed(db.put_doc(doc))
+ except Exception as e:
+ return defer.fail(CouchDBError(e.message))
+
+
+class CouchDBError(Exception):
+ pass