• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
5from __future__ import absolute_import, division, print_function
6
7import base64
8
9from six.moves.urllib.parse import quote, urlencode
10
11
12def _generate_uri(hotp, type_name, account_name, issuer, extra_parameters):
13    parameters = [
14        ("digits", hotp._length),
15        ("secret", base64.b32encode(hotp._key)),
16        ("algorithm", hotp._algorithm.name.upper()),
17    ]
18
19    if issuer is not None:
20        parameters.append(("issuer", issuer))
21
22    parameters.extend(extra_parameters)
23
24    uriparts = {
25        "type": type_name,
26        "label": ("%s:%s" % (quote(issuer), quote(account_name)) if issuer
27                  else quote(account_name)),
28        "parameters": urlencode(parameters),
29    }
30    return "otpauth://{type}/{label}?{parameters}".format(**uriparts)
31