mirror of
https://github.com/router-for-me/CLIProxyAPIPlus.git
synced 2026-04-24 07:40:28 +00:00
feat(auth): add support for managing custom headers in auth files
Closes #2457
This commit is contained in:
68
sdk/cliproxy/auth/custom_headers.go
Normal file
68
sdk/cliproxy/auth/custom_headers.go
Normal file
@@ -0,0 +1,68 @@
|
||||
package auth
|
||||
|
||||
import "strings"
|
||||
|
||||
func ExtractCustomHeadersFromMetadata(metadata map[string]any) map[string]string {
|
||||
if len(metadata) == 0 {
|
||||
return nil
|
||||
}
|
||||
raw, ok := metadata["headers"]
|
||||
if !ok || raw == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
out := make(map[string]string)
|
||||
switch headers := raw.(type) {
|
||||
case map[string]string:
|
||||
for key, value := range headers {
|
||||
name := strings.TrimSpace(key)
|
||||
if name == "" {
|
||||
continue
|
||||
}
|
||||
val := strings.TrimSpace(value)
|
||||
if val == "" {
|
||||
continue
|
||||
}
|
||||
out[name] = val
|
||||
}
|
||||
case map[string]any:
|
||||
for key, value := range headers {
|
||||
name := strings.TrimSpace(key)
|
||||
if name == "" {
|
||||
continue
|
||||
}
|
||||
rawVal, ok := value.(string)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
val := strings.TrimSpace(rawVal)
|
||||
if val == "" {
|
||||
continue
|
||||
}
|
||||
out[name] = val
|
||||
}
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
|
||||
if len(out) == 0 {
|
||||
return nil
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func ApplyCustomHeadersFromMetadata(auth *Auth) {
|
||||
if auth == nil || len(auth.Metadata) == 0 {
|
||||
return
|
||||
}
|
||||
headers := ExtractCustomHeadersFromMetadata(auth.Metadata)
|
||||
if len(headers) == 0 {
|
||||
return
|
||||
}
|
||||
if auth.Attributes == nil {
|
||||
auth.Attributes = make(map[string]string)
|
||||
}
|
||||
for name, value := range headers {
|
||||
auth.Attributes["header:"+name] = value
|
||||
}
|
||||
}
|
||||
50
sdk/cliproxy/auth/custom_headers_test.go
Normal file
50
sdk/cliproxy/auth/custom_headers_test.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestExtractCustomHeadersFromMetadata(t *testing.T) {
|
||||
meta := map[string]any{
|
||||
"headers": map[string]any{
|
||||
" X-Test ": " value ",
|
||||
"": "ignored",
|
||||
"X-Empty": " ",
|
||||
"X-Num": float64(1),
|
||||
},
|
||||
}
|
||||
|
||||
got := ExtractCustomHeadersFromMetadata(meta)
|
||||
want := map[string]string{"X-Test": "value"}
|
||||
if !reflect.DeepEqual(got, want) {
|
||||
t.Fatalf("ExtractCustomHeadersFromMetadata() = %#v, want %#v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestApplyCustomHeadersFromMetadata(t *testing.T) {
|
||||
auth := &Auth{
|
||||
Metadata: map[string]any{
|
||||
"headers": map[string]string{
|
||||
"X-Test": "new",
|
||||
"X-Empty": " ",
|
||||
},
|
||||
},
|
||||
Attributes: map[string]string{
|
||||
"header:X-Test": "old",
|
||||
"keep": "1",
|
||||
},
|
||||
}
|
||||
|
||||
ApplyCustomHeadersFromMetadata(auth)
|
||||
|
||||
if got := auth.Attributes["header:X-Test"]; got != "new" {
|
||||
t.Fatalf("header:X-Test = %q, want %q", got, "new")
|
||||
}
|
||||
if _, ok := auth.Attributes["header:X-Empty"]; ok {
|
||||
t.Fatalf("expected header:X-Empty to be absent, got %#v", auth.Attributes["header:X-Empty"])
|
||||
}
|
||||
if got := auth.Attributes["keep"]; got != "1" {
|
||||
t.Fatalf("keep = %q, want %q", got, "1")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user