From d9d985e39654451390464c206afe94de5debc93b Mon Sep 17 00:00:00 2001 From: Dominik Schmidt Date: Thu, 15 Oct 2026 12:16:18 +0200 Subject: [PATCH] feat(graph): navigate public links through the real drive like a mountpoint --- services/graph/pkg/middleware/auth.go | 29 +++++++++++ services/graph/pkg/service/v0/driveitems.go | 56 +++++++++------------ services/graph/pkg/service/v0/drives.go | 3 ++ 3 files changed, 57 insertions(+), 31 deletions(-) diff --git a/services/graph/pkg/middleware/auth.go b/services/graph/pkg/middleware/auth.go index 212f3383d0..4bd4524d03 100644 --- a/services/graph/pkg/middleware/auth.go +++ b/services/graph/pkg/middleware/auth.go @@ -1,8 +1,12 @@ package middleware import ( + "context" "net/http" + "strings" + authpb "github.com/cs3org/go-cs3apis/cs3/auth/provider/v1beta1" + link "github.com/cs3org/go-cs3apis/cs3/sharing/link/v1beta1" gmmetadata "go-micro.dev/v4/metadata" "google.golang.org/grpc/metadata" @@ -13,6 +17,7 @@ import ( "github.com/opencloud-eu/reva/v3/pkg/auth/scope" revactx "github.com/opencloud-eu/reva/v3/pkg/ctx" "github.com/opencloud-eu/reva/v3/pkg/token/manager/jwt" + "github.com/opencloud-eu/reva/v3/pkg/utils" ) // authOptions initializes the available default options. @@ -84,6 +89,9 @@ func Auth(opts ...account.Option) func(http.Handler) http.Handler { return } + if share := publicShareOfScope(tokenScope); share != nil { + ctx = context.WithValue(ctx, publicShareKey{}, share) + } ctx = revactx.ContextSetToken(ctx, t) ctx = revactx.ContextSetUser(ctx, u) ctx = gmmetadata.Set(ctx, opkgm.AccountID, u.GetId().GetOpaqueId()) @@ -104,3 +112,24 @@ func Auth(opts ...account.Option) func(http.Handler) http.Handler { }) } } + +type publicShareKey struct{} + +// PublicShareFromContext returns the public link a request is authenticated with. +func PublicShareFromContext(ctx context.Context) (*link.PublicShare, bool) { + share, ok := ctx.Value(publicShareKey{}).(*link.PublicShare) + return share, ok +} + +func publicShareOfScope(scopes map[string]*authpb.Scope) *link.PublicShare { + for key, s := range scopes { + if !strings.HasPrefix(key, "publicshare:") { + continue + } + share := &link.PublicShare{} + if err := utils.UnmarshalJSONToProtoV1(s.GetResource().GetValue(), share); err == nil && share.GetToken() != "" { + return share + } + } + return nil +} diff --git a/services/graph/pkg/service/v0/driveitems.go b/services/graph/pkg/service/v0/driveitems.go index 513c0ee60b..150d2a9824 100644 --- a/services/graph/pkg/service/v0/driveitems.go +++ b/services/graph/pkg/service/v0/driveitems.go @@ -69,7 +69,7 @@ func publicLinkOf(r *http.Request) (publicLink, bool) { return l, ok } -func publicDriveRequest(r *http.Request) bool { +func publicLinkRequest(r *http.Request) bool { _, ok := publicLinkOf(r) return ok } @@ -77,12 +77,12 @@ func publicDriveRequest(r *http.Request) bool { // publicLinkGate lets a link without ListContainer reach only its drive and upload sessions. func (g Graph) publicLinkGate(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - driveID, err := parseIDParam(r, "driveID") - if err != nil || !graphm.IsPublicLinkDrive(&driveID) { + share, ok := graphm.PublicShareFromContext(r.Context()) + if !ok { next.ServeHTTP(w, r) return } - l, err := g.resolvePublicLink(r.Context(), &driveID) + l, err := g.resolvePublicLink(r.Context(), share.GetToken()) if err != nil { errorcode.RenderError(w, r, err) return @@ -123,32 +123,35 @@ func sanitizePublicDriveInfos(l publicLink, infos ...*storageprovider.ResourceIn } } -// pointParentsAtPublicDrive keeps a visitor inside the drive the items were reached through. -func pointParentsAtPublicDrive(l publicLink, items ...*libregraph.DriveItem) { - for _, item := range items { - if item.ParentReference != nil { - item.ParentReference.SetDriveId(l.driveID) - } +// publicLinkRemoteItem points the public drive at the shared item, like a share +// mountpoint, without the owner's alias, path or folder size. +func publicLinkRemoteItem(root *storageprovider.ResourceInfo) *libregraph.RemoteItem { + item, err := cs3ResourceToRemoteItem(root) + if err != nil { + return nil } + item.Name = libregraph.PtrString(root.GetName()) + item.Path = nil + item.DriveAlias = nil + item.Size = nil + return item } type publicLinkKey struct{} type publicLink struct { - driveID string - root *storageprovider.ResourceInfo - grant *storageprovider.ResourcePermissions + root *storageprovider.ResourceInfo + grant *storageprovider.ResourcePermissions } -// resolvePublicLink looks up the link whose token is the public drive's opaque id. -func (g Graph) resolvePublicLink(ctx context.Context, driveID *storageprovider.ResourceId) (publicLink, error) { +func (g Graph) resolvePublicLink(ctx context.Context, token string) (publicLink, error) { gatewayClient, err := g.gatewaySelector.Next() if err != nil { return publicLink{}, err } shareResp, err := gatewayClient.GetPublicShare(ctx, &link.GetPublicShareRequest{ Ref: &link.PublicShareReference{ - Spec: &link.PublicShareReference_Token{Token: driveID.GetOpaqueId()}, + Spec: &link.PublicShareReference_Token{Token: token}, }, }) if err := errorcode.FromCS3Status(shareResp.GetStatus(), err); err != nil { @@ -163,9 +166,8 @@ func (g Graph) resolvePublicLink(ctx context.Context, driveID *storageprovider.R return publicLink{}, err } return publicLink{ - driveID: storagespace.FormatResourceID(driveID), - root: statResp.GetInfo(), - grant: shareResp.GetShare().GetPermissions().GetPermissions(), + root: statResp.GetInfo(), + grant: shareResp.GetShare().GetPermissions().GetPermissions(), }, nil } @@ -217,7 +219,7 @@ func (g Graph) CreateUploadSession(w http.ResponseWriter, r *http.Request) { http.Error(w, err.Error(), http.StatusBadRequest) return } - if publicDriveRequest(r) && !plainName(cusr.Item.Name) { + if publicLinkRequest(r) && !plainName(cusr.Item.Name) { errorcode.InvalidRequest.Render(w, r, http.StatusBadRequest, "the item name must not contain a path") return } @@ -408,9 +410,6 @@ func (g Graph) GetDriveItem(w http.ResponseWriter, r *http.Request) { errorcode.GeneralException.Render(w, r, http.StatusInternalServerError, err.Error()) return } - if l, ok := publicLinkOf(r); ok { - pointParentsAtPublicDrive(l, driveItem) - } if driveItemPropertySelected(r, _selectAllowedValues) { setDriveItemAllowedValues(driveItem, res.GetInfo()) @@ -425,7 +424,7 @@ func (g Graph) GetDriveItem(w http.ResponseWriter, r *http.Request) { driveItem.Children = children } - if driveItemPropertySelected(r, _selectShareTypes) && !publicDriveRequest(r) { + if driveItemPropertySelected(r, _selectShareTypes) && !publicLinkRequest(r) { infos := []*storageprovider.ResourceInfo{res.GetInfo()} driveItem.LibreGraphShareTypes = shareTypesOf(res.GetInfo(), g.listLinkShares(ctx, infos)) } @@ -485,7 +484,7 @@ func (g Graph) listDriveItemChildren(w http.ResponseWriter, r *http.Request, dri childrenRequest := &storageprovider.ListContainerRequest{ Ref: &storageprovider.Reference{ResourceId: driveItemID}, } - if driveItemPropertySelected(r, _selectShareTypes) && !publicDriveRequest(r) { + if driveItemPropertySelected(r, _selectShareTypes) && !publicLinkRequest(r) { childrenRequest.FieldMask = shareTypesFieldMask } @@ -520,15 +519,10 @@ func (g Graph) listDriveItemChildren(w http.ResponseWriter, r *http.Request, dri errorcode.GeneralException.Render(w, r, http.StatusInternalServerError, err.Error()) return nil, false } - if public { - for i := range files { - pointParentsAtPublicDrive(l, &files[i]) - } - } setDriveItemsAllowedValues(r, files, res.GetInfos()) - if driveItemPropertySelected(r, _selectShareTypes) && !publicDriveRequest(r) { + if driveItemPropertySelected(r, _selectShareTypes) && !publicLinkRequest(r) { g.addShareTypes(r.Context(), files, res.GetInfos()) } diff --git a/services/graph/pkg/service/v0/drives.go b/services/graph/pkg/service/v0/drives.go index 9feaeba0d5..6b50add7ca 100644 --- a/services/graph/pkg/service/v0/drives.go +++ b/services/graph/pkg/service/v0/drives.go @@ -379,6 +379,9 @@ func (g Graph) getSingleDrive(w http.ResponseWriter, r *http.Request, apiVersion errorcode.ItemNotFound.Render(w, r, http.StatusNotFound, "no drive returned from storage") return case num == 1: + if l, ok := publicLinkOf(r); ok && spaces[0].Root != nil { + spaces[0].Root.RemoteItem = publicLinkRemoteItem(l.root) + } render.Status(r, http.StatusOK) render.JSON(w, r, spaces[0]) default: