-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpDigestClient.go
More file actions
187 lines (172 loc) · 4.54 KB
/
Copy pathHttpDigestClient.go
File metadata and controls
187 lines (172 loc) · 4.54 KB
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
// Add digest authenticates header for http request
// reference resources: https://github.com/ryanjdew/http-digest-auth-client
//
// Example:
//
// url := "https://xxxxxxxxx"
// username := "yourname"
// password := "pass"
// params := "params"
// req, err := http.NewRequest("POST", url, strings.NewReader(params))
// req.Header.Set("Content-Type", "application/json")
// err = httpAuthClient.ApplyHttpDigestAuth(username, password, url, req)
// if err != nil {
// log.Fatal(err)
// } else {
// resp, err := http.DefaultClient.Do(req)
// if err != nil {
// log.Fatal(err)
// }
// var b []byte
// b, err = ioutil.ReadAll(resp.Body)
// fmt.Println(resp.StatusCode, string(b), err)
// }
package httpAuthClient
import (
"crypto/md5"
"crypto/rand"
"encoding/base64"
"fmt"
"io"
"net/http"
"net/url"
"strings"
)
// DigestHeaders tracks the state of authentication
type DigestHeaders struct {
Realm string
Qop string
Method string
Nonce string
Opaque string
Algorithm string
HA1 string
HA2 string
Cnonce string
Path string
Nc int16
Username string
Password string
}
// add Auth authenticates against a given URI for the request
func ApplyHttpDigestAuth(username, password, uri string, request *http.Request) error {
client := &http.Client{}
req, err := http.NewRequest("GET", uri, nil)
if err != nil {
return err
}
resp, err := client.Do(req)
if err != nil {
return err
}
if resp.StatusCode == 401 {
authn := digestAuthParams(resp)
algorithm := authn["algorithm"]
d := &DigestHeaders{}
u, _ := url.Parse(uri)
d.Path = u.RequestURI()
d.Realm = authn["realm"]
d.Qop = authn["qop"]
d.Nonce = authn["nonce"]
d.Opaque = authn["opaque"]
if algorithm == "" {
d.Algorithm = "MD5"
} else {
d.Algorithm = authn["algorithm"]
}
d.Nc = 0x0
d.Username = username
d.Password = password
d.applyAuth(request)
return nil
}
return fmt.Errorf("response status code should have been 401, it was %v", resp.StatusCode)
}
func (d *DigestHeaders) digestChecksum() {
switch d.Algorithm {
case "MD5":
// A1
h := md5.New()
A1 := fmt.Sprintf("%s:%s:%s", d.Username, d.Realm, d.Password)
io.WriteString(h, A1)
d.HA1 = fmt.Sprintf("%x", h.Sum(nil))
// A2
h = md5.New()
A2 := fmt.Sprintf("%s:%s", d.Method, d.Path)
io.WriteString(h, A2)
d.HA2 = fmt.Sprintf("%x", h.Sum(nil))
case "MD5-sess":
// A1
h := md5.New()
A1 := fmt.Sprintf("%s:%s:%s", d.Username, d.Realm, d.Password)
io.WriteString(h, A1)
haPre := fmt.Sprintf("%x", h.Sum(nil))
h = md5.New()
A1 = fmt.Sprintf("%s:%s:%s", haPre, d.Nonce, d.Cnonce)
io.WriteString(h, A1)
d.HA1 = fmt.Sprintf("%x", h.Sum(nil))
// A2
h = md5.New()
A2 := fmt.Sprintf("%s:%s", d.Method, d.Path)
io.WriteString(h, A2)
d.HA2 = fmt.Sprintf("%x", h.Sum(nil))
default:
//token
}
}
// ApplyAuth adds proper auth header to the passed request
func (d *DigestHeaders) applyAuth(req *http.Request) {
d.Nc += 0x1
d.Cnonce = randomKey()
d.Method = req.Method
d.Path = req.URL.RequestURI()
d.digestChecksum()
response := doMD5(strings.Join([]string{d.HA1, d.Nonce, fmt.Sprintf("%08x", d.Nc),
d.Cnonce, d.Qop, d.HA2}, ":"))
AuthHeader := fmt.Sprintf(`Digest username="%s", realm="%s", nonce="%s", uri="%s", cnonce="%s", nc=%08x, qop=%s, response="%s", algorithm=%s`,
d.Username, d.Realm, d.Nonce, d.Path, d.Cnonce, d.Nc, d.Qop, response, d.Algorithm)
if d.Opaque != "" {
AuthHeader = fmt.Sprintf(`%s, opaque="%s"`, AuthHeader, d.Opaque)
}
// fmt.Printf("%v\n", AuthHeader)
req.Header.Set("Authorization", AuthHeader)
}
/*
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, 12)
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 doMD5(data string) string {
digest := md5.New()
digest.Write([]byte(data))
return fmt.Sprintf("%x", digest.Sum(nil))
}