Allow unicode input for JOSE Base64

This commit is contained in:
Jakub Warmuz 2014-11-23 22:27:22 +01:00
parent 22bea4c975
commit 421f541271
2 changed files with 6 additions and 2 deletions

View file

@ -97,7 +97,7 @@ def jose_b64decode(arg, decoding='utf-8'):
"""JOSE Base64 decode.
:param arg: Base64 string to be decoded.
:type arg: str
:type arg: str or unicode
:param decoding: Name of the encoding to be performed after
Base64 decoding.
@ -107,5 +107,6 @@ def jose_b64decode(arg, decoding='utf-8'):
:rtype: str or unicode
"""
decoded = base64.urlsafe_b64decode(arg + '=' * (4 - (len(arg) % 4)))
ascii = arg.encode('ascii') # equivalent to str(arg), for unicode input
decoded = base64.urlsafe_b64decode(ascii + '=' * (4 - (len(ascii) % 4)))
return decoded if decoding is None else decoded.decode(decoding)

View file

@ -126,6 +126,9 @@ class JOSEB64DecodeTest(unittest.TestCase):
def test_with_encoding(self):
self.assertEqual(self._call('xIU=', 'utf-8'), u'\u0105')
def test_unicode(self):
self.assertEqual(self._call(u'YQ', None), 'a')
if __name__ == '__main__':
unittest.main()