namespace.go 1.1 KB

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