-
Notifications
You must be signed in to change notification settings - Fork 160
/
auth_request_adjustment.go
49 lines (40 loc) · 1.04 KB
/
auth_request_adjustment.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package director
import (
"encoding/base64"
"fmt"
"net/http"
)
type RedirectFunc func(*http.Request, []*http.Request) error
type AuthRequestAdjustment struct {
authFunc func(bool) (string, error)
username string
password string
}
func NewAuthRequestAdjustment(
authFunc func(bool) (string, error),
username string,
password string,
) AuthRequestAdjustment {
return AuthRequestAdjustment{
authFunc: authFunc,
username: username,
password: password,
}
}
func (a AuthRequestAdjustment) NeedsReadjustment(resp *http.Response) bool {
return resp.StatusCode == 401
}
func (a AuthRequestAdjustment) Adjust(req *http.Request, retried bool) error {
if len(a.username) > 0 {
data := []byte(fmt.Sprintf("%s:%s", a.username, a.password))
encodedBasicAuth := base64.StdEncoding.EncodeToString(data)
req.Header.Set("Authorization", fmt.Sprintf("Basic %s", encodedBasicAuth))
} else if a.authFunc != nil {
authHeader, err := a.authFunc(retried)
if err != nil {
return err
}
req.Header.Set("Authorization", authHeader)
}
return nil
}