Skip to content

Commit

Permalink
WIP get endpoint
Browse files Browse the repository at this point in the history
Signed-off-by: jkoberg <jkoberg@owncloud.com>
  • Loading branch information
kobergj committed Mar 30, 2022
1 parent bcd218e commit f3aed1b
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 1 deletion.
2 changes: 2 additions & 0 deletions internal/http/services/owncloud/ocdav/dav.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ func (h *DavHandler) Handler(s *svc) http.Handler {
head, r.URL.Path = router.ShiftPath(r.URL.Path)

switch head {
case "test":
s.HandleGetToken(w, r)
case "avatars":
h.AvatarsHandler.Handler(s).ServeHTTP(w, r)
case "files":
Expand Down
2 changes: 1 addition & 1 deletion internal/http/services/owncloud/ocdav/ocdav.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func (s *svc) Close() error {
}

func (s *svc) Unprotected() []string {
return []string{"/status.php", "/remote.php/dav/public-files/", "/apps/files/", "/index.php/f/", "/index.php/s/"}
return []string{"/status.php", "/remote.php/dav/public-files/" /*"/remote.php/dav/test/",*/, "/apps/files/", "/index.php/f/", "/index.php/s/"}
}

func (s *svc) Handler() http.Handler {
Expand Down
127 changes: 127 additions & 0 deletions internal/http/services/owncloud/ocdav/publicfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,26 @@
package ocdav

import (
"context"
"encoding/json"
"encoding/xml"
"fmt"
"net/http"
"path"
"path/filepath"

rpc "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1"
link "github.com/cs3org/go-cs3apis/cs3/sharing/link/v1beta1"
provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
typesv1beta1 "github.com/cs3org/go-cs3apis/cs3/types/v1beta1"
"github.com/cs3org/reva/v2/internal/http/services/owncloud/ocdav/net"
"github.com/cs3org/reva/v2/internal/http/services/owncloud/ocdav/propfind"
"github.com/cs3org/reva/v2/pkg/appctx"
ctxpkg "github.com/cs3org/reva/v2/pkg/ctx"
"github.com/cs3org/reva/v2/pkg/rgrpc/todo/pool"
"github.com/cs3org/reva/v2/pkg/rhttp/router"
rtrace "github.com/cs3org/reva/v2/pkg/trace"
"google.golang.org/grpc/metadata"
)

// PublicFileHandler handles requests on a shared file. it needs to be wrapped in a collection
Expand Down Expand Up @@ -83,6 +92,124 @@ func (h *PublicFileHandler) Handler(s *svc) http.Handler {
})
}

// TokenInfo contains information about the token
type TokenInfo struct {
Token string `xml:"token"`
LinkURL string `xml:"linkurl"`
PasswordProtected bool `xml:"passwordprotected"`

StorageID string
OpaqueID string
Path string
}

// HandleGetToken will return details about the token. NOTE: this endpoint is publicly available.
func (s *svc) HandleGetToken(w http.ResponseWriter, r *http.Request) {
c, err := pool.GetGatewayServiceClient(s.c.GatewaySvc)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
tkn, _ := router.ShiftPath(r.URL.Path)
t := TokenInfo{Token: tkn}

{
ctx := context.Background()
// get token details - if possible
q := r.URL.Query()
sig := q.Get("signature")
expiration := q.Get("expiration")
// We restrict the pre-signed urls to downloads.
if sig != "" && expiration != "" && r.Method != http.MethodGet {
w.WriteHeader(http.StatusUnauthorized)
return
}
res, err := handleSignatureAuth(ctx, c, tkn, sig, expiration)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
switch res.Status.Code {
case rpc.Code_CODE_OK:
// nothing to do
case rpc.Code_CODE_PERMISSION_DENIED:
if res.Status.Message == "wrong password" {
t.PasswordProtected = true
b, err := xml.Marshal(t)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Write(b)
w.WriteHeader(http.StatusOK)
return
}
fallthrough
default:
w.WriteHeader(http.StatusInternalServerError)
return
}
ctx = ctxpkg.ContextSetToken(ctx, res.Token)
ctx = ctxpkg.ContextSetUser(ctx, res.User)
ctx = metadata.AppendToOutgoingContext(ctx, ctxpkg.TokenHeader, res.Token)

r = r.WithContext(ctx)
sRes, err := getTokenStatInfo(ctx, c, tkn)
if err != nil || sRes.Status.Code != rpc.Code_CODE_OK {
w.WriteHeader(http.StatusInternalServerError)
return
}

ls := &link.PublicShare{}
_ = json.Unmarshal(sRes.Info.Opaque.Map["link-share"].Value, ls)

t.StorageID = ls.ResourceId.GetStorageId()
t.OpaqueID = ls.ResourceId.GetOpaqueId()

baseURI, ok := ctx.Value(net.CtxKeyBaseURI).(string)
if ok {
ref := path.Join(baseURI, sRes.Info.Path)
if sRes.Info.Type == provider.ResourceType_RESOURCE_TYPE_CONTAINER {
ref += "/"
}
t.LinkURL = ref
}
}

{
u, ok := ctxpkg.ContextGetUser(r.Context())
if ok {
//ref := &provider.Reference{
//ResourceId: &provider.ResourceId{StorageId: t.StorageID, OpaqueId: t.OpaqueID},
//Path: t.Path,
//}
ref := &provider.Reference{
ResourceId: &provider.ResourceId{
StorageId: t.StorageID, // "4c510ada-c86b-4815-8820-42cdf82c3d51",
OpaqueId: "1fe0481e-7bbb-4fb3-bb8f-a21231cf9e92",
},
Path: "",
}
ctx := context.Background()
ctx = ctxpkg.ContextSetUser(ctx, u)
res, err := c.Stat(r.Context(), &provider.StatRequest{
Ref: ref})
fmt.Println(res, err)
}

}

b, err := xml.Marshal(t)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}

w.Write(b)
w.WriteHeader(http.StatusOK)
return
}

// ns is the namespace that is prefixed to the path in the cs3 namespace
func (s *svc) handlePropfindOnToken(w http.ResponseWriter, r *http.Request, ns string, onContainer bool) {
ctx, span := rtrace.Provider.Tracer("ocdav").Start(r.Context(), "token_propfind")
Expand Down

0 comments on commit f3aed1b

Please sign in to comment.