From 9e1efff826fe2ed52561a6398030bf534f0f99a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adolfo=20G=C3=B3mez=20Garc=C3=ADa?= Date: Fri, 12 Sep 2014 17:28:23 +0200 Subject: [PATCH] Adding six to modules, and fixing up a few related issues --- server/src/uds/core/auths/auth.py | 5 ++- server/src/uds/core/managers/CryptoManager.py | 37 ++++++++++++------- .../src/uds/core/managers/DownloadsManager.py | 7 +++- server/src/uds/models/Authenticator.py | 12 +++--- server/src/uds/models/User.py | 3 +- server/src/uds/web/views.py | 4 ++ 6 files changed, 46 insertions(+), 22 deletions(-) diff --git a/server/src/uds/core/auths/auth.py b/server/src/uds/core/auths/auth.py index 280f58edd..c43fd5b68 100644 --- a/server/src/uds/core/auths/auth.py +++ b/server/src/uds/core/auths/auth.py @@ -50,7 +50,7 @@ from uds.models import User import logging -__updated__ = '2014-09-09' +__updated__ = '2014-09-12' logger = logging.getLogger(__name__) authLogger = logging.getLogger('authLog') @@ -289,6 +289,9 @@ def webLogout(request, exit_url=None): request.session.clear() if exit_url is None: exit_url = GlobalConfig.LOGIN_URL.get() + if GlobalConfig.REDIRECT_TO_HTTPS.getBool() is True: + exit_url = exit_url.replace('http://', 'https://') + # Try to delete session return HttpResponseRedirect(request.build_absolute_uri(exit_url)) diff --git a/server/src/uds/core/managers/CryptoManager.py b/server/src/uds/core/managers/CryptoManager.py index ba6c7bf49..7a6786412 100644 --- a/server/src/uds/core/managers/CryptoManager.py +++ b/server/src/uds/core/managers/CryptoManager.py @@ -40,6 +40,7 @@ import hashlib import array import logging +import six logger = logging.getLogger(__name__) @@ -64,26 +65,34 @@ class CryptoManager(object): CryptoManager.instance = CryptoManager() return CryptoManager.instance - def encrypt(self, string): - atfork() - return self._rsa.encrypt(string.encode('utf-8'), '')[0].encode(CryptoManager.CODEC) + def encrypt(self, value): + if isinstance(value, six.text_type): + value = value.encode('utf-8') - def decrypt(self, string): + atfork() + return six.text_type(self._rsa.encrypt(value, six.b(''))[0].encode(CryptoManager.CODEC)) + + def decrypt(self, value): + if isinstance(value, six.text_type): + value = value.encode('utf-8') # import inspect try: atfork() - return self._rsa.decrypt(string.decode(CryptoManager.CODEC)).decode('utf-8') + return six.text_type(self._rsa.decrypt(value.decode(CryptoManager.CODEC)).decode('utf-8')) except: - logger.exception('Decripting: {0}'.format(string)) + logger.exception('Decripting: {0}'.format(value)) # logger.error(inspect.stack()) return 'decript error' def xor(self, s1, s2): - s1, s2 = s1.encode('utf-8'), s2.encode('utf-8') + if isinstance(s1, six.text_type): + s1 = s1.encode('utf-8') + if isinstance(s2, six.text_type): + s2 = s2.encode('utf-8') mult = (len(s1) / len(s2)) + 1 - s1 = array.array(b'B', s1) - s2 = array.array(b'B', s2 * mult) - return array.array(b'B', (s1[i] ^ s2[i] for i in range(len(s1)))).tostring() + s1 = array.array(str('B'), s1) + s2 = array.array(str('B'), s2 * mult) + return six.binary_type(array.array(str('B'), (s1[i] ^ s2[i] for i in range(len(s1)))).tostring()).decode('utf-8') def loadPrivateKey(self, rsaKey): try: @@ -103,8 +112,10 @@ class CryptoManager(object): return certificate.replace('-----BEGIN CERTIFICATE-----', '').replace('-----END CERTIFICATE-----', '').replace('\n', '') def hash(self, value): + if isinstance(value, six.text_type): + value = value.encode('utf-8') + if value is '' or value is None: return '' - if isinstance(value, unicode): - value = value.encode('utf-8') - return hashlib.sha1(value).hexdigest() + + return six.text_type(hashlib.sha1(value).hexdigest()) diff --git a/server/src/uds/core/managers/DownloadsManager.py b/server/src/uds/core/managers/DownloadsManager.py index 5a9fb5512..d0b59d9ad 100644 --- a/server/src/uds/core/managers/DownloadsManager.py +++ b/server/src/uds/core/managers/DownloadsManager.py @@ -36,6 +36,9 @@ import os import uuid from django.http import HttpResponse, Http404 from django.core.servers.basehttp import FileWrapper + +import six + import logging logger = logging.getLogger(__name__) @@ -49,7 +52,7 @@ class DownloadsManager(object): import os.path, sys DownloadsManager.manager().registerDownloadable('test.exe', _('comments for test'), - os.path.dirname(sys.modules[__package__].__file__) + '/files/test.exe', + os.path.join(os.path.dirname(sys.modules[__package__].__file__), 'files/test.exe'), 'application/x-msdos-program') ''' _manager = None @@ -71,7 +74,7 @@ class DownloadsManager(object): @param path: path to file @params zip: If download as zip ''' - _id = unicode(uuid.uuid5(self._namespace, str(name))) + _id = six.text_type(uuid.uuid5(self._namespace, six.binary_type(name))) self._downloadables[_id] = {'name': name, 'comment': comment, 'path': path, 'mime': mime} def getDownloadables(self): diff --git a/server/src/uds/models/Authenticator.py b/server/src/uds/models/Authenticator.py index c8a3704bd..ef7e7c757 100644 --- a/server/src/uds/models/Authenticator.py +++ b/server/src/uds/models/Authenticator.py @@ -33,9 +33,11 @@ from __future__ import unicode_literals -__updated__ = '2014-09-10' +__updated__ = '2014-09-12' from django.db import models +from django.utils.encoding import python_2_unicode_compatible + from uds.core.Environment import Environment from uds.core.util import log from django.db.models import signals @@ -47,7 +49,7 @@ import logging logger = logging.getLogger(__name__) - +@python_2_unicode_compatible class Authenticator(models.Model): ''' This class represents an Authenticator inside the platform. @@ -179,9 +181,6 @@ class Authenticator(models.Model): except Exception: return falseIfNotExists - def __unicode__(self): - return u"{0} of type {1} (id:{2})".format(self.name, self.data_type, self.id) - @staticmethod def all(): ''' @@ -211,5 +210,8 @@ class Authenticator(models.Model): logger.debug('Before delete auth '.format(toDelete)) + def __str__(self): + return u"{0} of type {1} (id:{2})".format(self.name, self.data_type, self.id) + # Connects a pre deletion signal to Authenticator signals.pre_delete.connect(Authenticator.beforeDelete, sender=Authenticator) diff --git a/server/src/uds/models/User.py b/server/src/uds/models/User.py index c41a12a2e..ef56847dc 100644 --- a/server/src/uds/models/User.py +++ b/server/src/uds/models/User.py @@ -33,7 +33,7 @@ from __future__ import unicode_literals -__updated__ = '2014-04-24' +__updated__ = '2014-09-12' from django.db import models from uds.core.util import log @@ -126,6 +126,7 @@ class User(models.Model): def logout(self): ''' Invoked to log out this user + Returns the url where to redirect user, or None if default url will be used ''' return self.getManager().logout(self.name) diff --git a/server/src/uds/web/views.py b/server/src/uds/web/views.py index 47c87f34c..9d0279ff5 100644 --- a/server/src/uds/web/views.py +++ b/server/src/uds/web/views.py @@ -85,8 +85,10 @@ def login(request, smallName=None): logger.debug('Small name: {0}'.format(smallName)) + logger.debug(request.method) if request.method == 'POST': if 'uds' not in request.COOKIES: + logger.debug('Request does not have uds cookie') return errors.errorView(request, errors.COOKIES_NEEDED) # We need cookies to keep session data request.session.cycle_key() form = LoginForm(request.POST, smallName=smallName) @@ -109,6 +111,7 @@ def login(request, smallName=None): authLogLogin(request, authenticator, userName, java, os, 'Temporarily blocked') else: user = authenticate(userName, form.cleaned_data['password'], authenticator) + logger.debug('User: {}'.format(user)) if user is None: logger.debug("Invalid credentials for user {0}".format(userName)) @@ -117,6 +120,7 @@ def login(request, smallName=None): form.add_form_error('Invalid credentials') authLogLogin(request, authenticator, userName, java, os, 'Invalid credentials') else: + logger.debug('User {} has logged in'.format(userName)) cache.remove(cacheKey) # Valid login, remove cached tries response = HttpResponseRedirect(reverse('uds.web.views.index')) webLogin(request, response, user, form.cleaned_data['password'])