Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move user context methods into separate package #1982

Merged
merged 5 commits into from
Aug 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions changelog/unreleased/userctx-package.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Change: Move user context methods into a separate `userctx` package

https://github.com/cs3org/reva/pull/1982
4 changes: 2 additions & 2 deletions cmd/reva/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ import (
rpc "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1"
provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
"github.com/cs3org/reva/internal/http/services/datagateway"
ctxpkg "github.com/cs3org/reva/pkg/ctx"
"github.com/cs3org/reva/pkg/errtypes"
"github.com/cs3org/reva/pkg/rhttp"
tokenpkg "github.com/cs3org/reva/pkg/token"
"github.com/cs3org/reva/pkg/utils"
"github.com/pkg/errors"
"github.com/studio-b12/gowebdav"
Expand Down Expand Up @@ -188,7 +188,7 @@ func checkDownloadWebdavRef(protocols []*gateway.FileDownloadProtocol) (io.Reade
}

c := gowebdav.NewClient(p.DownloadEndpoint, "", "")
c.SetHeader(tokenpkg.TokenHeader, token)
c.SetHeader(ctxpkg.TokenHeader, token)

reader, err := c.ReadStream(filePath)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions cmd/reva/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (

gateway "github.com/cs3org/go-cs3apis/cs3/gateway/v1beta1"
rpc "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1"
"github.com/cs3org/reva/pkg/token"
ctxpkg "github.com/cs3org/reva/pkg/ctx"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
)
Expand All @@ -41,8 +41,8 @@ func getAuthContext() context.Context {
log.Println(err)
return ctx
}
ctx = token.ContextSetToken(ctx, t)
ctx = metadata.AppendToOutgoingContext(ctx, token.TokenHeader, t)
ctx = ctxpkg.ContextSetToken(ctx, t)
ctx = metadata.AppendToOutgoingContext(ctx, ctxpkg.TokenHeader, t)
return ctx
}

