pyauth / pyotp

Python One-Time Password Library
https://pyauth.github.io/pyotp/
Other
2.96k stars 323 forks source link

provisioning_uri doesn't work with unicode in python2 #21

Closed gdamjan closed 8 years ago

gdamjan commented 8 years ago
import pyotp
t = pyotp.TOTP('secret')
t.provisioning_uri(u'кирилица')

the exception is:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/damjan/.local/lib/python2.7/site-packages/pyotp/totp.py", line 54, in provisioning_uri
    return utils.build_uri(self.secret, name, issuer_name=issuer_name)
  File "/home/damjan/.local/lib/python2.7/site-packages/pyotp/utils.py", line 40, in build_uri
    'name': quote(name, safe='@'),
  File "/usr/lib/python2.7/urllib.py", line 1303, in quote
    return ''.join(map(quoter, s))
KeyError: u'\u043a'

the same code works in python3 and its str type (which is same as unicode) and seems to url-escape the string.

I'm not sure what the standards say about it though?

kislyuk commented 8 years ago

pyotp uses urllib to do the URI encoding. In python2, urllib doesn't have proper Unicode support; quote() expects bytes where it should expect a string. The solution is to either switch to Python 3, or to encode your unicode strings into byte strings before passing them over to provisioning_uri().

I could make this behavior conditional on Python version in pyotp, but I would prefer not to.

gdamjan commented 8 years ago

or just document the deficiency of python2. that would be enough for me personally, since I already use python3 for my demo stuff.

kislyuk commented 8 years ago

This is a general deficiency of Python 2, and the traceback does indicate that the error occurs in the standard library, so I see no need to note it in our documentation. Thanks for reporting though.