Signed-off-by: Bryan Frimin <bryan@probo.com>
This commit is contained in:
Bryan Frimin
2026-05-23 12:13:43 -07:00
parent 7405565699
commit de325af4d9

View File

@@ -20,8 +20,6 @@ import (
"fmt" "fmt"
"maps" "maps"
"net/http" "net/http"
"slices"
"strings"
"github.com/vikstrous/dataloadgen" "github.com/vikstrous/dataloadgen"
"go.probo.inc/probo/pkg/cookiebanner" "go.probo.inc/probo/pkg/cookiebanner"
@@ -440,39 +438,17 @@ func (f *batchFetcher) fetchAuthorizesIndividually(
return result, nil return result, nil
} }
// EncodeAuthorizeKeyAttributes returns a canonical (sorted-key) JSON
// encoding of attrs for use as AuthorizeKey.ResourceAttributes. Maps that
// compare equal produce identical strings; nil or empty attrs encode to "".
func EncodeAuthorizeKeyAttributes(attrs policy.Attributes) string { func EncodeAuthorizeKeyAttributes(attrs policy.Attributes) string {
if len(attrs) == 0 { if len(attrs) == 0 {
return "" return ""
} }
keys := slices.Sorted(maps.Keys(attrs)) b, _ := json.Marshal(attrs)
var sb strings.Builder return string(b)
sb.WriteByte('{')
for i, k := range keys {
if i > 0 {
sb.WriteByte(',')
}
kb, _ := json.Marshal(k)
sb.Write(kb)
sb.WriteByte(':')
vb, _ := json.Marshal(attrs[k])
sb.Write(vb)
}
sb.WriteByte('}')
return sb.String()
} }
func decodeAuthorizeKeyAttributes(s string) (policy.Attributes, error) { func decodeAuthorizeKeyAttributes(s string) (policy.Attributes, error) {
if s == "" {
return nil, nil
}
attrs := policy.Attributes{} attrs := policy.Attributes{}
if err := json.Unmarshal([]byte(s), &attrs); err != nil { if err := json.Unmarshal([]byte(s), &attrs); err != nil {
return nil, err return nil, err