riki_usermanager
User Manager for Riki system
Implementer: Sierra OBryan
Python: >=3.7
Problem
There was no way to register new user and delete existing users from the riki system
Solution
Implement a new user manager with easy to use endpoints (login, logout, register, unregister) to manage user in a SQLite database
APIs
- Login
- Logout
- Register
- Unregister
Usage
Command line
Instantiate the UserManager
user_manager = UserManager(get_db())
where get_db() returns a database connection to a database with a users table according to schema.sql
Register a test user Using test database
1. user_manager.register(User("name", "pswd"))
2. When successful also call flask user manager login
Login a test user Success
1. user_manager.login("name", "pswd")
2. When successful also call flask user manager login
Logout a test user
1. user_manager.logout(User("name", "pswd"))
2. When successful also call flask user manager logout
Unregister test user
1. user_manager.unregister(User("name", "pswd"))
2. When successful also call flask user manager logout
Command line
Create a test database
input: python rkusermanager.py -c "" "" ""
output: test.db
Register a test user
Using test database
input: python rkusermanager.py -r "test.db" "name" "1234"
output: User(username='name', password='1234', active=1, roles=[], authentication_method=0, authenticated=True, hash='', anonymous=False)
Using any database with correct schema (defined in schema.sql)
input: python rkusermanager.py -r "/tmp/riki.db" "name" "1234"
output: User(username='name', password='1234', active=1, roles=[], authentication_method=0, authenticated=True, hash='', anonymous=False)
Login a test user
Success
input: python rkusermanager.py -l "test.db" "name" "1234"
output: True
Failure
input: python rkusermanager.py -l "test.db" "name" "1234"
output: False
Logout a test user
input: python rkusermanager.py -o "test.db" "name" "1234"
output: True
Unregister test user
input: python rkusermanager.py -u "test.db" "name" "1234"
output: True
Drop table
input: python rkusermanager.py -c "" "" ""
output: True
riki_usermanager.User
AuthMethodEnum Objects
class AuthMethodEnum(Enum)
Enum that stores supported authentication methods
User Objects
@dataclass
class User()
Represents User entry in the sqlite3 database
Variables:
- username (str): The user's name. Used as the primary key in the database.
- password (str): The user's password. Stored as text in sqlite.
- roles (str): The roles a user has. It's a list of string, but will be stored as a single text value in sqlite.
- authentication_method (int): Used to reference an authentication method by number.
- authenticated (bool): Flag for whether a user has been authenticated. Stored in sqlite as an int.
- hash (str): Stored result if password has been hashed.
- anonymous (bool): Flag for anonymous users. Since a registered user is not anonymous, this is not stored in sqlite.
is_authenticated
| is_authenticated()
Returns whether the User is authenticated.
Arguments:
selfint - The current instance of User
Returns:
bool:authentication state (indicates if user is logged in)
is_active
| is_active()
Returns whether the User is active. Required by flask-login.
Arguments:
selfint - The current instance of User
Returns:
bool:active state
is_anonymous
| is_anonymous()
Returns whether the User is anonymous. In this case, all users are not.
Arguments:
selfint - The current instance of User
Returns:
bool:anonymous state
get_id
| get_id()
Returns the username of a user. Required by flask-login.
Arguments:
selfint - The current instance of User
Returns:
str:username
from_dict
| @staticmethod
| from_dict(user: Dict[str, Any]) -> 'User'
converts array of sql data into dictionary
Arguments:
dataList[str] - sql array of data
Returns:
Dict[str, Any]: Dictionary of values
riki_usermanager.UserManager
UserManager Objects
class UserManager()
A very simple User Manager, that manages User objects and writes them to database
__init__
| __init__(db: sqlite3.Connection)
Create UserManager object
Arguments:
- db (sqlite3.Connection): preexisting sqlite3 connection object
login
| login(username: str, password: str)
Logins in a user after username and password have been validated (username and password are not null). This function checks if a user exists (get_user), then checks password (check_password) and then authenticates the user (update). It returns the user if login was sucessful and False if login fails.
Arguments:
usernameString - usernamepasswordString - password
Returns:
User- on successbool: False - on Failure
Depends on:
get_usercheck_passwordupdate
logout
| logout(user: User)
Logouts user updates the user in database with authenticated = false and returns a True if this was successful, else False.
Arguments:
userUser - User object
Returns:
bool-Trueon success, else False
Depends on:
update
register
| register(user: 'User')
Creates a new user and authenticates a new user after username and password are validated
Arguments:
userUser - User object
Returns:
User- on successbool: False - on Failure
Depends on:
add_userlogin
unregister
| unregister(user: User)
Deletes a user's profile
Arguments:
userUser - User object
Returns:
bool-Trueon success, else False
Depends on:
update
add_user
| add_user(user: 'User')
Creates new user in the database
Arguments:
userUser - User object
Returns:
User- on successbool: False - on Failure
get_user
| get_user(username: str)
Get User from the database
Args:
- name (str): users name
Returns:
- User | None: User object if user with the given username is found, otherwise nothing is returned.
delete_user
| delete_user(username: str)
Deletes user from the database
Arguments:
namestr - users username
Returns:
bool- True if delete was successful, otherwise False
update
| update(user: 'User')
Update user from userdata dictionary
Arguments:
userUser - User object
Returns:
bool- True if delete was successful, otherwise False
check_password
| @staticmethod
| check_password(user: User, password: str) -> bool
Check if user password matches the password in the database
Arguments:
passwordstr - user password
Returns:
bool- did password match
Release files for Riki-User-Manager 1.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| Riki_User_Manager-1.0.1-py3-none-any.whl | Python 3 | none | any | Details |
Release files / Riki_User_Manager-1.0.1-py3-none-any.whl
| Download URL | Riki_User_Manager-1.0.1-py3-none-any.whl |
|---|---|
| Size | 8.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
f98f3698c33ad5a65084bb4f5e9e5c60ed691c353832a7f07c8c3260a9ddd444
|
|
BLAKE2b-256 checksum How to use checksums |
a8e2a0f4a3a22192e28660d4b88889e3414580d658ad48ebc6eb8c80d165b147
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4
|