feat(api): /devices register + paseto v4 (middleware Bearer, bootstrap mobile du token)
This commit is contained in:
@@ -1,8 +1,36 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/vaultdrop/backend/pkg/api"
|
||||
)
|
||||
|
||||
func DevicesRegister(c *gin.Context) { api.NotImplemented(c) }
|
||||
var deviceIDPattern = regexp.MustCompile(`^[0-9a-f]{32}$`)
|
||||
|
||||
type DeviceRegisterRequest struct {
|
||||
DeviceID string `json:"deviceId"`
|
||||
}
|
||||
|
||||
func DevicesRegister(c *gin.Context) {
|
||||
|
||||
var req DeviceRegisterRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil || req.DeviceID == "" {
|
||||
api.Error(c, 400, "INVALID_REQUEST", "missing deviceId")
|
||||
return
|
||||
}
|
||||
if !deviceIDPattern.MatchString(req.DeviceID) {
|
||||
api.Error(c, 400, "INVALID_DEVICE_ID", "deviceId must be 32 lowercase hex chars")
|
||||
return
|
||||
}
|
||||
|
||||
token, err := Auth.Issue(req.DeviceID)
|
||||
if err != nil {
|
||||
api.Error(c, 500, "TOKEN_ERROR", "could not issue token")
|
||||
return
|
||||
}
|
||||
|
||||
api.OK(c, gin.H{"deviceId": req.DeviceID, "token": token})
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user