Skip to content
This repository was archived by the owner on Jun 1, 2023. It is now read-only.

Commit b761285

Browse files
committed
Configure and logging handling.
1 parent ae8d825 commit b761285

File tree

3 files changed

+131
-2
lines changed

3 files changed

+131
-2
lines changed

src/oidcrp/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,10 +354,10 @@ def client_setup(self, iss_id='', user=''):
354354
else:
355355
registration_type = _fe.registration_type
356356

357-
if registration_type == 'implicit':
357+
if registration_type == 'automatic':
358358
_sc.client_id = client.client_id = _fe.entity_id
359359
_sc.redirect_uris = _sc.behaviour['redirect_uris']
360-
else:
360+
else: # explicit
361361
self.do_client_registration(client, iss_id)
362362

363363
self.issuer2rp[issuer] = client

src/oidcrp/configure.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
"""Configuration management for RP"""
2+
3+
from typing import Dict
4+
5+
from oidcrp.logging import configure_logging
6+
from oidcrp.util import get_http_params
7+
from oidcrp.util import load_yaml_config
8+
from oidcrp.util import lower_or_upper
9+
10+
try:
11+
from secrets import token_urlsafe as rnd_token
12+
except ImportError:
13+
from oidcendpoint import rndstr as rnd_token
14+
15+
16+
class Configuration:
17+
"""RP Configuration"""
18+
19+
def __init__(self, conf: Dict) -> None:
20+
self.logger = configure_logging(config=conf.get('logging')).getChild(__name__)
21+
22+
# server info
23+
self.domain = lower_or_upper(conf, "domain")
24+
self.port = lower_or_upper(conf, "port")
25+
for param in ["server_name", "base_url"]:
26+
_pre = lower_or_upper(conf, param)
27+
if _pre:
28+
if '{domain}' in _pre:
29+
setattr(self, param, _pre.format(domain=self.domain, port=self.port))
30+
else:
31+
setattr(self, param, _pre)
32+
33+
# HTTP params
34+
_params = get_http_params(conf.get("http_params"))
35+
if _params:
36+
self.httpc_params = _params
37+
else:
38+
_params = {'verify', lower_or_upper(conf, "verify_ssl", True)}
39+
40+
self.web_conf = lower_or_upper(conf, "webserver")
41+
42+
# diverse
43+
for param in ["html_home", "session_cookie_name", "preferred_url_scheme",
44+
"services", "federation"]:
45+
setattr(self, param, lower_or_upper(conf, param))
46+
47+
rp_keys_conf = lower_or_upper(conf, 'rp_keys')
48+
if rp_keys_conf is None:
49+
rp_keys_conf = lower_or_upper(conf, 'oidc_keys')
50+
setattr(self, "rp_keys", rp_keys_conf)
51+
52+
_clients = lower_or_upper(conf, "clients")
53+
for key, spec in _clients.items():
54+
if key == "":
55+
continue
56+
if not spec.get("redirect_uris"):
57+
continue
58+
59+
_redirects = []
60+
for _r in spec["redirect_uris"]:
61+
if '{domain}' in _r:
62+
_redirects.append(_r.format(domain=self.domain, port=self.port))
63+
else:
64+
_redirects.append(_r)
65+
spec["redirect_uris"] = _redirects
66+
67+
setattr(self, "clients", _clients)
68+
69+
hash_seed = lower_or_upper(conf, 'hash_seed')
70+
if not hash_seed:
71+
hash_seed = rnd_token(32)
72+
setattr(self, "hash_seed", hash_seed)
73+
74+
@classmethod
75+
def create_from_config_file(cls, filename: str):
76+
"""Load configuration as YAML"""
77+
return cls(load_yaml_config(filename))

src/oidcrp/logging.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
"""Common logging functions"""
2+
3+
import os
4+
import logging
5+
from logging.config import dictConfig
6+
7+
import yaml
8+
9+
10+
LOGGING_CONF = 'logging.yaml'
11+
12+
LOGGING_DEFAULT = {
13+
'version': 1,
14+
'formatters': {
15+
'default': {
16+
'format': '%(asctime)s %(name)s %(levelname)s %(message)s'
17+
}
18+
},
19+
'handlers': {
20+
'default': {
21+
'class': 'logging.StreamHandler',
22+
'formatter': 'default'
23+
}
24+
},
25+
'root': {
26+
'handlers': ['default'],
27+
'level': 'INFO'
28+
}
29+
}
30+
31+
32+
def configure_logging(debug: bool = False, config: dict = None,
33+
filename: str = LOGGING_CONF) -> logging.Logger:
34+
"""Configure logging"""
35+
36+
if config is not None:
37+
config_dict = config
38+
config_source = 'dictionary'
39+
elif filename is not None and os.path.exists(filename):
40+
with open(filename, "rt") as file:
41+
config_dict = yaml.load(file)
42+
config_source = 'file'
43+
else:
44+
config_dict = LOGGING_DEFAULT
45+
config_source = 'default'
46+
47+
if debug:
48+
config_dict['root']['level'] = 'DEBUG'
49+
50+
dictConfig(config_dict)
51+
logging.debug("Configured logging using %s", config_source)
52+
return logging.getLogger()

0 commit comments

Comments
 (0)