forked from xinsnake/go-http-digest-auth-client
-
Notifications
You must be signed in to change notification settings - Fork 3
/
digest_auth_client.go
159 lines (137 loc) · 3.21 KB
/
digest_auth_client.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package digest_auth_client
import (
"bytes"
"errors"
"io"
"io/ioutil"
"net/http"
"sync"
)
type DigestTransport struct {
password string
username string
auth *authorization
authmux sync.Mutex
transport http.RoundTripper
}
const (
READ_LIMIT int64 = 128 * 1024
)
var (
AuthRetryNeeded = errors.New("retry request with authentication")
)
// NewRequest creates a new DigestTransport object
func NewDigestTransport(username, password string, transport http.RoundTripper) *DigestTransport {
return &DigestTransport{
username: username,
password: password,
transport: transport,
}
}
// Execute initialise the request and get a response
func (dt *DigestTransport) RoundTrip(req *http.Request) (resp *http.Response, err error) {
reqCopy := req.Clone(req.Context())
if req.Body != nil {
defer req.Body.Close()
}
var bodyRead *bytes.Buffer
var bodyLeft io.Reader
if req.Body != nil && req.GetBody == nil {
bodyRead = new(bytes.Buffer)
bodyLeft = io.TeeReader(req.Body, bodyRead)
reqCopy.Body = io.NopCloser(bodyLeft)
}
// fire first request
if resp, err = dt.tryReq(reqCopy); err != nil {
if err == AuthRetryNeeded {
cleanupBody(resp.Body)
// rearm request body and retry with new auth
if req.Body != nil {
if req.GetBody == nil {
reqCopy.Body = io.NopCloser(io.MultiReader(bodyRead, bodyLeft))
} else {
newBody, err := req.GetBody()
if err != nil {
return nil, err
}
reqCopy.Body = newBody
}
}
resp, err = dt.tryReq(reqCopy)
if err == AuthRetryNeeded {
return resp, nil
}
return resp, err
} else {
return nil, err
}
} else {
return resp, nil
}
}
func (dt *DigestTransport) tryReq(req *http.Request) (*http.Response, error) {
var (
auth *authorization
wa *wwwAuthenticate
waString string
err error
resp *http.Response
)
dt.authmux.Lock()
auth = dt.auth
if auth != nil {
// Having existing auth
auth, err = dt.auth.refreshAuthorization(req)
if err != nil {
dt.authmux.Unlock()
return nil, err
}
dt.auth = auth
dt.authmux.Unlock()
resp, err = dt.executeAuthorizedRequest(req, auth.toString())
if err != nil {
return nil, err
}
} else {
dt.authmux.Unlock()
// Never seen auth challenge from server
resp, err = dt.transport.RoundTrip(req)
if err != nil {
return nil, err
}
}
if resp.StatusCode != 401 {
return resp, nil
}
if waString = resp.Header.Get("WWW-Authenticate"); waString == "" {
return resp, err
}
wa, err = newWwwAuthenticate(waString)
if err != nil {
return nil, err
}
if wa.Type != "Digest" {
return resp, nil
}
auth, err = newAuthorization(wa, dt.username, dt.password, req)
if err != nil {
return nil, err
}
dt.authmux.Lock()
dt.auth = auth
dt.authmux.Unlock()
return resp, AuthRetryNeeded
}
func (dt *DigestTransport) executeAuthorizedRequest(req *http.Request, authString string) (resp *http.Response, err error) {
req.Header.Set("Authorization", authString)
return dt.transport.RoundTrip(req)
}
// Does cleanup of HTTP response in order to make it reusable by keep-alive
// logic of HTTP transport
func cleanupBody(body io.ReadCloser) {
io.Copy(ioutil.Discard, &io.LimitedReader{
R: body,
N: READ_LIMIT,
})
body.Close()
}