A person can make a password for each app that connects to their server, in
Settings, Apps and devices. The secret is shown once, can be taken back, and
is for the music API of other apps only: it cannot sign in or change the
account. Secrets are kept sealed (AES-256-GCM, key derived from the jwt secret)
because token login needs them in clear, and a hash finds a device by its key.
- migration 0034 device_passwords (sqlite, postgres)
- internal/devices service, storage repo
- GET/POST /me/devices, DELETE /me/devices/{id}, in the OpenAPI spec
- Settings section with copy and remove
105 lines
3.2 KiB
Go
105 lines
3.2 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"io"
|
|
"net/http"
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"git.kmalloc.org/squid/sonda/internal/devices"
|
|
)
|
|
|
|
// deviceService is what the handlers need from devices.Service.
|
|
type deviceService interface {
|
|
Create(ctx context.Context, userID int64, name string) (devices.Device, string, error)
|
|
List(ctx context.Context, userID int64) ([]devices.Device, error)
|
|
Revoke(ctx context.Context, userID, id int64) error
|
|
}
|
|
|
|
// deviceHandlers serve the passwords of the signed-in user's apps and devices.
|
|
type deviceHandlers struct{ svc deviceService }
|
|
|
|
// deviceJSON is a device as a person sees it: never with its secret, except in the answer that makes it.
|
|
type deviceJSON struct {
|
|
ID int64 `json:"id"`
|
|
Name string `json:"name"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
LastUsedAt *time.Time `json:"last_used_at,omitempty"`
|
|
}
|
|
|
|
type createdDeviceJSON struct {
|
|
deviceJSON
|
|
Secret string `json:"secret"`
|
|
}
|
|
|
|
func toDeviceJSON(d devices.Device) deviceJSON {
|
|
return deviceJSON{ID: d.ID, Name: d.Name, CreatedAt: d.CreatedAt.UTC(), LastUsedAt: d.LastUsedAt}
|
|
}
|
|
|
|
func (h *deviceHandlers) list(c *gin.Context) {
|
|
list, err := h.svc.List(c.Request.Context(), currentUserID(c))
|
|
if err != nil {
|
|
_ = c.Error(err)
|
|
writeError(c, http.StatusInternalServerError, "internal", "internal error")
|
|
return
|
|
}
|
|
out := make([]deviceJSON, len(list))
|
|
for i, d := range list {
|
|
out[i] = toDeviceJSON(d)
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"devices": out})
|
|
}
|
|
|
|
func (h *deviceHandlers) create(c *gin.Context) {
|
|
body, err := io.ReadAll(c.Request.Body)
|
|
if err != nil {
|
|
writeError(c, http.StatusBadRequest, "invalid_input", "malformed JSON body")
|
|
return
|
|
}
|
|
// One object with the name and nothing else: what comes back is a secret.
|
|
dec := json.NewDecoder(bytes.NewReader(body))
|
|
dec.DisallowUnknownFields()
|
|
var req struct {
|
|
Name string `json:"name"`
|
|
}
|
|
if err := dec.Decode(&req); err != nil || dec.More() {
|
|
writeError(c, http.StatusBadRequest, "invalid_input", "send the name of the device")
|
|
return
|
|
}
|
|
d, secret, err := h.svc.Create(c.Request.Context(), currentUserID(c), req.Name)
|
|
switch {
|
|
case errors.Is(err, devices.ErrInvalidName):
|
|
writeError(c, http.StatusBadRequest, "invalid_name", err.Error())
|
|
case errors.Is(err, devices.ErrTooMany):
|
|
writeError(c, http.StatusConflict, "too_many_devices", err.Error())
|
|
case err != nil:
|
|
_ = c.Error(err)
|
|
writeError(c, http.StatusInternalServerError, "internal", "internal error")
|
|
default:
|
|
c.Header("Cache-Control", "no-store")
|
|
c.JSON(http.StatusCreated, createdDeviceJSON{deviceJSON: toDeviceJSON(d), Secret: secret})
|
|
}
|
|
}
|
|
|
|
func (h *deviceHandlers) revoke(c *gin.Context) {
|
|
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
if err != nil || id < 1 {
|
|
writeError(c, http.StatusNotFound, "device_not_found", "device not found")
|
|
return
|
|
}
|
|
switch err := h.svc.Revoke(c.Request.Context(), currentUserID(c), id); {
|
|
case errors.Is(err, devices.ErrNotFound):
|
|
writeError(c, http.StatusNotFound, "device_not_found", "device not found")
|
|
case err != nil:
|
|
_ = c.Error(err)
|
|
writeError(c, http.StatusInternalServerError, "internal", "internal error")
|
|
default:
|
|
c.Status(http.StatusNoContent)
|
|
}
|
|
}
|