feat: add RegenerateDeviceToken API endpoint
- Add handler to regenerate device auth tokens - Add PUT /api/devices/:id/regenerate-token route - Returns new token and sync URLs for device configuration
This commit is contained in:
@@ -76,6 +76,7 @@ type DeviceInfo struct {
|
||||
SyncFrequency int32 `json:"sync_frequency_minutes"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
DeviceMetadata json.RawMessage `json:"device_metadata,omitempty"`
|
||||
AuthToken string `json:"auth_token"`
|
||||
}
|
||||
|
||||
type DeviceUpdateRequest struct {
|
||||
@@ -263,6 +264,7 @@ func (h *DeviceHandler) ListDevices(c echo.Context) error {
|
||||
SyncFrequency: syncFreq,
|
||||
CreatedAt: device.CreatedAt.Time,
|
||||
DeviceMetadata: device.DeviceMetadata,
|
||||
AuthToken: device.AuthToken,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -306,6 +308,7 @@ func (h *DeviceHandler) GetDevicesData(c echo.Context) ([]DeviceInfo, error) {
|
||||
SyncFrequency: syncFreq,
|
||||
CreatedAt: device.CreatedAt.Time,
|
||||
DeviceMetadata: device.DeviceMetadata,
|
||||
AuthToken: device.AuthToken,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -485,6 +488,91 @@ func (h *DeviceHandler) DeleteDevice(c echo.Context) error {
|
||||
return c.NoContent(http.StatusNoContent)
|
||||
}
|
||||
|
||||
func (h *DeviceHandler) RegenerateDeviceToken(c echo.Context) error {
|
||||
// Verify JWT authentication
|
||||
userID := c.Get("user_id").(string)
|
||||
userUUID, err := uuid.Parse(userID)
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusUnauthorized, map[string]string{"error": "invalid user ID"})
|
||||
}
|
||||
|
||||
// Parse device ID from URL parameter
|
||||
deviceID, err := uuid.Parse(c.Param("id"))
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusBadRequest, map[string]string{"error": "invalid device ID"})
|
||||
}
|
||||
|
||||
pgDeviceID := pgtype.UUID{Bytes: [16]byte(deviceID), Valid: true}
|
||||
|
||||
// Verify device exists and belongs to user
|
||||
device, err := h.db.GetDevice(c.Request().Context(), pgDeviceID)
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusNotFound, map[string]string{"error": "device not found"})
|
||||
}
|
||||
|
||||
if device.UserID.Bytes != userUUID {
|
||||
return c.JSON(http.StatusForbidden, map[string]string{"error": "access denied"})
|
||||
}
|
||||
|
||||
// Generate new auth token
|
||||
newToken, err := generateDeviceToken()
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusInternalServerError, map[string]string{"error": "failed to generate token"})
|
||||
}
|
||||
|
||||
// Update device with new token
|
||||
updatedDevice, err := h.db.UpdateDeviceAuthToken(c.Request().Context(), database.UpdateDeviceAuthTokenParams{
|
||||
ID: pgDeviceID,
|
||||
AuthToken: newToken,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusInternalServerError, map[string]string{"error": "failed to update token"})
|
||||
}
|
||||
|
||||
// Return new token with device info
|
||||
syncEnabled := updatedDevice.SyncEnabled.Bool && updatedDevice.SyncEnabled.Valid
|
||||
autoSync := updatedDevice.AutoSync.Bool && updatedDevice.AutoSync.Valid
|
||||
syncFreq := int32(0)
|
||||
if updatedDevice.SyncFrequencyMinutes.Valid {
|
||||
syncFreq = updatedDevice.SyncFrequencyMinutes.Int32
|
||||
}
|
||||
|
||||
// Build sync URLs with new token
|
||||
syncURLs := map[string]string{}
|
||||
baseURL := h.cfg.BaseURL
|
||||
|
||||
switch updatedDevice.DeviceType {
|
||||
case "kobo":
|
||||
syncURLs["sync_url"] = fmt.Sprintf("%s/api/sync/kobo/%s", baseURL, newToken)
|
||||
syncURLs["markup"] = fmt.Sprintf("%s/api/sync/kobo/%s/markup", baseURL, newToken)
|
||||
syncURLs["bookmark"] = fmt.Sprintf("%s/api/sync/kobo/%s/bookmark", baseURL, newToken)
|
||||
syncURLs["init"] = fmt.Sprintf("%s/api/sync/kobo/%s/v1/initialization", baseURL, newToken)
|
||||
case "koreader":
|
||||
syncURLs["progress"] = fmt.Sprintf("%s/api/sync/koreader/progress", baseURL)
|
||||
syncURLs["metadata"] = fmt.Sprintf("%s/api/sync/koreader/metadata", baseURL)
|
||||
syncURLs["bookmarks"] = fmt.Sprintf("%s/api/sync/koreader/bookmarks", baseURL)
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, map[string]interface{}{
|
||||
"message": "Token regenerated successfully",
|
||||
"auth_token": newToken,
|
||||
"device": DeviceInfo{
|
||||
ID: updatedDevice.ID.Bytes,
|
||||
DeviceName: updatedDevice.DeviceName,
|
||||
DeviceType: updatedDevice.DeviceType,
|
||||
LastSync: (*time.Time)(&updatedDevice.LastSync.Time),
|
||||
LastSeen: (*time.Time)(&updatedDevice.LastSeen.Time),
|
||||
SyncEnabled: syncEnabled,
|
||||
AutoSync: autoSync,
|
||||
SyncFrequency: syncFreq,
|
||||
CreatedAt: updatedDevice.CreatedAt.Time,
|
||||
DeviceMetadata: updatedDevice.DeviceMetadata,
|
||||
},
|
||||
"sync_urls": syncURLs,
|
||||
})
|
||||
}
|
||||
|
||||
func (h *DeviceHandler) ApproveDevice(c echo.Context) error {
|
||||
userID := c.Get("user_id").(string)
|
||||
userUUID, err := uuid.Parse(userID)
|
||||
|
||||
@@ -19,6 +19,10 @@ func registerDeviceRoutes(cfg *Config) {
|
||||
devices.GET("/:id", cfg.DeviceHandler.GetDevice)
|
||||
devices.PUT("/:id", cfg.DeviceHandler.UpdateDevice)
|
||||
devices.DELETE("/:id", cfg.DeviceHandler.DeleteDevice)
|
||||
|
||||
// Token regeneration endpoint (JWT authentication required)
|
||||
devices.PUT("/:id/regenerate-token", cfg.DeviceHandler.RegenerateDeviceToken)
|
||||
|
||||
devices.GET("/pending", cfg.DeviceHandler.ListPendingRegistrations)
|
||||
devices.GET("/approve/:registration_id", cfg.DeviceHandler.ApproveDevice)
|
||||
devices.POST("/reject/:registration_id", cfg.DeviceHandler.RejectDevice)
|
||||
|
||||
Reference in New Issue
Block a user