Spaces:
Sleeping
Sleeping
Deploy backend 33c9c90
Browse files- app/auth.py +76 -0
- app/domain/errors.py +4 -0
- app/domain/job.py +1 -0
- app/main.py +1 -1
- app/routes/jobs.py +43 -14
- app/routes/uploads.py +10 -4
- app/services/jobs.py +3 -2
- app/settings.py +4 -0
- pyproject.toml +1 -0
- uv.lock +11 -0
app/auth.py
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Who is asking: the Firebase ID token the web app sends as `Authorization: Bearer <token>`.
|
| 2 |
+
|
| 3 |
+
The token is a JWT that Firebase Auth signs with Google's keys. It is checked here against those
|
| 4 |
+
public keys, the project it was issued for and its expiry, so the server holds no secret. Every
|
| 5 |
+
job records the uid that created it, and only that user can see or change it.
|
| 6 |
+
"""
|
| 7 |
+
|
| 8 |
+
from __future__ import annotations
|
| 9 |
+
|
| 10 |
+
import logging
|
| 11 |
+
from collections.abc import Callable
|
| 12 |
+
from functools import cache
|
| 13 |
+
from typing import Protocol
|
| 14 |
+
|
| 15 |
+
import jwt
|
| 16 |
+
from fastapi import Request
|
| 17 |
+
|
| 18 |
+
from app.domain.errors import CodedError, ErrorCode
|
| 19 |
+
|
| 20 |
+
log = logging.getLogger(__name__)
|
| 21 |
+
|
| 22 |
+
# Firebase Auth's signing keys, as a JSON Web Key Set (Google rotates them; PyJWKClient caches).
|
| 23 |
+
KEYS_URL = (
|
| 24 |
+
"https://www.googleapis.com/service_accounts/v1/jwk/securetoken@system.gserviceaccount.com"
|
| 25 |
+
)
|
| 26 |
+
ISSUER = "https://securetoken.google.com/{project}"
|
| 27 |
+
LOCAL_USER = "local" # the one user of a server run with auth_required off (tests, local work)
|
| 28 |
+
_LEEWAY_SECONDS = 30 # clock drift between Google and this server
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
class SigningKey(Protocol):
|
| 32 |
+
@property
|
| 33 |
+
def key(self) -> object: ...
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
type KeyFor = Callable[[str], SigningKey]
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
@cache
|
| 40 |
+
def _google_keys() -> jwt.PyJWKClient:
|
| 41 |
+
return jwt.PyJWKClient(KEYS_URL, cache_keys=True, lifespan=3600)
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
def google_key_for(token: str) -> SigningKey:
|
| 45 |
+
return _google_keys().get_signing_key_from_jwt(token)
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
def user_of(token: str, project: str, key_for: KeyFor | None = None) -> str:
|
| 49 |
+
"""The Firebase uid the token was issued to, once its signature, project and time check.
|
| 50 |
+
``key_for``: where the signing key comes from (Google's, looked up when called)."""
|
| 51 |
+
try:
|
| 52 |
+
key = (key_for or google_key_for)(token).key
|
| 53 |
+
claims = jwt.decode(
|
| 54 |
+
token,
|
| 55 |
+
key=key, # type: ignore[arg-type]
|
| 56 |
+
algorithms=["RS256"],
|
| 57 |
+
audience=project,
|
| 58 |
+
issuer=ISSUER.format(project=project),
|
| 59 |
+
leeway=_LEEWAY_SECONDS,
|
| 60 |
+
options={"require": ["exp", "iat", "sub", "aud", "iss"]},
|
| 61 |
+
)
|
| 62 |
+
except (jwt.PyJWTError, ValueError) as error:
|
| 63 |
+
# The reason only (expired, wrong project ...), never the token.
|
| 64 |
+
log.info("sign-in refused", extra={"reason": type(error).__name__})
|
| 65 |
+
raise CodedError(ErrorCode.UNAUTHENTICATED) from error
|
| 66 |
+
uid = claims.get("sub")
|
| 67 |
+
if not isinstance(uid, str) or not uid:
|
| 68 |
+
raise CodedError(ErrorCode.UNAUTHENTICATED)
|
| 69 |
+
return uid
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
def bearer(request: Request) -> str:
|
| 73 |
+
scheme, _, token = request.headers.get("authorization", "").partition(" ")
|
| 74 |
+
if scheme.lower() != "bearer" or not token.strip():
|
| 75 |
+
raise CodedError(ErrorCode.UNAUTHENTICATED)
|
| 76 |
+
return token.strip()
|
app/domain/errors.py
CHANGED
|
@@ -27,6 +27,7 @@ class ErrorCode(StrEnum):
|
|
| 27 |
OCR_REQUIRED = "ocr_required"
|
| 28 |
UNSUPPORTED_LAYOUT = "unsupported_layout"
|
| 29 |
NO_RECORDS = "no_records"
|
|
|
|
| 30 |
JOB_NOT_FOUND = "job_not_found"
|
| 31 |
JOB_EXPIRED = "job_expired"
|
| 32 |
JOB_STATE_CONFLICT = "job_state_conflict"
|
|
@@ -108,6 +109,9 @@ ERRORS: Mapping[ErrorCode, ErrorSpec] = {
|
|
| 108 |
HTTPStatus.UNPROCESSABLE_ENTITY,
|
| 109 |
"No entries were found in the file. Check that it is the right one.",
|
| 110 |
),
|
|
|
|
|
|
|
|
|
|
| 111 |
ErrorCode.JOB_NOT_FOUND: ErrorSpec(
|
| 112 |
HTTPStatus.NOT_FOUND, "This job does not exist. Start a new one."
|
| 113 |
),
|
|
|
|
| 27 |
OCR_REQUIRED = "ocr_required"
|
| 28 |
UNSUPPORTED_LAYOUT = "unsupported_layout"
|
| 29 |
NO_RECORDS = "no_records"
|
| 30 |
+
UNAUTHENTICATED = "unauthenticated"
|
| 31 |
JOB_NOT_FOUND = "job_not_found"
|
| 32 |
JOB_EXPIRED = "job_expired"
|
| 33 |
JOB_STATE_CONFLICT = "job_state_conflict"
|
|
|
|
| 109 |
HTTPStatus.UNPROCESSABLE_ENTITY,
|
| 110 |
"No entries were found in the file. Check that it is the right one.",
|
| 111 |
),
|
| 112 |
+
ErrorCode.UNAUTHENTICATED: ErrorSpec(
|
| 113 |
+
HTTPStatus.UNAUTHORIZED, "Your sign-in has ended. Sign in again to continue."
|
| 114 |
+
),
|
| 115 |
ErrorCode.JOB_NOT_FOUND: ErrorSpec(
|
| 116 |
HTTPStatus.NOT_FOUND, "This job does not exist. Start a new one."
|
| 117 |
),
|
app/domain/job.py
CHANGED
|
@@ -137,6 +137,7 @@ class Job(BaseModel):
|
|
| 137 |
|
| 138 |
id: JobId
|
| 139 |
tool: str
|
|
|
|
| 140 |
folder: Path
|
| 141 |
options: SerializeAsAny[BaseModel]
|
| 142 |
created_at: datetime
|
|
|
|
| 137 |
|
| 138 |
id: JobId
|
| 139 |
tool: str
|
| 140 |
+
owner: str # the Firebase uid that created the job; only that user may see or change it
|
| 141 |
folder: Path
|
| 142 |
options: SerializeAsAny[BaseModel]
|
| 143 |
created_at: datetime
|
app/main.py
CHANGED
|
@@ -65,7 +65,7 @@ def create_app(
|
|
| 65 |
CORSMiddleware,
|
| 66 |
allow_origins=[settings.frontend_origin],
|
| 67 |
allow_methods=["GET", "POST", "DELETE"],
|
| 68 |
-
allow_headers=["Content-Type"],
|
| 69 |
expose_headers=["Content-Disposition"],
|
| 70 |
)
|
| 71 |
app.include_router(health.router)
|
|
|
|
| 65 |
CORSMiddleware,
|
| 66 |
allow_origins=[settings.frontend_origin],
|
| 67 |
allow_methods=["GET", "POST", "DELETE"],
|
| 68 |
+
allow_headers=["Content-Type", "Authorization"],
|
| 69 |
expose_headers=["Content-Disposition"],
|
| 70 |
)
|
| 71 |
app.include_router(health.router)
|
app/routes/jobs.py
CHANGED
|
@@ -4,17 +4,18 @@ from collections.abc import Mapping
|
|
| 4 |
from datetime import timedelta
|
| 5 |
from urllib.parse import quote
|
| 6 |
|
| 7 |
-
from fastapi import APIRouter, Request, Response
|
| 8 |
from fastapi.responses import FileResponse
|
| 9 |
from starlette.concurrency import run_in_threadpool
|
| 10 |
|
|
|
|
| 11 |
from app.domain.errors import CodedError, ErrorCode
|
| 12 |
from app.domain.job import Job, JobId, Succeeded
|
| 13 |
from app.domain.tool import Tool, ToolResult
|
| 14 |
from app.routes.errors import ERROR_RESPONSES
|
| 15 |
from app.routes.uploads import FILES_FIELD, OPTIONS_FIELD, receive_job, request_body
|
| 16 |
from app.schemas import JobView, MappingRequest, PasswordsRequest, job_view
|
| 17 |
-
from app.services.jobs import JobStore
|
| 18 |
from app.services.runner import JobRunner
|
| 19 |
from app.settings import Settings
|
| 20 |
|
|
@@ -61,8 +62,23 @@ def router(
|
|
| 61 |
api = APIRouter(prefix="/api/jobs", tags=["jobs"], responses=ERROR_RESPONSES)
|
| 62 |
ttl = timedelta(seconds=settings.job_ttl_seconds)
|
| 63 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
@api.post("/{tool}", status_code=202, openapi_extra=_UPLOAD_BODY)
|
| 65 |
-
async def create_job(tool: str, request: Request) -> JobView:
|
| 66 |
"""Upload the files and start the job, in one request."""
|
| 67 |
spec = tools.get(tool)
|
| 68 |
if spec is None:
|
|
@@ -74,40 +90,45 @@ def router(
|
|
| 74 |
runner,
|
| 75 |
request.headers.get("content-type", ""),
|
| 76 |
request_body(request),
|
|
|
|
| 77 |
)
|
| 78 |
return job_view(store.get(job_id), ttl)
|
| 79 |
|
| 80 |
@api.get("/{job_id}")
|
| 81 |
-
|
| 82 |
"""The job's state. Cheap enough to poll every second."""
|
| 83 |
-
return job_view(
|
| 84 |
|
| 85 |
@api.post("/{job_id}/passwords", status_code=202)
|
| 86 |
-
def supply_passwords(
|
|
|
|
|
|
|
| 87 |
"""Give the password of every file the job asked for. The job runs again from the start."""
|
|
|
|
| 88 |
passwords = {name: secret.get_secret_value() for name, secret in body.passwords.items()}
|
| 89 |
runner.resume(JobId(job_id), passwords)
|
| 90 |
return job_view(store.get(JobId(job_id)), ttl)
|
| 91 |
|
| 92 |
@api.post("/{job_id}/mapping", status_code=202)
|
| 93 |
-
def supply_mapping(job_id: str, body: MappingRequest) -> JobView:
|
| 94 |
"""Confirm or change how each register column is read. The job runs again from the start."""
|
|
|
|
| 95 |
runner.remap(JobId(job_id), body.sheets)
|
| 96 |
return job_view(store.get(JobId(job_id)), ttl)
|
| 97 |
|
| 98 |
@api.get("/{job_id}/result")
|
| 99 |
-
|
| 100 |
-
return _result(
|
| 101 |
|
| 102 |
@api.get(
|
| 103 |
"/{job_id}/downloads/{name}",
|
| 104 |
response_class=FileResponse,
|
| 105 |
responses={200: {"content": {"application/octet-stream": {}}}},
|
| 106 |
)
|
| 107 |
-
|
| 108 |
"""The workbook or an extra download the result names. The job stays until it expires or
|
| 109 |
is deleted, so a file can be downloaded again."""
|
| 110 |
-
job =
|
| 111 |
result = _result(job)
|
| 112 |
if name not in {result.workbook, *(extra.name for extra in result.extra_downloads)}:
|
| 113 |
raise CodedError(
|
|
@@ -126,9 +147,17 @@ def router(
|
|
| 126 |
)
|
| 127 |
|
| 128 |
@api.delete("/{job_id}", status_code=204)
|
| 129 |
-
def delete_job(job_id: str) -> Response:
|
| 130 |
-
"""Stop the job if it runs and delete its files now. Deleting twice is not an error
|
| 131 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
return Response(status_code=204)
|
| 133 |
|
| 134 |
return api
|
|
|
|
| 4 |
from datetime import timedelta
|
| 5 |
from urllib.parse import quote
|
| 6 |
|
| 7 |
+
from fastapi import APIRouter, Depends, Request, Response
|
| 8 |
from fastapi.responses import FileResponse
|
| 9 |
from starlette.concurrency import run_in_threadpool
|
| 10 |
|
| 11 |
+
from app.auth import LOCAL_USER, bearer, user_of
|
| 12 |
from app.domain.errors import CodedError, ErrorCode
|
| 13 |
from app.domain.job import Job, JobId, Succeeded
|
| 14 |
from app.domain.tool import Tool, ToolResult
|
| 15 |
from app.routes.errors import ERROR_RESPONSES
|
| 16 |
from app.routes.uploads import FILES_FIELD, OPTIONS_FIELD, receive_job, request_body
|
| 17 |
from app.schemas import JobView, MappingRequest, PasswordsRequest, job_view
|
| 18 |
+
from app.services.jobs import JobNotFoundError, JobStore
|
| 19 |
from app.services.runner import JobRunner
|
| 20 |
from app.settings import Settings
|
| 21 |
|
|
|
|
| 62 |
api = APIRouter(prefix="/api/jobs", tags=["jobs"], responses=ERROR_RESPONSES)
|
| 63 |
ttl = timedelta(seconds=settings.job_ttl_seconds)
|
| 64 |
|
| 65 |
+
def signed_in(request: Request) -> str:
|
| 66 |
+
"""The Firebase uid of the caller. A sync dependency: FastAPI runs it in a thread, so
|
| 67 |
+
fetching Google's keys the first time never blocks the event loop."""
|
| 68 |
+
if not settings.auth_required:
|
| 69 |
+
return LOCAL_USER
|
| 70 |
+
return user_of(bearer(request), settings.firebase_project_id)
|
| 71 |
+
|
| 72 |
+
def owned(job_id: str, uid: str) -> Job:
|
| 73 |
+
"""The job, when it is the caller's. Another user's job is "not found", so a job id
|
| 74 |
+
tells nothing about who else uses the server."""
|
| 75 |
+
job = store.get(JobId(job_id))
|
| 76 |
+
if job.owner != uid:
|
| 77 |
+
raise CodedError(ErrorCode.JOB_NOT_FOUND)
|
| 78 |
+
return job
|
| 79 |
+
|
| 80 |
@api.post("/{tool}", status_code=202, openapi_extra=_UPLOAD_BODY)
|
| 81 |
+
async def create_job(tool: str, request: Request, uid: str = Depends(signed_in)) -> JobView:
|
| 82 |
"""Upload the files and start the job, in one request."""
|
| 83 |
spec = tools.get(tool)
|
| 84 |
if spec is None:
|
|
|
|
| 90 |
runner,
|
| 91 |
request.headers.get("content-type", ""),
|
| 92 |
request_body(request),
|
| 93 |
+
uid,
|
| 94 |
)
|
| 95 |
return job_view(store.get(job_id), ttl)
|
| 96 |
|
| 97 |
@api.get("/{job_id}")
|
| 98 |
+
def get_job(job_id: str, uid: str = Depends(signed_in)) -> JobView:
|
| 99 |
"""The job's state. Cheap enough to poll every second."""
|
| 100 |
+
return job_view(owned(job_id, uid), ttl)
|
| 101 |
|
| 102 |
@api.post("/{job_id}/passwords", status_code=202)
|
| 103 |
+
def supply_passwords(
|
| 104 |
+
job_id: str, body: PasswordsRequest, uid: str = Depends(signed_in)
|
| 105 |
+
) -> JobView:
|
| 106 |
"""Give the password of every file the job asked for. The job runs again from the start."""
|
| 107 |
+
owned(job_id, uid)
|
| 108 |
passwords = {name: secret.get_secret_value() for name, secret in body.passwords.items()}
|
| 109 |
runner.resume(JobId(job_id), passwords)
|
| 110 |
return job_view(store.get(JobId(job_id)), ttl)
|
| 111 |
|
| 112 |
@api.post("/{job_id}/mapping", status_code=202)
|
| 113 |
+
def supply_mapping(job_id: str, body: MappingRequest, uid: str = Depends(signed_in)) -> JobView:
|
| 114 |
"""Confirm or change how each register column is read. The job runs again from the start."""
|
| 115 |
+
owned(job_id, uid)
|
| 116 |
runner.remap(JobId(job_id), body.sheets)
|
| 117 |
return job_view(store.get(JobId(job_id)), ttl)
|
| 118 |
|
| 119 |
@api.get("/{job_id}/result")
|
| 120 |
+
def get_result(job_id: str, uid: str = Depends(signed_in)) -> ToolResult:
|
| 121 |
+
return _result(owned(job_id, uid))
|
| 122 |
|
| 123 |
@api.get(
|
| 124 |
"/{job_id}/downloads/{name}",
|
| 125 |
response_class=FileResponse,
|
| 126 |
responses={200: {"content": {"application/octet-stream": {}}}},
|
| 127 |
)
|
| 128 |
+
def download(job_id: str, name: str, uid: str = Depends(signed_in)) -> FileResponse:
|
| 129 |
"""The workbook or an extra download the result names. The job stays until it expires or
|
| 130 |
is deleted, so a file can be downloaded again."""
|
| 131 |
+
job = owned(job_id, uid)
|
| 132 |
result = _result(job)
|
| 133 |
if name not in {result.workbook, *(extra.name for extra in result.extra_downloads)}:
|
| 134 |
raise CodedError(
|
|
|
|
| 147 |
)
|
| 148 |
|
| 149 |
@api.delete("/{job_id}", status_code=204)
|
| 150 |
+
def delete_job(job_id: str, uid: str = Depends(signed_in)) -> Response:
|
| 151 |
+
"""Stop the job if it runs and delete its files now. Deleting twice is not an error, and
|
| 152 |
+
another user's job is left alone the same way."""
|
| 153 |
+
try:
|
| 154 |
+
mine = store.get(JobId(job_id)).owner == uid
|
| 155 |
+
except JobNotFoundError:
|
| 156 |
+
mine = True # already gone: the runner's discard is a no-op
|
| 157 |
+
except CodedError:
|
| 158 |
+
mine = True # expired: discard removes what is left
|
| 159 |
+
if mine:
|
| 160 |
+
runner.discard(JobId(job_id))
|
| 161 |
return Response(status_code=204)
|
| 162 |
|
| 163 |
return api
|
app/routes/uploads.py
CHANGED
|
@@ -188,9 +188,10 @@ def _check_file(tool: Tool, filename: str, count: int) -> None:
|
|
| 188 |
|
| 189 |
|
| 190 |
class _Intake:
|
| 191 |
-
def __init__(self, tool: Tool, store: JobStore) -> None:
|
| 192 |
self.tool = tool
|
| 193 |
self.store = store
|
|
|
|
| 194 |
self.options: BaseModel | None = None
|
| 195 |
self.job_id: JobId | None = None
|
| 196 |
self.files = 0
|
|
@@ -209,15 +210,20 @@ class _Intake:
|
|
| 209 |
if self.job_id is None:
|
| 210 |
if self.options is None:
|
| 211 |
self.options = _parse_options(self.tool, b"{}")
|
| 212 |
-
self.job_id = self.store.create(self.tool.id, self.options).id
|
| 213 |
return self.job_id
|
| 214 |
|
| 215 |
|
| 216 |
def receive_job(
|
| 217 |
-
tool: Tool,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 218 |
) -> JobId:
|
| 219 |
"""Create, fill and submit one job from an upload. Blocking, so run it in a worker thread."""
|
| 220 |
-
intake = _Intake(tool, store)
|
| 221 |
try:
|
| 222 |
for part in _parts(_events(_boundary(content_type), body)):
|
| 223 |
if part.filename is None and part.name == OPTIONS_FIELD:
|
|
|
|
| 188 |
|
| 189 |
|
| 190 |
class _Intake:
|
| 191 |
+
def __init__(self, tool: Tool, store: JobStore, owner: str) -> None:
|
| 192 |
self.tool = tool
|
| 193 |
self.store = store
|
| 194 |
+
self.owner = owner
|
| 195 |
self.options: BaseModel | None = None
|
| 196 |
self.job_id: JobId | None = None
|
| 197 |
self.files = 0
|
|
|
|
| 210 |
if self.job_id is None:
|
| 211 |
if self.options is None:
|
| 212 |
self.options = _parse_options(self.tool, b"{}")
|
| 213 |
+
self.job_id = self.store.create(self.tool.id, self.options, self.owner).id
|
| 214 |
return self.job_id
|
| 215 |
|
| 216 |
|
| 217 |
def receive_job(
|
| 218 |
+
tool: Tool,
|
| 219 |
+
store: JobStore,
|
| 220 |
+
runner: JobRunner,
|
| 221 |
+
content_type: str,
|
| 222 |
+
body: Iterable[bytes],
|
| 223 |
+
owner: str,
|
| 224 |
) -> JobId:
|
| 225 |
"""Create, fill and submit one job from an upload. Blocking, so run it in a worker thread."""
|
| 226 |
+
intake = _Intake(tool, store, owner)
|
| 227 |
try:
|
| 228 |
for part in _parts(_events(_boundary(content_type), body)):
|
| 229 |
if part.filename is None and part.name == OPTIONS_FIELD:
|
app/services/jobs.py
CHANGED
|
@@ -39,7 +39,7 @@ class JobExpiredError(CodedError):
|
|
| 39 |
|
| 40 |
|
| 41 |
class JobStore(Protocol):
|
| 42 |
-
def create(self, tool: str, options: BaseModel) -> Job: ...
|
| 43 |
def get(self, job_id: JobId) -> Job: ...
|
| 44 |
def update(self, job_id: JobId, change: Change) -> Job: ...
|
| 45 |
def save_input(self, job_id: JobId, filename: str, chunks: Iterable[bytes]) -> Path: ...
|
|
@@ -89,12 +89,13 @@ class InMemoryJobStore:
|
|
| 89 |
if leftover.is_dir() and _JOB_FOLDER.match(leftover.name):
|
| 90 |
self._try_remove(leftover)
|
| 91 |
|
| 92 |
-
def create(self, tool: str, options: BaseModel) -> Job:
|
| 93 |
job_id = JobId(uuid.uuid4().hex)
|
| 94 |
now = self._clock()
|
| 95 |
job = Job(
|
| 96 |
id=job_id,
|
| 97 |
tool=tool,
|
|
|
|
| 98 |
folder=self._root / job_id,
|
| 99 |
options=options,
|
| 100 |
created_at=now,
|
|
|
|
| 39 |
|
| 40 |
|
| 41 |
class JobStore(Protocol):
|
| 42 |
+
def create(self, tool: str, options: BaseModel, owner: str) -> Job: ...
|
| 43 |
def get(self, job_id: JobId) -> Job: ...
|
| 44 |
def update(self, job_id: JobId, change: Change) -> Job: ...
|
| 45 |
def save_input(self, job_id: JobId, filename: str, chunks: Iterable[bytes]) -> Path: ...
|
|
|
|
| 89 |
if leftover.is_dir() and _JOB_FOLDER.match(leftover.name):
|
| 90 |
self._try_remove(leftover)
|
| 91 |
|
| 92 |
+
def create(self, tool: str, options: BaseModel, owner: str) -> Job:
|
| 93 |
job_id = JobId(uuid.uuid4().hex)
|
| 94 |
now = self._clock()
|
| 95 |
job = Job(
|
| 96 |
id=job_id,
|
| 97 |
tool=tool,
|
| 98 |
+
owner=owner,
|
| 99 |
folder=self._root / job_id,
|
| 100 |
options=options,
|
| 101 |
created_at=now,
|
app/settings.py
CHANGED
|
@@ -51,6 +51,10 @@ class Settings(BaseSettings):
|
|
| 51 |
frontend_origin: Annotated[str, StringConstraints(pattern=r"^https?://[^/\s]+$")] = (
|
| 52 |
"http://localhost:3000"
|
| 53 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
@property
|
| 56 |
def max_file_bytes(self) -> int:
|
|
|
|
| 51 |
frontend_origin: Annotated[str, StringConstraints(pattern=r"^https?://[^/\s]+$")] = (
|
| 52 |
"http://localhost:3000"
|
| 53 |
)
|
| 54 |
+
# The Firebase project whose sign-ins the jobs accept (a public id, not a secret).
|
| 55 |
+
firebase_project_id: str = "audit-automation-2e276"
|
| 56 |
+
# Off only for tests and local work: every request is then the one local user.
|
| 57 |
+
auth_required: bool = True
|
| 58 |
|
| 59 |
@property
|
| 60 |
def max_file_bytes(self) -> int:
|
pyproject.toml
CHANGED
|
@@ -14,6 +14,7 @@ dependencies = [
|
|
| 14 |
"pillow>=13.0.0",
|
| 15 |
"pydantic>=2.14.1",
|
| 16 |
"pydantic-settings>=2.16.1",
|
|
|
|
| 17 |
"pypdfium2>=5.14.0",
|
| 18 |
"pytesseract>=0.3.13",
|
| 19 |
"python-multipart>=0.0.32",
|
|
|
|
| 14 |
"pillow>=13.0.0",
|
| 15 |
"pydantic>=2.14.1",
|
| 16 |
"pydantic-settings>=2.16.1",
|
| 17 |
+
"pyjwt>=2.10",
|
| 18 |
"pypdfium2>=5.14.0",
|
| 19 |
"pytesseract>=0.3.13",
|
| 20 |
"python-multipart>=0.0.32",
|
uv.lock
CHANGED
|
@@ -116,6 +116,7 @@ dependencies = [
|
|
| 116 |
{ name = "pillow" },
|
| 117 |
{ name = "pydantic" },
|
| 118 |
{ name = "pydantic-settings" },
|
|
|
|
| 119 |
{ name = "pypdfium2" },
|
| 120 |
{ name = "pytesseract" },
|
| 121 |
{ name = "python-multipart" },
|
|
@@ -146,6 +147,7 @@ requires-dist = [
|
|
| 146 |
{ name = "pillow", specifier = ">=13.0.0" },
|
| 147 |
{ name = "pydantic", specifier = ">=2.14.1" },
|
| 148 |
{ name = "pydantic-settings", specifier = ">=2.16.1" },
|
|
|
|
| 149 |
{ name = "pypdfium2", specifier = ">=5.14.0" },
|
| 150 |
{ name = "pytesseract", specifier = ">=0.3.13" },
|
| 151 |
{ name = "python-multipart", specifier = ">=0.0.32" },
|
|
@@ -1265,6 +1267,15 @@ wheels = [
|
|
| 1265 |
{ url = "https://files.pythonhosted.org/packages/71/46/17f022dd3e953bf20a04a028a21ec746d942f8d2af30fa0f124fa0e6a684/pygments-2.21.0-py3-none-any.whl", hash = "sha256:2363c69b61c4a97c838da3b130dcd6468f4848992b21a82f2a63ec34377137d9", size = 1250147, upload-time = "2026-08-17T08:02:44.912Z" },
|
| 1266 |
]
|
| 1267 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1268 |
[[package]]
|
| 1269 |
name = "pypdfium2"
|
| 1270 |
version = "5.14.0"
|
|
|
|
| 116 |
{ name = "pillow" },
|
| 117 |
{ name = "pydantic" },
|
| 118 |
{ name = "pydantic-settings" },
|
| 119 |
+
{ name = "pyjwt" },
|
| 120 |
{ name = "pypdfium2" },
|
| 121 |
{ name = "pytesseract" },
|
| 122 |
{ name = "python-multipart" },
|
|
|
|
| 147 |
{ name = "pillow", specifier = ">=13.0.0" },
|
| 148 |
{ name = "pydantic", specifier = ">=2.14.1" },
|
| 149 |
{ name = "pydantic-settings", specifier = ">=2.16.1" },
|
| 150 |
+
{ name = "pyjwt", specifier = ">=2.10" },
|
| 151 |
{ name = "pypdfium2", specifier = ">=5.14.0" },
|
| 152 |
{ name = "pytesseract", specifier = ">=0.3.13" },
|
| 153 |
{ name = "python-multipart", specifier = ">=0.0.32" },
|
|
|
|
| 1267 |
{ url = "https://files.pythonhosted.org/packages/71/46/17f022dd3e953bf20a04a028a21ec746d942f8d2af30fa0f124fa0e6a684/pygments-2.21.0-py3-none-any.whl", hash = "sha256:2363c69b61c4a97c838da3b130dcd6468f4848992b21a82f2a63ec34377137d9", size = 1250147, upload-time = "2026-08-17T08:02:44.912Z" },
|
| 1268 |
]
|
| 1269 |
|
| 1270 |
+
[[package]]
|
| 1271 |
+
name = "pyjwt"
|
| 1272 |
+
version = "2.15.1"
|
| 1273 |
+
source = { registry = "https://pypi.org/simple" }
|
| 1274 |
+
sdist = { url = "https://files.pythonhosted.org/packages/43/ea/5194e52748b0da83d71e082d75496eaec6e58f419f5e184786ded517e6a9/pyjwt-2.15.1.tar.gz", hash = "sha256:4f259e80cdfb6b3fc18a7de51fd1ef9ec79652f25019bae68975ca2468a34df8", size = 121252, upload-time = "2026-09-28T18:40:42.598Z" }
|
| 1275 |
+
wheels = [
|
| 1276 |
+
{ url = "https://files.pythonhosted.org/packages/50/ca/44de4e75f8aadc457f0634be3b542815078ded46dca30efb960edeecad6e/pyjwt-2.15.1-py3-none-any.whl", hash = "sha256:42d59d631f7768a1028a64c7ff581a9bf7519804daf91fc5b6c56e30eec5e193", size = 33860, upload-time = "2026-09-28T18:40:41.429Z" },
|
| 1277 |
+
]
|
| 1278 |
+
|
| 1279 |
[[package]]
|
| 1280 |
name = "pypdfium2"
|
| 1281 |
version = "5.14.0"
|