diff --git a/sso/models.py b/sso/models.py index ccb0376..d7481bf 100644 --- a/sso/models.py +++ b/sso/models.py @@ -17,6 +17,9 @@ class CorporateOnlyService(Exception): class ExistingUser(Exception): pass +class ServiceError(Exception): + pass + ## Models class SSOUser(models.Model): @@ -129,6 +132,8 @@ class ServiceAccount(models.Model): reddit = RedditAccount.objects.filter(user=self.user) self.service_uid = api.add_user(self.username, self.password, user=self.user, character=self.character, eveapi=eveapi, reddit=reddit) + if not self.service_uid: + raise ServiceError('Error occured while trying to create the Service Account, please try again later') else: raise ExistingUser('Username %s has already been took' % self.username) else: diff --git a/sso/services/jabber/__init__.py b/sso/services/jabber/__init__.py index 0566f4c..41f56f5 100644 --- a/sso/services/jabber/__init__.py +++ b/sso/services/jabber/__init__.py @@ -16,7 +16,6 @@ class JabberService(BaseService): if settings.JABBER_METHOD == "xmpp": self.method = "xmpp" self.jabberadmin = JabberAdmin(settings.JABBER_SERVER, settings.JABBER_AUTH_USER, settings.JABBER_AUTH_PASSWD) - self.jabberadmin.connect() else: self.method = "cmd" self.ejctl = eJabberdCtl(sudo=settings.JABBER_SUDO) diff --git a/sso/services/jabber/xmppclient.py b/sso/services/jabber/xmppclient.py index ebb9c01..9828d10 100644 --- a/sso/services/jabber/xmppclient.py +++ b/sso/services/jabber/xmppclient.py @@ -22,14 +22,14 @@ class JabberAdmin(): self._client.disconnect() def connect(self): + if not hasattr(self, '_client'): + client = xmpp.Client(self.jid.getDomain(), debug=[]) - client = xmpp.Client(self.jid.getDomain(), debug=[]) + client.connect(server=('dredd.it', 5222)) + client.auth(self.username, self.password) + client.sendInitPresence() - client.connect(server=('dredd.it', 5222)) - client.auth(self.username, self.password) - client.sendInitPresence() - - self._client = client + self._client = client def _construct_iq_req(self, xmlns, node): n = xmpp.Node('command', attrs={'xmlns': xmlns, 'node': node}) @@ -49,6 +49,10 @@ class JabberAdmin(): def adduser(self, username, password): + try: + self.connect() + except: + return False # Send request and get the Session ID resp = self._client.SendAndWaitForResponse(self._construct_iq_req('http://jabber.org/protocol/commands', 'http://jabber.org/protocol/admin#add-user')) sessionid = resp.getTagAttr('command','sessionid') @@ -70,6 +74,10 @@ class JabberAdmin(): def deluser(self, username): + try: + self.connect() + except: + return False # Send request and get the Session ID resp = self._client.SendAndWaitForResponse(self._construct_iq_req('http://jabber.org/protocol/commands', 'http://jabber.org/protocol/admin#delete-user')) sessionid = resp.getTagAttr('command','sessionid') @@ -88,6 +96,10 @@ class JabberAdmin(): return False def resetpassword(self, username, password): + try: + self.connect() + except: + return False # Send request and get the Session ID resp = self._client.SendAndWaitForResponse(self._construct_iq_req('http://jabber.org/protocol/commands', 'http://jabber.org/protocol/admin#change-user-password')) sessionid = resp.getTagAttr('command','sessionid') @@ -108,6 +120,10 @@ class JabberAdmin(): def checkuser(self, username): + try: + self.connect() + except: + return False # Send request and get the Session ID resp = self._client.SendAndWaitForResponse(self._construct_iq_req('http://jabber.org/protocol/commands', 'http://jabber.org/protocol/admin#get-user-password')) sessionid = resp.getTagAttr('command','sessionid') diff --git a/sso/views.py b/sso/views.py index 9ae060d..27fc0ea 100644 --- a/sso/views.py +++ b/sso/views.py @@ -130,6 +130,8 @@ def service_add(request): acc.save() except ExistingUser: error = "User by this name already exists, your account has not been created" + except ServiceError: + error = "A error occured while trying to create the Service Account, please try again later" else: error = None