4
4
"bytes"
5
5
"encoding/json"
6
6
"io"
7
- "io/ioutil"
8
7
"net/http"
9
8
"net/url"
10
9
@@ -67,14 +66,6 @@ func (a *AuthenticatorRemoteJSON) Config(config json.RawMessage) (*Authenticator
67
66
return nil , NewErrAuthenticatorMisconfigured (a , err )
68
67
}
69
68
70
- if len (c .ExtraFrom ) == 0 {
71
- c .ExtraFrom = "extra"
72
- }
73
-
74
- if len (c .SubjectFrom ) == 0 {
75
- c .SubjectFrom = "subject"
76
- }
77
-
78
69
return & c , nil
79
70
}
80
71
@@ -100,11 +91,19 @@ func (a *AuthenticatorRemoteJSON) Authenticate(r *http.Request, session *Authent
100
91
)
101
92
102
93
if err = json .Unmarshal (subjectRaw , & subject ); err != nil {
103
- return helper .ErrForbidden .WithReasonf ("The configured subject_from GJSON path returned an error on JSON output: %s" , err .Error ()).WithDebugf ("GJSON path: %s\n Body: %s\n Result: %s" , cfg .SubjectFrom , body , subjectRaw ).WithTrace (err )
94
+ return helper .
95
+ ErrForbidden .
96
+ WithReasonf ("The configured subject_from GJSON path returned an error on JSON output: %s" , err .Error ()).
97
+ WithDebugf ("GJSON path: %s\n Body: %s\n Result: %s" , cfg .SubjectFrom , body , subjectRaw ).
98
+ WithTrace (err )
104
99
}
105
100
106
101
if err = json .Unmarshal (extraRaw , & extra ); err != nil {
107
- return helper .ErrForbidden .WithReasonf ("The configured extra_from GJSON path returned an error on JSON output: %s" , err .Error ()).WithDebugf ("GJSON path: %s\n Body: %s\n Result: %s" , cfg .ExtraFrom , body , extraRaw ).WithTrace (err )
102
+ return helper .
103
+ ErrForbidden .
104
+ WithReasonf ("The configured extra_from GJSON path returned an error on JSON output: %s" , err .Error ()).
105
+ WithDebugf ("GJSON path: %s\n Body: %s\n Result: %s" , cfg .ExtraFrom , body , extraRaw ).
106
+ WithTrace (err )
108
107
}
109
108
110
109
session .Subject = subject
@@ -127,7 +126,10 @@ func forwardMethod(r *http.Request, cfg *AuthenticatorRemoteJSONConfiguration) s
127
126
func forwardRequestToAuthenticator (r * http.Request , method string , serviceURL string , preservePath bool ) (json.RawMessage , error ) {
128
127
reqUrl , err := url .Parse (serviceURL )
129
128
if err != nil {
130
- return nil , errors .WithStack (herodot .ErrInternalServerError .WithReasonf ("Unable to parse remote URL: %s" , err ))
129
+ return nil , errors .WithStack (
130
+ herodot .
131
+ ErrInternalServerError .WithReasonf ("Unable to parse remote URL: %s" , err ),
132
+ )
131
133
}
132
134
133
135
if ! preservePath {
@@ -136,15 +138,24 @@ func forwardRequestToAuthenticator(r *http.Request, method string, serviceURL st
136
138
137
139
var forwardRequestBody io.ReadCloser = nil
138
140
if r .Body != nil {
139
- body , err := ioutil .ReadAll (r .Body )
141
+ body , err := io .ReadAll (r .Body )
140
142
if err != nil {
141
143
return nil , helper .ErrBadRequest .WithReason (err .Error ()).WithTrace (err )
142
144
}
143
145
146
+ err = r .Body .Close ()
147
+ if err != nil {
148
+ return nil , errors .WithStack (
149
+ herodot .
150
+ ErrInternalServerError .
151
+ WithReasonf ("Could not close body reader: %s\n " , err ),
152
+ )
153
+ }
154
+
144
155
// Unfortunately the body reader needs to be read once to forward the request,
145
156
// thus the upstream request will fail miserably without recreating a fresh ReaderCloser
146
- forwardRequestBody = ioutil .NopCloser (bytes .NewReader (body ))
147
- r .Body = ioutil .NopCloser (bytes .NewReader (body ))
157
+ forwardRequestBody = io .NopCloser (bytes .NewReader (body ))
158
+ r .Body = io .NopCloser (bytes .NewReader (body ))
148
159
}
149
160
150
161
req := http.Request {
@@ -163,7 +174,7 @@ func forwardRequestToAuthenticator(r *http.Request, method string, serviceURL st
163
174
164
175
func handleResponse (r * http.Response ) (json.RawMessage , error ) {
165
176
if r .StatusCode == http .StatusOK {
166
- body , err := ioutil .ReadAll (r .Body )
177
+ body , err := io .ReadAll (r .Body )
167
178
if err != nil {
168
179
return json.RawMessage {}, errors .WithStack (herodot .ErrInternalServerError .WithReasonf ("Remote server returned error: %+v" , err ))
169
180
}
0 commit comments