From ebfcf5a1643bf51ad61331bf9141082e1d1b3dd5 Mon Sep 17 00:00:00 2001 From: Roman Perekhod Date: Wed, 4 Dec 2024 19:51:33 +0100 Subject: [PATCH] use generic wopiContext.scope decoder --- .../pkg/connector/fileconnector.go | 11 ++++- .../pkg/middleware/wopicontext.go | 42 +++++++------------ 2 files changed, 24 insertions(+), 29 deletions(-) diff --git a/services/collaboration/pkg/connector/fileconnector.go b/services/collaboration/pkg/connector/fileconnector.go index 6104372ff2..9c6d1de3b1 100644 --- a/services/collaboration/pkg/connector/fileconnector.go +++ b/services/collaboration/pkg/connector/fileconnector.go @@ -16,6 +16,7 @@ import ( gatewayv1beta1 "github.com/cs3org/go-cs3apis/cs3/gateway/v1beta1" userv1beta1 "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1" rpcv1beta1 "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1" + link "github.com/cs3org/go-cs3apis/cs3/sharing/link/v1beta1" providerv1beta1 "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1" typesv1beta1 "github.com/cs3org/go-cs3apis/cs3/types/v1beta1" ctxpkg "github.com/cs3org/reva/v2/pkg/ctx" @@ -1224,8 +1225,14 @@ func (f *FileConnector) CheckFileInfo(ctx context.Context) (*ConnectorResponse, privateLinkURL.Path = path.Join(ocisURL.Path, "f", storagespace.FormatResourceID(statRes.GetInfo().GetId())) parentFolderURL := &url.URL{} *parentFolderURL = *ocisURL - parentFolderURL.Path = path.Join(ocisURL.Path, "f", storagespace.FormatResourceID(statRes.GetInfo().GetParentId())) - if publicShare := wopiContext.GetPublicShare(); publicShare != nil && isPublicShare { + if !isPublicShare { + parentFolderURL.Path = path.Join(ocisURL.Path, "f", storagespace.FormatResourceID(statRes.GetInfo().GetParentId())) + } else { + publicShare := &link.PublicShare{} + err := wopiContext.GetScopeByKeyPrefix("publicshare:", publicShare) + if err != nil { + logger.Error().Err(err).Msg("CheckFileInfo: error getting public share scope") + } parentFolderURL.Path = path.Join(ocisURL.Path, "s", publicShare.GetToken()) } // fileinfo map diff --git a/services/collaboration/pkg/middleware/wopicontext.go b/services/collaboration/pkg/middleware/wopicontext.go index 5bda65e339..f3a8826feb 100644 --- a/services/collaboration/pkg/middleware/wopicontext.go +++ b/services/collaboration/pkg/middleware/wopicontext.go @@ -12,12 +12,13 @@ import ( "time" appproviderv1beta1 "github.com/cs3org/go-cs3apis/cs3/app/provider/v1beta1" - link "github.com/cs3org/go-cs3apis/cs3/sharing/link/v1beta1" + auth "github.com/cs3org/go-cs3apis/cs3/auth/provider/v1beta1" providerv1beta1 "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1" ctxpkg "github.com/cs3org/reva/v2/pkg/ctx" rjwt "github.com/cs3org/reva/v2/pkg/token/manager/jwt" "github.com/cs3org/reva/v2/pkg/utils" "github.com/golang-jwt/jwt/v5" + "github.com/golang/protobuf/proto" "github.com/owncloud/ocis/v2/services/collaboration/pkg/config" "github.com/owncloud/ocis/v2/services/collaboration/pkg/helpers" "github.com/rs/zerolog" @@ -38,21 +39,20 @@ type WopiContext struct { FileReference *providerv1beta1.Reference TemplateReference *providerv1beta1.Reference ViewMode appproviderv1beta1.ViewMode - publicShare *link.PublicShare + // scope contains decoded Scope map from the AccessToken + scope map[string]*auth.Scope } -// GetPublicShare returns the public share from the WopiContext or nil if it doesn't exist -func (w *WopiContext) GetPublicShare() *link.PublicShare { - if w != nil { - return w.publicShare - } - return nil -} - -// EvaluatePublicShare unmashals the public share from the scope and -func (w *WopiContext) EvaluatePublicShare() *link.PublicShare { - if w != nil { - return w.publicShare +// GetScopeByKeyPrefix returns the scope from the AccessToken Scope map by key prefix +func (w *WopiContext) GetScopeByKeyPrefix(keyPrefix string, m proto.Message) error { + for k, v := range w.scope { + if strings.HasPrefix(k, keyPrefix) && v.Resource.Decoder == "json" { + err := utils.UnmarshalJSONToProtoV1(v.Resource.Value, m) + if err != nil { + return fmt.Errorf("can't unmarshal public share from scope: %w", err) + } + break + } } return nil } @@ -146,20 +146,8 @@ func WopiContextAuthMiddleware(cfg *config.Config, st microstore.Store, next htt return } - for k, v := range scope { - if strings.HasPrefix(k, "publicshare:") && v.Resource.Decoder == "json" { - share := &link.PublicShare{} - err := utils.UnmarshalJSONToProtoV1(v.Resource.Value, share) - if err != nil { - wopiLogger.Error().Err(err).Msg("can't unmarshal public share from scope") - } else { - claims.WopiContext.publicShare = share - } - break - } - } - claims.WopiContext.AccessToken = wopiContextAccessToken + claims.WopiContext.scope = scope ctx = context.WithValue(ctx, wopiContextKey, claims.WopiContext) // authentication for the CS3 api