Skip to content
This repository was archived by the owner on Jun 21, 2022. It is now read-only.

Commit 0341f2a

Browse files
committed
finished coding, need some testing
1 parent 8c7a8b4 commit 0341f2a

File tree

3 files changed

+114
-7
lines changed

3 files changed

+114
-7
lines changed

authorization.go

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package digest_auth_client
22

33
import (
4+
"bytes"
45
"crypto/md5"
56
"crypto/sha256"
67
"fmt"
@@ -107,5 +108,53 @@ func (ah *authorization) hash(a string) (s string) {
107108
}
108109

109110
func (ah *authorization) toString() string {
110-
return ""
111+
var buffer bytes.Buffer
112+
113+
buffer.WriteString("Digest ")
114+
115+
if ah.Algorithm != "" {
116+
buffer.WriteString(fmt.Sprintf("algorithm=%s, ", ah.Algorithm))
117+
}
118+
119+
if ah.Cnonce != "" {
120+
buffer.WriteString(fmt.Sprintf("cnonce=\"%s\", ", ah.Cnonce))
121+
}
122+
123+
if ah.Nc != 0 {
124+
buffer.WriteString(fmt.Sprintf("cnonce=%d, ", ah.Nc))
125+
}
126+
127+
if ah.Opaque != "" {
128+
buffer.WriteString(fmt.Sprintf("opaque=\"%s\", ", ah.Opaque))
129+
}
130+
131+
if ah.Qop != "" {
132+
buffer.WriteString(fmt.Sprintf("qop=%s, ", ah.Qop))
133+
}
134+
135+
if ah.Realm != "" {
136+
buffer.WriteString(fmt.Sprintf("realm=\"%s\", ", ah.Realm))
137+
}
138+
139+
if ah.Response != "" {
140+
buffer.WriteString(fmt.Sprintf("response=\"%s\", ", ah.Response))
141+
}
142+
143+
if ah.Response != "" {
144+
buffer.WriteString(fmt.Sprintf("response=\"%s\", ", ah.Response))
145+
}
146+
147+
if ah.Uri != "" {
148+
buffer.WriteString(fmt.Sprintf("uri=\"%s\", ", ah.Uri))
149+
}
150+
151+
if ah.Userhash {
152+
buffer.WriteString("userhash=true, ")
153+
}
154+
155+
if ah.Username != "" {
156+
buffer.WriteString(fmt.Sprintf("username=\"%s\", ", ah.Username))
157+
}
158+
159+
return buffer.String()
111160
}

digest_auth_client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func (dr *DigestRequest) executeDigest(resp *http.Response) (*http.Response, err
5858
return nil, fmt.Errorf("Failed to get WWW-Authenticate header, please check your server configuration.")
5959
}
6060

61-
if wa, err = newWAHeader(waString); err != nil {
61+
if wa, err = newWwwAuthenticate(waString); err != nil {
6262
return nil, err
6363
}
6464

www_authenticate.go

Lines changed: 63 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package digest_auth_client
22

3+
import (
4+
"regexp"
5+
"strings"
6+
)
7+
38
type wwwAuthenticate struct {
49
Algorithm string // unquoted
510
Domain string // quoted
@@ -12,10 +17,63 @@ type wwwAuthenticate struct {
1217
Userhash bool // quoted
1318
}
1419

15-
func newWAHeader(newWAHeaderString string) (*wwwAuthenticate, error) {
16-
return nil, nil
17-
}
20+
func newWwwAuthenticate(s string) (*wwwAuthenticate, error) {
21+
22+
var wa = wwwAuthenticate{}
23+
24+
algorithmRegex := regexp.MustCompile(`algorithm=([^ ,]+)`)
25+
algorithmMatch := algorithmRegex.FindStringSubmatch(s)
26+
if algorithmMatch != nil {
27+
wa.Algorithm = algorithmMatch[1]
28+
}
29+
30+
domainRegex := regexp.MustCompile(`domain="(.+?)"`)
31+
domainMatch := domainRegex.FindStringSubmatch(s)
32+
if domainMatch != nil {
33+
wa.Domain = domainMatch[1]
34+
}
35+
36+
nonceRegex := regexp.MustCompile(`nonce="(.+?)"`)
37+
nonceMatch := nonceRegex.FindStringSubmatch(s)
38+
if nonceMatch != nil {
39+
wa.Nonce = nonceMatch[1]
40+
}
41+
42+
opaqueRegex := regexp.MustCompile(`opaque="(.+?)"`)
43+
opaqueMatch := opaqueRegex.FindStringSubmatch(s)
44+
if opaqueMatch != nil {
45+
wa.Opaque = opaqueMatch[1]
46+
}
47+
48+
qopRegex := regexp.MustCompile(`qop="(.+?)"`)
49+
qopMatch := qopRegex.FindStringSubmatch(s)
50+
if qopMatch != nil {
51+
wa.Qop = qopMatch[1]
52+
}
53+
54+
realmRegex := regexp.MustCompile(`realm="(.+?)"`)
55+
realmMatch := realmRegex.FindStringSubmatch(s)
56+
if realmMatch != nil {
57+
wa.Realm = realmMatch[1]
58+
}
59+
60+
staleRegex := regexp.MustCompile(`stale=([^ ,])"`)
61+
staleMatch := staleRegex.FindStringSubmatch(s)
62+
if staleMatch != nil {
63+
wa.Stale = (strings.ToLower(staleMatch[1]) == "true")
64+
}
65+
66+
charsetRegex := regexp.MustCompile(`charset="(.+?)"`)
67+
charsetMatch := charsetRegex.FindStringSubmatch(s)
68+
if charsetMatch != nil {
69+
wa.Charset = charsetMatch[1]
70+
}
71+
72+
userhashRegex := regexp.MustCompile(`userhash=([^ ,])"`)
73+
userhashMatch := userhashRegex.FindStringSubmatch(s)
74+
if userhashMatch != nil {
75+
wa.Userhash = (strings.ToLower(userhashMatch[1]) == "true")
76+
}
1877

19-
func (wa *wwwAuthenticate) fromString(s string) (*wwwAuthenticate, error) {
20-
return wa, nil
78+
return &wa, nil
2179
}

0 commit comments

Comments
 (0)