]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#1304] Fixed unicode handling
authorFrancis Dupont <fdupont@isc.org>
Sat, 11 Jul 2020 16:42:06 +0000 (18:42 +0200)
committerFrancis Dupont <fdupont@isc.org>
Sat, 12 Sep 2020 08:49:43 +0000 (10:49 +0200)
src/bin/shell/kea-shell.in
src/bin/shell/tests/shell_unittest.py.in

index d4fa02f10134c7f177ad29d8e1739edbb3e6ef1f..32125655c22b9614e835fbfa00ba94cd638b5aab 100644 (file)
@@ -30,9 +30,12 @@ from kea_conn import CARequest # CAResponse
 if sys.version_info[0] == 2:
     # This is Python 2.x
     import kea_connector2 as kea_connector
+    def auth8(s):
+        return unicode(s, 'utf-8')
 elif sys.version_info[0] == 3:
     # This is Python 3.x
     import kea_connector3 as kea_connector
+    auth8 = str
 else:
     # This is... have no idea what it is.
     raise SystemExit("Unknown python version:" + str(sys.version_info[0]))
@@ -70,9 +73,9 @@ def shell_body():
     parser.add_argument('--service', nargs="?", action="append",
                         help='target spcified service. If not specified,'
                         'control agent will receive command.')
-    parser.add_argument('--auth-user', type=str, default='',
+    parser.add_argument('--auth-user', type=auth8, default='',
                         help='Basic HTTP authentication user')
-    parser.add_argument('--auth-password', type=str, default='',
+    parser.add_argument('--auth-password', type=auth8, default='',
                         help='Basic HTTP authentication password')
     parser.add_argument('command', type=str, nargs="?",
                         default='list-commands',
@@ -94,9 +97,9 @@ def shell_body():
     params.http_port = cmd_args.port
     params.path += cmd_args.path
     if cmd_args.auth_user is not '':
-        user = cmd_args.auth_user.encode('latin1')
-        password = cmd_args.auth_password.encode('latin1')
-        secret = b':'.join((user, password))
+        user = cmd_args.auth_user
+        password = cmd_args.auth_password
+        secret = b':'.join((user.encode('utf-8'), password.encode('utf-8')))
         if sys.version_info[0] == 3:
             params.auth = b64encode(secret).strip().decode('ascii')
         else:
index 74c320749fc65fab01c328644bfdd77cab53f59d..b65b65c7278376f9ae8bb1dcd5968b88e5ab31f9 100644 (file)
@@ -147,8 +147,8 @@ class CARequestUnitTest(unittest.TestCase):
         """
         user = 'foo'
         password = 'bar'
-        buser = user.encode('latin1')
-        bpassword = password.encode('latin1')
+        buser = user.encode('utf-8')
+        bpassword = password.encode('utf-8')
         secret = b':'.join((buser, bpassword))
         self.assertEqual(b'foo:bar', secret)
         if sys.version_info[0] == 3:
@@ -157,6 +157,26 @@ class CARequestUnitTest(unittest.TestCase):
             auth = b64encode(secret).strip().encode('ascii')
         self.assertEqual('Zm9vOmJhcg==', auth)
 
+    def test_auth_unicode(self):
+        """
+        This test check if the basic HTTP authentication uses UTF-8 encoding.
+        """
+        if sys.version_info[0] == 3:
+            user = 'libert\xe9'
+            password = '\xe9galit\xe9'
+        else:
+            user = u'libert\xe9'
+            password = u'\xe9galit\xe9'
+        buser = user.encode('utf-8')
+        bpassword = password.encode('utf-8')
+        secret = b':'.join((buser, bpassword))
+        self.assertEqual(b'libert\xc3\xa9:\xc3\xa9galit\xc3\xa9', secret)
+        if sys.version_info[0] == 3:
+            auth = b64encode(secret).strip().decode('ascii')
+        else:
+            auth = b64encode(secret).strip().encode('ascii')
+        self.assertEqual('bGliZXJ0w6k6w6lnYWxpdMOp', auth)
+
     def test_header_auth(self):
         """
         This test checks if the basic HTTP authentication header is