Skip to content

Commit

Permalink
incusd/auth: Add location support in ObjectFromRequest
Browse files Browse the repository at this point in the history
Signed-off-by: Stéphane Graber <stgraber@stgraber.org>
  • Loading branch information
stgraber committed Dec 4, 2023
1 parent da2bbe6 commit 36fdbf6
Showing 1 changed file with 29 additions and 15 deletions.
44 changes: 29 additions & 15 deletions internal/server/auth/authorization_objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down

0 comments on commit 36fdbf6

Please sign in to comment.