@@ -7,35 +7,37 @@ import (
7
7
"time"
8
8
)
9
9
10
- var (
11
- auth * authorization
12
- wa * wwwAuthenticate
13
- )
14
-
15
10
type DigestRequest struct {
16
11
Body string
17
12
Method string
18
13
Password string
19
14
Uri string
20
15
Username string
16
+ Auth * authorization
17
+ Wa * wwwAuthenticate
21
18
}
22
19
23
20
func NewRequest (username string , password string , method string , uri string , body string ) DigestRequest {
24
21
25
22
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 {
26
29
27
30
dr .Body = body
28
31
dr .Method = method
29
32
dr .Password = password
30
33
dr .Uri = uri
31
34
dr .Username = username
32
-
33
35
return dr
34
36
}
35
37
36
38
func (dr * DigestRequest ) Execute () (resp * http.Response , err error ) {
37
39
38
- if auth == nil {
40
+ if dr . Auth == nil {
39
41
var req * http.Request
40
42
if req , err = http .NewRequest (dr .Method , dr .Uri , bytes .NewReader ([]byte (dr .Body ))); err != nil {
41
43
return nil , err
@@ -56,26 +58,44 @@ func (dr *DigestRequest) Execute() (resp *http.Response, err error) {
56
58
}
57
59
58
60
func (dr * DigestRequest ) executeNewDigest (resp * http.Response ) (* http.Response , error ) {
61
+ var (
62
+ auth * authorization
63
+ err error
64
+ wa * wwwAuthenticate
65
+ )
59
66
60
67
waString := resp .Header .Get ("WWW-Authenticate" )
61
68
if waString == "" {
62
69
return nil , fmt .Errorf ("Failed to get WWW-Authenticate header, please check your server configuration." )
63
70
}
64
71
wa = newWwwAuthenticate (waString )
72
+ dr .Wa = wa
65
73
66
- authString := newAuthorization (dr ).toString ()
74
+ if auth , err = newAuthorization (dr ); err != nil {
75
+ return nil , err
76
+ }
77
+ authString := auth .toString ()
67
78
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
+ }
69
85
}
70
86
71
87
func (dr * DigestRequest ) executeExistingDigest () (* http.Response , error ) {
72
- var err error
88
+ var (
89
+ auth * authorization
90
+ err error
91
+ )
73
92
74
- if auth , err = auth .refreshAuthorization (dr ); err != nil {
93
+ if auth , err = dr . Auth .refreshAuthorization (dr ); err != nil {
75
94
return nil , err
76
95
}
96
+ dr .Auth = auth
77
97
78
- authString := auth .toString ()
98
+ authString := dr . Auth .toString ()
79
99
return dr .executeRequest (authString )
80
100
}
81
101
@@ -89,7 +109,7 @@ func (dr *DigestRequest) executeRequest(authString string) (*http.Response, erro
89
109
return nil , err
90
110
}
91
111
92
- fmt .Printf ("AUTHSTRING: %s\n \n " , authString )
112
+ // fmt.Printf("AUTHSTRING: %s\n\n", authString)
93
113
req .Header .Add ("Authorization" , authString )
94
114
95
115
client := & http.Client {
0 commit comments