Skip to content

Commit

Permalink
formatting & doc
Browse files Browse the repository at this point in the history
  • Loading branch information
icholy committed Jan 19, 2025
1 parent 7a207b9 commit b1b6f62
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
16 changes: 8 additions & 8 deletions digest.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ type Options struct {
URI string
GetBody func() (io.ReadCloser, error)
Count int
A1 string
Username string
Password string

// used for testing
// The following are provided for advanced use cases where the client needs
// to override the default digest calculation behavior. Most users should
// leave these fields unset.
A1 string
Cnonce string
}

Expand Down Expand Up @@ -77,18 +79,16 @@ func Digest(chal *Challenge, o Options) (*Credentials, error) {
if cred.Userhash {
cred.Username = hashf(h, "%s:%s", o.Username, cred.Realm)
}

// generate the a1 hash if one was not provided
a1 := o.A1

if a1 == "" {
a1 = hashf(h, "%s:%s:%s", o.Username, cred.Realm, o.Password)
}

// generate the response
switch {
case len(chal.QOP) == 0:
cred.Response = hashf(h, "%s:%s:%s",
a1, // A1
a1,
cred.Nonce,
hashf(h, "%s:%s", o.Method, o.URI), // A2
)
Expand All @@ -101,7 +101,7 @@ func Digest(chal *Challenge, o Options) (*Credentials, error) {
cred.Nc = 1
}
cred.Response = hashf(h, "%s:%s:%08x:%s:%s:%s",
a1, // A1
a1,
cred.Nonce,
cred.Nc,
cred.Cnonce,
Expand All @@ -121,7 +121,7 @@ func Digest(chal *Challenge, o Options) (*Credentials, error) {
return nil, fmt.Errorf("digest: failed to read body for auth-int: %w", err)
}
cred.Response = hashf(h, "%s:%s:%08x:%s:%s:%s",
a1, // A1
a1,
cred.Nonce,
cred.Nc,
cred.Cnonce,
Expand Down
15 changes: 8 additions & 7 deletions digest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,17 @@ func TestDigestSHA256(t *testing.T) {
}

func TestDigestA1(t *testing.T) {
h := sha256.New()
fmt.Fprintf(h, "%s:%s:%s",
"Mufasa", // username
"[email protected]", // realm
"Circle of Life", // password
)
a1 := hex.EncodeToString(h.Sum(nil))
opt := Options{
Method: "GET",
URI: "/dir/index.html",
A1: sha265Hash("%s:%s:%s", "Mufasa", "[email protected]", "Circle of Life"),
A1: a1,
Cnonce: "f2/wE4q74E6zIJEtWaHKaf5wv/H5QzzpXusqGemxURZJ",
}
chal := &Challenge{
Expand Down Expand Up @@ -204,9 +211,3 @@ func TestDigestAuthInt(t *testing.T) {
Nc: 1,
})
}

func sha265Hash(format string, args ...interface{}) string {
h := sha256.New()
fmt.Fprintf(h, format, args...)
return hex.EncodeToString(h.Sum(nil))
}

0 comments on commit b1b6f62

Please sign in to comment.