-
Notifications
You must be signed in to change notification settings - Fork 31
/
auth.go
210 lines (191 loc) · 5.61 KB
/
auth.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
package http_digest_auth
import (
"crypto/md5"
"crypto/rand"
"crypto/tls"
"encoding/base64"
"encoding/hex"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"net/url"
"os"
"strings"
"time"
)
var (
connectTimeOut = time.Duration(10 * time.Second)
readWriteTimeout = time.Duration(20 * time.Second)
userAgent = "AtScale"
)
type myjar struct {
jar map[string][]*http.Cookie
}
func (p *myjar) SetCookies(u *url.URL, cookies []*http.Cookie) {
p.jar[u.Host] = cookies
}
func (p *myjar) Cookies(u *url.URL) []*http.Cookie {
return p.jar[u.Host]
}
const (
nc = "00000001"
)
func Auth(username string, password string, uri string) (bool, error) {
client := DefaultTimeoutClient()
jar := &myjar{}
jar.jar = make(map[string][]*http.Cookie)
client.Jar = jar
var req *http.Request
var resp *http.Response
var err error
req, err = http.NewRequest("GET", uri, nil)
if err != nil {
return false, err
}
headers := http.Header{
"User-Agent": []string{userAgent},
"Accept": []string{"*/*"},
"Accept-Encoding": []string{"identity"},
"Connection": []string{"Keep-Alive"},
"Host": []string{req.Host},
}
req.Header = headers
resp, err = client.Do(req)
if err != nil {
return false, err
}
// you HAVE to read the whole body and then close it to reuse the http connection
// otherwise it *could* fail in certain environments (behind proxy for instance)
io.Copy(ioutil.Discard, resp.Body)
resp.Body.Close()
if resp.StatusCode == http.StatusUnauthorized {
var authorization map[string]string = DigestAuthParams(resp)
realmHeader := authorization["realm"]
qopHeader := authorization["qop"]
nonceHeader := authorization["nonce"]
opaqueHeader := authorization["opaque"]
algorithm := authorization["algorithm"]
realm := realmHeader
// A1
h := md5.New()
A1 := fmt.Sprintf("%s:%s:%s", username, realm, password)
io.WriteString(h, A1)
HA1 := hex.EncodeToString(h.Sum(nil))
// A2
h = md5.New()
A2 := fmt.Sprintf("GET:%s", "/auth")
io.WriteString(h, A2)
HA2 := hex.EncodeToString(h.Sum(nil))
// response
cnonce := RandomKey()
response := H(strings.Join([]string{HA1, nonceHeader, nc, cnonce, qopHeader, HA2}, ":"))
// now make header
AuthHeader := fmt.Sprintf(`Digest username="%s", realm="%s", nonce="%s", uri="%s", response="%s", qop=%s, nc=%s, cnonce="%s", opaque="%s", algorithm="%s"`,
username, realmHeader, nonceHeader, "/auth", response, qopHeader, nc, cnonce, opaqueHeader, algorithm)
headers := http.Header{
"User-Agent": []string{userAgent},
"Accept": []string{"*/*"},
"Accept-Encoding": []string{"identity"},
"Connection": []string{"Keep-Alive"},
"Host": []string{req.Host},
"Authorization": []string{AuthHeader},
}
//req, err = http.NewRequest("GET", uri, nil)
req.Header = headers
resp, err = client.Do(req)
if err != nil {
return false, err
}
defer resp.Body.Close()
} else {
return false, fmt.Errorf("response status code should have been 401, it was %v", resp.StatusCode)
}
return resp.StatusCode == http.StatusOK, err
}
/*
Parse Authorization header from the http.Request. Returns a map of
auth parameters or nil if the header is not a valid parsable Digest
auth header.
*/
func DigestAuthParams(r *http.Response) map[string]string {
s := strings.SplitN(r.Header.Get("Www-Authenticate"), " ", 2)
if len(s) != 2 || s[0] != "Digest" {
return nil
}
result := map[string]string{}
for _, kv := range strings.Split(s[1], ",") {
parts := strings.SplitN(kv, "=", 2)
if len(parts) != 2 {
continue
}
result[strings.Trim(parts[0], "\" ")] = strings.Trim(parts[1], "\" ")
}
return result
}
func RandomKey() string {
k := make([]byte, 8)
for bytes := 0; bytes < len(k); {
n, err := rand.Read(k[bytes:])
if err != nil {
panic("rand.Read() failed")
}
bytes += n
}
return base64.StdEncoding.EncodeToString(k)
}
/*
H function for MD5 algorithm (returns a lower-case hex MD5 digest)
*/
func H(data string) string {
digest := md5.New()
digest.Write([]byte(data))
return hex.EncodeToString(digest.Sum(nil))
}
func timeoutDialer(cTimeout time.Duration, rwTimeout time.Duration) func(net, addr string) (c net.Conn, err error) {
return func(netw, addr string) (net.Conn, error) {
conn, err := net.DialTimeout(netw, addr, cTimeout)
if err != nil {
return nil, err
}
if rwTimeout > 0 {
conn.SetDeadline(time.Now().Add(rwTimeout))
}
return conn, nil
}
}
// apps will set three OS variables:
// atscale_http_sslcert - location of the http ssl cert
// atscale_http_sslkey - location of the http ssl key
// atscale_disable_keepalives - disable http keep alives
func NewTimeoutClient(cTimeout time.Duration, rwTimeout time.Duration) *http.Client {
certLocation := os.Getenv("atscale_http_sslcert")
keyLocation := os.Getenv("atscale_http_sslkey")
disableKeepAlives := os.Getenv("atscale_disable_keepalives")
disableKeepAlivesBool := false
if disableKeepAlives == "true" {
disableKeepAlivesBool = true
}
// default
tlsConfig := &tls.Config{InsecureSkipVerify: true}
if len(certLocation) > 0 && len(keyLocation) > 0 {
// Load client cert if available
cert, err := tls.LoadX509KeyPair(certLocation, keyLocation)
if err == nil {
tlsConfig = &tls.Config{Certificates: []tls.Certificate{cert}, InsecureSkipVerify: true}
} else {
fmt.Printf("Error loading X509 Key Pair:%v\n", err)
}
}
return &http.Client{
Transport: &http.Transport{
TLSClientConfig: tlsConfig,
DisableKeepAlives: disableKeepAlivesBool,
Dial: timeoutDialer(cTimeout, rwTimeout),
},
}
}
func DefaultTimeoutClient() *http.Client {
return NewTimeoutClient(connectTimeOut, readWriteTimeout)
}