refactor
blallo 2020-08-29 20:12:02 +02:00
parent e41e03f464
commit b979002f78
Signed by: blallo
GPG Key ID: 0CBE577C9B72DC3F
6 changed files with 35 additions and 58 deletions

View File

@ -13,7 +13,6 @@ from phi.async_ldap.client import (
AsyncClient,
)
BASE_DN = "dc=unit,dc=macaomilano,dc=org"

View File

@ -1,6 +1,8 @@
from datetime import datetime
def serialize(d):
return {k: (v.isoformat() if isinstance(v, datetime) else v)
for k, v in d.items()}
def serialize(obj):
return {
k: (v.isoformat() if isinstance(v, datetime) else v)
for k, v in dict(obj).items()
}

View File

@ -125,11 +125,7 @@ def _validate_port(ctx, param, value):
help="Path to a yaml configuration for the logger.",
)
@click.option(
"--debug",
"debug",
is_flag=True,
default=False,
help="Set the log level to debug.",
"--debug", "debug", is_flag=True, default=False, help="Set the log level to debug.",
)
def cli(
host,
@ -204,8 +200,8 @@ def prepare_config_from_cli(
}
_logging = {}
if log_conf:
with open(log_conf) as l:
_logging = yaml.safe_load(l)
with open(log_conf) as log_conf_fd:
_logging = yaml.safe_load(log_conf_fd)
return {"core": _core, "ldap": _ldap, "logging": _logging}

View File

@ -309,9 +309,7 @@ class User(Hackers):
try:
await self.modify("userPassword", hash_pass(new_pass))
except PhiAttributeMissing:
raise PhiUnauthorized(
user=self.client.username,
)
raise PhiUnauthorized(user=self.client.username,)
alog.info("User(%s): password modified", self.name)
async def verify_password(self, given_pass):
@ -322,9 +320,7 @@ class User(Hackers):
try:
match_pass = res[0]["userPassword"][0] == hash_pass(given_pass)
except KeyError:
raise PhiUnauthorized(
user=self.client.username,
)
raise PhiUnauthorized(user=self.client.username,)
return match_pass

View File

@ -2,16 +2,10 @@ import os.path
import pkg_resources
import yaml
NAME = 'phi'
NAME = "phi"
DEFAULT_CONFIG = {
"core": {
"listen": {
"host": "localhost",
"port": 8080,
}
},
"core": {"listen": {"host": "localhost", "port": 8080}},
"ldap": {
"host": "localhost",
"port": 389,
@ -27,9 +21,7 @@ DEFAULT_CONFIG = {
},
"logging": {
"version": 1,
"formatters": {
"default": {"format": "[%(name)s %(levelname)s] %(message)s"}
},
"formatters": {"default": {"format": "[%(name)s %(levelname)s] %(message)s"}},
"handlers": {
"console": {
"class": "logging.StreamHandler",
@ -40,34 +32,26 @@ DEFAULT_CONFIG = {
"class": "logging.FileHandler",
"formatter": "default",
"filename": "phi.log",
}
},
},
"loggers": {
"phi": {
"level": "INFO",
"handlers": ["console", "file"],
},
"aiohttp": {
"level": "INFO",
"handlers": ["console", "file"],
},
"ldap3": {
"level": "WARNING",
"handlers": ["console", "file"],
}
}
}
"phi": {"level": "INFO", "handlers": ["console", "file"],},
"aiohttp": {"level": "INFO", "handlers": ["console", "file"],},
"ldap3": {"level": "WARNING", "handlers": ["console", "file"],},
},
},
}
DUMMY_CONFIG = {"core": {}, "ldap": {}, "logging": {}}
CONFIG_FILE = 'config.yml'
CONFIG_PATHS = ['./',
'~/.config/' + NAME + '/',
'/usr/local/etc/' + NAME + '/',
'/etc/' + NAME + '/']
CONFIG_FILES = [os.path.join(p, CONFIG_FILE)
for p in CONFIG_PATHS]
CONFIG_FILE = "config.yml"
CONFIG_PATHS = [
"./",
"~/.config/" + NAME + "/",
"/usr/local/etc/" + NAME + "/",
"/etc/" + NAME + "/",
]
CONFIG_FILES = [os.path.join(p, CONFIG_FILE) for p in CONFIG_PATHS]
def get_config(config_path=None):
@ -84,7 +68,7 @@ def get_config(config_path=None):
return config_path, config
for f in CONFIG_FILES:
try:
with open(f, 'r') as c:
with open(f, "r") as c:
config = yaml.safe_load(c)
return (f, config)
except FileNotFoundError:

View File

@ -4,10 +4,10 @@ from phi.ldap.utils import flatten_attributes
def user_attributes_mapping(client):
return {
client.attribute_id: 'uid',
client.attribute_mail: 'mail',
'createTimestamp': 'created_at',
'modifyTimestamp': 'modified_at'
client.attribute_id: "uid",
client.attribute_mail: "mail",
"createTimestamp": "created_at",
"modifyTimestamp": "modified_at",
}
@ -19,8 +19,8 @@ def get_user_by_uid(client, uid):
mapping = user_attributes_mapping(client)
user = {mapping[k]: v
for k, v in entry['attributes'].items()
if k in mapping.keys()}
user = {
mapping[k]: v for k, v in entry["attributes"].items() if k in mapping.keys()
}
return flatten_attributes(user)