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

Commit cb42ea2

Browse files
committed
a little bit more structure
1 parent 69afe94 commit cb42ea2

File tree

3 files changed

+130
-38
lines changed

3 files changed

+130
-38
lines changed

digest_auth_client.go

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

33
import (
4-
"crypto/md5"
5-
"crypto/sha256"
4+
"bytes"
65
"fmt"
7-
"hash"
8-
"io"
6+
"net/http"
97
"strings"
8+
"time"
109
)
1110

12-
type AuthorizationHeader struct {
13-
Algorithm string // unquoted
14-
Body string // request value
15-
Cnonce string // quoted
16-
Method string // request value
17-
Nc string // unquoted
18-
Opaque string // quoted
19-
Qop string // unquoted
20-
Realm string // quoted
21-
Resposne string // quoted
22-
Uri string // quoted
23-
Userhash string // quoted
24-
Username string // quoted
25-
Username_ string // quoted
11+
type DigestRequest struct {
12+
Body string
13+
Method string
14+
Password string
15+
Uri string
16+
Username string
2617
}
2718

28-
func (ah *AuthorizationHeader) ComputeResponse() AuthorizationHeader {
29-
return *ah
30-
}
19+
func (dr *DigestRequest) NewRequest(
20+
username string, password string, method string, uri string, body string) DigestRequest {
21+
22+
dr.Body = body
23+
dr.Method = method
24+
dr.Password = password
25+
dr.Uri = uri
26+
dr.Body = body
3127

32-
func (ah *AuthorizationHeader) ComputeA1() AuthorizationHeader {
33-
return *ah
28+
return *dr
3429
}
3530

36-
func (ah *AuthorizationHeader) ComputeA2() (s string) {
31+
func (dr *DigestRequest) Execute() (*http.Response, error) {
32+
33+
req, err := http.NewRequest(dr.Method, dr.Uri, bytes.NewReader([]byte(dr.Body)))
34+
if err != nil {
35+
return nil, err
36+
}
3737

38-
if strings.Compare(ah.Qop, "auth") == 0 || strings.Compare(ah.Qop, "") == 0 {
39-
s = fmt.Sprintf("%s:%s", ah.Method, ah.Uri)
38+
client := &http.Client{
39+
Timeout: 30 * time.Second,
4040
}
41+
resp, err := client.Do(req)
4142

42-
if strings.Compare(ah.Qop, "auth-int") == 0 {
43-
s = fmt.Sprintf("%s:%s", s, ah.Hash(ah.Body))
43+
if resp.StatusCode == 401 {
44+
return dr.executeDigest(resp)
4445
}
4546

46-
return
47+
return resp, err
4748
}
4849

49-
func (ah *AuthorizationHeader) Hash(a string) (s string) {
50+
func (dr *DigestRequest) executeDigest(resp *http.Response) (*http.Response, error) {
5051

51-
var h hash.Hash
52+
wwwAuthenticateHeaderString := resp.Header.Get("WWW-Authenticate")
5253

53-
if strings.Compare(ah.Algorithm, "MD5") == 0 {
54-
h = md5.New()
55-
} else if strings.Compare(ah.Algorithm, "SHA-256") == 0 {
56-
h = sha256.New()
54+
if strings.Compare(wwwAuthenticateHeaderString, "") == 0 {
55+
return nil, fmt.Errorf("Failed to get WWW-Authenticate header, please check your server configuration.")
5756
}
5857

59-
io.WriteString(h, a)
60-
s = string(h.Sum(nil))
58+
wwwAuthenticateHeader, err = newWwwAuthenticateHeader(wwwAuthenticateHeaderString)
6159

62-
return
60+
return nil, nil
6361
}

header_authorization.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package digest_auth_client
2+
3+
import (
4+
"crypto/md5"
5+
"crypto/sha256"
6+
"fmt"
7+
"hash"
8+
"io"
9+
"strings"
10+
)
11+
12+
type authorizationHeader struct {
13+
Algorithm string // unquoted
14+
Cnonce string // quoted
15+
Nc string // unquoted
16+
Nounce string // quoted
17+
Opaque string // quoted
18+
Qop string // unquoted
19+
Realm string // quoted
20+
Resposne string // quoted
21+
Uri string // quoted
22+
Userhash string // quoted
23+
Username string // quoted
24+
Username_ string // quoted
25+
}
26+
27+
func (ah *authorizationHeader) ComputeResponse() authorizationHeader {
28+
return *ah
29+
}
30+
31+
func (ah *authorizationHeader) ComputeA1(password string) (s string) {
32+
33+
if strings.Compare(ah.Algorithm, "") == 0 ||
34+
strings.Compare(ah.Algorithm, "MD5") == 0 ||
35+
strings.Compare(ah.Algorithm, "SHA-256") == 0 {
36+
s = fmt.Sprintf("%s:%s:%s", ah.Username, ah.Realm, password)
37+
}
38+
39+
if strings.Compare(ah.Algorithm, "MD5-sess") ||
40+
strings.Compare(ah.Algorithm, "SHA-256-sess") {
41+
upHash := ah.Hash(fmt.Sprintf("%s:%s:%s", ah.Username, ah.Realm, password))
42+
s = fmt.Sprintf("%s:%s:%s", upHash, ah.Nc)
43+
}
44+
45+
return
46+
}
47+
48+
func (ah *authorizationHeader) ComputeA2() (s string) {
49+
50+
if strings.Compare(ah.Qop, "auth") == 0 || strings.Compare(ah.Qop, "") == 0 {
51+
s = fmt.Sprintf("%s:%s", ah.Method, ah.Uri)
52+
}
53+
54+
if strings.Compare(ah.Qop, "auth-int") == 0 {
55+
s = fmt.Sprintf("%s:%s", s, ah.Hash(ah.Body))
56+
}
57+
58+
return
59+
}
60+
61+
func (ah *authorizationHeader) Hash(a string) (s string) {
62+
63+
var h hash.Hash
64+
65+
if strings.Compare(ah.Algorithm, "MD5") == 0 ||
66+
strings.Compare(ah.Algorithm, "MD5-sess") == 0 {
67+
h = md5.New()
68+
} else if strings.Compare(ah.Algorithm, "SHA-256") == 0 ||
69+
strings.Compare(ah.Algorithm, "SHA-256-sess") == 0 {
70+
h = sha256.New()
71+
}
72+
73+
io.WriteString(h, a)
74+
s = string(h.Sum(nil))
75+
76+
return
77+
}

header_www_authenticate.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package digest_auth_client
2+
3+
type wwwAuthenticate struct {
4+
Algorithm string // unquoted
5+
Domain string // quoted
6+
Nonce string // quoted
7+
Opaque string // quoted
8+
Qop string // quoted
9+
Realm string // quoted
10+
Stale bool // unquoted
11+
charset string // quoted
12+
userhash bool // quoted
13+
}
14+
15+
func newWwwAuthenticateHeader(newWwwAuthenticateHeaderString string) (*wwwAuthenticate, error) {
16+
17+
}

0 commit comments

Comments
 (0)