Files
romm/backend/endpoints/responses/device/auth.py
Georges-Antoine Assi 0f75e6aaf4 fix(client-tokens): let a client token revoke itself without me.write (#5419)
* fix(client-tokens): let a client token revoke itself without me.write

A device that signs out or re-pairs had no way to revoke its own token
unless it held me.write, which also lets it mint tokens and approve
pairings. DELETE /api/client-tokens/{id} now skips the me.write check
when the id is the token authenticating the request.

Fixes #5274

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(client-tokens): code-review pass on self-revoke

Annotate raise_auth_error as NoReturn, trim the delete_token docstring, and
cover a me.write token revoking a sibling, the revoked token failing to
authenticate, and socket closure on self-revoke.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(client-tokens): revoke the calling token through DELETE /current

Self-revoke moves to an id-less route so a device never needs to know its
token id, and DELETE /{token_id} keeps me.write in its decorator and in
OpenAPI. The device-authorization token response now also returns token_id.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* refactor(client-tokens): simplify pass, reuse raise_auth_error for /current

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* revert(client-tokens): drop DELETE /current and the device-flow token_id

Self-revoke stays on DELETE /{token_id} with the in-handler check.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* feat(device-auth): return the issued token's id from the device token poll

A device paired through the device-authorization flow can now revoke its
own token with DELETE /api/client-tokens/{id}, as tokens from /exchange can.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Update docs/BACKEND_ARCHITECTURE.md

* docs: format client tokens table

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 19:05:52 -04:00

83 lines
2.5 KiB
Python

from pydantic import Field, field_validator
from handler.auth.constants import Scope
from ..base import BaseModel, UTCDatetime
# A request can never legitimately ask for more than every known scope.
_MAX_REQUESTED_SCOPES = len(Scope)
_VALID_SCOPES = {str(s) for s in Scope}
class DeviceAuthInitPayload(BaseModel):
client_device_identifier: str = Field(min_length=1, max_length=255)
name: str = Field(min_length=1, max_length=255)
client: str = Field(min_length=1, max_length=50)
platform: str | None = Field(default=None, max_length=50)
client_version: str | None = Field(default=None, max_length=50)
requested_scopes: list[str] = Field(min_length=1, max_length=_MAX_REQUESTED_SCOPES)
@field_validator("requested_scopes")
@classmethod
def _validate_requested_scopes(cls, value: list[str]) -> list[str]:
# Reject invalid scopes at the boundary
invalid = [s for s in value if s not in _VALID_SCOPES]
if invalid:
raise ValueError(f"Unknown scopes: {', '.join(sorted(set(invalid)))}")
return value
class DeviceAuthInitResponse(BaseModel):
device_code: str
user_code: str
verification_path: str = Field(
description=(
"Relative web-UI path (/pair/device). The client joins it with the "
"origin it was configured to reach; the server is origin-agnostic."
)
)
verification_path_complete: str = Field(
description="Same path with ?user_code= appended, for QR display."
)
expires_in: int
interval: int
class DeviceAuthPendingSchema(BaseModel):
client_device_identifier: str
name: str
client: str
platform: str | None
client_version: str | None
requested_scopes: list[str]
allowed_scopes: list[str]
expires_at: UTCDatetime
class DeviceAuthApprovePayload(BaseModel):
user_code: str = Field(min_length=1, max_length=32)
approved_scopes: list[str] = Field(min_length=1)
device_name: str | None = Field(default=None, max_length=255)
expires_in: str | None = None
class DeviceAuthApproveResponse(BaseModel):
device_id: str
device_name: str | None
class DeviceAuthDenyPayload(BaseModel):
user_code: str = Field(min_length=1, max_length=32)
class DeviceAuthTokenPayload(BaseModel):
device_code: str = Field(min_length=1, max_length=128)
class DeviceAuthTokenResponse(BaseModel):
id: int
access_token: str
device_id: str
scopes: list[str]
expires_at: UTCDatetime | None