Validate even imported files and users
Created a new file for the validation functions, should be soon(TM) used too in the userapplication-script but dont hurrypull/4/head
parent
8274cbb27e
commit
0c1f7ea252
@ -0,0 +1,52 @@
|
|||||||
|
import re
|
||||||
|
import pwd
|
||||||
|
|
||||||
|
|
||||||
|
def checkUsernameCharacters(username: str):
|
||||||
|
if re.match("[a-z]+[a-z0-9]", username):
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def checkUsernameLength(username: str):
|
||||||
|
if len(username) > 16:
|
||||||
|
return False
|
||||||
|
if len(username) < 3:
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def checkUserExists(username: str):
|
||||||
|
try:
|
||||||
|
pwd.getpwnam(username)
|
||||||
|
except KeyError:
|
||||||
|
return True # User already exists
|
||||||
|
else:
|
||||||
|
return False # User doesnt exist
|
||||||
|
|
||||||
|
|
||||||
|
def checkSSHKey(key: str):
|
||||||
|
# taken from https://github.com/hashbang/provisor/blob/master/provisor/utils.py, all belongs to them! ;)
|
||||||
|
import base64
|
||||||
|
if len(key) > 8192 or len(key) < 80:
|
||||||
|
return False
|
||||||
|
|
||||||
|
key = key.replace("\"", "").replace("'", "").replace("\\\"", "")
|
||||||
|
key = key.split(' ')
|
||||||
|
types = ['ecdsa-sha2-nistp256', 'ecdsa-sha2-nistp384',
|
||||||
|
'ecdsa-sha2-nistp521', 'ssh-rsa', 'ssh-dss', 'ssh-ed25519']
|
||||||
|
if key[0] not in types:
|
||||||
|
return False
|
||||||
|
try:
|
||||||
|
base64.decodebytes(bytes(key[1], "utf-8"))
|
||||||
|
except TypeError:
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def checkEmail(mail: str):
|
||||||
|
if not re.match("(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)", mail):
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
return True
|
Loading…
Reference in New Issue