auth
Authentication Service LayerLink
Provides pure-Python business-logic functions for user registration, login, logout, token validation, and token refresh. The functions are independent of the Flask-RESTful API layer and raise domain-specific exceptions, making them reusable across different interfaces and straightforward to unit-test.
Key FeaturesLink
- Register new users with required-field validation.
- Check whether a username already exists.
- Authenticate credentials and obtain JWT access and refresh tokens.
- Invalidate a session (logout) and return the logged-out username.
- Validate JWT tokens and retrieve basic user information.
- Refresh access tokens using a refresh token.
- Consistent, domain-specific exception hierarchy (
MissingFieldError,InvalidCredentialsError,TokenError, etc.).
Usage ExamplesLink
>>> from plantdb.server.services.auth import register_user, logout_user, authenticate_user
>>> from plantdb.commons.test_database import test_database
>>> # Initialize the database (creates base directory if needed)
>>> db = test_database(no_auth=True)
>>> db.connect()
>>> register_user(db, {"username": "alice", "fullname": "Alice", "password": "secret"})
>>> logout_user(db, db.session_manager._admin_token)
>>> access_token = authenticate_user(db, "alice", "secret")
>>> print(access_token)
phQk4Uez7Gim3SkY6oBzrCBvos2eHS_wU-_ZvvoPCQI
InvalidCredentialsError
Link
Bases: ValueError
Raised when supplied credentials are not valid.
-
Reference API
server
server
services
auth
authenticate_user
MissingFieldError
Link
Bases: ValueError
Raised when a required field is missing from the request payload.
-
Reference API
server
server
services
auth
register_user
TokenError
Link
Bases: ValueError
Raised for generic token-related problems (validation / refresh).
authenticate_user
Link
authenticate_user(db, username, password)
Authenticate credentials and return (access_token, refresh_token).
Raises:
| Type | Description |
|---|---|
InvalidCredentialsError
|
If the DB |
Source code in plantdb/server/services/auth.py
158 159 160 161 162 163 164 165 166 167 168 169 | |
check_username_exists
Link
check_username_exists(db, username)
Return True if username exists in the database.
The underlying DB exposes rbac_manager.users.exists.
Source code in plantdb/server/services/auth.py
150 151 152 153 154 155 | |
logout_user
Link
Invalidate a session and return the username that was logged out.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
FSDB
|
Database instance exposing |
required |
|
str
|
JWT token extracted from the request (passed via |
required |
|
Logger | None
|
Optional logger. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
Username of the user whose session was terminated. |
Raises:
| Type | Description |
|---|---|
NoAuthUserError
|
If logout fails for any reason. |
Source code in plantdb/server/services/auth.py
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 | |
refresh_token
Link
refresh_token(db, refresh_token)
Refresh an access token using a valid refresh token.
Returns a tuple (new_access_token, new_refresh_token).
Raises:
TokenError: If the refresh token is invalid or the refresh operation fails.
Source code in plantdb/server/services/auth.py
219 220 221 222 223 224 225 226 227 228 229 230 231 232 | |
register_user
Link
Create a new user record in the supplied database.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
FSDB
|
An FSDB instance that implements a |
required |
|
Dict[str, Any]
|
Mapping containing the keys |
required |
Other Parameters:
| Name | Type | Description |
|---|---|---|
token |
str
|
The token for authentication. |
logger |
Logger
|
Optional logger used for diagnostic messages.
If |
Raises:
| Type | Description |
|---|---|
MissingFieldError
|
If any of the required keys ( |
UserAlreadyExistsError
|
If the underlying |
SessionValidationError
|
Propagated unchanged when the database signals that the operation is invalid for the current session (e.g., unauthorized). |
Source code in plantdb/server/services/auth.py
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 | |
validate_token
Link
validate_token(db, token)
Validate a JWT and return basic user information.
Returns a mapping with username and fullname.
Raises:
TokenError: If validation fails.
Source code in plantdb/server/services/auth.py
205 206 207 208 209 210 211 212 213 214 215 216 | |