1
0
mirror of https://github.com/dkmstr/openuds.git synced 2024-12-22 13:34:04 +03:00

Adding six to modules, and fixing up a few related issues

This commit is contained in:
Adolfo Gómez García 2014-09-12 17:28:23 +02:00
parent 42f8487e75
commit 9e1efff826
6 changed files with 46 additions and 22 deletions

View File

@ -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))

View File

@ -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())

View File

@ -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):

View File

@ -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)

View File

@ -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)

View File

@ -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'])