Normalize unix usernames to be under 32char

JupyterHub usernames do not have this restriction,
causing user creation to fail when usernames are too large.
This commit is contained in:
yuvipanda
2018-09-12 16:46:59 -07:00
parent f163f62c89
commit 9b0248e0c3
3 changed files with 58 additions and 5 deletions

24
tljh/normalize.py Normal file
View File

@@ -0,0 +1,24 @@
"""
Functions to normalize various inputs
"""
import hashlib
def generate_system_username(username):
"""
Generate a posix username from given username.
If username < 26 char, we just return it.
Else, we hash the username, truncate username at
26 char, append a '-' and first add 5char of hash.
This makes sure our usernames are always under 32char.
"""
if len(username) < 26:
return username
userhash = hashlib.sha256(username.encode('utf-8')).hexdigest()
return '{username_trunc}-{hash}'.format(
username_trunc=username[:26],
hash=userhash[:5]
)