use generic wopiContext.scope decoder

This commit is contained in:
Roman Perekhod
2024-12-04 19:51:33 +01:00
parent 8d83ac4459
commit ebfcf5a164
2 changed files with 24 additions and 29 deletions

View File

@@ -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

View File

@@ -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