@@ -98,64 +98,23 @@ func (h *HttpProxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
9898 return
9999 }
100100
101- //request targets redirectURI, attempt to parse state and redirect to original URL
102- if u .Host == r .Host {
103- h .recoverIncomingState (w , r , dst )
104- return
105- }
106-
107101 //request targets service, check if response has a redirect uri and state and attempt to change it to the proxy redirectURI
108102 if dst .Host == r .Host {
109103 h .changeRedirectURI (w , r , dst )
110104 return
111105 }
106+
107+ //request targets redirectURI, attempt to parse state and redirect to original URL
108+ if u .Host == r .Host {
109+ h .recoverIncomingState (w , r , dst )
110+ return
111+ }
112112 }
113113
114114 // We don't have any matching OAUTH2Proxy resources matching the host
115115 w .WriteHeader (http .StatusServiceUnavailable )
116116}
117117
118- // recoverIncomingState attempts to parse the incoming state (if there is any) and redirect the request back to the original redirect_uri
119- func (h * HttpProxy ) recoverIncomingState (w http.ResponseWriter , r * http.Request , dst * OAUTH2Proxy ) error {
120- vals := r .URL .Query ()
121- str := vals .Get ("state" )
122- state := & state {}
123-
124- h .log .Info ("request matches redirectURL, attempt to recover state" , "host" , r .Host , "state" , str )
125-
126- err := json .Unmarshal ([]byte (str ), state )
127- if err != nil {
128- h .log .Info ("contains undecodable state" , "request" , r .RequestURI , "host" , dst .Host , "err" , err )
129- w .WriteHeader (http .StatusBadRequest )
130- return err
131- }
132-
133- u , err := url .Parse (state .OrigRedirectURI )
134- if err != nil {
135- h .log .Info ("could not decode original redirect uri" , "request" , r .RequestURI , "host" , dst .Host , "origRedirectURI" , state .OrigRedirectURI , "err" , err )
136- w .WriteHeader (http .StatusBadRequest )
137- return err
138- }
139-
140- r .URL .Path = u .Path
141- r .URL .Host = u .Host
142-
143- if state .OrigState != "" {
144- vals .Set ("state" , state .OrigState )
145- } else {
146- vals .Del ("state" )
147- }
148-
149- r .URL .RawQuery = vals .Encode ()
150-
151- h .log .Info ("recovered original state and modified path" , "url" , r .URL .String (), "host" , dst .Host , "path" , u .Path , "state" , state .OrigState )
152-
153- w .Header ().Set ("Location" , r .URL .String ())
154- w .WriteHeader (http .StatusSeeOther )
155-
156- return nil
157- }
158-
159118// proxy request to target
160119// if the request matches a path and the response contains a location header, the proxy
161120// attempts to change the redirect_url in the location uri to the configured proxy target
@@ -237,3 +196,44 @@ func matchPath(p string, list []string) bool {
237196
238197 return false
239198}
199+
200+ // recoverIncomingState attempts to parse the incoming state (if there is any) and redirect the request back to the original redirect_uri
201+ func (h * HttpProxy ) recoverIncomingState (w http.ResponseWriter , r * http.Request , dst * OAUTH2Proxy ) error {
202+ vals := r .URL .Query ()
203+ str := vals .Get ("state" )
204+ state := & state {}
205+
206+ h .log .Info ("request matches redirectURL, attempt to recover state" , "host" , r .Host , "state" , str )
207+
208+ err := json .Unmarshal ([]byte (str ), state )
209+ if err != nil {
210+ h .log .Info ("contains undecodable state" , "request" , r .RequestURI , "host" , dst .Host , "err" , err )
211+ w .WriteHeader (http .StatusBadRequest )
212+ return err
213+ }
214+
215+ u , err := url .Parse (state .OrigRedirectURI )
216+ if err != nil {
217+ h .log .Info ("could not decode original redirect uri" , "request" , r .RequestURI , "host" , dst .Host , "origRedirectURI" , state .OrigRedirectURI , "err" , err )
218+ w .WriteHeader (http .StatusBadRequest )
219+ return err
220+ }
221+
222+ r .URL .Path = u .Path
223+ r .URL .Host = u .Host
224+
225+ if state .OrigState != "" {
226+ vals .Set ("state" , state .OrigState )
227+ } else {
228+ vals .Del ("state" )
229+ }
230+
231+ r .URL .RawQuery = vals .Encode ()
232+
233+ h .log .Info ("recovered original state and modified path" , "url" , r .URL .String (), "host" , dst .Host , "path" , u .Path , "state" , state .OrigState )
234+
235+ w .Header ().Set ("Location" , r .URL .String ())
236+ w .WriteHeader (http .StatusSeeOther )
237+
238+ return nil
239+ }
0 commit comments