Middlewares: add forwardAuth.authResponseHeadersRegex
This commit is contained in:
parent
b5198e63c4
commit
49cdb67ddc
12 changed files with 116 additions and 24 deletions
|
@ -6,6 +6,7 @@ import (
|
|||
"io/ioutil"
|
||||
"net"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
|
@ -25,13 +26,14 @@ const (
|
|||
)
|
||||
|
||||
type forwardAuth struct {
|
||||
address string
|
||||
authResponseHeaders []string
|
||||
next http.Handler
|
||||
name string
|
||||
client http.Client
|
||||
trustForwardHeader bool
|
||||
authRequestHeaders []string
|
||||
address string
|
||||
authResponseHeaders []string
|
||||
authResponseHeadersRegex *regexp.Regexp
|
||||
next http.Handler
|
||||
name string
|
||||
client http.Client
|
||||
trustForwardHeader bool
|
||||
authRequestHeaders []string
|
||||
}
|
||||
|
||||
// NewForward creates a forward auth middleware.
|
||||
|
@ -66,6 +68,14 @@ func NewForward(ctx context.Context, next http.Handler, config dynamic.ForwardAu
|
|||
fa.client.Transport = tr
|
||||
}
|
||||
|
||||
if config.AuthResponseHeadersRegex != "" {
|
||||
re, err := regexp.Compile(config.AuthResponseHeadersRegex)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error compiling regular expression %s: %w", config.AuthResponseHeadersRegex, err)
|
||||
}
|
||||
fa.authResponseHeadersRegex = re
|
||||
}
|
||||
|
||||
return fa, nil
|
||||
}
|
||||
|
||||
|
@ -156,6 +166,20 @@ func (fa *forwardAuth) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
|||
}
|
||||
}
|
||||
|
||||
if fa.authResponseHeadersRegex != nil {
|
||||
for headerKey := range req.Header {
|
||||
if fa.authResponseHeadersRegex.MatchString(headerKey) {
|
||||
req.Header.Del(headerKey)
|
||||
}
|
||||
}
|
||||
|
||||
for headerKey, headerValues := range forwardResponse.Header {
|
||||
if fa.authResponseHeadersRegex.MatchString(headerKey) {
|
||||
req.Header[headerKey] = append([]string(nil), headerValues...)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
req.RequestURI = req.URL.RequestURI()
|
||||
fa.next.ServeHTTP(rw, req)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue