7
7
from typing import List
8
8
from typing import Optional
9
9
10
+ from oidcmsg .configure import Base
11
+
10
12
from oidcrp .logging import configure_logging
11
13
from oidcrp .util import load_yaml_config
12
14
from oidcrp .util import lower_or_upper
16
18
except ImportError :
17
19
from cryptojwt import rndstr as rnd_token
18
20
19
- DEFAULT_FILE_ATTRIBUTE_NAMES = ['server_key' , 'server_cert' , 'filename' , 'template_dir' ,
20
- 'private_path' , 'public_path' , 'db_file' ]
21
-
22
-
23
- def add_base_path (conf : dict , base_path : str , file_attributes : List [str ]):
24
- for key , val in conf .items ():
25
- if key in file_attributes :
26
- if val .startswith ("/" ):
27
- continue
28
- elif val == "" :
29
- conf [key ] = "./" + val
30
- else :
31
- conf [key ] = os .path .join (base_path , val )
32
- if isinstance (val , dict ):
33
- conf [key ] = add_base_path (val , base_path , file_attributes )
34
-
35
- return conf
36
-
37
-
38
- def set_domain_and_port (conf : dict , uris : List [str ], domain : str , port : int ):
39
- for key , val in conf .items ():
40
- if key in uris :
41
- if not val :
42
- continue
43
-
44
- if isinstance (val , list ):
45
- _new = [v .format (domain = domain , port = port ) for v in val ]
46
- else :
47
- _new = val .format (domain = domain , port = port )
48
- conf [key ] = _new
49
- elif isinstance (val , dict ):
50
- conf [key ] = set_domain_and_port (val , uris , domain , port )
51
- return conf
52
-
53
-
54
- class Base :
55
- """ Configuration base class """
56
-
57
- def __init__ (self ,
58
- conf : Dict ,
59
- base_path : str = '' ,
60
- file_attributes : Optional [List [str ]] = None ,
61
- ):
62
-
63
- if file_attributes is None :
64
- file_attributes = DEFAULT_FILE_ATTRIBUTE_NAMES
65
-
66
- if base_path and file_attributes :
67
- # this adds a base path to all paths in the configuration
68
- add_base_path (conf , base_path , file_attributes )
69
-
70
- def __getitem__ (self , item ):
71
- if item in self .__dict__ :
72
- return self .__dict__ [item ]
73
- else :
74
- raise KeyError
75
-
76
- def get (self , item , default = None ):
77
- return getattr (self , item , default )
78
-
79
- def __contains__ (self , item ):
80
- return item in self .__dict__
81
-
82
- def items (self ):
83
- for key in self .__dict__ :
84
- if key .startswith ('__' ) and key .endswith ('__' ):
85
- continue
86
- yield key , getattr (self , key )
87
-
88
- def extend (self , entity_conf , conf , base_path , file_attributes , domain , port ):
89
- for econf in entity_conf :
90
- _path = econf .get ("path" )
91
- _cnf = conf
92
- if _path :
93
- for step in _path :
94
- _cnf = _cnf [step ]
95
- _attr = econf ["attr" ]
96
- _cls = econf ["class" ]
97
- setattr (self , _attr ,
98
- _cls (_cnf , base_path = base_path , file_attributes = file_attributes ,
99
- domain = domain , port = port ))
100
-
101
-
102
21
URIS = [
103
22
"redirect_uris" , 'post_logout_redirect_uris' , 'frontchannel_logout_uri' ,
104
23
'backchannel_logout_uri' , 'issuer' , 'base_url' ]
@@ -112,23 +31,17 @@ def __init__(self,
112
31
domain : Optional [str ] = "127.0.0.1" ,
113
32
port : Optional [int ] = 80 ,
114
33
file_attributes : Optional [List [str ]] = None ,
34
+ dir_attributes : Optional [List [str ]] = None ,
115
35
):
116
36
117
- Base .__init__ (self , conf , base_path = base_path , file_attributes = file_attributes )
118
-
119
- _keys_conf = lower_or_upper (conf , 'rp_keys' )
120
- if _keys_conf is None :
121
- _keys_conf = lower_or_upper (conf , 'oidc_keys' ) # legacy
122
-
123
- self .keys = _keys_conf
37
+ Base .__init__ (self , conf ,
38
+ base_path = base_path ,
39
+ domain = domain ,
40
+ port = port ,
41
+ file_attributes = file_attributes ,
42
+ dir_attributes = dir_attributes )
124
43
125
- if not domain :
126
- domain = conf .get ("domain" , "127.0.0.1" )
127
-
128
- if not port :
129
- port = conf .get ("port" , 80 )
130
-
131
- conf = set_domain_and_port (conf , URIS , domain , port )
44
+ self .key_conf = lower_or_upper (conf , 'rp_keys' ) or lower_or_upper (conf , 'oidc_keys' )
132
45
self .clients = lower_or_upper (conf , "clients" )
133
46
134
47
hash_seed = lower_or_upper (conf , 'hash_seed' )
@@ -155,8 +68,10 @@ def __init__(self,
155
68
file_attributes : Optional [List [str ]] = None ,
156
69
domain : Optional [str ] = "" ,
157
70
port : Optional [int ] = 0 ,
71
+ dir_attributes : Optional [List [str ]] = None ,
158
72
):
159
- Base .__init__ (self , conf , base_path = base_path , file_attributes = file_attributes )
73
+ Base .__init__ (self , conf , base_path = base_path , file_attributes = file_attributes ,
74
+ dir_attributes = dir_attributes )
160
75
161
76
log_conf = conf .get ('logging' )
162
77
if log_conf :
@@ -166,40 +81,35 @@ def __init__(self,
166
81
167
82
self .web_conf = lower_or_upper (conf , "webserver" )
168
83
169
- # entity info
170
- if not domain :
171
- domain = conf .get ("domain" , "127.0.0.1" )
172
-
173
- if not port :
174
- port = conf .get ("port" , 80 )
175
-
176
84
if entity_conf :
177
85
self .extend (entity_conf = entity_conf , conf = conf , base_path = base_path ,
178
- file_attributes = file_attributes , domain = domain , port = port )
179
-
180
-
181
- def create_from_config_file (cls ,
182
- filename : str ,
183
- base_path : Optional [str ] = '' ,
184
- entity_conf : Optional [List [dict ]] = None ,
185
- file_attributes : Optional [List [str ]] = None ,
186
- domain : Optional [str ] = "" ,
187
- port : Optional [int ] = 0 ):
188
- if filename .endswith (".yaml" ):
189
- """Load configuration as YAML"""
190
- _cnf = load_yaml_config (filename )
191
- elif filename .endswith (".json" ):
192
- _str = open (filename ).read ()
193
- _cnf = json .loads (_str )
194
- elif filename .endswith (".py" ):
195
- head , tail = os .path .split (filename )
196
- tail = tail [:- 3 ]
197
- module = importlib .import_module (tail )
198
- _cnf = getattr (module , "CONFIG" )
199
- else :
200
- raise ValueError ("Unknown file type" )
201
-
202
- return cls (_cnf ,
203
- entity_conf = entity_conf ,
204
- base_path = base_path , file_attributes = file_attributes ,
205
- domain = domain , port = port )
86
+ file_attributes = file_attributes , domain = domain , port = port ,
87
+ dir_attributes = dir_attributes )
88
+
89
+
90
+ # def create_from_config_file(cls,
91
+ # filename: str,
92
+ # base_path: Optional[str] = '',
93
+ # entity_conf: Optional[List[dict]] = None,
94
+ # file_attributes: Optional[List[str]] = None,
95
+ # dir_attributes: Optional[List[str]] = None,
96
+ # domain: Optional[str] = "",
97
+ # port: Optional[int] = 0):
98
+ # if filename.endswith(".yaml"):
99
+ # """Load configuration as YAML"""
100
+ # _cnf = load_yaml_config(filename)
101
+ # elif filename.endswith(".json"):
102
+ # _str = open(filename).read()
103
+ # _cnf = json.loads(_str)
104
+ # elif filename.endswith(".py"):
105
+ # head, tail = os.path.split(filename)
106
+ # tail = tail[:-3]
107
+ # module = importlib.import_module(tail)
108
+ # _cnf = getattr(module, "CONFIG")
109
+ # else:
110
+ # raise ValueError("Unknown file type")
111
+ #
112
+ # return cls(_cnf,
113
+ # entity_conf=entity_conf,
114
+ # base_path=base_path, file_attributes=file_attributes,
115
+ # domain=domain, port=port, dir_attributes=dir_attributes)
0 commit comments