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

Commit 9da83de

Browse files
authored
Merge pull request #6 from paul-hoehne/master
Adding headers support & rename
2 parents 3667601 + 11167a6 commit 9da83de

File tree

2 files changed

+34
-13
lines changed

2 files changed

+34
-13
lines changed

authorization.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ type authorization struct {
2323
Qop string // unquoted
2424
Realm string // quoted
2525
Response string // quoted
26-
Uri string // quoted
26+
URI string // quoted
2727
Userhash bool // quoted
2828
Username string // quoted
2929
Username_ string // quoted
@@ -40,7 +40,7 @@ func newAuthorization(dr *DigestRequest) (*authorization, error) {
4040
Qop: "",
4141
Realm: dr.Wa.Realm,
4242
Response: "",
43-
Uri: "",
43+
URI: "",
4444
Userhash: dr.Wa.Userhash,
4545
Username: "",
4646
Username_: "", // TODO
@@ -61,12 +61,12 @@ func (ah *authorization) refreshAuthorization(dr *DigestRequest) (*authorization
6161

6262
ah.Cnonce = ah.hash(fmt.Sprintf("%d:%s:my_value", time.Now().UnixNano(), dr.Username))
6363

64-
url, err := url.Parse(dr.Uri)
64+
url, err := url.Parse(dr.URI)
6565
if err != nil {
6666
return nil, err
6767
}
6868

69-
ah.Uri = url.RequestURI()
69+
ah.URI = url.RequestURI()
7070
ah.Response = ah.computeResponse(dr)
7171

7272
return ah, nil
@@ -98,12 +98,12 @@ func (ah *authorization) computeA2(dr *DigestRequest) string {
9898

9999
if matched, _ := regexp.MatchString("auth-int", dr.Wa.Qop); matched {
100100
ah.Qop = "auth-int"
101-
return fmt.Sprintf("%s:%s:%s", dr.Method, ah.Uri, ah.hash(dr.Body))
101+
return fmt.Sprintf("%s:%s:%s", dr.Method, ah.URI, ah.hash(dr.Body))
102102
}
103103

104104
if dr.Wa.Qop == "auth" || dr.Wa.Qop == "" {
105105
ah.Qop = "auth"
106-
return fmt.Sprintf("%s:%s", dr.Method, ah.Uri)
106+
return fmt.Sprintf("%s:%s", dr.Method, ah.URI)
107107
}
108108

109109
return ""
@@ -162,8 +162,8 @@ func (ah *authorization) toString() string {
162162
buffer.WriteString(fmt.Sprintf("response=\"%s\", ", ah.Response))
163163
}
164164

165-
if ah.Uri != "" {
166-
buffer.WriteString(fmt.Sprintf("uri=\"%s\", ", ah.Uri))
165+
if ah.URI != "" {
166+
buffer.WriteString(fmt.Sprintf("uri=\"%s\", ", ah.URI))
167167
}
168168

169169
if ah.Userhash {

digest_auth_client.go

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package digest_auth_client
22

33
import (
44
"bytes"
5+
"crypto/tls"
56
"fmt"
67
"net/http"
78
"time"
@@ -11,10 +12,12 @@ type DigestRequest struct {
1112
Body string
1213
Method string
1314
Password string
14-
Uri string
15+
URI string
1516
Username string
17+
Header http.Header
1618
Auth *authorization
1719
Wa *wwwAuthenticate
20+
CertVal bool
1821
}
1922

2023
type DigestTransport struct {
@@ -26,6 +29,7 @@ type DigestTransport struct {
2629
func NewRequest(username, password, method, uri, body string) DigestRequest {
2730
dr := DigestRequest{}
2831
dr.UpdateRequest(username, password, method, uri, body)
32+
dr.CertVal = true
2933
return dr
3034
}
3135

@@ -43,8 +47,9 @@ func (dr *DigestRequest) UpdateRequest(username, password, method, uri, body str
4347
dr.Body = body
4448
dr.Method = method
4549
dr.Password = password
46-
dr.Uri = uri
50+
dr.URI = uri
4751
dr.Username = username
52+
dr.Header = make(map[string][]string)
4853
return dr
4954
}
5055

@@ -74,13 +79,22 @@ func (dr *DigestRequest) Execute() (resp *http.Response, err error) {
7479
}
7580

7681
var req *http.Request
77-
if req, err = http.NewRequest(dr.Method, dr.Uri, bytes.NewReader([]byte(dr.Body))); err != nil {
82+
if req, err = http.NewRequest(dr.Method, dr.URI, bytes.NewReader([]byte(dr.Body))); err != nil {
7883
return nil, err
7984
}
85+
req.Header = dr.Header
8086

8187
client := &http.Client{
8288
Timeout: 30 * time.Second,
8389
}
90+
91+
if !dr.CertVal {
92+
tr := &http.Transport{
93+
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
94+
}
95+
client.Transport = tr
96+
}
97+
8498
if resp, err = client.Do(req); err != nil {
8599
return nil, err
86100
}
@@ -135,15 +149,22 @@ func (dr *DigestRequest) executeExistingDigest() (resp *http.Response, err error
135149
func (dr *DigestRequest) executeRequest(authString string) (resp *http.Response, err error) {
136150
var req *http.Request
137151

138-
if req, err = http.NewRequest(dr.Method, dr.Uri, bytes.NewReader([]byte(dr.Body))); err != nil {
152+
if req, err = http.NewRequest(dr.Method, dr.URI, bytes.NewReader([]byte(dr.Body))); err != nil {
139153
return nil, err
140154
}
141-
155+
req.Header = dr.Header
142156
req.Header.Add("Authorization", authString)
143157

144158
client := &http.Client{
145159
Timeout: 30 * time.Second,
146160
}
147161

162+
if !dr.CertVal {
163+
tr := &http.Transport{
164+
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
165+
}
166+
client.Transport = tr
167+
}
168+
148169
return client.Do(req)
149170
}

0 commit comments

Comments
 (0)