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

Commit 1ea4067

Browse files
committed
now allow updated request
1 parent 3a02c39 commit 1ea4067

File tree

2 files changed

+42
-24
lines changed

2 files changed

+42
-24
lines changed

authorization.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,19 @@ type authorization struct {
2929
Username_ string // quoted
3030
}
3131

32-
func newAuthorization(dr *DigestRequest) *authorization {
32+
func newAuthorization(dr *DigestRequest) (*authorization, error) {
3333

3434
ah := authorization{
35-
Algorithm: wa.Algorithm,
35+
Algorithm: dr.Wa.Algorithm,
3636
Cnonce: "",
3737
Nc: 0,
38-
Nonce: wa.Nonce,
39-
Opaque: wa.Opaque,
38+
Nonce: dr.Wa.Nonce,
39+
Opaque: dr.Wa.Opaque,
4040
Qop: "",
41-
Realm: wa.Realm,
41+
Realm: dr.Wa.Realm,
4242
Response: "",
4343
Uri: "",
44-
Userhash: wa.Userhash,
44+
Userhash: dr.Wa.Userhash,
4545
Username: dr.Username,
4646
Username_: "", // TODO
4747
}
@@ -50,9 +50,7 @@ func newAuthorization(dr *DigestRequest) *authorization {
5050
ah.Username = ah.hash(fmt.Sprintf("%s:%s", ah.Username, ah.Realm))
5151
}
5252

53-
ah.refreshAuthorization(dr)
54-
55-
return &ah
53+
return ah.refreshAuthorization(dr)
5654
}
5755

5856
func (ah *authorization) refreshAuthorization(dr *DigestRequest) (*authorization, error) {
@@ -96,12 +94,12 @@ func (ah *authorization) computeA1(dr *DigestRequest) string {
9694

9795
func (ah *authorization) computeA2(dr *DigestRequest) string {
9896

99-
if matched, _ := regexp.MatchString("auth-int", wa.Qop); matched {
97+
if matched, _ := regexp.MatchString("auth-int", dr.Wa.Qop); matched {
10098
ah.Qop = "auth-int"
10199
return fmt.Sprintf("%s:%s:%s", dr.Method, ah.Uri, ah.hash(dr.Body))
102100
}
103101

104-
if wa.Qop == "auth" || wa.Qop == "" {
102+
if dr.Wa.Qop == "auth" || dr.Wa.Qop == "" {
105103
ah.Qop = "auth"
106104
return fmt.Sprintf("%s:%s", dr.Method, ah.Uri)
107105
}

digest_auth_client.go

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,35 +7,37 @@ import (
77
"time"
88
)
99

10-
var (
11-
auth *authorization
12-
wa *wwwAuthenticate
13-
)
14-
1510
type DigestRequest struct {
1611
Body string
1712
Method string
1813
Password string
1914
Uri string
2015
Username string
16+
Auth *authorization
17+
Wa *wwwAuthenticate
2118
}
2219

2320
func NewRequest(username string, password string, method string, uri string, body string) DigestRequest {
2421

2522
dr := DigestRequest{}
23+
dr.UpdateRequest(username, password, method, uri, body)
24+
return dr
25+
}
26+
27+
func (dr *DigestRequest) UpdateRequest(username string,
28+
password string, method string, uri string, body string) *DigestRequest {
2629

2730
dr.Body = body
2831
dr.Method = method
2932
dr.Password = password
3033
dr.Uri = uri
3134
dr.Username = username
32-
3335
return dr
3436
}
3537

3638
func (dr *DigestRequest) Execute() (resp *http.Response, err error) {
3739

38-
if auth == nil {
40+
if dr.Auth == nil {
3941
var req *http.Request
4042
if req, err = http.NewRequest(dr.Method, dr.Uri, bytes.NewReader([]byte(dr.Body))); err != nil {
4143
return nil, err
@@ -56,26 +58,44 @@ func (dr *DigestRequest) Execute() (resp *http.Response, err error) {
5658
}
5759

5860
func (dr *DigestRequest) executeNewDigest(resp *http.Response) (*http.Response, error) {
61+
var (
62+
auth *authorization
63+
err error
64+
wa *wwwAuthenticate
65+
)
5966

6067
waString := resp.Header.Get("WWW-Authenticate")
6168
if waString == "" {
6269
return nil, fmt.Errorf("Failed to get WWW-Authenticate header, please check your server configuration.")
6370
}
6471
wa = newWwwAuthenticate(waString)
72+
dr.Wa = wa
6573

66-
authString := newAuthorization(dr).toString()
74+
if auth, err = newAuthorization(dr); err != nil {
75+
return nil, err
76+
}
77+
authString := auth.toString()
6778

68-
return dr.executeRequest(authString)
79+
if resp, err := dr.executeRequest(authString); err != nil {
80+
return nil, err
81+
} else {
82+
dr.Auth = auth
83+
return resp, nil
84+
}
6985
}
7086

7187
func (dr *DigestRequest) executeExistingDigest() (*http.Response, error) {
72-
var err error
88+
var (
89+
auth *authorization
90+
err error
91+
)
7392

74-
if auth, err = auth.refreshAuthorization(dr); err != nil {
93+
if auth, err = dr.Auth.refreshAuthorization(dr); err != nil {
7594
return nil, err
7695
}
96+
dr.Auth = auth
7797

78-
authString := auth.toString()
98+
authString := dr.Auth.toString()
7999
return dr.executeRequest(authString)
80100
}
81101

@@ -89,7 +109,7 @@ func (dr *DigestRequest) executeRequest(authString string) (*http.Response, erro
89109
return nil, err
90110
}
91111

92-
fmt.Printf("AUTHSTRING: %s\n\n", authString)
112+
// fmt.Printf("AUTHSTRING: %s\n\n", authString)
93113
req.Header.Add("Authorization", authString)
94114

95115
client := &http.Client{

0 commit comments

Comments
 (0)