自动补充 MLS 一次性密钥包
This commit is contained in:
1 parent
6b728a0996
commit
cf8129f4aa
8 files changed
+69
-11
No files matched your search
@@ -29,6 +29,16 @@ type KeyPackage struct {
|
||||
var ErrSignature = errors.New("invalid key package device signature")
|
||||
var ErrPackageLimit = errors.New("device key package limit reached")
|
||||
|
||||
func (s *Store) AvailablePackages(ctx context.Context, device string) (int, error) {
|
||||
var available int
|
||||
err := s.db.QueryRow(ctx, `
|
||||
SELECT count(*) FROM device_key_packages k JOIN devices d ON d.id=k.device_id
|
||||
WHERE d.id=$1 AND d.revoked_at IS NULL AND k.claimed_at IS NULL
|
||||
AND k.created_at > now() - interval '24 hours'
|
||||
`, device).Scan(&available)
|
||||
return available, err
|
||||
}
|
||||
|
||||
func (s *Store) LocalIdentityKey(ctx context.Context, device string) ([]byte, error) {
|
||||
var key []byte
|
||||
err := s.db.QueryRow(ctx, "SELECT public_key FROM devices WHERE id=$1 AND revoked_at IS NULL", device).Scan(&key)
|
||||
|
||||
@@ -53,6 +53,7 @@ func New(accounts *account.Store, contactStore *contacts.Store, envelopeStore *d
|
||||
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))
|
||||
mux.HandleFunc("GET /api/v1/key-packages", a.authorized(a.packageStatus))
|
||||
mux.HandleFunc("POST /api/v1/mls-credential", a.authorized(a.publishCredential))
|
||||
mux.HandleFunc("POST /api/v1/attachments/{id}", a.authorized(a.uploadAttachment))
|
||||
mux.HandleFunc("GET /api/v1/attachments/{id}", a.authorized(a.downloadAttachment))
|
||||
|
||||
@@ -51,6 +51,18 @@ func (a *API) publishPackage(w http.ResponseWriter, r *http.Request, _ string, i
|
||||
}
|
||||
}
|
||||
|
||||
func (a *API) packageStatus(w http.ResponseWriter, r *http.Request, _ string, identity account.Identity) {
|
||||
if !a.limit(w, r, "package-status:"+identity.Device.ID, 60) {
|
||||
return
|
||||
}
|
||||
available, err := a.delivery.AvailablePackages(r.Context(), identity.Device.ID)
|
||||
if err != nil {
|
||||
unavailable(w, err)
|
||||
return
|
||||
}
|
||||
respond(w, 200, map[string]int{"available": available})
|
||||
}
|
||||
|
||||
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) {
|
||||
|
||||
Reference in new issue
Block a user