diff options
Diffstat (limited to 'htpasswd.py')
-rw-r--r-- | htpasswd.py | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/htpasswd.py b/htpasswd.py new file mode 100644 index 0000000..3641c34 --- /dev/null +++ b/htpasswd.py | |||
@@ -0,0 +1,35 @@ | |||
1 | #!/usr/bin/python | ||
2 | """ | ||
3 | Apache htaccess File Library | ||
4 | by Mike Crute on July 12, 2008 | ||
5 | for SoftGroup Interactive, Inc. | ||
6 | Released under the terms of the BSD license. | ||
7 | |||
8 | A collection of classes and functions to manipulate apache htaccess files. | ||
9 | """ | ||
10 | |||
11 | __all__ = [ "generate_user" ] | ||
12 | |||
13 | def hash_password(passwd, ctype="crypt"): | ||
14 | """Create an Apache-style password hash. | ||
15 | This is basically just a simplfified interface to apachelib.password | ||
16 | for use in generating htaccess files. Valid ctypes are crypt, sha and | ||
17 | md5. | ||
18 | """ | ||
19 | if ctype is "crypt": | ||
20 | from apachelib.password import crypt_password | ||
21 | return crypt_password(passwd) | ||
22 | elif ctype is "sha": | ||
23 | from apachelib.password import sha_password | ||
24 | return sha_password(passwd) | ||
25 | elif ctype is "md5": | ||
26 | from apachelib.password import md5_password | ||
27 | return md5_password(passwd) | ||
28 | |||
29 | # We should never get here | ||
30 | raise ValueError("%s is not a valid value for ctype." % ctype) | ||
31 | |||
32 | def generate_user(username, passwd, ctype="crypt"): | ||
33 | """Generate a single htaccess line. | ||
34 | """ | ||
35 | return "%s:%s" % (username, hash_password(passwd, ctype)) | ||