Skip to content

Commit 7a207b9

Browse files
authored
add support for passing a1 into the digest (#15)
1 parent f2e2eb0 commit 7a207b9

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

digest.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ type Options struct {
2727
URI string
2828
GetBody func() (io.ReadCloser, error)
2929
Count int
30+
A1 string
3031
Username string
3132
Password string
3233

@@ -76,11 +77,18 @@ func Digest(chal *Challenge, o Options) (*Credentials, error) {
7677
if cred.Userhash {
7778
cred.Username = hashf(h, "%s:%s", o.Username, cred.Realm)
7879
}
80+
81+
a1 := o.A1
82+
83+
if a1 == "" {
84+
a1 = hashf(h, "%s:%s:%s", o.Username, cred.Realm, o.Password)
85+
}
86+
7987
// generate the response
8088
switch {
8189
case len(chal.QOP) == 0:
8290
cred.Response = hashf(h, "%s:%s:%s",
83-
hashf(h, "%s:%s:%s", o.Username, cred.Realm, o.Password), // A1
91+
a1, // A1
8492
cred.Nonce,
8593
hashf(h, "%s:%s", o.Method, o.URI), // A2
8694
)
@@ -93,7 +101,7 @@ func Digest(chal *Challenge, o Options) (*Credentials, error) {
93101
cred.Nc = 1
94102
}
95103
cred.Response = hashf(h, "%s:%s:%08x:%s:%s:%s",
96-
hashf(h, "%s:%s:%s", o.Username, cred.Realm, o.Password), // A1
104+
a1, // A1
97105
cred.Nonce,
98106
cred.Nc,
99107
cred.Cnonce,
@@ -113,7 +121,7 @@ func Digest(chal *Challenge, o Options) (*Credentials, error) {
113121
return nil, fmt.Errorf("digest: failed to read body for auth-int: %w", err)
114122
}
115123
cred.Response = hashf(h, "%s:%s:%08x:%s:%s:%s",
116-
hashf(h, "%s:%s:%s", o.Username, cred.Realm, o.Password), // A1
124+
a1, // A1
117125
cred.Nonce,
118126
cred.Nc,
119127
cred.Cnonce,

digest_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package digest
22

33
import (
4+
"crypto/sha256"
5+
"encoding/hex"
6+
"fmt"
47
"testing"
58

69
"gotest.tools/v3/assert"
@@ -108,6 +111,35 @@ func TestDigestSHA256(t *testing.T) {
108111
})
109112
}
110113

114+
func TestDigestA1(t *testing.T) {
115+
opt := Options{
116+
Method: "GET",
117+
URI: "/dir/index.html",
118+
A1: sha265Hash("%s:%s:%s", "Mufasa", "[email protected]", "Circle of Life"),
119+
Cnonce: "f2/wE4q74E6zIJEtWaHKaf5wv/H5QzzpXusqGemxURZJ",
120+
}
121+
chal := &Challenge{
122+
123+
Nonce: "7ypf/xlj9XXwfDPEoM4URrv/xwf94BcCAzFZH4GiTo0v",
124+
Algorithm: "SHA-256",
125+
Opaque: "FQhe/qaU925kfnzjCev0ciny7QMkPqMAFRtzCUYo5tdS",
126+
QOP: []string{"auth", "auth-int"},
127+
}
128+
cred, err := Digest(chal, opt)
129+
assert.NilError(t, err)
130+
assert.DeepEqual(t, cred, &Credentials{
131+
132+
Nonce: "7ypf/xlj9XXwfDPEoM4URrv/xwf94BcCAzFZH4GiTo0v",
133+
URI: "/dir/index.html",
134+
Response: "753927fa0e85d155564e2e272a28d1802ca10daf4496794697cf8db5856cb6c1",
135+
Algorithm: "SHA-256",
136+
Cnonce: "f2/wE4q74E6zIJEtWaHKaf5wv/H5QzzpXusqGemxURZJ",
137+
Opaque: "FQhe/qaU925kfnzjCev0ciny7QMkPqMAFRtzCUYo5tdS",
138+
QOP: "auth",
139+
Nc: 1,
140+
})
141+
}
142+
111143
func TestDigestUserhash(t *testing.T) {
112144
opt := Options{
113145
Method: "GET",
@@ -172,3 +204,9 @@ func TestDigestAuthInt(t *testing.T) {
172204
Nc: 1,
173205
})
174206
}
207+
208+
func sha265Hash(format string, args ...interface{}) string {
209+
h := sha256.New()
210+
fmt.Fprintf(h, format, args...)
211+
return hex.EncodeToString(h.Sum(nil))
212+
}

0 commit comments

Comments
 (0)