auth
Authentication service layer.
This module contains pure‑Python functions that implement the business logic for
user registration, login, logout, token validation and token refresh. The
functions are deliberately small (atomic) and raise domain‑specific exceptions
instead of returning HTTP responses. Flask‑RESTful resources in
plantdb.server.api.auth act as thin adapters that translate these exceptions
into proper HTTP status codes and JSON payloads.
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
134 135 136 137 138 139 140 141 142 143 144 145 | |
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
126 127 128 129 130 131 | |
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
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 | |
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
195 196 197 198 199 200 201 202 203 204 205 206 207 208 | |
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
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 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 | |
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
181 182 183 184 185 186 187 188 189 190 191 192 | |