htpasswd is a library for working with htpasswd user (only basic authorization) and group files.
supports both the crypt method and the MD5 encryption method.
for the md5 method you MUST have openssl installed (and it must be in the system path.) does not use python openSSL bindings, but actually calls out to openssl. Not the best security method, feel free to fix for me.
- Python 2.6 or 2.7
- orderedmultidict >= 0.7
- nose >= 1.1.2 (for tests)
import htpasswd
with htpasswd.Basic("/path/to/user.db") as userdb:
try:
userdb.add("bob", "password")
except htpasswd.basic.UserExists, e:
print e
try:
userdb.change_password("alice", "newpassword")
except htpasswd.basic.UserNotExists, e:
print e
with htpasswd.Group("/path/to/group.db") as groupdb:
try:
groupdb.add_user("bob", "admins")
except htpasswd.group.UserAlreadyInAGroup, e:
print e
try:
groupdb.delete_user("alice", "managers")
except htpasswd.group.UserNotInAGroup, e:
print e
to encrypt with md5, just add mode='md5' to htpasswd.Basic(userdb, mode='md5')
__contains__(user)
users
add(user, password)
delete(user)
change_password(user, password)
_crypt_password(password)
__contains__(group)
groups
is_user_in(user, group)
add_user(user, group)
delete_user(user, group)
That's a general exception from which others are inherited.
Raised by Basic.add
if user already exists.
Raised by Basic.delete
and Basic.change_password
if there is no such user.
Raised by Group.delete_user
if there is no such group.
Raised by Group.add_user
if user is already in a group.
Raised by Group.delete_user
if user isn't in a group.
raised by _encrypt_password if mode is not 'crypt' or 'md5'.