Skip to content
Snippets Groups Projects

ENH: add function that generates passwords

Merged Henrik tom Wörden requested to merge f-password into dev

Files

+ 42
3
@@ -26,6 +26,9 @@
@@ -26,6 +26,9 @@
"""missing docstring."""
"""missing docstring."""
 
import re
 
import string
 
import random
from caosdb.common.utils import xml2str
from caosdb.common.utils import xml2str
from caosdb.connection.connection import get_connection
from caosdb.connection.connection import get_connection
from caosdb.exceptions import (EntityDoesNotExistError, HTTPClientError,
from caosdb.exceptions import (EntityDoesNotExistError, HTTPClientError,
@@ -56,7 +59,8 @@ def set_server_property(key, value):
@@ -56,7 +59,8 @@ def set_server_property(key, value):
con._form_data_request(method="POST", path="_server_properties",
con._form_data_request(method="POST", path="_server_properties",
params={key: value}).read()
params={key: value}).read()
except EntityDoesNotExistError:
except EntityDoesNotExistError:
raise ServerConfigurationException("Debug mode in server is probably disabled.") from None
raise ServerConfigurationException(
 
"Debug mode in server is probably disabled.") from None
def get_server_properties():
def get_server_properties():
@@ -71,9 +75,11 @@ def get_server_properties():
@@ -71,9 +75,11 @@ def get_server_properties():
"""
"""
con = get_connection()
con = get_connection()
try:
try:
body = con._http_request(method="GET", path="_server_properties").response
body = con._http_request(
 
method="GET", path="_server_properties").response
except EntityDoesNotExistError:
except EntityDoesNotExistError:
raise ServerConfigurationException("Debug mode in server is probably disabled.") from None
raise ServerConfigurationException(
 
"Debug mode in server is probably disabled.") from None
xml = etree.parse(body)
xml = etree.parse(body)
props = dict()
props = dict()
@@ -108,6 +114,39 @@ def get_server_property(key):
@@ -108,6 +114,39 @@ def get_server_property(key):
return get_server_properties()[key]
return get_server_properties()[key]
 
def generate_password(length: int):
 
"""Create a random password that fulfills the security requirements
 
 
Parameters
 
----------
 
length : int
 
Length of the generated password. Has to be greater than 7.
 
 
Returns
 
-------
 
password : string
 
Generated random password of the given length
 
 
Raises
 
------
 
ValueError:
 
If the length is less than 8.
 
"""
 
minimum_password_length = 8
 
if length < minimum_password_length:
 
raise ValueError("CaosDB passwords have to be at least {} characters.".format(
 
minimum_password_length))
 
sample_letters = string.ascii_letters + string.digits + "!#$%*+-/:;?_"
 
password = ''.join((random.choice(sample_letters) for i in range(length)))
 
 
while not re.match(r"(?=.*[A-Z])(?=.*[a-z])(?=.*\d)(?=.*[\W_]).{8,}",
 
password):
 
password = ''.join((random.choice(sample_letters)
 
for i in range(length)))
 
 
return password
 
 
def _retrieve_user(name, realm=None, **kwargs):
def _retrieve_user(name, realm=None, **kwargs):
con = get_connection()
con = get_connection()
try:
try:
Loading