summaryrefslogtreecommitdiffstats
path: root/py-bin
diff options
context:
space:
mode:
authoralice <alice@immerda.ch>2012-08-30 20:26:17 +0200
committeralice <alice@immerda.ch>2012-10-19 15:20:06 +0200
commitad515064bad23936f0f0be9a393db2af3f129a57 (patch)
tree5cc30db6e2f61eb1c1c565f58055991249a2e55d /py-bin
parentd91ede1c2a0ce3f0e0ba56da49fa5720231d1a8a (diff)
trying to glue the loose ends together
Diffstat (limited to 'py-bin')
-rw-r--r--py-bin/jabberman.py113
1 files changed, 104 insertions, 9 deletions
diff --git a/py-bin/jabberman.py b/py-bin/jabberman.py
index ec7c20c..fd131da 100644
--- a/py-bin/jabberman.py
+++ b/py-bin/jabberman.py
@@ -4,7 +4,7 @@ import shelve, sqlite3, atexit, bcrypt, sha, hmac, random, os, time, re, datetim
import config
from ejabberdctl import EJabberdCtl
-class JabberUser:
+class JabberUserOld:
def __init__(self, user_id):
self.user, self.domain = user_id.split("@")
self.accounts = []
@@ -67,10 +67,10 @@ class JabberUser:
def remove_account(self, jabber_id):
self.accounts.remove(jabber_id)
-class JabberAccount:
+class JabberAccountOld:
def __init__(self, jabber_id):
self.user, self.server = jabber_id.split("@")
-
+
def get_jabber_id(self):
return self.user + "@" + self.server
@@ -80,17 +80,112 @@ class JabberAccount:
# FIXME: handle transactions properly
# TODO: create dummy "unit-test" based on a db fixture
-class JabberUser2:
+class JabberUser:
def __init__(self, id, jid, password, web_user_id):
self.id, self.jid, self.password, self.web_user_id = id, jid, password, web_user_id
+
+ def get_jabber_id(self):
+ return self.jid
+
+ @staticmethod
+ def generate_token():
+ data = str(random.getrandbits(256)) + str(time.time()*1000) + str(os.getpid())
+ return "+" + hmac.new(config.the_secret, data, sha).hexdigest()
+
+ def set_token(self, token):
+ self.token = token
-class WebUser2:
+class WebUser:
def __init__(self, id, email, token, token_date):
self.id, self.email, self.token, self.token_date = id, email, token, token_date
-class JabberDB2:
+ def get_primary_jid(self):
+ user, server = self.email.split("@")
+ return user + "@jabber." + server
+
+class JabberDB:
def __init__(self):
self.__connection = sqlite3.connect(config.sqlite_path)
+ atexit.register(self.__connection.close) # FIXME: test that!
+
+ def login_user(self, user_id, password):
+ user = self.get_web_user(user_id)
+ primary_jid = user.get_primary_jid()
+ if user and self.validate_jid(primary_jid, password):
+ return user
+ return None
+
+ def generate_token(self, user_id):
+ if self.get_web_user(user_id):
+ return (False, "Benutzer existiert bereits!")
+
+ return (True, JabberUser.generate_token())
+
+ def prepare_user(self, user_id):
+ if self.get_web_user(user_id):
+ return (False, "Benutzer existiert bereits!")
+
+ #user = JabberUser(user_id)
+ #user.set_token(token)
+ #self.__store_user(user)
+ generate_web_user(user_id)
+
+ return (True, "Benutzer registriert, Aktivierung noch ausstehend.")
+
+ def validate_token(self, user_id, token):
+ user = self.get_web_user(user_id)
+ if not user:
+ return (False, "Zugriff verweigert.")
+ #fixme: is this actually needed?
+ return user.token == token
+
+ def activate_user(self, user_id, password, token):
+ user = self.get_web_user(user_id)
+ if not user:
+ return (False, "Zugriff verweigert.")
+
+ success = update_web_user(self, user_id, password, token)
+ if not success:
+ return (False, status)
+
+ return (True, user)
+
+ def add_account(self, user_id, jabber_id, password, check_only = False):
+ user = self.get_web_user(user_id)
+ if not user:
+ return (False, "Zugriff verweigert.")
+
+ account = self.is_jid(jabber_id)
+ if account:
+ return (False, "Sorry, Jabber Benutzerkonto %s bereits vergeben." % jabber_id)
+
+ if check_only:
+ return (True, "Jabber kann hinzugefuegt werden.")
+
+ self.create_jid(jabber_id, password, user.id)
+
+ return (True, "Jabber Konto hinzugefuegt.")
+
+ def remove_account(self, user_id, jabber_id, check_only = False):
+ user = self.get_web_user(user_id)
+ if (not user) or (not user.has_account(jabber_id)):
+ return (False, "Zugriff verweigert.")
+
+ if check_only:
+ return (True, "Jabber darf geloescht werden.")
+
+ self.delete_jid(jabber_id)
+
+ return (True, "Jabber Konto geloescht.")
+
+ def change_primary_jid_password(self, user_id, password):
+ user = self.get_web_user(user_id)
+ if not user:
+ return (False, "Zugriff verweigert.")
+
+ primary_jid = user.get_primary_jid()
+ self.update_jid(primary_jid, password)
+ return (True, "Passwort geaendert.")
def generate_web_user(self, email):
token = str(random.getrandbits(60))
@@ -112,7 +207,7 @@ class JabberDB2:
cur = self.__connection.cursor()
cur.execute("SELECT * FROM web_users where email=?", (email,))
row = cur.fetchone()
- if row == None
+ if row == None:
return False
web_user_id, token, token_date = row[0]
@@ -124,7 +219,7 @@ class JabberDB2:
user, domain = email.split("@")
jid = user + "@jabber." + domain
- self.create_jid(jid, plain_pwd, web_user_id):
+ self.create_jid(jid, plain_pwd, web_user_id)
return True
def validate_jid(self, jid, plain_pwd):
@@ -190,7 +285,7 @@ class JabberDB2:
-class JabberDB:
+class JabberDBOld:
def __init__(self):
self.db = shelve.open(config.jabberdb_path, 'c')
atexit.register(self.db.close)