@@ -21,6 +21,7 @@ type Config struct {
2121 PayloadHeaders map [string ]string `json:"payloadHeaders,omitempty"`
2222 AuthQueryParam string `json:"authQueryParam,omitempty"`
2323 AuthCookieName string `json:"authCookieName,omitempty"`
24+ ForwardAuth bool `json:"forwardAuth,omitempty"`
2425}
2526
2627func CreateConfig () * Config {
@@ -29,6 +30,7 @@ func CreateConfig() *Config {
2930 Optional : false ,
3031 AuthQueryParam : "authToken" ,
3132 AuthCookieName : "authToken" ,
33+ ForwardAuth : false ,
3234 }
3335}
3436
@@ -40,6 +42,7 @@ type JWT struct {
4042 payloadHeaders map [string ]string
4143 authQueryParam string
4244 authCookieName string
45+ forwardAuth bool
4346}
4447
4548type Token struct {
@@ -57,6 +60,7 @@ func New(ctx context.Context, next http.Handler, config *Config, name string) (h
5760 payloadHeaders : config .PayloadHeaders ,
5861 authQueryParam : config .AuthQueryParam ,
5962 authCookieName : config .AuthCookieName ,
63+ forwardAuth : config .ForwardAuth ,
6064 }, nil
6165}
6266
@@ -159,12 +163,28 @@ func (j *JWT) extractTokenFromCookie(request *http.Request) string {
159163 if err != nil {
160164 return ""
161165 }
166+ if ! j .forwardAuth {
167+ cookies := request .Cookies ()
168+ request .Header .Del ("Cookie" )
169+ for _ , c := range cookies {
170+ if c .Name != j .authCookieName {
171+ request .AddCookie (c )
172+ }
173+ }
174+ }
162175 return cookie .Value
163176}
164177
165178func (j * JWT ) extractTokenFromQuery (request * http.Request ) string {
166179 if request .URL .Query ().Has (j .authQueryParam ) {
167- return request .URL .Query ().Get (j .authQueryParam )
180+ token := request .URL .Query ().Get (j .authQueryParam )
181+ if ! j .forwardAuth {
182+ qry := request .URL .Query ()
183+ qry .Del (j .authQueryParam )
184+ request .URL .RawQuery = qry .Encode ()
185+ request .RequestURI = request .URL .RequestURI ()
186+ }
187+ return token
168188 }
169189 return ""
170190}
@@ -178,5 +198,9 @@ func (j *JWT) extractTokenFromHeader(request *http.Request) string {
178198 if ! strings .HasPrefix (auth , "Bearer " ) {
179199 return ""
180200 }
201+
202+ if ! j .forwardAuth {
203+ request .Header .Del ("Authorization" )
204+ }
181205 return auth [7 :]
182206}
0 commit comments