Expand Down
8 changes: 4 additions & 4 deletions cmd/reva/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ import (
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"
ctxpkg "github.com/cs3org/reva/pkg/ctx"
"github.com/cs3org/reva/pkg/errtypes"
tokenpkg "github.com/cs3org/reva/pkg/token"
"github.com/eventials/go-tus"
"github.com/eventials/go-tus/memorystore"
"github.com/studio-b12/gowebdav"
Expand Down Expand Up @@ -176,8 +176,8 @@ func uploadCommand() *command {
if err != nil {
return err
}
if token, ok := tokenpkg.ContextGetToken(ctx); ok {
c.Header.Add(tokenpkg.TokenHeader, token)
if token, ok := ctxpkg.ContextGetToken(ctx); ok {
c.Header.Add(ctxpkg.TokenHeader, token)
}
c.Header.Add(datagateway.TokenTransportHeader, p.Token)
tusc, err := tus.NewClient(dataServerURL, c)
Expand Down Expand Up @@ -272,7 +272,7 @@ func checkUploadWebdavRef(protocols []*gateway.FileUploadProtocol, md os.FileInf
}

c := gowebdav.NewClient(p.UploadEndpoint, "", "")
c.SetHeader(tokenpkg.TokenHeader, token)
c.SetHeader(ctxpkg.TokenHeader, token)
c.SetHeader("Upload-Length", strconv.FormatInt(md.Size(), 10))

if err = c.WriteStream(filePath, fd, 0700); err != nil {
Expand Down
18 changes: 9 additions & 9 deletions internal/grpc/interceptors/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ import (
userpb "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1"
"github.com/cs3org/reva/pkg/appctx"
"github.com/cs3org/reva/pkg/auth/scope"
ctxpkg "github.com/cs3org/reva/pkg/ctx"
"github.com/cs3org/reva/pkg/errtypes"
"github.com/cs3org/reva/pkg/sharedconf"
"github.com/cs3org/reva/pkg/token"
tokenmgr "github.com/cs3org/reva/pkg/token/manager/registry"
"github.com/cs3org/reva/pkg/user"
"github.com/cs3org/reva/pkg/utils"
"github.com/mitchellh/mapstructure"
"github.com/pkg/errors"
Expand Down Expand Up @@ -90,19 +90,19 @@ func NewUnary(m map[string]interface{}, unprotected []string) (grpc.UnaryServerI

// If a token is present, set it anyway, as we might need the user info
// to decide the storage provider.
tkn, ok := token.ContextGetToken(ctx)
tkn, ok := ctxpkg.ContextGetToken(ctx)
if ok {
u, err := dismantleToken(ctx, tkn, req, tokenManager, conf.GatewayAddr)
if err == nil {
ctx = user.ContextSetUser(ctx, u)
ctx = ctxpkg.ContextSetUser(ctx, u)
}
}
return handler(ctx, req)
}

span.AddAttributes(trace.BoolAttribute("auth_enabled", true))

tkn, ok := token.ContextGetToken(ctx)
tkn, ok := ctxpkg.ContextGetToken(ctx)

if !ok || tkn == "" {
log.Warn().Msg("access token not found or empty")
Expand All @@ -124,7 +124,7 @@ func NewUnary(m map[string]interface{}, unprotected []string) (grpc.UnaryServerI
trace.StringAttribute("token", tkn))
span.AddAttributes(trace.StringAttribute("user", u.String()), trace.StringAttribute("token", tkn))

ctx = user.ContextSetUser(ctx, u)
ctx = ctxpkg.ContextSetUser(ctx, u)
return handler(ctx, req)
}
return interceptor, nil
Expand Down Expand Up @@ -161,19 +161,19 @@ func NewStream(m map[string]interface{}, unprotected []string) (grpc.StreamServe

// If a token is present, set it anyway, as we might need the user info
// to decide the storage provider.
tkn, ok := token.ContextGetToken(ctx)
tkn, ok := ctxpkg.ContextGetToken(ctx)
if ok {
u, err := dismantleToken(ctx, tkn, ss, tokenManager, conf.GatewayAddr)
if err == nil {
ctx = user.ContextSetUser(ctx, u)
ctx = ctxpkg.ContextSetUser(ctx, u)
ss = newWrappedServerStream(ctx, ss)
}
}

return handler(srv, ss)
}

tkn, ok := token.ContextGetToken(ctx)
tkn, ok := ctxpkg.ContextGetToken(ctx)

if !ok || tkn == "" {
log.Warn().Msg("access token not found")
Expand All @@ -188,7 +188,7 @@ func NewStream(m map[string]interface{}, unprotected []string) (grpc.StreamServe
}

// store user and core access token in context.
ctx = user.ContextSetUser(ctx, u)
ctx = ctxpkg.ContextSetUser(ctx, u)
wrapped := newWrappedServerStream(ctx, ss)
return handler(srv, wrapped)
}
Expand Down
14 changes: 7 additions & 7 deletions internal/grpc/interceptors/token/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ package token
import (
"context"

"github.com/cs3org/reva/pkg/token"
ctxpkg "github.com/cs3org/reva/pkg/ctx"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
)
Expand All @@ -32,11 +32,11 @@ func NewUnary() grpc.UnaryServerInterceptor {
interceptor := func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
md, ok := metadata.FromIncomingContext(ctx)
if ok && md != nil {
if val, ok := md[token.TokenHeader]; ok {
if val, ok := md[ctxpkg.TokenHeader]; ok {
if len(val) > 0 && val[0] != "" {
tkn := val[0]
ctx = token.ContextSetToken(ctx, tkn)
ctx = metadata.AppendToOutgoingContext(ctx, token.TokenHeader, tkn)
ctx = ctxpkg.ContextSetToken(ctx, tkn)
ctx = metadata.AppendToOutgoingContext(ctx, ctxpkg.TokenHeader, tkn)
}
}
}
Expand All @@ -54,11 +54,11 @@ func NewStream() grpc.StreamServerInterceptor {

md, ok := metadata.FromIncomingContext(ss.Context())
if ok && md != nil {
if val, ok := md[token.TokenHeader]; ok {
if val, ok := md[ctxpkg.TokenHeader]; ok {
if len(val) > 0 && val[0] != "" {
tkn := val[0]
ctx = token.ContextSetToken(ctx, tkn)
ctx = metadata.AppendToOutgoingContext(ctx, token.TokenHeader, tkn)
ctx = ctxpkg.ContextSetToken(ctx, tkn)
ctx = metadata.AppendToOutgoingContext(ctx, ctxpkg.TokenHeader, tkn)
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions internal/grpc/services/gateway/appprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ import (
storageprovider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
typespb "github.com/cs3org/go-cs3apis/cs3/types/v1beta1"
"github.com/cs3org/reva/pkg/appctx"
ctxpkg "github.com/cs3org/reva/pkg/ctx"
"github.com/cs3org/reva/pkg/errtypes"
"github.com/cs3org/reva/pkg/rgrpc/status"
"github.com/cs3org/reva/pkg/rgrpc/todo/pool"
"github.com/cs3org/reva/pkg/token"
"github.com/pkg/errors"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
Expand Down Expand Up @@ -157,8 +157,8 @@ func (s *svc) openFederatedShares(ctx context.Context, targetURL string, vm gate
}

gatewayClient := gateway.NewGatewayAPIClient(conn)
remoteCtx := token.ContextSetToken(context.Background(), ep.token)
remoteCtx = metadata.AppendToOutgoingContext(remoteCtx, token.TokenHeader, ep.token)
remoteCtx := ctxpkg.ContextSetToken(context.Background(), ep.token)
remoteCtx = metadata.AppendToOutgoingContext(remoteCtx, ctxpkg.TokenHeader, ep.token)

res, err := gatewayClient.OpenInApp(remoteCtx, appProviderReq)
if err != nil {
Expand All @@ -171,7 +171,7 @@ func (s *svc) openFederatedShares(ctx context.Context, targetURL string, vm gate
func (s *svc) openLocalResources(ctx context.Context, ri *storageprovider.ResourceInfo,
vm gateway.OpenInAppRequest_ViewMode, app string) (*providerpb.OpenInAppResponse, error) {

accessToken, ok := token.ContextGetToken(ctx)
accessToken, ok := ctxpkg.ContextGetToken(ctx)
if !ok || accessToken == "" {
return &providerpb.OpenInAppResponse{
Status: status.NewUnauthenticated(ctx, errtypes.InvalidCredentials("Access token is invalid or empty"), ""),
Expand Down
15 changes: 7 additions & 8 deletions internal/grpc/services/gateway/authprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,10 @@ import (
storageprovider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
"github.com/cs3org/reva/pkg/appctx"
"github.com/cs3org/reva/pkg/auth/scope"
ctxpkg "github.com/cs3org/reva/pkg/ctx"
"github.com/cs3org/reva/pkg/errtypes"
"github.com/cs3org/reva/pkg/rgrpc/status"
"github.com/cs3org/reva/pkg/rgrpc/todo/pool"
tokenpkg "github.com/cs3org/reva/pkg/token"
userpkg "github.com/cs3org/reva/pkg/user"
"github.com/cs3org/reva/pkg/utils"
"github.com/pkg/errors"
"google.golang.org/grpc/metadata"
Expand Down Expand Up @@ -113,9 +112,9 @@ func (s *svc) Authenticate(ctx context.Context, req *gateway.AuthenticateRequest
return res, nil
}

ctx = tokenpkg.ContextSetToken(ctx, token)
ctx = userpkg.ContextSetUser(ctx, res.User)
ctx = metadata.AppendToOutgoingContext(ctx, tokenpkg.TokenHeader, token)
ctx = ctxpkg.ContextSetToken(ctx, token)
ctx = ctxpkg.ContextSetUser(ctx, res.User)
ctx = metadata.AppendToOutgoingContext(ctx, ctxpkg.TokenHeader, token)
scope, err := s.expandScopes(ctx, res.TokenScope)
if err != nil {
err = errors.Wrap(err, "authsvc: error expanding token scope")
Expand Down Expand Up @@ -144,9 +143,9 @@ func (s *svc) Authenticate(ctx context.Context, req *gateway.AuthenticateRequest

// we need to pass the token to authenticate the CreateHome request.
// TODO(labkode): appending to existing context will not pass the token.
ctx = tokenpkg.ContextSetToken(ctx, token)
ctx = userpkg.ContextSetUser(ctx, res.User)
ctx = metadata.AppendToOutgoingContext(ctx, tokenpkg.TokenHeader, token) // TODO(jfd): hardcoded metadata key. use PerRPCCredentials?
ctx = ctxpkg.ContextSetToken(ctx, token)
ctx = ctxpkg.ContextSetUser(ctx, res.User)
ctx = metadata.AppendToOutgoingContext(ctx, ctxpkg.TokenHeader, token) // TODO(jfd): hardcoded metadata key. use PerRPCCredentials?

// create home directory
createHomeRes, err := s.CreateHome(ctx, &storageprovider.CreateHomeRequest{})
Expand Down
12 changes: 6 additions & 6 deletions internal/grpc/services/gateway/webdavstorageprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ import (
ocmprovider "github.com/cs3org/go-cs3apis/cs3/ocm/provider/v1beta1"
provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
types "github.com/cs3org/go-cs3apis/cs3/types/v1beta1"
ctxpkg "github.com/cs3org/reva/pkg/ctx"
"github.com/cs3org/reva/pkg/errtypes"
"github.com/cs3org/reva/pkg/token"
"github.com/pkg/errors"
"github.com/studio-b12/gowebdav"
)
Expand All @@ -56,7 +56,7 @@ func (s *svc) webdavRefStat(ctx context.Context, targetURL string, nameQueries .
}

c := gowebdav.NewClient(webdavEP, "", "")
c.SetHeader(token.TokenHeader, ep.token)
c.SetHeader(ctxpkg.TokenHeader, ep.token)

// TODO(ishank011): We need to call PROPFIND ourselves as we need to retrieve
// ownloud-specific fields to get the resource ID and permissions.
Expand All @@ -83,7 +83,7 @@ func (s *svc) webdavRefLs(ctx context.Context, targetURL string, nameQueries ...
}

c := gowebdav.NewClient(webdavEP, "", "")
c.SetHeader(token.TokenHeader, ep.token)
c.SetHeader(ctxpkg.TokenHeader, ep.token)

// TODO(ishank011): We need to call PROPFIND ourselves as we need to retrieve
// ownloud-specific fields to get the resource ID and permissions.
Expand Down Expand Up @@ -116,7 +116,7 @@ func (s *svc) webdavRefMkdir(ctx context.Context, targetURL string, nameQueries
}

c := gowebdav.NewClient(webdavEP, "", "")
c.SetHeader(token.TokenHeader, ep.token)
c.SetHeader(ctxpkg.TokenHeader, ep.token)

err = c.Mkdir(ep.filePath, 0700)
if err != nil {
Expand Down Expand Up @@ -149,7 +149,7 @@ func (s *svc) webdavRefMove(ctx context.Context, targetURL, src, destination str
}

c := gowebdav.NewClient(srcWebdavEP, "", "")
c.SetHeader(token.TokenHeader, srcEP.token)
c.SetHeader(ctxpkg.TokenHeader, srcEP.token)

err = c.Rename(srcEP.filePath, destEP.filePath, true)
if err != nil {
Expand All @@ -174,7 +174,7 @@ func (s *svc) webdavRefDelete(ctx context.Context, targetURL string, nameQueries
}

c := gowebdav.NewClient(webdavEP, "", "")
c.SetHeader(token.TokenHeader, ep.token)
c.SetHeader(ctxpkg.TokenHeader, ep.token)

err = c.Remove(ep.filePath)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions internal/grpc/services/preferences/preferences.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ import (

userpb "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1"
preferences "github.com/cs3org/go-cs3apis/cs3/preferences/v1beta1"
ctxpkg "github.com/cs3org/reva/pkg/ctx"
"github.com/cs3org/reva/pkg/rgrpc"
"github.com/cs3org/reva/pkg/rgrpc/status"
"github.com/cs3org/reva/pkg/user"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -67,7 +67,7 @@ func (s *service) Register(ss *grpc.Server) {
}

func getUser(ctx context.Context) (*userpb.User, error) {
u, ok := user.ContextGetUser(ctx)
u, ok := ctxpkg.ContextGetUser(ctx)
if !ok {
err := errors.Wrap(contextUserRequiredErr("userrequired"), "preferences: error getting user from ctx")
return nil, err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ import (
link "github.com/cs3org/go-cs3apis/cs3/sharing/link/v1beta1"
provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
"github.com/cs3org/reva/pkg/appctx"
ctxpkg "github.com/cs3org/reva/pkg/ctx"
"github.com/cs3org/reva/pkg/errtypes"
"github.com/cs3org/reva/pkg/publicshare"
"github.com/cs3org/reva/pkg/publicshare/manager/registry"
"github.com/cs3org/reva/pkg/rgrpc"
"github.com/cs3org/reva/pkg/rgrpc/status"
"github.com/cs3org/reva/pkg/user"
"github.com/mitchellh/mapstructure"
"github.com/pkg/errors"
"google.golang.org/grpc"
Expand Down Expand Up @@ -110,7 +110,7 @@ func (s *service) CreatePublicShare(ctx context.Context, req *link.CreatePublicS
log := appctx.GetLogger(ctx)
log.Info().Str("publicshareprovider", "create").Msg("create public share")

u, ok := user.ContextGetUser(ctx)
u, ok := ctxpkg.ContextGetUser(ctx)
if !ok {
log.Error().Msg("error getting user from context")
}
Expand All @@ -131,7 +131,7 @@ func (s *service) RemovePublicShare(ctx context.Context, req *link.RemovePublicS
log := appctx.GetLogger(ctx)
log.Info().Str("publicshareprovider", "remove").Msg("remove public share")

user := user.ContextMustGetUser(ctx)
user := ctxpkg.ContextMustGetUser(ctx)
err := s.sm.RevokePublicShare(ctx, user, req.Ref)
if err != nil {
return &link.RemovePublicShareResponse{
Expand Down Expand Up @@ -174,7 +174,7 @@ func (s *service) GetPublicShare(ctx context.Context, req *link.GetPublicShareRe
log := appctx.GetLogger(ctx)
log.Info().Str("publicshareprovider", "get").Msg("get public share")

u, ok := user.ContextGetUser(ctx)
u, ok := ctxpkg.ContextGetUser(ctx)
if !ok {
log.Error().Msg("error getting user from context")
}
Expand All @@ -193,7 +193,7 @@ func (s *service) GetPublicShare(ctx context.Context, req *link.GetPublicShareRe
func (s *service) ListPublicShares(ctx context.Context, req *link.ListPublicSharesRequest) (*link.ListPublicSharesResponse, error) {
log := appctx.GetLogger(ctx)
log.Info().Str("publicshareprovider", "list").Msg("list public share")
user, _ := user.ContextGetUser(ctx)
user, _ := ctxpkg.ContextGetUser(ctx)

shares, err := s.sm.ListPublicShares(ctx, user, req.Filters, &provider.ResourceInfo{}, req.GetSign())
if err != nil {
Expand All @@ -214,7 +214,7 @@ func (s *service) UpdatePublicShare(ctx context.Context, req *link.UpdatePublicS
log := appctx.GetLogger(ctx)
log.Info().Str("publicshareprovider", "update").Msg("update public share")

u, ok := user.ContextGetUser(ctx)
u, ok := ctxpkg.ContextGetUser(ctx)
if !ok {
log.Error().Msg("error getting user from context")
}
Expand Down
Loading