Package: pgusers
User authentication and session management based on PostgreSQLInstallationJust download it from PYPI using pip:pip install pgusersUsageIn order to use this module, you instantiate aUserSpaceobject:importpgusersusp=pgusers.UserSpace("userlist",host="dbhost.domain.com",port=5432,user="dbuser")Whereuserlistmust be the name of an existing database instance on
the PostgreSQL host. The rest of the keyword arguments are those needed to
create a connection to the database server and are passed straight to
thepsycopg2module.Repeated connections to the same userspace return the same object.The following are the methods available toUserSpaceinstances.create_user(self, username, password, email, admin=False, extra_data=None)Creates a new user. Returns a numeric userid for the user created.username:The username to be created, if there is already a record with that username, aBadCallErrorexception is raised.password:The user’s password in cleartext.email:Email for notifications or password recovery.BadCallErroris raised if the email exists in the database.admin:A boolean value indicating whether the user is an administrator.extra_data:Any data that will be attached to the record. This can be anything that can be serialised using thepicklemodule from the standard library.validate_user(self, username, password, extra_data=None)Validates (or logs in) a user. Returns a tuple with a string
containing a session key, a boolean value indicating whether the user is
an admin, and the numeric userid. If the user was not found, the returned
tuple would be("",False, None)username:The username to be authenticated.password:The password in clear text.extra_data:Optional data that will be attached to the session. This can be anything that can be serialised using thepicklemodule from the standard library.delete_user(self, username=None, userid=None)Delete a user given either its username or userid. Either username or userid
must be specified. ReturnsOKif deleted,NOT_FOUNDif not found.username:The username.userid:The numeric userid.change_password(self, userid, newpassword, oldpassword=None)Change a user’s password. Returns eitherOK,NOT_FOUNDorREJECTEDThrows BadCallError if neither username or userid are specified.userid:The numeric user id, as returned bycreate_user()newpassword:The new password, in cleartext.oldpassword:The current password, in cleartext. If specified, oldpassword is checked against the current password and the call will fail if they don’t match. Ifoldpasswordis not specified, the password will be changed unconditionally.check_key(self, key)Reset the session timeout. Resets the key’s Time To Live to the default value.
Returns a tuple of the form(rc, username, userid, extra_data)where rc
can be eitherOK,NOT_FOUNDorEXPIRED. IfNOT_FOUNDorEXPIRED, username
and userid will beNone.key:The session key as returned byvalidate_user()set_session_TTL(self, secs)Sets the TTL for all sessions. All new sessions or checked sessions will
be set to this new TTL value.secs:The number of seconds of Time To Live. The default TTL for a session is currently 864000 seconds, or 10 days. This might change in future releases.find_user(self, username=None, email=None, userid=None)Find a user given either its username, its email or its userid. At least
one of those must be supplied.This method
does not validate the user or any session. It is used to retrieve information
about a user in the database.Returns a dictionary with fieldsuserid,username,email,admin,
andextra_data.Noneif the user was not found.username:The username.email:The email.userid:The numeric userid.modify_user(self, userid, username=None, email=None, extra_data=None)Modify user data. ReturnsOKif successfulNOT_FOUNDif not.userid:The user id as returned bycreate_user()username:The new username to change, if specified.email:The new email to change, if specified.extra_data:The new extra_data to change, if specified.is_admin(self, userid)Checks whether the user is admin. ReturnsTrueif it is,Falseif not.userid:The user id as returned bycreate_user()set_admin(self, userid, admin=True)Grant or revoke admin privileges to the user. To revoke, call with admin set toFalseuserid:The user id as returned bycreate_user()admin:If set toTrueor not specified, mark the user as administrator. If set toFalse, revokes administrator rights.all_users(self)Generator yielding (userid, username, email, admin) tuples for all users
in the userspace.list_sessions(self, uid, expired=False)Generator yielding all sessions for a user or for all users (i.e. all sessions) if uid is 0. If
expired is set toTrue, yield only the sessions that have expired.
Yields tuples of the form(username, key, expiration)userid:The user id as returned bycreate_user(), or 0 to return all sessions.expired:Boolean indicating whether the method should only return expired sessions.kill_sessions(self, uid, expired=False)Kill the sessions for a user or for all users (i.e. all sessions) if uid is 0. If
expired is set toTrue, kill only the sessions that have expired.Killinga
session removes it from the database, efectively invalidating it.userid:The user id as returned bycreate_user(), or 0 to kill all sessions.expired:Boolean indicating whether the method should only kill the expired sessions.LicenseThis software is licensed under the terms of theMIT license.