1 """ Configuration parser
2 """
3 from ConfigParser import ConfigParser
4 from StringIO import StringIO
5 import logging
6 from pkg_resources import EntryPoint
7 import sys
8
9 from repoze.who.interfaces import IAuthenticator
10 from repoze.who.interfaces import IChallengeDecider
11 from repoze.who.interfaces import IChallenger
12 from repoze.who.interfaces import IIdentifier
13 from repoze.who.interfaces import IMetadataProvider
14 from repoze.who.interfaces import IPlugin
15 from repoze.who.interfaces import IRequestClassifier
16 from repoze.who.middleware import PluggableAuthenticationMiddleware
17
19 if name:
20 return EntryPoint.parse('x=%s' % name).load(False)
21
24 self.here = here
25 self.request_classifier = None
26 self.challenge_decider = None
27 self.plugins = {}
28 self.identifiers = []
29 self.authenticators = []
30 self.challengers = []
31 self.mdproviders = []
32 self.remote_user_key = 'REMOTE_USER'
33
35 if options is None:
36 options = {}
37 obj = _resolve(name)
38 if not iface.providedBy(obj):
39 obj = obj(**options)
40 return obj
41
43 obj = self.plugins.get(name)
44 if obj is None:
45 obj = self._makePlugin(name, iface)
46 return obj
47
49 lines = proptext.split()
50 for line in lines:
51
52 if ';' in line:
53 plugin_name, classifier = line.split(';')
54 else:
55 plugin_name = line
56 classifier = None
57
58 plugin = self._getPlugin(plugin_name, iface)
59
60 if classifier is not None:
61 classifications = getattr(plugin, 'classifications', None)
62 if classifications is None:
63 classifications = plugin.classifications = {}
64 classifications[iface] = classifier
65
66 attr.append((plugin_name, plugin))
67
69 if getattr(text, 'readline', None) is None:
70 text = StringIO(text)
71 cp = ConfigParser(defaults={'here': self.here})
72 cp.readfp(text)
73
74 for s_id in [x for x in cp.sections() if x.startswith('plugin:')]:
75 plugin_id = s_id[len('plugin:'):]
76 options = dict(cp.items(s_id))
77 if 'use' in options:
78 name = options.pop('use')
79 del options['here']
80 obj = self._makePlugin(name, IPlugin, options)
81 self.plugins[plugin_id] = obj
82
83 if 'general' in cp.sections():
84 general = dict(cp.items('general'))
85
86 rc = general.get('request_classifier')
87 if rc is not None:
88 rc = self._getPlugin(rc, IRequestClassifier)
89 self.request_classifier = rc
90
91 cd = general.get('challenge_decider')
92 if cd is not None:
93 cd = self._getPlugin(cd, IChallengeDecider)
94 self.challenge_decider = cd
95
96 ru = general.get('remote_user_key')
97 if ru is not None:
98 self.remote_user_key = ru
99
100 if 'identifiers' in cp.sections():
101 identifiers = dict(cp.items('identifiers'))
102 self._parsePluginSequence(self.identifiers,
103 identifiers['plugins'],
104 IIdentifier,
105 )
106
107 if 'authenticators' in cp.sections():
108 authenticators = dict(cp.items('authenticators'))
109 self._parsePluginSequence(self.authenticators,
110 authenticators['plugins'],
111 IAuthenticator,
112 )
113
114 if 'challengers' in cp.sections():
115 challengers = dict(cp.items('challengers'))
116 self._parsePluginSequence(self.challengers,
117 challengers['plugins'],
118 IChallenger,
119 )
120
121 if 'mdproviders' in cp.sections():
122 mdproviders = dict(cp.items('mdproviders'))
123 self._parsePluginSequence(self.mdproviders,
124 mdproviders['plugins'],
125 IMetadataProvider,
126 )
127
128
129 _LEVELS = {'debug': logging.DEBUG,
130 'info': logging.INFO,
131 'warning': logging.WARNING,
132 'error': logging.ERROR,
133 }
134
137 parser = WhoConfig(global_conf['here'])
138 parser.parse(open(config_file))
139 log_stream = None
140
141 if log_file is not None:
142 if log_file.lower() == 'stdout':
143 log_stream = sys.stdout
144 else:
145 log_stream = open(log_file, 'wb')
146
147 if log_level is None:
148 log_level = logging.INFO
149 else:
150 log_level = _LEVELS[log_level.lower()]
151
152 return PluggableAuthenticationMiddleware(
153 app,
154 parser.identifiers,
155 parser.authenticators,
156 parser.challengers,
157 parser.mdproviders,
158 parser.request_classifier,
159 parser.challenge_decider,
160 log_stream,
161 log_level,
162 parser.remote_user_key,
163 )
164