Backend Python API¶
main.py
Entry point for the Quill Medical FastAPI application.
This module configures the API, dependencies, authentication,
and patient record routes. All endpoints are exposed under the
/api
prefix. In development, Swagger UI and ReDoc documentation
are also served under /api/docs
and /api/redoc
.
TotpSetupOut ¶
Bases: BaseModel
Output model containing a provisioning URI for authenticator apps.
Source code in app/main.py
318 319 320 321 |
|
TotpVerifyIn ¶
Bases: BaseModel
Input model for verifying a TOTP code during setup.
Source code in app/main.py
351 352 353 354 |
|
set_auth_cookies ¶
set_auth_cookies(response, access, refresh, xsrf)
Set authentication cookies for access, refresh, and CSRF tokens.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
response
|
Response
|
The outgoing FastAPI response object. |
required |
access
|
str
|
Encoded access token (short-lived). |
required |
refresh
|
str
|
Encoded refresh token (long-lived). |
required |
xsrf
|
str
|
Cross-site request forgery token. |
required |
Source code in app/main.py
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 |
|
clear_auth_cookies ¶
clear_auth_cookies(response)
Clear authentication cookies from the client.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
response
|
Response
|
The outgoing FastAPI response object. |
required |
Source code in app/main.py
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
|
current_user ¶
current_user(request, db=DEP_GET_SESSION)
Get the currently authenticated user from cookies.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
request
|
Request
|
Incoming FastAPI request. |
required |
db
|
Session
|
Active SQLAlchemy session. |
DEP_GET_SESSION
|
Returns:
Name | Type | Description |
---|---|---|
User |
User
|
The authenticated and active user. |
Raises:
Type | Description |
---|---|
HTTPException
|
If the user is not authenticated or inactive. |
Source code in app/main.py
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
|
require_roles ¶
require_roles(*need)
Create a dependency that enforces required roles on a route.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*need
|
str
|
Role names that the caller must possess. |
()
|
Returns:
Name | Type | Description |
---|---|---|
Callable |
A dependency for injection into route |
Raises:
Type | Description |
---|---|
HTTPException
|
403 if the user lacks required roles. |
Source code in app/main.py
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
|
require_csrf ¶
require_csrf(request, u=DEP_CURRENT_USER)
Validate CSRF by comparing header and cookie and checking the signature.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
request
|
Request
|
Incoming request carrying the header and cookie. |
required |
u
|
User
|
Current authenticated user. |
DEP_CURRENT_USER
|
Returns:
Name | Type | Description |
---|---|---|
User |
The validated user (pass-through). |
Raises:
Type | Description |
---|---|
HTTPException
|
403 on CSRF failure. |
Source code in app/main.py
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
|
login ¶
login(data, response, db=DEP_GET_SESSION)
Authenticate a user and set auth cookies.
Validates username/password, optionally verifies a time-based one-time code when two-factor is enabled, then issues access/refresh/CSRF cookies.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
LoginIn
|
Login payload including username, password, and optional TOTP. |
required |
response
|
Response
|
Outgoing response used to set cookies. |
required |
db
|
Session
|
Database session. |
DEP_GET_SESSION
|
Returns:
Name | Type | Description |
---|---|---|
dict |
dict
|
Minimal result and the caller's username/roles. |
Raises:
Type | Description |
---|---|
HTTPException
|
400 for invalid credentials or TOTP; 401 if token decoding fails. |
Source code in app/main.py
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 |
|
register ¶
register(payload, db=DEP_GET_SESSION)
Register a new user with username, email, and password.
Performs minimal validation and uniqueness checks, then stores a hashed password.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
payload
|
RegisterIn
|
Registration data. |
required |
db
|
Session
|
Database session. |
DEP_GET_SESSION
|
Returns:
Name | Type | Description |
---|---|---|
dict |
Confirmation payload. |
Raises:
Type | Description |
---|---|
HTTPException
|
400 if fields are missing/short or username/email already exists. |
Source code in app/main.py
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 |
|
totp_setup ¶
totp_setup(u=DEP_CURRENT_USER, db=DEP_GET_SESSION)
Create (if missing) a TOTP secret and return a provisioning URI.
The frontend should render the URI as a QR code for an authenticator app.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
u
|
User
|
Current authenticated user (injected). |
DEP_CURRENT_USER
|
db
|
Session
|
Database session. |
DEP_GET_SESSION
|
Returns:
Name | Type | Description |
---|---|---|
TotpSetupOut |
Provisioning URI encoded with issuer and account name. |
Source code in app/main.py
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 |
|
totp_verify ¶
totp_verify(payload, u=DEP_CURRENT_USER, db=DEP_GET_SESSION)
Verify a TOTP code and enable two-factor authentication for the user.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
payload
|
TotpVerifyIn
|
The six-digit code from the authenticator app. |
required |
u
|
User
|
Current authenticated user. |
DEP_CURRENT_USER
|
db
|
Session
|
Database session. |
DEP_GET_SESSION
|
Returns:
Name | Type | Description |
---|---|---|
dict |
Confirmation payload. |
Raises:
Type | Description |
---|---|
HTTPException
|
400 if no secret exists or code is invalid. |
Source code in app/main.py
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 |
|
totp_disable ¶
totp_disable(u=DEP_REQUIRE_CSRF, db=DEP_GET_SESSION)
Disable TOTP for the current user.
CSRF protection is required.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
u
|
User
|
Current authenticated user (CSRF-checked). |
DEP_REQUIRE_CSRF
|
db
|
Session
|
Database session. |
DEP_GET_SESSION
|
Returns:
Name | Type | Description |
---|---|---|
dict |
Confirmation payload. |
Source code in app/main.py
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 |
|
logout ¶
logout(response, _u=DEP_CURRENT_USER)
Log out the current user by clearing auth cookies.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
response
|
Response
|
Outgoing response used to clear cookies. |
required |
_u
|
User
|
Current authenticated user (unused; enforces auth). |
DEP_CURRENT_USER
|
Returns:
Name | Type | Description |
---|---|---|
dict |
Confirmation payload. |
Source code in app/main.py
418 419 420 421 422 423 424 425 426 427 428 429 430 |
|
me ¶
me(u=DEP_CURRENT_USER)
Return a minimal profile for the current user.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
u
|
User
|
Current authenticated user. |
DEP_CURRENT_USER
|
Returns:
Name | Type | Description |
---|---|---|
dict |
Basic identity fields and role names. |
Source code in app/main.py
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 |
|
refresh ¶
refresh(response, request, db=DEP_GET_SESSION)
Rotate refresh token and issue a new access token.
Reads the refresh token cookie, validates it, then sets new access/refresh/CSRF cookies.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
response
|
Response
|
Outgoing response used to set cookies. |
required |
request
|
Request
|
Incoming request (reads refresh cookie). |
required |
db
|
Session
|
Database session. |
DEP_GET_SESSION
|
Returns:
Name | Type | Description |
---|---|---|
dict |
Confirmation payload. |
Raises:
Type | Description |
---|---|
HTTPException
|
401 if the refresh token is missing/invalid or user is inactive. |
Source code in app/main.py
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 |
|
create_patient_repo ¶
create_patient_repo(payload)
Create a new patient repository and seed it with a README.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
payload
|
PatientCreate
|
Patient identifier and any setup metadata. |
required |
Returns:
Name | Type | Description |
---|---|---|
dict |
Repo name and initialisation status. |
Raises:
Type | Description |
---|---|
HTTPException
|
500 on storage/write errors. |
Source code in app/main.py
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 |
|
list_patients ¶
list_patients(u=DEP_CURRENT_USER)
List patient repositories and any stored demographics.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
u
|
User
|
Current authenticated user. |
DEP_CURRENT_USER
|
Returns:
Name | Type | Description |
---|---|---|
dict |
Array of patient repo names with optional demographics. |
Source code in app/main.py
529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 |
|
upsert_demographics ¶
upsert_demographics(patient_id, demographics)
Create or update demographics for a patient.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
patient_id
|
str
|
Patient identifier. |
required |
demographics
|
Demographics
|
Structured demographics payload. |
required |
Returns:
Name | Type | Description |
---|---|---|
dict |
Repo name and stored path. |
Raises:
Type | Description |
---|---|
HTTPException
|
500 on write errors. |
Source code in app/main.py
559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 |
|
get_demographics ¶
get_demographics(patient_id, u=DEP_CURRENT_USER)
Fetch demographics JSON for a given patient.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
patient_id
|
str
|
Patient identifier. |
required |
u
|
User
|
Current authenticated user. |
DEP_CURRENT_USER
|
Returns:
Name | Type | Description |
---|---|---|
dict |
Repo name and raw demographics content. |
Raises:
Type | Description |
---|---|
HTTPException
|
404 if not found; 500 on read errors. |
Source code in app/main.py
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 |
|
write_letter ¶
write_letter(patient_id, letter)
Write and store a new letter in the patient’s repository.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
patient_id
|
str
|
Patient identifier. |
required |
letter
|
LetterIn
|
Letter metadata and markdown body. |
required |
Returns:
Name | Type | Description |
---|---|---|
dict |
Repo and stored file path. |
Raises:
Type | Description |
---|---|
HTTPException
|
500 on write errors. |
Source code in app/main.py
618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 |
|
read_letter ¶
read_letter(patient_id, name, u=DEP_CURRENT_USER)
Read a specific letter file from the patient’s repository.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
patient_id
|
str
|
Patient identifier. |
required |
name
|
str
|
Letter filename (with or without .md). |
required |
u
|
User
|
Current authenticated user. |
DEP_CURRENT_USER
|
Returns:
Name | Type | Description |
---|---|---|
dict |
Repo, resolved file path, and markdown content. |
Raises:
Type | Description |
---|---|
HTTPException
|
404 if not found; 500 on read errors. |
Source code in app/main.py
658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 |
|