Skip to content

Commit

Permalink
feat(responsehandler): Add user into all template data
Browse files Browse the repository at this point in the history
  • Loading branch information
oxyno-zeta committed Jul 3, 2021
1 parent 186ad45 commit d044d8a
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 3 deletions.
3 changes: 3 additions & 0 deletions pkg/s3-proxy/authx/authentication/basic-auth.go
Expand Up @@ -50,6 +50,9 @@ func (s *service) basicAuthMiddleware(res *config.Resource) func(http.Handler) h
// Create new request with new context
r = r.WithContext(ctx)

// Update response handler to have the latest context values
resHan.UpdateRequestAndResponse(r, w)

// Find user credentials
cred := funk.Find(basicAuthUserConfigList, func(cred *config.BasicAuthUserConfig) bool {
return cred.User == username
Expand Down
3 changes: 3 additions & 0 deletions pkg/s3-proxy/authx/authentication/oidc.go
Expand Up @@ -309,6 +309,9 @@ func (s *service) oidcAuthorizationMiddleware(res *config.Resource) func(http.Ha
// Create new request with new context
r = r.WithContext(ctx)

// Update response handler to have the latest context values
resHan.UpdateRequestAndResponse(r, w)

// Check if email is verified or not
if !ouser.EmailVerified {
// Create error
Expand Down
3 changes: 3 additions & 0 deletions pkg/s3-proxy/response-handler/client.go
Expand Up @@ -81,6 +81,9 @@ type ResponseHandler interface {
loadFileContent func(ctx context.Context, path string) (string, error),
err error,
)
// UpdateRequestAndResponse will update request and response in object.
// This will used to update request and response in order to have the latest context values.
UpdateRequestAndResponse(req *http.Request, res http.ResponseWriter)
}

// NewHandler will return a new response handler object.
Expand Down
4 changes: 3 additions & 1 deletion pkg/s3-proxy/response-handler/error-handlers.go
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"net/http"

"github.com/oxyno-zeta/s3-proxy/pkg/s3-proxy/authx/models"
"github.com/oxyno-zeta/s3-proxy/pkg/s3-proxy/config"
"github.com/oxyno-zeta/s3-proxy/pkg/s3-proxy/log"
"github.com/pkg/errors"
Expand Down Expand Up @@ -266,9 +267,9 @@ func (h *handler) handleGenericErrorTemplate(
}

// Create data
// TODO Add user
data := errorData{
Request: h.req,
User: models.GetAuthenticatedUserFromContext(h.req.Context()),
Path: h.req.URL.RequestURI(),
Error: err,
}
Expand Down Expand Up @@ -347,6 +348,7 @@ func (h *handler) InternalServerError(
// Create data
data := errorData{
Request: h.req,
User: models.GetAuthenticatedUserFromContext(h.req.Context()),
Path: h.req.URL.RequestURI(),
Error: err,
}
Expand Down
11 changes: 10 additions & 1 deletion pkg/s3-proxy/response-handler/handler.go
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"
"strings"

"github.com/oxyno-zeta/s3-proxy/pkg/s3-proxy/authx/models"
"github.com/oxyno-zeta/s3-proxy/pkg/s3-proxy/config"
)

Expand All @@ -16,6 +17,13 @@ type handler struct {
targetKey string
}

func (h *handler) UpdateRequestAndResponse(req *http.Request, res http.ResponseWriter) {
// Update request
h.req = req
// Update response
h.res = res
}

func (h *handler) PreconditionFailed() {
h.res.WriteHeader(http.StatusPreconditionFailed)
}
Expand All @@ -42,9 +50,9 @@ func (h *handler) TargetList() {
}

// Create data structure
// TODO Add user
data := targetListData{
Request: h.req,
User: models.GetAuthenticatedUserFromContext(h.req.Context()),
Targets: cfg.Targets,
}

Expand Down Expand Up @@ -125,6 +133,7 @@ func (h *handler) FoldersFilesList(
// Create bucket list data for templating
data := &bucketListingData{
Request: h.req,
User: models.GetAuthenticatedUserFromContext(h.req.Context()),
Entries: entries,
BucketName: targetCfg.Bucket.Name,
Name: targetCfg.Name,
Expand Down
6 changes: 5 additions & 1 deletion pkg/s3-proxy/response-handler/internal-models.go
Expand Up @@ -3,27 +3,31 @@ package responsehandler
import (
"net/http"

"github.com/oxyno-zeta/s3-proxy/pkg/s3-proxy/authx/models"
"github.com/oxyno-zeta/s3-proxy/pkg/s3-proxy/config"
)

// bucketListingData Bucket listing data for templating.
type bucketListingData struct {
Request *http.Request
User models.GenericUser
Entries []*Entry
BucketName string
Name string
Path string // Deprecated
Path string
}

// errorData represents the structure used by error templating.
type errorData struct {
Request *http.Request
User models.GenericUser
Path string
Error error
}

// targetListData represents the structure used by target list templating.
type targetListData struct {
Request *http.Request
User models.GenericUser
Targets map[string]*config.TargetConfig
}

0 comments on commit d044d8a

Please sign in to comment.