Skip to content

Commit 3901e94

Browse files
authored
Merge pull request #54 from delta10/feat/add-username-to-filter-proxy
feat: add username to possible auth header
2 parents 8436db1 + ef51441 commit 3901e94

2 files changed

Lines changed: 17 additions & 10 deletions

File tree

cmd/filter-proxy/main.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ type ClaimsWithGroups struct {
3333
type AuthorizationResponse struct {
3434
Result bool `json:"result"`
3535
ResponseFilter string `json:"response_filter"`
36+
Username string `json:"username"`
3637
}
3738

3839
func main() {
@@ -69,7 +70,7 @@ func main() {
6970
r.URL.Scheme = backendBaseUrl.Scheme
7071

7172
for headerKey, headerValue := range backend.Auth.Header {
72-
parsedHeaderValue := utils.EnvSubst(headerValue)
73+
parsedHeaderValue := utils.EnvSubst(headerValue, nil)
7374
r.Header.Set(headerKey, parsedHeaderValue)
7475
}
7576

@@ -246,12 +247,14 @@ func main() {
246247
transport := &http.Transport{TLSClientConfig: tlsConfig}
247248

248249
if backend.Auth.Basic.Username != "" && backend.Auth.Basic.Password != "" {
249-
parsedPassword := utils.EnvSubst(backend.Auth.Basic.Password)
250+
parsedPassword := utils.EnvSubst(backend.Auth.Basic.Password, nil)
250251
backendRequest.SetBasicAuth(backend.Auth.Basic.Username, parsedPassword)
251252
}
252253

253254
for headerKey, headerValue := range backend.Auth.Header {
254-
parsedHeaderValue := utils.EnvSubst(headerValue)
255+
parsedHeaderValue := utils.EnvSubst(headerValue, map[string]string{
256+
"REQUEST_USERNAME": authorizationResponse.Username,
257+
})
255258
backendRequest.Header.Set(headerKey, parsedHeaderValue)
256259
}
257260

internal/utils/utils.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ package utils
22

33
import (
44
"encoding/base64"
5-
"github.com/delta10/filter-proxy/internal/wfs"
65
"net"
76
"net/http"
87
"net/url"
98
"os"
109
"regexp"
1110
"strings"
11+
12+
"github.com/delta10/filter-proxy/internal/wfs"
1213
)
1314

1415
func QueryParamsToLower(queryParams url.Values) url.Values {
@@ -70,19 +71,22 @@ func DelHopHeaders(header http.Header) {
7071
}
7172
}
7273

73-
func EnvSubst(input string) string {
74+
func EnvSubst(input string, additionalReplacements map[string]string) string {
7475
re := regexp.MustCompile(`\${([^}]+)}`)
7576

76-
result := re.ReplaceAllStringFunc(input, func(match string) string {
77+
return re.ReplaceAllStringFunc(input, func(match string) string {
7778
varName := match[2 : len(match)-1]
79+
if additionalReplacements != nil {
80+
if value, exists := additionalReplacements[varName]; exists {
81+
return value
82+
}
83+
}
7884
if value, exists := os.LookupEnv(varName); exists {
7985
return value
8086
}
81-
82-
return ""
87+
// Leave the original pattern if no replacement found
88+
return match
8389
})
84-
85-
return result
8690
}
8791

8892
func ReadUserIP(r *http.Request) string {

0 commit comments

Comments
 (0)