Skip to content

Commit

Permalink
Initial implementation of the new OpenInApp API
Browse files Browse the repository at this point in the history
  • Loading branch information
glpatcern committed May 27, 2021
1 parent 81d3cb7 commit 0f7ec3a
Show file tree
Hide file tree
Showing 10 changed files with 201 additions and 71 deletions.
7 changes: 7 additions & 0 deletions changelog/unreleased/improve-open-in-app.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Enhancement: Open in app workflow using the new API

This provides a new `open-in-app` command for the CLI and the implementation
on the appprovider gateway service for the new API, including the option
to specify the appplication to use, thus overriding the preconfigured one.

https://github.com/cs3org/reva/pull/1723
2 changes: 1 addition & 1 deletion cmd/reva/completer.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func (c *Completer) argumentCompleter(args ...string) []prompt.Suggest {
return prompt.FilterHasPrefix(c.lsArgumentCompleter(false), args[2], true)
}

case "rm", "stat", "share-create", "ocm-share-create", "public-share-create", "open-file-in-app-provider", "download":
case "rm", "stat", "share-create", "ocm-share-create", "public-share-create", "open-in-app", "open-file-in-app-provider", "download":
if len(args) == 2 {
return prompt.FilterHasPrefix(c.lsArgumentCompleter(false), args[1], true)
}
Expand Down
1 change: 1 addition & 0 deletions cmd/reva/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ var (
shareUpdateCommand(),
shareListReceivedCommand(),
shareUpdateReceivedCommand(),
openInAppCommand(),
openFileInAppProviderCommand(),
transferCreateCommand(),
transferGetStatusCommand(),
Expand Down
4 changes: 2 additions & 2 deletions cmd/reva/open-file-in-app-provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func openFileInAppProviderCommand() *command {
}
path := cmd.Args()[0]

vm := getViewMode(*viewMode)
vm := getViewModeDeprecated(*viewMode)

client, err := getClient()
if err != nil {
Expand Down Expand Up @@ -91,7 +91,7 @@ func openFileInAppProviderCommand() *command {
return cmd
}

func getViewMode(viewMode string) gateway.OpenFileInAppProviderRequest_ViewMode {
func getViewModeDeprecated(viewMode string) gateway.OpenFileInAppProviderRequest_ViewMode {
switch viewMode {
case "view":
return gateway.OpenFileInAppProviderRequest_VIEW_MODE_VIEW_ONLY
Expand Down
107 changes: 107 additions & 0 deletions cmd/reva/open-in-app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// Copyright 2018-2021 CERN
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// In applying this license, CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.

package main

import (
"fmt"
"io"

gateway "github.com/cs3org/go-cs3apis/cs3/gateway/v1beta1"
rpc "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1"
provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
typespb "github.com/cs3org/go-cs3apis/cs3/types/v1beta1"
"github.com/pkg/errors"
)

func openInAppCommand() *command {
cmd := newCommand("open-in-app")
cmd.Description = func() string { return "open a reference in an external app provider" }
cmd.Usage = func() string {
return "Usage: open-in-app [-flags] [-viewmode view|read|write] [-app appname] <path>"
}
viewMode := cmd.String("viewmode", "view", "the view permissions, defaults to view")
app := cmd.String("app", "", "the application if the default is to be overridden for the file's mimetype")
insecureFlag := cmd.Bool("insecure", false, "disables grpc transport security")
skipVerifyFlag := cmd.Bool("skip-verify", false, "whether to skip verifying remote reva's certificate chain and host name")

cmd.ResetFlags = func() {
*viewMode = "view"
*app = ""
*insecureFlag = false
*skipVerifyFlag = false
}

cmd.Action = func(w ...io.Writer) error {
ctx := getAuthContext()
if cmd.NArg() < 1 {
return errors.New("Invalid arguments: " + cmd.Usage())
}
path := cmd.Args()[0]

vm := getViewMode(*viewMode)

client, err := getClient()
if err != nil {
return err
}

ref := &provider.Reference{
Spec: &provider.Reference_Path{Path: path},
}

opaqueObj := &typespb.Opaque{
Map: map[string]*typespb.OpaqueEntry{},
}
if *insecureFlag {
opaqueObj.Map["insecure"] = &typespb.OpaqueEntry{}
}
if *skipVerifyFlag {
opaqueObj.Map["skip-verify"] = &typespb.OpaqueEntry{}
}

openRequest := &gateway.OpenInAppRequest{Ref: ref, ViewMode: vm, App: *app, Opaque: opaqueObj}

openRes, err := client.OpenInApp(ctx, openRequest)
if err != nil {
return err
}

if openRes.Status.Code != rpc.Code_CODE_OK {
return formatError(openRes.Status)
}

fmt.Println("App URL: " + openRes.AppUrl)

return nil
}
return cmd
}

func getViewMode(viewMode string) gateway.OpenInAppRequest_ViewMode {
switch viewMode {
case "view":
return gateway.OpenInAppRequest_VIEW_MODE_VIEW_ONLY
case "read":
return gateway.OpenInAppRequest_VIEW_MODE_READ_ONLY
case "write":
return gateway.OpenInAppRequest_VIEW_MODE_READ_WRITE
default:
return gateway.OpenInAppRequest_VIEW_MODE_INVALID
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@ description: >
# _struct: config_

{{% dir name="iopsecret" type="string" default="" %}}
The iopsecret used to connect to the wopiserver. [[Ref]](https://github.com/cs3org/reva/tree/master/internal/grpc/services/appprovider/appprovider.go#L59)
The iopsecret used to connect to the wopiserver. [[Ref]](https://github.com/cs3org/reva/tree/master/internal/grpc/services/appprovider/appprovider.go#L60)
{{< highlight toml >}}
[grpc.services.appprovider]
iopsecret = ""
{{< /highlight >}}
{{% /dir %}}

{{% dir name="wopiurl" type="string" default="" %}}
The wopiserver's URL. [[Ref]](https://github.com/cs3org/reva/tree/master/internal/grpc/services/appprovider/appprovider.go#L60)
The wopiserver's URL. [[Ref]](https://github.com/cs3org/reva/tree/master/internal/grpc/services/appprovider/appprovider.go#L61)
{{< highlight toml >}}
[grpc.services.appprovider]
wopiurl = ""
{{< /highlight >}}
{{% /dir %}}

{{% dir name="wopibridgeurl" type="string" default="" %}}
The wopibridge's URL. [[Ref]](https://github.com/cs3org/reva/tree/master/internal/grpc/services/appprovider/appprovider.go#L61)
The wopibridge's URL. [[Ref]](https://github.com/cs3org/reva/tree/master/internal/grpc/services/appprovider/appprovider.go#L62)
{{< highlight toml >}}
[grpc.services.appprovider]
wopibridgeurl = ""
Expand Down
12 changes: 6 additions & 6 deletions docs/content/en/docs/config/packages/auth/manager/oidc/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,47 +9,47 @@ description: >
# _struct: config_

{{% dir name="insecure" type="bool" default=false %}}
Whether to skip certificate checks when sending requests. [[Ref]](https://github.com/cs3org/reva/tree/master/pkg/auth/manager/oidc/oidc.go#L53)
Whether to skip certificate checks when sending requests. [[Ref]](https://github.com/cs3org/reva/tree/master/pkg/auth/manager/oidc/oidc.go#L55)
{{< highlight toml >}}
[auth.manager.oidc]
insecure = false
{{< /highlight >}}
{{% /dir %}}

{{% dir name="issuer" type="string" default="" %}}
The issuer of the OIDC token. [[Ref]](https://github.com/cs3org/reva/tree/master/pkg/auth/manager/oidc/oidc.go#L54)
The issuer of the OIDC token. [[Ref]](https://github.com/cs3org/reva/tree/master/pkg/auth/manager/oidc/oidc.go#L56)
{{< highlight toml >}}
[auth.manager.oidc]
issuer = ""
{{< /highlight >}}
{{% /dir %}}

{{% dir name="id_claim" type="string" default="sub" %}}
The claim containing the ID of the user. [[Ref]](https://github.com/cs3org/reva/tree/master/pkg/auth/manager/oidc/oidc.go#L55)
The claim containing the ID of the user. [[Ref]](https://github.com/cs3org/reva/tree/master/pkg/auth/manager/oidc/oidc.go#L57)
{{< highlight toml >}}
[auth.manager.oidc]
id_claim = "sub"
{{< /highlight >}}
{{% /dir %}}

{{% dir name="uid_claim" type="string" default="" %}}
The claim containing the UID of the user. [[Ref]](https://github.com/cs3org/reva/tree/master/pkg/auth/manager/oidc/oidc.go#L56)
The claim containing the UID of the user. [[Ref]](https://github.com/cs3org/reva/tree/master/pkg/auth/manager/oidc/oidc.go#L58)
{{< highlight toml >}}
[auth.manager.oidc]
uid_claim = ""
{{< /highlight >}}
{{% /dir %}}

{{% dir name="gid_claim" type="string" default="" %}}
The claim containing the GID of the user. [[Ref]](https://github.com/cs3org/reva/tree/master/pkg/auth/manager/oidc/oidc.go#L57)
The claim containing the GID of the user. [[Ref]](https://github.com/cs3org/reva/tree/master/pkg/auth/manager/oidc/oidc.go#L59)
{{< highlight toml >}}
[auth.manager.oidc]
gid_claim = ""
{{< /highlight >}}
{{% /dir %}}

{{% dir name="gatewaysvc" type="string" default="" %}}
The endpoint at which the GRPC gateway is exposed. [[Ref]](https://github.com/cs3org/reva/tree/master/pkg/auth/manager/oidc/oidc.go#L58)
The endpoint at which the GRPC gateway is exposed. [[Ref]](https://github.com/cs3org/reva/tree/master/pkg/auth/manager/oidc/oidc.go#L60)
{{< highlight toml >}}
[auth.manager.oidc]
gatewaysvc = ""
Expand Down
2 changes: 1 addition & 1 deletion docs/content/en/docs/tutorials/wopi-tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,6 @@ Run according to instructions in the readme ("Run the WOPI server locally", http
## 7. Local changes
To try the connection you could for example go to a new reva terminal window and type
`./cmd/reva/reva -insecure login basic` - use einstein and relativity as log in ccredentials
`./cmd/reva/reva -insecure open-file-in-app-provider /home/example.txt read` - this should print out the app provider url in your terminal.
`./cmd/reva/reva -insecure open-in-app /home/example.txt read` - this should print out the app provider url in your terminal.

## 8. Enjoy your new Reva and wopiserver set up!
70 changes: 39 additions & 31 deletions internal/grpc/services/appprovider/appprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func (s *service) getWopiAppEndpoints(ctx context.Context) (map[string]interface
return appsURLMap, nil
}

func (s *service) OpenFileInAppProvider(ctx context.Context, req *providerpb.OpenFileInAppProviderRequest) (*providerpb.OpenFileInAppProviderResponse, error) {
func (s *service) OpenInApp(ctx context.Context, req *providerpb.OpenInAppRequest) (*providerpb.OpenInAppResponse, error) {

log := appctx.GetLogger(ctx)

Expand Down Expand Up @@ -189,15 +189,15 @@ func (s *service) OpenFileInAppProvider(ctx context.Context, req *providerpb.Ope
openRes, err := s.client.Do(httpReq)

if err != nil {
res := &providerpb.OpenFileInAppProviderResponse{
res := &providerpb.OpenInAppResponse{
Status: status.NewInternal(ctx, err, "appprovider: error performing open request to WOPI"),
}
return res, nil
}
defer openRes.Body.Close()

if openRes.StatusCode != http.StatusOK {
res := &providerpb.OpenFileInAppProviderResponse{
res := &providerpb.OpenInAppResponse{
Status: status.NewInvalid(ctx, fmt.Sprintf("appprovider: error performing open request to WOPI, status code: %d", openRes.StatusCode)),
}
return res, nil
Expand All @@ -210,37 +210,45 @@ func (s *service) OpenFileInAppProvider(ctx context.Context, req *providerpb.Ope
}
openResBody := buf.String()

// TODO call this e.g. once a day or a week, and cache the content in a shared map protected by a multi-reader Lock
appsURLMap, err := s.getWopiAppEndpoints(ctx)
if err != nil {
res := &providerpb.OpenFileInAppProviderResponse{
Status: status.NewInternal(ctx, err, "appprovider: getWopiAppEndpoints failed"),
}
return res, nil
}
viewOptions := appsURLMap[path.Ext(req.ResourceInfo.GetPath())]
viewOptionsMap, ok := viewOptions.(map[string]interface{})
if !ok {
res := &providerpb.OpenFileInAppProviderResponse{
Status: status.NewInvalid(ctx, "Incorrect parsing of the App URLs map from the WOPI server"),
}
return res, nil
}

var viewmode string
if req.ViewMode == providerpb.OpenFileInAppProviderRequest_VIEW_MODE_READ_WRITE {
if req.ViewMode == providerpb.OpenInAppRequest_VIEW_MODE_READ_WRITE {
viewmode = "edit"
} else {
viewmode = "view"
}

appProviderURL := fmt.Sprintf("%v", viewOptionsMap[viewmode])
if strings.Contains(appProviderURL, "?") {
appProviderURL += "&"
var appProviderURL string
if req.App == "" {
// Default behavior: work out the application URL to be used for this file
// TODO call this e.g. once a day or a week, and cache the content in a shared map protected by a multi-reader Lock
appsURLMap, err := s.getWopiAppEndpoints(ctx)
if err != nil {
res := &providerpb.OpenInAppResponse{
Status: status.NewInternal(ctx, err, "appprovider: getWopiAppEndpoints failed"),
}
return res, nil
}
viewOptions := appsURLMap[path.Ext(req.ResourceInfo.GetPath())]
viewOptionsMap, ok := viewOptions.(map[string]interface{})
if !ok {
res := &providerpb.OpenInAppResponse{
Status: status.NewInvalid(ctx, "Incorrect parsing of the App URLs map from the WOPI server"),
}
return res, nil
}

appProviderURL = fmt.Sprintf("%v", viewOptionsMap[viewmode])
if strings.Contains(appProviderURL, "?") {
appProviderURL += "&"
} else {
appProviderURL += "?"
}
appProviderURL = fmt.Sprintf("%sWOPISrc=%s", appProviderURL, openResBody)
} else {
appProviderURL += "?"
// User specified the application to use, generate the URL out of that
// TODO map the given req.App to the URL via config. For now assume it's a URL!
appProviderURL = fmt.Sprintf("%sWOPISrc=%s", req.App, openResBody)
}
appProviderURL = fmt.Sprintf("%sWOPISrc=%s", appProviderURL, openResBody)

// In case of applications served by the WOPI bridge, resolve the URL and go to the app
// Note that URL matching is performed via string matching, not via IP resolution: may need to fix this
Expand Down Expand Up @@ -270,12 +278,12 @@ func (s *service) OpenFileInAppProvider(ctx context.Context, req *providerpb.Ope
}

log.Info().Msg(fmt.Sprintf("Returning app provider URL %s", appProviderURL))
return &providerpb.OpenFileInAppProviderResponse{
Status: status.NewOK(ctx),
AppProviderUrl: appProviderURL,
return &providerpb.OpenInAppResponse{
Status: status.NewOK(ctx),
AppUrl: appProviderURL,
}, nil
}

func (s *service) OpenInApp(ctx context.Context, req *providerpb.OpenInAppRequest) (*providerpb.OpenInAppResponse, error) {
return nil, errtypes.NotSupported("Unimplemented")
func (s *service) OpenFileInAppProvider(ctx context.Context, req *providerpb.OpenFileInAppProviderRequest) (*providerpb.OpenFileInAppProviderResponse, error) {
return nil, errtypes.NotSupported("Deprecated")
}
Loading

0 comments on commit 0f7ec3a

Please sign in to comment.