refactor: move wopi context and related to middleware package

This commit is contained in:
Juan Pablo Villafáñez
2024-03-20 11:05:43 +01:00
parent aa58caef63
commit 0a413223b9
9 changed files with 24 additions and 56 deletions

View File

@@ -14,7 +14,7 @@ import (
providerv1beta1 "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
types "github.com/cs3org/go-cs3apis/cs3/types/v1beta1"
"github.com/owncloud/ocis/v2/services/collaboration/pkg/config"
"github.com/owncloud/ocis/v2/services/collaboration/pkg/internal/app"
"github.com/owncloud/ocis/v2/services/collaboration/pkg/middleware"
"github.com/rs/zerolog"
)
@@ -33,7 +33,7 @@ func NewContentConnector(gwc gatewayv1beta1.GatewayAPIClient, cfg *config.Config
// GetFile downloads the file from the storage
// https://docs.microsoft.com/en-us/microsoft-365/cloud-storage-partner-program/rest/files/getfile
func (c *ContentConnector) GetFile(ctx context.Context, writer io.Writer) error {
wopiContext, err := app.WopiContextFromCtx(ctx)
wopiContext, err := middleware.WopiContextFromCtx(ctx)
if err != nil {
return err
}
@@ -140,7 +140,7 @@ func (c *ContentConnector) GetFile(ctx context.Context, writer io.Writer) error
// PutFile uploads the file to the storage
// https://docs.microsoft.com/en-us/microsoft-365/cloud-storage-partner-program/rest/files/putfile
func (c *ContentConnector) PutFile(ctx context.Context, stream io.Reader, streamLength int64, lockID string) (string, error) {
wopiContext, err := app.WopiContextFromCtx(ctx)
wopiContext, err := middleware.WopiContextFromCtx(ctx)
if err != nil {
return "", err
}

View File

@@ -14,7 +14,7 @@ import (
typesv1beta1 "github.com/cs3org/go-cs3apis/cs3/types/v1beta1"
"github.com/google/uuid"
"github.com/owncloud/ocis/v2/services/collaboration/pkg/config"
"github.com/owncloud/ocis/v2/services/collaboration/pkg/internal/app"
"github.com/owncloud/ocis/v2/services/collaboration/pkg/middleware"
"github.com/rs/zerolog"
)
@@ -39,7 +39,7 @@ func NewFileConnector(gwc gatewayv1beta1.GatewayAPIClient, cfg *config.Config) *
// GetLock returns a lock or an empty string if no lock exists
// https://docs.microsoft.com/en-us/microsoft-365/cloud-storage-partner-program/rest/files/getlock
func (f *FileConnector) GetLock(ctx context.Context) (string, error) {
wopiContext, err := app.WopiContextFromCtx(ctx)
wopiContext, err := middleware.WopiContextFromCtx(ctx)
if err != nil {
return "", err
}
@@ -81,7 +81,7 @@ func (f *FileConnector) GetLock(ctx context.Context) (string, error) {
// https://docs.microsoft.com/en-us/microsoft-365/cloud-storage-partner-program/rest/files/lock
// https://docs.microsoft.com/en-us/microsoft-365/cloud-storage-partner-program/rest/files/unlockandrelock
func (f *FileConnector) Lock(ctx context.Context, lockID, oldLockID string) (string, error) {
wopiContext, err := app.WopiContextFromCtx(ctx)
wopiContext, err := middleware.WopiContextFromCtx(ctx)
if err != nil {
return "", err
}
@@ -205,7 +205,7 @@ func (f *FileConnector) Lock(ctx context.Context, lockID, oldLockID string) (str
// RefreshLock refreshes a provided lock for 30 minutes
// https://docs.microsoft.com/en-us/microsoft-365/cloud-storage-partner-program/rest/files/refreshlock
func (f *FileConnector) RefreshLock(ctx context.Context, lockID string) (string, error) {
wopiContext, err := app.WopiContextFromCtx(ctx)
wopiContext, err := middleware.WopiContextFromCtx(ctx)
if err != nil {
return "", err
}
@@ -302,7 +302,7 @@ func (f *FileConnector) RefreshLock(ctx context.Context, lockID string) (string,
// UnLock removes a given lock from a file
// https://docs.microsoft.com/en-us/microsoft-365/cloud-storage-partner-program/rest/files/unlock
func (f *FileConnector) UnLock(ctx context.Context, lockID string) (string, error) {
wopiContext, err := app.WopiContextFromCtx(ctx)
wopiContext, err := middleware.WopiContextFromCtx(ctx)
if err != nil {
return "", err
}
@@ -386,10 +386,10 @@ func (f *FileConnector) UnLock(ctx context.Context, lockID string) (string, erro
// CheckFileInfo returns information about the requested file and capabilities of the wopi server
// https://docs.microsoft.com/en-us/microsoft-365/cloud-storage-partner-program/rest/files/checkfileinfo
func (f *FileConnector) CheckFileInfo(ctx context.Context) (app.FileInfo, error) {
wopiContext, err := app.WopiContextFromCtx(ctx)
func (f *FileConnector) CheckFileInfo(ctx context.Context) (FileInfo, error) {
wopiContext, err := middleware.WopiContextFromCtx(ctx)
if err != nil {
return app.FileInfo{}, err
return FileInfo{}, err
}
logger := zerolog.Ctx(ctx)
@@ -399,7 +399,7 @@ func (f *FileConnector) CheckFileInfo(ctx context.Context) (app.FileInfo, error)
})
if err != nil {
logger.Error().Err(err).Msg("CheckFileInfo: stat failed")
return app.FileInfo{}, err
return FileInfo{}, err
}
if statRes.Status.Code != rpcv1beta1.Code_CODE_OK {
@@ -407,10 +407,10 @@ func (f *FileConnector) CheckFileInfo(ctx context.Context) (app.FileInfo, error)
Str("StatusCode", statRes.Status.Code.String()).
Str("StatusMsg", statRes.Status.Message).
Msg("CheckFileInfo: stat failed with unexpected status")
return app.FileInfo{}, NewConnectorError(500, statRes.Status.GetCode().String()+" "+statRes.Status.GetMessage())
return FileInfo{}, NewConnectorError(500, statRes.Status.GetCode().String()+" "+statRes.Status.GetMessage())
}
fileInfo := app.FileInfo{
fileInfo := FileInfo{
// OwnerID must use only alphanumeric chars (https://learn.microsoft.com/en-us/microsoft-365/cloud-storage-partner-program/rest/files/checkfileinfo/checkfileinfo-response#requirements-for-user-identity-properties)
OwnerID: hex.EncodeToString([]byte(statRes.Info.Owner.OpaqueId + "@" + statRes.Info.Owner.Idp)),
Size: int64(statRes.Info.Size),

View File

@@ -1,4 +1,4 @@
package app
package connector
type FileInfo struct {
// ------------

View File

@@ -1,32 +0,0 @@
package middleware
import (
"net/http"
"time"
"github.com/go-chi/chi/v5/middleware"
chimiddleware "github.com/go-chi/chi/v5/middleware"
"github.com/owncloud/ocis/v2/ocis-pkg/log"
)
// AccessLog is a middleware to log http requests at info level logging.
func AccessLog(logger log.Logger) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
wrap := middleware.NewWrapResponseWriter(w, r.ProtoMajor)
next.ServeHTTP(wrap, r)
logger.Info().
Str("proto", r.Proto).
Str("request", chimiddleware.GetReqID(r.Context())).
Str("remote-addr", r.RemoteAddr).
Str("method", r.Method).
Int("status", wrap.Status()).
Str("path", r.URL.Path).
Dur("duration", time.Since(start)).
Int("bytes", wrap.BytesWritten()).
Msg("access-log")
})
}
}

View File

@@ -1,4 +1,4 @@
package app
package middleware
import "github.com/golang-jwt/jwt/v4"

View File

@@ -1,4 +1,4 @@
package app
package middleware
import (
"crypto/aes"

View File

@@ -1,4 +1,4 @@
package app
package middleware
import (
"context"

View File

@@ -13,7 +13,7 @@ import (
"github.com/owncloud/ocis/v2/ocis-pkg/service/http"
"github.com/owncloud/ocis/v2/ocis-pkg/tracing"
"github.com/owncloud/ocis/v2/ocis-pkg/version"
"github.com/owncloud/ocis/v2/services/collaboration/pkg/internal/app"
colabmiddleware "github.com/owncloud/ocis/v2/services/collaboration/pkg/middleware"
"github.com/riandyrn/otelchi"
"go-micro.dev/v4"
)
@@ -111,7 +111,7 @@ func prepareRoutes(r *chi.Mux, options Options) {
r.Use(func(h stdhttp.Handler) stdhttp.Handler {
// authentication and wopi context
return app.WopiContextAuthMiddleware(options.Config.JWTSecret, h)
return colabmiddleware.WopiContextAuthMiddleware(options.Config.JWTSecret, h)
},
)

View File

@@ -18,7 +18,7 @@ import (
"github.com/owncloud/ocis/v2/ocis-pkg/log"
"github.com/owncloud/ocis/v2/services/collaboration/pkg/config"
"github.com/owncloud/ocis/v2/services/collaboration/pkg/internal/app"
"github.com/owncloud/ocis/v2/services/collaboration/pkg/middleware"
)
func NewHandler(opts ...Option) (*Service, func(), error) {
@@ -148,7 +148,7 @@ func (s *Service) OpenInApp(
appURL = editAppURL
}
cryptedReqAccessToken, err := app.EncryptAES([]byte(s.config.JWTSecret), req.AccessToken)
cryptedReqAccessToken, err := middleware.EncryptAES([]byte(s.config.JWTSecret), req.AccessToken)
if err != nil {
s.logger.Error().
Err(err).
@@ -161,7 +161,7 @@ func (s *Service) OpenInApp(
}, err
}
wopiContext := app.WopiContext{
wopiContext := middleware.WopiContext{
AccessToken: cryptedReqAccessToken,
FileReference: providerFileRef,
User: user,
@@ -183,7 +183,7 @@ func (s *Service) OpenInApp(
return nil, err
}
claims := &app.Claims{
claims := &middleware.Claims{
WopiContext: wopiContext,
RegisteredClaims: jwt.RegisteredClaims{
ExpiresAt: cs3Claims.ExpiresAt,