Revision e6e94655 lib/http/auth.py
b/lib/http/auth.py | ||
---|---|---|
197 | 197 |
|
198 | 198 |
""" |
199 | 199 |
raise NotImplementedError() |
200 |
|
|
201 |
|
|
202 |
class PasswordFileUser(object): |
|
203 |
"""Data structure for users from password file. |
|
204 |
|
|
205 |
""" |
|
206 |
def __init__(self, name, password, options): |
|
207 |
self.name = name |
|
208 |
self.password = password |
|
209 |
self.options = options |
|
210 |
|
|
211 |
|
|
212 |
def ReadPasswordFile(file_name): |
|
213 |
"""Reads a password file. |
|
214 |
|
|
215 |
Lines in the password file are of the following format: |
|
216 |
|
|
217 |
<username> <password> [options] |
|
218 |
|
|
219 |
Fields are separated by whitespace. Username and password are mandatory, |
|
220 |
options are optional and separated by comma (","). Empty lines and comments |
|
221 |
("#") are ignored. |
|
222 |
|
|
223 |
@type file_name: str |
|
224 |
@param file_name: Path to password file |
|
225 |
@rtype: dict |
|
226 |
@return: Dictionary containing L{PasswordFileUser} instances |
|
227 |
|
|
228 |
""" |
|
229 |
users = {} |
|
230 |
|
|
231 |
for line in utils.ReadFile(file_name).splitlines(): |
|
232 |
line = line.strip() |
|
233 |
|
|
234 |
# Ignore empty lines and comments |
|
235 |
if not line or line.startswith("#"): |
|
236 |
continue |
|
237 |
|
|
238 |
parts = line.split(None, 2) |
|
239 |
if len(parts) < 2: |
|
240 |
# Invalid line |
|
241 |
continue |
|
242 |
|
|
243 |
name = parts[0] |
|
244 |
password = parts[1] |
|
245 |
|
|
246 |
# Extract options |
|
247 |
options = [] |
|
248 |
if len(parts) >= 3: |
|
249 |
for part in parts[2].split(","): |
|
250 |
options.append(part.strip()) |
|
251 |
|
|
252 |
users[name] = PasswordFileUser(name, password, options) |
|
253 |
|
|
254 |
return users |
Also available in: Unified diff