From 12cddd458e935e03538d9674f9540ce895a805ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Graber?= Date: Mon, 4 Dec 2023 00:49:01 -0500 Subject: [PATCH] incusd/auth: Add location support in ObjectFromRequest MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Stéphane Graber --- internal/server/auth/authorization_objects.go | 44 ++++++++++++------- 1 file changed, 29 insertions(+), 15 deletions(-) diff --git a/internal/server/auth/authorization_objects.go b/internal/server/auth/authorization_objects.go index 1823be4e0ba..1eafbe14ea4 100644 --- a/internal/server/auth/authorization_objects.go +++ b/internal/server/auth/authorization_objects.go @@ -173,21 +173,6 @@ func ObjectFromRequest(r *http.Request, objectType ObjectType, muxVars ...string return ObjectServer(), nil } - muxValues := make([]string, 0, len(muxVars)) - vars := mux.Vars(r) - for _, muxVar := range muxVars { - muxValue, err := url.PathUnescape(vars[muxVar]) - if err != nil { - return "", fmt.Errorf("Failed to unescape mux var %q for object type %q: %w", muxVar, objectType, err) - } - - if muxValue == "" { - return "", fmt.Errorf("Mux var %q not found for object type %q", muxVar, objectType) - } - - muxValues = append(muxValues, muxValue) - } - values, err := url.ParseQuery(r.URL.RawQuery) if err != nil { return "", err @@ -198,6 +183,35 @@ func ObjectFromRequest(r *http.Request, objectType ObjectType, muxVars ...string projectName = "default" } + location := values.Get("target") + + muxValues := make([]string, 0, len(muxVars)) + vars := mux.Vars(r) + for _, muxVar := range muxVars { + var err error + var muxValue string + + if muxVar == "location" { + // Special handling for the location which is not present as a real mux var. + if location == "" { + continue + } + + muxValue = location + } else { + muxValue, err = url.PathUnescape(vars[muxVar]) + if err != nil { + return "", fmt.Errorf("Failed to unescape mux var %q for object type %q: %w", muxVar, objectType, err) + } + + if muxValue == "" { + return "", fmt.Errorf("Mux var %q not found for object type %q", muxVar, objectType) + } + } + + muxValues = append(muxValues, muxValue) + } + // If using projects API we want to pass in the mux var, not the query parameter. if objectType == ObjectTypeProject && strings.HasPrefix(r.URL.Path, fmt.Sprintf("/%s/projects", version.APIVersion)) { if len(muxValues) == 0 {