Basic token based authentication server
The MOTH CLI is very limited and only contains two commands.
The first command is moth create. This takes a file path and creates an empty MOTH database file.
The second command is moth run. This takes the path to a MOTH database and a server port, and starts the server.
Accessed through moth.moth, this class connects directly to the database without the use of an intermediary server.
Accessed through moth.server, this class can runs a server mirroring the methods, inputs, and responses of moth.moth.
Parameters can be sent to moth.server via JSON. For example, the call logout(token="TOKEN") is equivalent to an API call to /logout with the JSON data {token="TOKEN"}.
Return codes, and what exceptions they represent, are noted in the call documentation.
Can be started without blocking through moth.server.start_threaded.
Accessed through moth.client, this class connects to an instance of moth.server but behaves like moth.moth, including exceptions.
This may also return moth.utils.ServerError when the server responds in an unexpected way.
This is a collection of MOTH utilities, exceptions, and other internal classes accessible through moth.utils.
There are 3 primary functions included:
db_existstakes a path to a database file and returns whether it exists or not.make_dbtakes a path and creates a new database file at it if a database is not already present there. It will return if a new databse was created or not.reset_dbtakes a path to an existing database file and resets if it is present. It will not create a new database. It will return if it reset the database.
username: The name of the user accountpassword: The password of the user accounttoken: An access token associated with an accountuseridorid: An internal unique incremental ID associated with each accountexpires: A unix timestamp at which the associated token expires.valid: A boolean stating if the requested resource is valid or not.deleted: A boolean stating if the requested resource has been successfuly deleted.updated: A boolean stating if the requested resource has been successfuly updated.count: An integer representing the amount of matching resources present.
Create and return a user token.
Equivalent server call: /login [GET]
Takes: username, password
Returns: token, userid, username, expires
Error codes:
401 User does not existormoth.utils.NoUserError: User does not exist.401 Invalid passwordormoth.utils.InvalidPasswordError: User is valid but the provided password does not match.
Validate that a token exists.
Equivalent server call: /validate [GET]
Takes: token
Returns valid, userid, username, expires
Error codes:
401 Token does not existormoth.utils.InvalidTokenError: Token does not exist.401 Token expiredormoth.utils.TokenExpiredError: Token has expired.
Check if a password is valid without logging in.
Equivalent server call: /passvalid [GET]
Takes: username, password
Returns: valid
Error codes:
401 Unknown usernameormoth.utils.NoUserError: User does not exist.
Delete an access token.
Equivalent server call: /logout [DELETE]
Takes: token
Returns deleted
Error codes:
401 Token does not existormoth.utils.InvalidTokenError: Token does not exist.
Create a new user.
Equivalent server call: /new [PUT]
Takes: username, password
Returns: userid, username
Error codes:
409 User already existsormoth.utils.UserExistsError: User already exists.
Delete an existing user.
Equivalent server call: /del [DELETE]
Takes: id
Returns: deleted
Error codes:
401 User does not existormoth.utils.NoUserError: User does not exist.
Give a user a new password.
Equivalent server call: /setpass [PATCH]
Takes: id, password
Returns: updated
Error codes:
401 User does not existormoth.utils.NoUserError: User does not exist.
Check how many tokens a user has.
Equivalent server call: /gettokens [GET]
Takes: id
Returns: count
Error codes:
Retrieve a list of users.
Equivalent server call: /getusers [GET]
Takes:
Returns: [id, username]
Error codes:
Retrieve information about a specific user.
Equivalent server call: /getuser [GET]
Takes: id
Returns: id, username
Error codes:
401 User does not existormoth.utils.NoUserError: User does not exist.
Clear all tokens associated with a user.
Equivalent server call: /deltokens [DELETE]
Takes: id
Returns: deleted, count
Error codes:
This server is intended to be entirely backend, and does not do any credential validation before performing actions. It should never be accessible to untrusted programs, and programs intending to use MOTH should perform their own checks before passing the operation over to MOTH.