Skip to content

Commit 97dbc42

Browse files
committed
Support OCM shares as well on wopi apps
1 parent 8474891 commit 97dbc42

File tree

3 files changed

+49
-20
lines changed

3 files changed

+49
-20
lines changed

pkg/app/provider/wopi/wopi.go

+25-17
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ func New(m map[string]interface{}) (app.Provider, error) {
125125
}
126126

127127
wopiClient := rhttp.GetHTTPClient(
128-
rhttp.Timeout(time.Duration(5*int64(time.Second))),
128+
rhttp.Timeout(time.Duration(10*int64(time.Second))),
129129
rhttp.Insecure(c.InsecureConnections),
130130
)
131131
wopiClient.CheckRedirect = func(req *http.Request, via []*http.Request) error {
@@ -184,30 +184,25 @@ func (p *wopiProvider) GetAppURL(ctx context.Context, resource *provider.Resourc
184184
// either append `/files/spaces/<full_path>` or the proper URL prefix + `/<relative_path>`
185185
var rPath string
186186
var pathErr error
187-
_, ok = utils.HasPublicShareRole(u)
187+
_, pubrole := utils.HasPublicShareRole(u)
188+
_, ocmrole := utils.HasOCMShareRole(u)
188189
switch {
189-
case ok:
190+
case pubrole:
190191
// we are in a public link, username is not set so it will default to "Guest xyz"
191192
ut = anonymous
192193
rPath, pathErr = getPathForExternalLink(ctx, scopes, resource, publicLinkURLPrefix)
193194
if pathErr != nil {
194195
log.Warn().Err(pathErr).Msg("wopi: failed to extract relative path from public link scope")
195196
}
196-
case u.Username == "":
197+
case ocmrole:
197198
// OCM users have no username: use displayname@Idp
198199
ut = ocm
199-
idpURL, e := url.Parse(u.Id.Idp)
200-
if e != nil {
201-
q.Add("username", u.DisplayName+" @ "+u.Id.Idp)
202-
} else {
203-
q.Add("username", u.DisplayName+" @ "+idpURL.Hostname())
204-
}
200+
q.Add("username", u.DisplayName+" @ "+u.Id.Idp)
205201
// and resolve the folder
206202
rPath, pathErr = getPathForExternalLink(ctx, scopes, resource, ocmLinkURLPrefix)
207203
if pathErr != nil {
208204
log.Warn().Err(pathErr).Msg("wopi: failed to extract relative path from ocm link scope")
209205
}
210-
q.Add("usertype", "ocm")
211206
default:
212207
// in all other cases use the resource's path
213208
if ut == invalid {
@@ -508,12 +503,25 @@ func parseWopiDiscovery(body io.Reader) (map[string]map[string]string, error) {
508503
}
509504

510505
func getPathForExternalLink(ctx context.Context, scopes map[string]*authpb.Scope, resource *provider.ResourceInfo, pathPrefix string) (string, error) {
511-
shares, err := scope.GetPublicOcmSharesFromScopes(scopes)
506+
pubshares, err := scope.GetPublicSharesFromScopes(scopes)
507+
if err != nil {
508+
return "", err
509+
}
510+
ocmshares, err := scope.GetOCMSharesFromScopes(scopes)
512511
if err != nil {
513512
return "", err
514513
}
515-
if len(shares) > 1 {
516-
return "", errors.New("More than one public or OCM share found in the scope, lookup not implemented")
514+
var resId *provider.ResourceId
515+
var token string
516+
switch {
517+
case len(pubshares) == 1:
518+
resId = pubshares[0].ResourceId
519+
token = pubshares[0].Token
520+
case len(ocmshares) == 1:
521+
resId = ocmshares[0].ResourceId
522+
token = ocmshares[0].Token
523+
default:
524+
return "", errors.New("Either one public xor OCM share is supported, lookups not implemented")
517525
}
518526

519527
client, err := pool.GetGatewayServiceClient(pool.Endpoint(sharedconf.GetGatewaySVC("")))
@@ -522,7 +530,7 @@ func getPathForExternalLink(ctx context.Context, scopes map[string]*authpb.Scope
522530
}
523531
statRes, err := client.Stat(ctx, &provider.StatRequest{
524532
Ref: &provider.Reference{
525-
ResourceId: shares[0].ResourceId,
533+
ResourceId: resId,
526534
},
527535
})
528536
if err != nil {
@@ -531,7 +539,7 @@ func getPathForExternalLink(ctx context.Context, scopes map[string]*authpb.Scope
531539

532540
if statRes.Info.Path == resource.Path {
533541
// this is a direct link to the resource
534-
return pathPrefix + shares[0].Token, nil
542+
return pathPrefix + token, nil
535543
}
536544
// otherwise we are in a subfolder of the public link
537545
relPath, err := filepath.Rel(statRes.Info.Path, resource.Path)
@@ -541,5 +549,5 @@ func getPathForExternalLink(ctx context.Context, scopes map[string]*authpb.Scope
541549
if strings.HasPrefix(relPath, "../") {
542550
return "", errors.New("Scope path does not contain target resource")
543551
}
544-
return path.Join(pathPrefix+shares[0].Token, path.Dir(relPath)), nil
552+
return path.Join(pathPrefix+token, path.Dir(relPath)), nil
545553
}

pkg/auth/scope/ocmshare.go

+21
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
3333
registry "github.com/cs3org/go-cs3apis/cs3/storage/registry/v1beta1"
3434
types "github.com/cs3org/go-cs3apis/cs3/types/v1beta1"
35+
"github.com/cs3org/reva/pkg/errtypes"
3536
"github.com/cs3org/reva/pkg/utils"
3637
"github.com/rs/zerolog"
3738
)
@@ -134,3 +135,23 @@ func AddOCMShareScope(share *ocmv1beta1.Share, role authpb.Role, scopes map[stri
134135
}
135136
return scopes, nil
136137
}
138+
139+
// GetOCMSharesFromScopes returns all OCM shares in the given scope.
140+
func GetOCMSharesFromScopes(scopes map[string]*authpb.Scope) ([]*ocmv1beta1.Share, error) {
141+
var shares []*ocmv1beta1.Share
142+
for k, s := range scopes {
143+
if strings.HasPrefix(k, "ocmshare:") {
144+
res := s.Resource
145+
if res.Decoder != "json" {
146+
return nil, errtypes.InternalError("resource should be json encoded")
147+
}
148+
var share ocmv1beta1.Share
149+
err := utils.UnmarshalJSONToProtoV1(res.Value, &share)
150+
if err != nil {
151+
return nil, err
152+
}
153+
shares = append(shares, &share)
154+
}
155+
}
156+
return shares, nil
157+
}

pkg/auth/scope/publicshare.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,11 @@ func AddPublicShareScope(share *link.PublicShare, role authpb.Role, scopes map[s
136136
return scopes, nil
137137
}
138138

139-
// GetPublicOcmSharesFromScopes returns all public and OCM shares in the given scope.
140-
func GetPublicOcmSharesFromScopes(scopes map[string]*authpb.Scope) ([]*link.PublicShare, error) {
139+
// GetPublicSharesFromScopes returns all public shares in the given scope.
140+
func GetPublicSharesFromScopes(scopes map[string]*authpb.Scope) ([]*link.PublicShare, error) {
141141
var shares []*link.PublicShare
142142
for k, s := range scopes {
143-
if strings.HasPrefix(k, "publicshare:") || strings.HasPrefix(k, "ocmshare:") {
143+
if strings.HasPrefix(k, "publicshare:") {
144144
res := s.Resource
145145
if res.Decoder != "json" {
146146
return nil, errtypes.InternalError("resource should be json encoded")

0 commit comments

Comments
 (0)