From baad9d46cc2859c775c214503eec90f5cccd93e5 Mon Sep 17 00:00:00 2001 From: qaz741wsd856 Date: Mon, 1 Dec 2025 05:20:35 +0000 Subject: [PATCH] feat: expand device API with stub implementation --- src/handlers/devices.rs | 88 ++++++++++++++++++++++++++++++++++++++++- src/router.rs | 23 ++++++++++- 2 files changed, 109 insertions(+), 2 deletions(-) diff --git a/src/handlers/devices.rs b/src/handlers/devices.rs index 63a5920..5ce21c8 100644 --- a/src/handlers/devices.rs +++ b/src/handlers/devices.rs @@ -1,6 +1,9 @@ -use axum::Json; +use axum::{extract::Path, Json}; +use serde::Deserialize; use serde_json::{json, Value}; +use crate::error::AppError; + /// GET /devices /// /// Returns an empty list of devices. @@ -16,3 +19,86 @@ pub async fn get_devices() -> Json { })) } +/// GET /devices/knowndevice +/// +/// Checks if a device is known to the server. +/// Always returns false since we don't track devices. +/// This is used by clients to determine if a device has been previously used. +/// X-Request-Email and X-Device-Identifier headers are expected but we ignore them. +#[worker::send] +pub async fn get_known_device() -> Json { + // Always return false - we don't track devices + Json(false) +} + +/// GET /devices/identifier/{device_id} +/// +/// Returns information about a specific device. +/// Since we don't track devices, we return a 404-like error. +/// However, to avoid client errors, we return an empty device stub. +#[worker::send] +pub async fn get_device(Path(device_id): Path) -> Result, AppError> { + // Return a minimal device stub to prevent client errors + // The client may request device info after login + Ok(Json(json!({ + "id": device_id, + "name": "Unknown Device", + "type": 0, + "identifier": device_id, + "creationDate": "2000-01-01T00:00:00.000Z", + "object": "device" + }))) +} + +#[derive(Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct PushToken { + #[allow(dead_code)] + push_token: String, +} + +/// POST /devices/identifier/{device_id}/token +/// +/// Registers a push token for a device. +/// We accept but ignore this since push notifications are not implemented. +#[worker::send] +pub async fn post_device_token( + Path(_device_id): Path, + Json(_data): Json, +) -> Json { + // Accept but ignore - push notifications not implemented + Json(json!({})) +} + +/// PUT /devices/identifier/{device_id}/token +/// +/// Updates a push token for a device. +/// We accept but ignore this since push notifications are not implemented. +#[worker::send] +pub async fn put_device_token( + Path(_device_id): Path, + Json(_data): Json, +) -> Json { + // Accept but ignore - push notifications not implemented + Json(json!({})) +} + +/// PUT /devices/identifier/{device_id}/clear-token +/// +/// Clears the push token for a device. +/// We accept but ignore this since push notifications are not implemented. +#[worker::send] +pub async fn put_clear_device_token(Path(_device_id): Path) -> Json { + // Accept but ignore - push notifications not implemented + Json(json!({})) +} + +/// POST /devices/identifier/{device_id}/clear-token +/// +/// Clears the push token for a device. +/// We accept but ignore this since push notifications are not implemented. +#[worker::send] +pub async fn post_clear_device_token(Path(_device_id): Path) -> Json { + // Accept but ignore - push notifications not implemented + Json(json!({})) +} diff --git a/src/router.rs b/src/router.rs index 1f95bf9..75f4874 100644 --- a/src/router.rs +++ b/src/router.rs @@ -78,8 +78,29 @@ pub fn api_router(env: Env) -> Router { "/api/emergency-access/granted", get(emergency_access::get_granted_access), ) - // Devices (stub - returns empty list, device tracking not implemented) + // Devices (stub - device tracking not implemented, JWT-based auth) .route("/api/devices", get(devices::get_devices)) + .route("/api/devices/knowndevice", get(devices::get_known_device)) + .route( + "/api/devices/identifier/{device_id}", + get(devices::get_device), + ) + .route( + "/api/devices/identifier/{device_id}/token", + post(devices::post_device_token), + ) + .route( + "/api/devices/identifier/{device_id}/token", + put(devices::put_device_token), + ) + .route( + "/api/devices/identifier/{device_id}/clear-token", + put(devices::put_clear_device_token), + ) + .route( + "/api/devices/identifier/{device_id}/clear-token", + post(devices::post_clear_device_token), + ) // WebAuthn (stub - prevents 404 errors, passkeys not supported) .route( "/api/webauthn",