utils.py 982 B

123456789101112131415161718192021222324252627282930313233
  1. # This file is dual licensed under the terms of the Apache License, Version
  2. # 2.0, and the BSD License. See the LICENSE file in the root of this repository
  3. # for complete details.
  4. from __future__ import absolute_import, division, print_function
  5. import base64
  6. from six.moves.urllib.parse import quote, urlencode
  7. def _generate_uri(hotp, type_name, account_name, issuer, extra_parameters):
  8. parameters = [
  9. ("digits", hotp._length),
  10. ("secret", base64.b32encode(hotp._key)),
  11. ("algorithm", hotp._algorithm.name.upper()),
  12. ]
  13. if issuer is not None:
  14. parameters.append(("issuer", issuer))
  15. parameters.extend(extra_parameters)
  16. uriparts = {
  17. "type": type_name,
  18. "label": (
  19. "%s:%s" % (quote(issuer), quote(account_name))
  20. if issuer
  21. else quote(account_name)
  22. ),
  23. "parameters": urlencode(parameters),
  24. }
  25. return "otpauth://{type}/{label}?{parameters}".format(**uriparts)