π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Support public key lookup by user ID
Neeraj Guptaf45edf327252
diff --git a/server/pkg/api/user.go b/server/pkg/api/user.goindex c8609c7587..21cdcd28f1 100644--- a/server/pkg/api/user.go+++ b/server/pkg/api/user.go@@ -149,7 +149,18 @@ func (h *UserHandler) SetRecoveryKey(c *gin.Context) {// GetPublicKey returns the public key of a userfunc (h *UserHandler) GetPublicKey(c *gin.Context) {- publicKey, err := h.UserController.GetPublicKey(auth.GetUserID(c.Request.Header), c.Query("email"))+ var publicKey string+ var err error+ if userID := c.Query("userID"); userID != "" {+ targetUserID, parseErr := strconv.ParseInt(userID, 10, 64)+ if parseErr != nil || targetUserID <= 0 {+ handler.Error(c, stacktrace.Propagate(ente.ErrBadRequest, "invalid userID"))+ return+ }+ publicKey, err = h.UserController.GetPublicKeyByUserID(targetUserID)+ } else {+ publicKey, err = h.UserController.GetPublicKey(auth.GetUserID(c.Request.Header), c.Query("email"))+ }if err != nil {handler.Error(c, stacktrace.Propagate(err, ""))returndiff --git a/server/pkg/api/user_test.go b/server/pkg/api/user_test.goindex 9daaacce37..26d0a30d0f 100644--- a/server/pkg/api/user_test.go+++ b/server/pkg/api/user_test.go@@ -52,6 +52,49 @@ func TestSetAttributesHandlerRejectsLowMemoryLimit(t *testing.T) {assertAPIErrorResponse(t, recorder, http.StatusBadRequest, ente.BadRequest, "memory limit must be at least 128MB")}+func TestGetPublicKeyHandlerLooksUpUserByID(t *testing.T) {+ handler, db := setupUserHandlerTest(t)++ targetUserID := testutil.InsertUser(t, db, testutil.UserFixture{+ UserID: 103,+ Email: "[email protected]",+ CreationTime: 1,+ })+ keyAttributes := ente.KeyAttributes{+ KEKSalt: "kek-salt",+ KEKHash: "kek-hash",+ EncryptedKey: "encrypted-key",+ KeyDecryptionNonce: "key-decryption-nonce",+ PublicKey: "target-public-key",+ EncryptedSecretKey: "encrypted-secret-key",+ SecretKeyDecryptionNonce: "secret-key-decryption-nonce",+ MemLimit: 128 * 1024 * 1024,+ OpsLimit: 32,+ }+ if err := handler.UserController.UserRepo.SetKeyAttributes(targetUserID, keyAttributes); err != nil {+ t.Fatalf("failed to set key attributes: %v", err)+ }++ recorder := httptest.NewRecorder()+ req := httptest.NewRequest(http.MethodGet, "/users/public-key?userID="+strconv.FormatInt(targetUserID, 10), nil)+ router := gin.New()+ router.GET("/users/public-key", handler.GetPublicKey)+ router.ServeHTTP(recorder, req)++ if recorder.Code != http.StatusOK {+ t.Fatalf("unexpected status code: got %d want %d; body=%s", recorder.Code, http.StatusOK, recorder.Body.String())+ }+ var response struct {+ PublicKey string `json:"publicKey"`+ }+ if err := json.Unmarshal(recorder.Body.Bytes(), &response); err != nil {+ t.Fatalf("failed to decode response body: %v", err)+ }+ if response.PublicKey != keyAttributes.PublicKey {+ t.Fatalf("unexpected public key: got %q want %q", response.PublicKey, keyAttributes.PublicKey)+ }+}+func setupUserHandlerTest(t *testing.T) (*UserHandler, *sql.DB) {t.Helper()diff --git a/server/pkg/controller/user/user.go b/server/pkg/controller/user/user.goindex b31a7f8090..8b5a63bc94 100644--- a/server/pkg/controller/user/user.go+++ b/server/pkg/controller/user/user.go@@ -242,6 +242,11 @@ func (c *UserController) GetPublicKey(requesterUserID int64, email string) (striif err != nil {return "", stacktrace.Propagate(err, "")}+ return c.GetPublicKeyByUserID(userID)+}++// GetPublicKeyByUserID returns the public key of a user.+func (c *UserController) GetPublicKeyByUserID(userID int64) (string, error) {key, err := c.UserRepo.GetPublicKey(userID)if err != nil {return "", stacktrace.Propagate(err, "")