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, AsyncClient,
) )
BASE_DN = "dc=unit,dc=macaomilano,dc=org" BASE_DN = "dc=unit,dc=macaomilano,dc=org"

View File

@ -1,6 +1,8 @@
from datetime import datetime from datetime import datetime
def serialize(d): def serialize(obj):
return {k: (v.isoformat() if isinstance(v, datetime) else v) return {
for k, v in d.items()} 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.", help="Path to a yaml configuration for the logger.",
) )
@click.option( @click.option(
"--debug", "--debug", "debug", is_flag=True, default=False, help="Set the log level to debug.",
"debug",
is_flag=True,
default=False,
help="Set the log level to debug.",
) )
def cli( def cli(
host, host,
@ -204,8 +200,8 @@ def prepare_config_from_cli(
} }
_logging = {} _logging = {}
if log_conf: if log_conf:
with open(log_conf) as l: with open(log_conf) as log_conf_fd:
_logging = yaml.safe_load(l) _logging = yaml.safe_load(log_conf_fd)
return {"core": _core, "ldap": _ldap, "logging": _logging} return {"core": _core, "ldap": _ldap, "logging": _logging}

View File

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

View File

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

View File

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