绑定设备身份并分发一次性 MLS 密钥包
This commit is contained in:
1 parent
f30f01b699
commit
0b17dd07c5
11 files changed
+286
-11
No files matched your search
@@ -50,6 +50,9 @@ func New(accounts *account.Store, contactStore *contacts.Store, envelopeStore *d
|
||||
mux.HandleFunc("POST /api/v1/contacts/{id}", a.authorized(a.changeContact))
|
||||
mux.HandleFunc("GET /api/v1/inbox", a.authorized(a.inbox))
|
||||
mux.HandleFunc("POST /api/v1/inbox", a.authorized(a.sendEnvelope))
|
||||
mux.HandleFunc("GET /api/v1/contacts/{id}/devices", a.authorized(a.contactDevices))
|
||||
mux.HandleFunc("POST /api/v1/contacts/{id}/devices/{device}/claim", a.authorized(a.claimPackage))
|
||||
mux.HandleFunc("POST /api/v1/key-packages", a.authorized(a.publishPackage))
|
||||
return a.boundary(mux)
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
package httpapi
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"xhchat/server/internal/account"
|
||||
"xhchat/server/internal/delivery"
|
||||
)
|
||||
|
||||
func (a *API) contactDevices(w http.ResponseWriter, r *http.Request, _ string, identity account.Identity) {
|
||||
id := strings.ToLower(r.PathValue("id"))
|
||||
if !uuidPattern.MatchString(id) {
|
||||
problem(w, 404, "contact_not_found", "找不到该联系人")
|
||||
return
|
||||
}
|
||||
result, err := a.delivery.Directory(r.Context(), identity.User.ID, id)
|
||||
if err != nil {
|
||||
unavailable(w, err)
|
||||
return
|
||||
}
|
||||
respond(w, 200, result)
|
||||
}
|
||||
|
||||
func (a *API) publishPackage(w http.ResponseWriter, r *http.Request, _ string, identity account.Identity) {
|
||||
var item delivery.KeyPackage
|
||||
if !parseLimit(w, r, &item, 12<<10) {
|
||||
return
|
||||
}
|
||||
wire := item.Package
|
||||
if len(wire) < 32 || len(wire) > 8192 || wire[0] != 0 || wire[1] != 1 || wire[2] != 0 || wire[3] != 5 || len(item.Signature) != 64 {
|
||||
problem(w, 400, "invalid_package", "密钥包格式无效")
|
||||
return
|
||||
}
|
||||
if !a.limit(w, r, "package-publish:"+identity.Device.ID, 48) {
|
||||
return
|
||||
}
|
||||
err := a.delivery.PublishPackage(r.Context(), identity.User.ID, identity.Device.ID, item)
|
||||
switch {
|
||||
case errors.Is(err, delivery.ErrSignature):
|
||||
problem(w, 400, "invalid_package_signature", "密钥包设备签名无效")
|
||||
case errors.Is(err, delivery.ErrPackageLimit):
|
||||
problem(w, 409, "package_limit", "当前已有足够的未使用密钥包")
|
||||
case errors.Is(err, delivery.ErrUnavailable):
|
||||
problem(w, 404, "device_not_found", "设备不可用")
|
||||
case err != nil:
|
||||
unavailable(w, err)
|
||||
default:
|
||||
w.WriteHeader(204)
|
||||
}
|
||||
}
|
||||
|
||||
func (a *API) claimPackage(w http.ResponseWriter, r *http.Request, _ string, identity account.Identity) {
|
||||
id, device := strings.ToLower(r.PathValue("id")), strings.ToLower(r.PathValue("device"))
|
||||
if !uuidPattern.MatchString(id) || !uuidPattern.MatchString(device) {
|
||||
problem(w, 404, "package_unavailable", "密钥包不可用")
|
||||
return
|
||||
}
|
||||
if !a.limit(w, r, "package-claim:"+identity.Device.ID, 24) {
|
||||
return
|
||||
}
|
||||
item, err := a.delivery.ClaimPackage(r.Context(), identity.User.ID, identity.Device.ID, id, device)
|
||||
if errors.Is(err, delivery.ErrUnavailable) {
|
||||
problem(w, 404, "package_unavailable", "密钥包不可用")
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
unavailable(w, err)
|
||||
return
|
||||
}
|
||||
respond(w, 200, item)
|
||||
}
|
||||
Reference in new issue
Block a user