-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.go
239 lines (203 loc) · 6.23 KB
/
session.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package httpclient
import (
"crypto/rand"
"encoding/base64"
"fmt"
"io"
"net/url"
"sync"
)
type Session interface {
// Login returns a username and password for a specified uri
// and relam, or an error. If no authentication credentials
// could be found, NoCredentialsErr should be returned.
Login(uri *url.URL, realm string) (username, password string, err error)
// CNonce returns a random nonce for use in Digest authentication.
CNonce() (cnonce string, err error)
// Counter returns a hexidecimal string indicating the number
// of times that the specified nonce has been passed to
// Counter.
Counter(nonce string) string
// SetAuthorization caches the Authenticate header value for
// the specified uri and domains.
SetAuthorization(uri *url.URL, domain []string, auth string)
// Authorization returns the Authenticate header value cached
// for the specified uri
Authorization(uri *url.URL) (auth string)
// SetDigestCredentials caches the specified credentials hash
// string for the specified uri host and domains. If domain
// is an empty array, then the domain "/" is assumed.
SetDigestCredentials(uri *url.URL, domain []string, hash string)
// DigestCredentials returns the cached credentials hash
// string for the specified uri host.
DigestCredentials(uri *url.URL) (hash string)
// SetDigestSession caches the specified session hash string
// for the specified server
SetDigestSession(server, hash string)
// DigestSession returns the cached session hash string for
// the specified server
DigestSession(server string) (hash string)
// Duplicate creates n clones of rc. The returned io.ReadCloser
// must be closed by the caller. The original rc will always
// be closed when the function returns.
Duplicate(rc io.ReadCloser, n int) (clone []io.ReadCloser, err error)
// NewProxyReadCloser returns an implementation of ProxyReadCloser,
// useful for processing a request Body without losing the
// ability to then send the Body in a subsequent request.
NewProxyReadCloser() ProxyReadCloser
}
type session struct {
sync.RWMutex
credentials Credentials
authcache *AuthCache
md5cred map[string]string
md5sess map[string]string
counter *NonceCounter
rcDir string
rcLimit int
}
// NewSession returns an implementation of Session. The provided
// credentials will be used to return login usernames and passwords.
// nonceCap sets the limit on the number nonce values cached by
// the nonce Counter. dir specifies the temporary directory to use
// when cloning an io.Reader via NewProxyReadCloser, and limit indicates
// the in-memory limit for cloning data, after which the clone will
// be written to a temporary file in dir. If dir is the empty string,
// the OS default temporary directory will be used.
func NewSession(credentials Credentials, nonceCap int, dir string, limit int) Session {
return &session{
credentials: credentials,
authcache: NewAuthCache(),
md5cred: make(map[string]string),
md5sess: make(map[string]string),
counter: NewNonceCounter(nonceCap),
rcDir: dir,
rcLimit: limit,
}
}
func (session *session) Login(uri *url.URL, realm string) (username, password string, err error) {
return session.credentials.Login(uri, realm)
}
func (session *session) CNonce() (cnonce string, err error) {
buf := make([]byte, 12)
_, err = rand.Read(buf)
if err != nil {
return
}
cnonce = base64.StdEncoding.EncodeToString(buf)
return
}
func (session *session) Counter(nonce string) string {
session.Lock()
n := session.counter.Next(nonce)
session.Unlock()
return fmt.Sprintf("%08x", n)
}
func (session *session) SetAuthorization(uri *url.URL, domain []string, auth string) {
session.Lock()
defer session.Unlock()
if domain == nil || len(domain) == 0 {
root := &url.URL{
Scheme: uri.Scheme,
Opaque: uri.Opaque,
User: uri.User,
Host: uri.Host,
Path: "/",
RawQuery: "",
Fragment: "",
}
session.authcache.Set(root, auth)
return
}
for _, s := range domain {
ref, err := url.Parse(s)
if err == nil {
session.authcache.Set(uri.ResolveReference(ref), auth)
}
}
}
func (session *session) Authorization(uri *url.URL) (auth string) {
session.RLock()
defer session.RUnlock()
return session.authcache.Get(uri)
}
func (session *session) SetDigestCredentials(uri *url.URL, domain []string, hash string) {
if len(domain) == 0 {
domain = append(domain, "/")
}
spaces := make([]string, len(domain))
for _, s := range domain {
ref, err := url.Parse(s)
if err == nil {
abs := uri.ResolveReference(ref)
spaces = append(spaces, abs.Host+":"+abs.Path)
}
}
session.Lock()
defer session.Unlock()
for _, space := range spaces {
session.md5cred[space] = hash
}
}
func (session *session) DigestCredentials(uri *url.URL) (hash string) {
session.RLock()
defer session.RUnlock()
return session.md5cred[uri.Host+":/"]
}
func (session *session) SetDigestSession(server, hash string) {
session.Lock()
defer session.Unlock()
session.md5sess[server] = hash
}
func (session *session) DigestSession(server string) (hash string) {
session.RLock()
defer session.RUnlock()
return session.md5sess[server]
}
func (session *session) Duplicate(rc io.ReadCloser, n int) (clone []io.ReadCloser, err error) {
defer rc.Close()
prc := make([]ProxyReadCloser, n)
for i := 0; i < n; i++ {
prc[i] = session.NewProxyReadCloser()
}
writers := make([]io.Writer, n)
for i := 0; i < n; i++ {
writers[i] = prc[i].(io.Writer)
}
mw := io.MultiWriter(writers...)
_, err = io.Copy(mw, rc)
if err != nil {
err = fmt.Errorf("error cloning io.ReadCloser: %v", err)
return
}
for i := 0; i < n; i++ {
err = prc[i].Close()
if err != nil {
for j := i; j < n; j++ {
prc[i].Close()
x, _ := prc[i].ReadCloser()
if x != nil {
x.Close()
}
}
err = fmt.Errorf("error cloning io.ReadCloser: %v", err)
return
}
}
clone = make([]io.ReadCloser, n)
for i := 0; i < n; i++ {
clone[i], err = prc[i].ReadCloser()
if err != nil {
for j := 0; j < n; j++ {
clone[i].Close()
}
clone = nil
err = fmt.Errorf("error cloning io.ReadCloser: %v", err)
return
}
}
return clone, err
}
func (session *session) NewProxyReadCloser() ProxyReadCloser {
return NewMemFileReadCloser(session.rcDir, session.rcLimit)
}