namespace.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package authz
  2. import (
  3. "context"
  4. "net/http"
  5. "github.com/porter-dev/porter/api/server/shared/config"
  6. "github.com/porter-dev/porter/api/types"
  7. )
  8. type NamespaceScopedFactory struct {
  9. config *config.Config
  10. }
  11. func NewNamespaceScopedFactory(
  12. config *config.Config,
  13. ) *NamespaceScopedFactory {
  14. return &NamespaceScopedFactory{config}
  15. }
  16. func (p *NamespaceScopedFactory) Middleware(next http.Handler) http.Handler {
  17. return &NamespaceScopedMiddleware{next, p.config}
  18. }
  19. type NamespaceScopedMiddleware struct {
  20. next http.Handler
  21. config *config.Config
  22. }
  23. func (n *NamespaceScopedMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  24. // get the namespace from the URL param context
  25. reqScopes, _ := r.Context().Value(types.RequestScopeCtxKey).(map[types.PermissionScope]*types.RequestAction)
  26. namespace := reqScopes[types.NamespaceScope].Resource.Name
  27. ctx := NewNamespaceContext(r.Context(), namespace)
  28. r = r.WithContext(ctx)
  29. n.next.ServeHTTP(w, r)
  30. }
  31. func NewNamespaceContext(ctx context.Context, namespace string) context.Context {
  32. return context.WithValue(ctx, types.NamespaceScope, namespace)
  33. }