Skip to content

Commit c91208f

Browse files
authored
Merge pull request apache#1087 from datastax/python-1241
PYTHON-1241: Fix PlainTextAuthProvider fails with unicode chars and Python3
2 parents 25f9209 + 86168e0 commit c91208f

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

CHANGELOG.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Features
1111
Bug Fixes
1212
---------
1313
* Unable to connect to a cloud cluster using Ubuntu 20.04 (PYTHON-1238)
14+
* PlainTextAuthProvider fails with unicode chars and Python3 (PYTHON-1241)
1415
* [GRAPH] Can't write data in a Boolean field using the Fluent API (PYTHON-1239)
1516
* [GRAPH] Fix elementMap() result deserialization (PYTHON-1233)
1617

cassandra/auth.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,8 @@ def get_initial_challenge(self):
277277

278278
def evaluate_challenge(self, challenge):
279279
if challenge == six.b('PLAIN-START'):
280-
return six.b("\x00%s\x00%s" % (self.username, self.password))
280+
data = "\x00%s\x00%s" % (self.username, self.password)
281+
return data if six.PY2 else data.encode()
281282
raise Exception('Did not receive a valid challenge response from server')
282283

283284

tests/unit/test_auth.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# -*- coding: utf-8 -*-
2+
# # Copyright DataStax, Inc.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
import six
17+
from cassandra.auth import PlainTextAuthenticator
18+
19+
try:
20+
import unittest2 as unittest
21+
except ImportError:
22+
import unittest # noqa
23+
24+
25+
class TestPlainTextAuthenticator(unittest.TestCase):
26+
27+
def test_evaluate_challenge_with_unicode_data(self):
28+
authenticator = PlainTextAuthenticator("johnӁ", "doeӁ")
29+
self.assertEqual(
30+
authenticator.evaluate_challenge(six.ensure_binary('PLAIN-START')),
31+
six.ensure_binary("\x00johnӁ\x00doeӁ")
32+
)

0 commit comments

Comments
 (0)