Skip to content

Commit

Permalink
fix generate_token (#1531)
Browse files Browse the repository at this point in the history
  • Loading branch information
oberstet committed Mar 15, 2022
1 parent 4271b07 commit aa2e22e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
4 changes: 2 additions & 2 deletions autobahn/_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@
#
###############################################################################

__version__ = '22.2.2'
__version__ = '22.3.1.dev1'

__build__ = u'00000000-0000000'
__build__ = '00000000-0000000'
9 changes: 8 additions & 1 deletion autobahn/test/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import unittest
from binascii import b2a_hex

from autobahn.util import IdGenerator, parse_activation_code, generate_activation_code
from autobahn.util import IdGenerator, parse_activation_code, generate_activation_code, generate_token


class TestIdGenerator(unittest.TestCase):
Expand Down Expand Up @@ -58,3 +58,10 @@ def test_parse_invalid_activation_codes(self):
code = b2a_hex(os.urandom(20)).decode()
parsed_code = parse_activation_code(code)
self.assertEqual(None, parsed_code)

def test_generate_token(self):
token = generate_token(5, 4)
self.assertEqual(len(token), len('NUAG-UPQJ-MFGA-K5P5-MUGA'))
self.assertEqual(len(token.split('-')), 5)
for part in token.split('-'):
self.assertEqual(len(part), 4)
8 changes: 4 additions & 4 deletions autobahn/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,9 +358,9 @@ def newid(length=16):
@public
def generate_token(char_groups: int,
chars_per_group: int,
chars=Optional[str],
sep=Optional[str],
lower_case=False) -> str:
chars: Optional[str] = None,
sep: Optional[str] = None,
lower_case: Optional[bool] = False) -> str:
"""
Generate cryptographically strong tokens, which are strings like `M6X5-YO5W-T5IK`.
These can be used e.g. for used-only-once activation tokens or the like.
Expand Down Expand Up @@ -400,7 +400,7 @@ def generate_token(char_groups: int,
"""
assert(type(char_groups) == int)
assert(type(chars_per_group) == int)
assert(chars is None or type(chars) == str)
assert(chars is None or type(chars) == str), 'chars must be str, was {}'.format(type(chars))
chars = chars or DEFAULT_TOKEN_CHARS
if lower_case:
chars = chars.lower()
Expand Down

0 comments on commit aa2e22e

Please sign in to comment.