From 57466b3db244313cc10b806f242045c5bfba3c3e Mon Sep 17 00:00:00 2001 From: Pavel Korotkov Date: Wed, 22 Jul 2020 19:02:35 +0300 Subject: [PATCH] Add getting/setting bearer token at the auth package --- auth/bearer-token.go | 24 ++++++++++++++ cmd/gate/app-new-auth.go | 4 +-- http/http.go | 58 ---------------------------------- neofs/layer/neofs-container.go | 5 ++- 4 files changed, 27 insertions(+), 64 deletions(-) create mode 100644 auth/bearer-token.go delete mode 100644 http/http.go diff --git a/auth/bearer-token.go b/auth/bearer-token.go new file mode 100644 index 00000000..3694ca09 --- /dev/null +++ b/auth/bearer-token.go @@ -0,0 +1,24 @@ +package auth + +import ( + "context" + + "github.com/nspcc-dev/neofs-api-go/service" +) + +type contextKey string + +const bearerTokenContextKey contextKey = "bearer-token" + +// GetBearerToken returns a bearer token embedded into a context. +func GetBearerToken(ctx context.Context) *service.BearerTokenMsg { + if bt := ctx.Value(bearerTokenContextKey); bt != nil { + return bt.(*service.BearerTokenMsg) + } + return nil +} + +// SetBearerToken return a context with embedded bearer token. +func SetBearerToken(ctx context.Context, bearerToken *service.BearerTokenMsg) context.Context { + return context.WithValue(ctx, bearerTokenContextKey, bearerToken) +} diff --git a/cmd/gate/app-new-auth.go b/cmd/gate/app-new-auth.go index 9dd6aa93..9ab33394 100644 --- a/cmd/gate/app-new-auth.go +++ b/cmd/gate/app-new-auth.go @@ -1,12 +1,10 @@ package main import ( - "context" "net/http" "github.com/gorilla/mux" "github.com/minio/minio/auth" - s3http "github.com/minio/minio/http" "go.uber.org/zap" ) @@ -18,7 +16,7 @@ func attachNewUserAuth(router *mux.Router, center *auth.Center, log *zap.Logger) log.Error("failed to pass authentication", zap.Error(err)) // TODO: Handle any auth error by rejecting request. } - h.ServeHTTP(w, r.WithContext(context.WithValue(r.Context(), s3http.BearerTokenContextKey, bearerToken))) + h.ServeHTTP(w, r.WithContext(auth.SetBearerToken(r.Context(), bearerToken))) }) } diff --git a/http/http.go b/http/http.go deleted file mode 100644 index 9d1b3a7a..00000000 --- a/http/http.go +++ /dev/null @@ -1,58 +0,0 @@ -package http - -import ( - "fmt" - "net/http" - "strconv" - - "github.com/minio/minio/legacy/crypto" -) - -type ( - HTTPResponseWriter struct { - serverName string - serverRegion string - } - - // MimeType represents various MIME type used API responses. - MimeType string - - // ContextKey is a custom type used to pass values within contexts. - ContextKey string -) - -const BearerTokenContextKey ContextKey = "bearer-token" - -const ( - // MimeType_None means no response type. - MimeType_None MimeType = "" - // MimeType_ApplicationJSON means response type is JSON. - MimeType_ApplicationJSON MimeType = "application/json" - // MimeType_ApplicationXML means response type is XML. - MimeType_ApplicationXML MimeType = "application/xml" -) - -func NewHTTPResponseWriter(appName, appVersion, region string) *HTTPResponseWriter { - return &HTTPResponseWriter{ - serverName: fmt.Sprintf("%s/%s", appName, appVersion), - serverRegion: region, - } -} - -func (rw *HTTPResponseWriter) writeResponse(w http.ResponseWriter, statusCode int, response []byte, mimeType MimeType) { - w.Header().Set("Server", rw.serverName) - if len(rw.serverRegion) > 0 { - w.Header().Set("X-Amz-Bucket-Region", rw.serverRegion) - } - w.Header().Set("Accept-Ranges", "bytes") - crypto.RemoveSensitiveHeaders(w.Header()) - if mimeType != MimeType_None { - w.Header().Set("Content-Type", string(mimeType)) - } - w.Header().Set("Content-Length", strconv.Itoa(len(response))) - w.WriteHeader(statusCode) - if response != nil { - w.Write(response) - w.(http.Flusher).Flush() - } -} diff --git a/neofs/layer/neofs-container.go b/neofs/layer/neofs-container.go index 2f15e5e1..6d385481 100644 --- a/neofs/layer/neofs-container.go +++ b/neofs/layer/neofs-container.go @@ -6,7 +6,7 @@ import ( "go.uber.org/zap" - s3http "github.com/minio/minio/http" + "github.com/minio/minio/auth" "github.com/nspcc-dev/neofs-api-go/container" "github.com/nspcc-dev/neofs-api-go/refs" "github.com/nspcc-dev/neofs-api-go/service" @@ -17,8 +17,7 @@ func (n *neofsObject) containerList(ctx context.Context) ([]refs.CID, error) { req.OwnerID = n.owner req.SetTTL(service.SingleForwardingTTL) req.SetVersion(APIVersion) - bearerToken := ctx.Value(s3http.BearerTokenContextKey).(*service.BearerTokenMsg) - req.SetBearer(bearerToken) + req.SetBearer(auth.GetBearerToken(ctx)) err := service.SignRequestData(n.key, req) if err != nil {