Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

[server] API to enable joining collection via link

Neeraj Guptae74d705446e4
diff --git a/server/cmd/museum/main.go b/server/cmd/museum/main.go
index a4622e1b70..05c3f4bf8c 100644
--- a/server/cmd/museum/main.go
+++ b/server/cmd/museum/main.go
@@ -535,6 +535,7 @@ func main() {
privateAPI.GET("/collections", collectionHandler.Get)
privateAPI.GET("/collections/v2", collectionHandler.GetV2)
privateAPI.POST("/collections/share", collectionHandler.Share)
+ privateAPI.POST("/collections/join-link", collectionHandler.JoinLink)
privateAPI.POST("/collections/share-url", collectionHandler.ShareURL)
privateAPI.PUT("/collections/share-url", collectionHandler.UpdateShareURL)
privateAPI.DELETE("/collections/share-url/:collectionID", collectionHandler.UnShareURL)
diff --git a/server/ente/collection.go b/server/ente/collection.go
index 71b4c50ac2..9b9a0759d9 100644
--- a/server/ente/collection.go
+++ b/server/ente/collection.go
@@ -97,6 +97,11 @@ type AlterShareRequest struct {
Role *CollectionParticipantRole `json:"role"`
}
+type JoinCollectionViaLinkRequest struct {
+ CollectionID int64 `json:"collectionID" binding:"required"`
+ EncryptedKey string `json:"encryptedKey" binding:"required"`
+}
+
// AddFilesRequest represents a request to add files to a collection
type AddFilesRequest struct {
CollectionID int64 `json:"collectionID" binding:"required"`
diff --git a/server/ente/public_collection.go b/server/ente/public_collection.go
index 6e71e35b13..e91b916d14 100644
--- a/server/ente/public_collection.go
+++ b/server/ente/public_collection.go
@@ -3,6 +3,7 @@ package ente
import (
"database/sql/driver"
"encoding/json"
+ "github.com/ente-io/museum/pkg/utils/time"
"github.com/ente-io/stacktrace"
)
@@ -11,6 +12,7 @@ import (
type CreatePublicAccessTokenRequest struct {
CollectionID int64 `json:"collectionID" binding:"required"`
EnableCollect bool `json:"enableCollect"`
+ JoinViaLink bool `json:"joinViaLink"`
ValidTill int64 `json:"validTill"`
DeviceLimit int `json:"deviceLimit"`
}
@@ -26,6 +28,7 @@ type UpdatePublicAccessTokenRequest struct {
EnableDownload *bool `json:"enableDownload"`
EnableCollect *bool `json:"enableCollect"`
DisablePassword *bool `json:"disablePassword"`
+ JoinViaLink *bool `json:"joinViaLink"`
}
type VerifyPasswordRequest struct {
@@ -50,6 +53,7 @@ type PublicCollectionToken struct {
OpsLimit *int64
EnableDownload bool
EnableCollect bool
+ EnableJoin bool
}
// PublicURL represents information about non-disabled public url for a collection
@@ -62,9 +66,10 @@ type PublicURL struct {
EnableCollect bool `json:"enableCollect"`
PasswordEnabled bool `json:"passwordEnabled"`
// Nonce contains the nonce value for the password if the link is password protected.
- Nonce *string `json:"nonce,omitempty"`
- MemLimit *int64 `json:"memLimit,omitempty"`
- OpsLimit *int64 `json:"opsLimit,omitempty"`
+ Nonce *string `json:"nonce,omitempty"`
+ MemLimit *int64 `json:"memLimit,omitempty"`
+ OpsLimit *int64 `json:"opsLimit,omitempty"`
+ EnableJoinViaLink bool `json:"enableJoinViaLink"`
}
type PublicAccessContext struct {
@@ -88,6 +93,16 @@ type PublicCollectionSummary struct {
PassHash *string
}
+func (p PublicCollectionToken) CanJoin() error {
+ if p.IsDisabled {
+ return stacktrace.NewError("disabled token")
+ }
+ if p.ValidTill > 0 && p.ValidTill < time.Microseconds() {
+ return stacktrace.NewError("expired token")
+ }
+ return nil
+}
+
type AbuseReportRequest struct {
URL string `json:"url" binding:"required"`
Reason string `json:"reason" binding:"required"`
diff --git a/server/migrations/95_public_collection_join_album.down.sql b/server/migrations/95_public_collection_join_album.down.sql
new file mode 100644
index 0000000000..e6107881e0
--- /dev/null
+++ b/server/migrations/95_public_collection_join_album.down.sql
@@ -0,0 +1,2 @@
+ALTER TABLE public_collection_tokens
+ DROP COLUMN IF EXISTS enable_join;
\ No newline at end of file
diff --git a/server/migrations/95_public_collection_join_album.up.sql b/server/migrations/95_public_collection_join_album.up.sql
new file mode 100644
index 0000000000..d5b73572d5
--- /dev/null
+++ b/server/migrations/95_public_collection_join_album.up.sql
@@ -0,0 +1,9 @@
+BEGIN;
+ALTER table public_collection_tokens
+ ADD COLUMN IF NOT EXISTS enable_join bool DEFAULT TRUE;
+
+UPDATE public_collection_tokens SET enable_join = FALSE;
+
+ALTER TABLE public_collection_tokens ALTER COLUMN enable_join SET NOT NULL;
+
+COMMIT;
diff --git a/server/pkg/api/collection.go b/server/pkg/api/collection.go
index 94918bcf9d..57cc8c4f28 100644
--- a/server/pkg/api/collection.go
+++ b/server/pkg/api/collection.go
@@ -122,6 +122,20 @@ func (h *CollectionHandler) Share(c *gin.Context) {
})
}
+func (h *CollectionHandler) JoinLink(c *gin.Context) {
+ var request ente.JoinCollectionViaLinkRequest
+ if err := c.ShouldBindJSON(&request); err != nil {
+ handler.Error(c, stacktrace.Propagate(err, ""))
+ return
+ }
+ err := h.Controller.JoinViaLink(c, request)
+ if err != nil {
+ handler.Error(c, stacktrace.Propagate(err, ""))
+ return
+ }
+ c.JSON(http.StatusOK, gin.H{})
+}
+
// ShareURL generates a publicly sharable url
func (h *CollectionHandler) ShareURL(c *gin.Context) {
var request ente.CreatePublicAccessTokenRequest
diff --git a/server/pkg/controller/collection.go b/server/pkg/controller/collection.go
index dca53f80c3..95b8e1ed93 100644
--- a/server/pkg/controller/collection.go
+++ b/server/pkg/controller/collection.go
@@ -181,6 +181,47 @@ func (c *CollectionController) Share(ctx *gin.Context, req ente.AlterShareReques
return sharees, nil
}
+func (c *CollectionController) JoinViaLink(ctx *gin.Context, req ente.JoinCollectionViaLinkRequest) error {
+ userID := auth.GetUserID(ctx.Request.Header)
+ collection, err := c.CollectionRepo.Get(req.CollectionID)
+ if err != nil {
+ return stacktrace.Propagate(err, "")
+ }
+ if collection.Owner.ID == userID {
+ return stacktrace.Propagate(ente.ErrBadRequest, "owner can not join via link")
+ }
+ if !collection.AllowSharing() {
+ return stacktrace.Propagate(ente.ErrBadRequest, fmt.Sprintf("joining %s is not allowed", collection.Type))
+ }
+ publicCollectionToken, err := c.PublicCollectionCtrl.GetCollectionSummaryByToken(ctx, req.CollectionID)
+ if err != nil {
+ return stacktrace.Propagate(err, "")
+ }
+
+ if canJoin := publicCollectionToken.CanJoin(); canJoin != nil {
+ return stacktrace.Propagate(ente.ErrBadRequest, fmt.Sprintf("can not join collection: %s", canJoin.Error()))
+ }
+ accessToken := auth.GetAccessToken(ctx)
+ if publicCollectionToken.Token != accessToken {
+ return stacktrace.Propagate(ente.ErrPermissionDenied, "token doesn't match collection")
+ }
+ if publicCollectionToken.PassHash != nil && *publicCollectionToken.PassHash != "" {
+ accessTokenJWT := auth.GetAccessTokenJWT(ctx)
+ if passCheckErr := c.PublicCollectionCtrl.ValidateJWTToken(ctx, accessTokenJWT, *publicCollectionToken.PassHash); passCheckErr != nil {
+ return stacktrace.Propagate(passCheckErr, "")
+ }
+ }
+ err = c.BillingCtrl.HasActiveSelfOrFamilySubscription(collection.Owner.ID, true)
+ if err != nil {
+ return stacktrace.Propagate(err, "")
+ }
+ role := ente.VIEWER
+ if publicCollectionToken.EnableCollect {
+ role = ente.COLLABORATOR
+ }
+ return c.CollectionRepo.Share(req.CollectionID, collection.Owner.ID, userID, req.EncryptedKey, role, time.Microseconds())
+}
+
// UnShare unshares a collection with a user
func (c *CollectionController) UnShare(ctx *gin.Context, cID int64, fromUserID int64, toUserEmail string) ([]ente.CollectionUser, error) {
toUserID, err := c.UserRepo.GetUserIDWithEmail(toUserEmail)
diff --git a/server/pkg/controller/public_collection.go b/server/pkg/controller/public_collection.go
index d526837ddd..74733b4459 100644
--- a/server/pkg/controller/public_collection.go
+++ b/server/pkg/controller/public_collection.go
@@ -90,6 +90,10 @@ func (c *PublicCollectionController) CreateAccessToken(ctx context.Context, req
return response, nil
}
+func (c *PublicCollectionController) GetCollectionSummaryByToken(ctx context.Context, collectionID int64) (ente.PublicCollectionToken, error) {
+ return c.PublicCollectionRepo.GetActivePublicCollectionToken(ctx, collectionID)
+}
+
func (c *PublicCollectionController) CreateFile(ctx *gin.Context, file ente.File, app ente.App) (ente.File, error) {
collection, err := c.GetPublicCollection(ctx, true)
if err != nil {
diff --git a/server/pkg/repo/public_collection.go b/server/pkg/repo/public_collection.go
index 91b96fca78..6717688b58 100644
--- a/server/pkg/repo/public_collection.go
+++ b/server/pkg/repo/public_collection.go
@@ -91,14 +91,14 @@ func (pcr *PublicCollectionRepository) GetCollectionToActivePublicURLMap(ctx con
// Note: The token could be expired or deviceLimit is already reached
func (pcr *PublicCollectionRepository) GetActivePublicCollectionToken(ctx context.Context, collectionID int64) (ente.PublicCollectionToken, error) {
row := pcr.DB.QueryRowContext(ctx, `SELECT id, collection_id, access_token, valid_till, device_limit,
- is_disabled, pw_hash, pw_nonce, mem_limit, ops_limit, enable_download, enable_collect FROM
+ is_disabled, pw_hash, pw_nonce, mem_limit, ops_limit, enable_download, enable_collect, enable_join FROM
public_collection_tokens WHERE collection_id = $1 and is_disabled = FALSE`,
collectionID)
//defer rows.Close()
ret := ente.PublicCollectionToken{}
err := row.Scan(&ret.ID, &ret.CollectionID, &ret.Token, &ret.ValidTill, &ret.DeviceLimit,
- &ret.IsDisabled, &ret.PassHash, &ret.Nonce, &ret.MemLimit, &ret.OpsLimit, &ret.EnableDownload, &ret.EnableCollect)
+ &ret.IsDisabled, &ret.PassHash, &ret.Nonce, &ret.MemLimit, &ret.OpsLimit, &ret.EnableDownload, &ret.EnableCollect, &ret.EnableJoin)
if err != nil {
return ente.PublicCollectionToken{}, stacktrace.Propagate(err, "")
}