From b29bba23812162bcc0ff85a95344f8520d5b27c7 Mon Sep 17 00:00:00 2001 From: Nikolai R Kristiansen Date: Sun, 16 Aug 2015 21:11:58 +0200 Subject: [PATCH] Revert "Usersync: Try fixing unicode errors" This reverts commit 84ba4beb927f23e5f1398d051c5ead64b98f5e53. --- neuf_radius/utils.py | 2 +- neuf_userinfo/forms.py | 3 +-- neuf_userinfo/rijndael.py | 5 +++-- neuf_userinfo/tests.py | 45 +-------------------------------------- neuf_userinfo/utils.py | 5 +++-- neuf_userinfo/views.py | 4 ++-- userinfosite/settings.py | 2 -- 7 files changed, 11 insertions(+), 55 deletions(-) diff --git a/neuf_radius/utils.py b/neuf_radius/utils.py index fb7ba8a..63ebb67 100644 --- a/neuf_radius/utils.py +++ b/neuf_radius/utils.py @@ -37,4 +37,4 @@ def create_radius_user(username, password, dry_run=False): logger.debug('Created radius user \'{}\''.format(username)) - return True + return True \ No newline at end of file diff --git a/neuf_userinfo/forms.py b/neuf_userinfo/forms.py index e05966b..af36fca 100644 --- a/neuf_userinfo/forms.py +++ b/neuf_userinfo/forms.py @@ -159,5 +159,4 @@ class NewUserForm(forms.Form): # Rename firstname to first_name and lastname to last_name cleaned_data['first_name'] = cleaned_data.pop('firstname') cleaned_data['last_name'] = cleaned_data.pop('lastname') - - return cleaned_data + return cleaned_data \ No newline at end of file diff --git a/neuf_userinfo/rijndael.py b/neuf_userinfo/rijndael.py index 54b6dcf..9fed6f0 100644 --- a/neuf_userinfo/rijndael.py +++ b/neuf_userinfo/rijndael.py @@ -27,6 +27,7 @@ If any strings are of the wrong length a ValueError is thrown # an intellectual property claim against the reference # code, in which case it can be made public domain by # deleting all the comments and renaming all the variables + import copy import string @@ -310,7 +311,7 @@ class rijndael: result.append((S[(t[(i + s1) % BC] >> 16) & 0xFF] ^ (tt >> 16)) & 0xFF) result.append((S[(t[(i + s2) % BC] >> 8) & 0xFF] ^ (tt >> 8)) & 0xFF) result.append((S[ t[(i + s3) % BC] & 0xFF] ^ tt ) & 0xFF) - return u''.join(map(unichr, result)) + return ''.join(map(chr, result)) def decrypt(self, ciphertext): if len(ciphertext) != self.block_size: @@ -353,7 +354,7 @@ class rijndael: result.append((Si[(t[(i + s1) % BC] >> 16) & 0xFF] ^ (tt >> 16)) & 0xFF) result.append((Si[(t[(i + s2) % BC] >> 8) & 0xFF] ^ (tt >> 8)) & 0xFF) result.append((Si[ t[(i + s3) % BC] & 0xFF] ^ tt ) & 0xFF) - return u''.join(map(unichr, result)) + return ''.join(map(chr, result)) def encrypt(key, block): return rijndael(key, len(block)).encrypt(block) diff --git a/neuf_userinfo/tests.py b/neuf_userinfo/tests.py index 683977e..0fabb9f 100644 --- a/neuf_userinfo/tests.py +++ b/neuf_userinfo/tests.py @@ -1,44 +1 @@ -# coding: utf-8 -from __future__ import unicode_literals -from django.conf import settings -from django.core.urlresolvers import reverse -from django.test import TestCase -from neuf_userinfo.utils import decrypt_rijndael, encrypt_rijndael - - -class UserSyncTest(TestCase): - def test_usersync_decode_unicode_password(self): - usersync_url = reverse('usersync') - clear_password = 'berombråk123' - key = settings.INSIDE_USERSYNC_ENC_KEY - - password = encrypt_rijndael(key, clear_password) - - request_params = { - 'api_key': 'lol', - 'firstname': 'a', - 'lastname': 'b', - 'password': password, - 'email': 'a@b.com', - 'username': 'test', - 'groups': 'dns-alle' - } - response = self.client.get(usersync_url, request_params) - self.assertEqual(response.status_code, 200, response) - - -class RijndaelTest(TestCase): - def test_decode_unicode_password(self): - clear_password = 'berombråk123' - key = settings.INSIDE_USERSYNC_ENC_KEY - - password = encrypt_rijndael(key, clear_password) - self.assertEqual(decrypt_rijndael(key, password), clear_password) - - def test_decode_ascii_password(self): - clear_password = 'beromkos123' - key = settings.INSIDE_USERSYNC_ENC_KEY - - password = encrypt_rijndael(key, clear_password) - self.assertEqual(decrypt_rijndael(key, password), clear_password) - +from django.test import TestCase \ No newline at end of file diff --git a/neuf_userinfo/utils.py b/neuf_userinfo/utils.py index fb0ef47..3cc4835 100644 --- a/neuf_userinfo/utils.py +++ b/neuf_userinfo/utils.py @@ -44,14 +44,15 @@ def encrypt_rijndael(key, plaintext): for start in range(0, len(padded_text), BLOCK_SIZE): ciphertext += r.encrypt(padded_text[start:start+BLOCK_SIZE]) - encoded = base64.b64encode(ciphertext.encode('utf-8')) + encoded = base64.b64encode(ciphertext) return encoded def decrypt_rijndael(key, encoded): padded_key = key.ljust(KEY_SIZE, '\0') - ciphertext = base64.b64decode(encoded).decode('utf-8') + + ciphertext = base64.b64decode(encoded) r = rijndael.rijndael(padded_key, BLOCK_SIZE) diff --git a/neuf_userinfo/views.py b/neuf_userinfo/views.py index 825e19f..6004281 100644 --- a/neuf_userinfo/views.py +++ b/neuf_userinfo/views.py @@ -118,11 +118,11 @@ class AddNewUserView(View): """ Adds a new user to our internal services. See .utils.add_new_user for details. """ if not self.validate_api_key(request.GET.get('api_key', '')): - return JsonResponse({'errors': 'Invalid api_key'}, status=400) + return JsonResponse({'errors': 'Invalid api_key'}) form = NewUserForm(request.GET) if not form.is_valid(): - return JsonResponse({'errors': form.errors}, status=400) + return JsonResponse({'errors': form.errors}) user = form.cleaned_data logger.debug('Adding user \'{}\'.'.format(user['username'])) diff --git a/userinfosite/settings.py b/userinfosite/settings.py index dcda421..e5177e9 100644 --- a/userinfosite/settings.py +++ b/userinfosite/settings.py @@ -1,4 +1,3 @@ -# coding: utf-8 """ Django settings for this project. @@ -10,7 +9,6 @@ https://docs.djangoproject.com/en/1.6/ref/settings/ """ # Build paths inside the project like this: os.path.join(BASE_DIR, ...) -from __future__ import unicode_literals import os BASE_DIR = os.path.dirname(os.path.dirname(__file__))