Intercept parameters from http redirects (#2111)
This commit is contained in:
parent
7a178b4fc4
commit
ef68405fb8
2 changed files with 34 additions and 13 deletions
28
util/request/intercept.go
Normal file
28
util/request/intercept.go
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
package request
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type InterceptResult = func() (string, error)
|
||||
|
||||
// InterceptRedirect captures a redirect url parameter
|
||||
func InterceptRedirect(param string, stop bool) (func(req *http.Request, via []*http.Request) error, InterceptResult) {
|
||||
var val string
|
||||
return func(req *http.Request, via []*http.Request) error {
|
||||
if val == "" {
|
||||
if val = req.URL.Query().Get(param); val != "" && stop {
|
||||
return http.ErrUseLastResponse
|
||||
}
|
||||
}
|
||||
return nil
|
||||
},
|
||||
func() (string, error) {
|
||||
var err error
|
||||
if val == "" {
|
||||
err = fmt.Errorf("%s not found", param)
|
||||
}
|
||||
return val, err
|
||||
}
|
||||
}
|
||||
|
|
@ -36,7 +36,6 @@ func (v *Identity) Login(user, password string) error {
|
|||
|
||||
var nToken Token
|
||||
var realm string
|
||||
var resp *http.Response
|
||||
var code string
|
||||
|
||||
if err == nil {
|
||||
|
|
@ -95,20 +94,14 @@ func (v *Identity) Login(user, password string) error {
|
|||
})
|
||||
|
||||
if err == nil {
|
||||
v.CheckRedirect = func(req *http.Request, via []*http.Request) error { return http.ErrUseLastResponse }
|
||||
resp, err = v.Do(req)
|
||||
v.CheckRedirect = nil
|
||||
var param request.InterceptResult
|
||||
v.Client.CheckRedirect, param = request.InterceptRedirect("code", true)
|
||||
|
||||
if err == nil {
|
||||
resp.Body.Close()
|
||||
|
||||
var location *url.URL
|
||||
if location, err = url.Parse(resp.Header.Get("Location")); err == nil {
|
||||
if code = location.Query().Get("code"); code == "" {
|
||||
err = fmt.Errorf("missing auth code: %v", location)
|
||||
}
|
||||
}
|
||||
if _, err = v.Do(req); err == nil {
|
||||
code, err = param()
|
||||
}
|
||||
|
||||
v.CheckRedirect = nil
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue