Skip to main content

User Manager utilities for the Riki

Project description

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:

  • self int - 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:

  • self int - 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:

  • self int - The current instance of User

Returns:

bool:anonymous state

get_id

 | get_id()

Returns the username of a user. Required by flask-login.

Arguments:

  • self int - 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:

  • data List[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:

  • username String - username
  • password String - password

Returns:

  • User- on success
  • bool: False - on Failure

Depends on:

  • get_user
  • check_password
  • update

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:

  • user User - User object

Returns:

  • bool - True on 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:

  • user User - User object

Returns:

  • User- on success
  • bool: False - on Failure

Depends on:

  • add_user
  • login

unregister

 | unregister(user: User)

Deletes a user's profile

Arguments:

  • user User - User object

Returns:

  • bool - True on success, else False

Depends on:

  • update

add_user

 | add_user(user: 'User')

Creates new user in the database

Arguments:

  • user User - User object

Returns:

  • User- on success
  • bool: 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:

  • name str - users username

Returns:

  • bool - True if delete was successful, otherwise False

update

 | update(user: 'User')

Update user from userdata dictionary

Arguments:

  • user User - 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:

  • password str - user password

Returns:

  • bool - did password match

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Built Distribution

Riki_User_Manager-1.0.1-py3-none-any.whl (8.7 kB view hashes)

Uploaded py3

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page