Merge pull request #6 from paul-hoehne/master
Adding headers support & rename
diff --git a/authorization.go b/authorization.go
index 82e78b7..79cc762 100644
--- a/authorization.go
+++ b/authorization.go
@@ -23,7 +23,7 @@
Qop string // unquoted
Realm string // quoted
Response string // quoted
- Uri string // quoted
+ URI string // quoted
Userhash bool // quoted
Username string // quoted
Username_ string // quoted
@@ -40,7 +40,7 @@
Qop: "",
Realm: dr.Wa.Realm,
Response: "",
- Uri: "",
+ URI: "",
Userhash: dr.Wa.Userhash,
Username: "",
Username_: "", // TODO
@@ -61,12 +61,12 @@
ah.Cnonce = ah.hash(fmt.Sprintf("%d:%s:my_value", time.Now().UnixNano(), dr.Username))
- url, err := url.Parse(dr.Uri)
+ url, err := url.Parse(dr.URI)
if err != nil {
return nil, err
}
- ah.Uri = url.RequestURI()
+ ah.URI = url.RequestURI()
ah.Response = ah.computeResponse(dr)
return ah, nil
@@ -98,12 +98,12 @@
if matched, _ := regexp.MatchString("auth-int", dr.Wa.Qop); matched {
ah.Qop = "auth-int"
- return fmt.Sprintf("%s:%s:%s", dr.Method, ah.Uri, ah.hash(dr.Body))
+ return fmt.Sprintf("%s:%s:%s", dr.Method, ah.URI, ah.hash(dr.Body))
}
if dr.Wa.Qop == "auth" || dr.Wa.Qop == "" {
ah.Qop = "auth"
- return fmt.Sprintf("%s:%s", dr.Method, ah.Uri)
+ return fmt.Sprintf("%s:%s", dr.Method, ah.URI)
}
return ""
@@ -162,8 +162,8 @@
buffer.WriteString(fmt.Sprintf("response=\"%s\", ", ah.Response))
}
- if ah.Uri != "" {
- buffer.WriteString(fmt.Sprintf("uri=\"%s\", ", ah.Uri))
+ if ah.URI != "" {
+ buffer.WriteString(fmt.Sprintf("uri=\"%s\", ", ah.URI))
}
if ah.Userhash {
diff --git a/digest_auth_client.go b/digest_auth_client.go
index 2058e39..f796c11 100644
--- a/digest_auth_client.go
+++ b/digest_auth_client.go
@@ -2,6 +2,7 @@
import (
"bytes"
+ "crypto/tls"
"fmt"
"net/http"
"time"
@@ -11,10 +12,12 @@
Body string
Method string
Password string
- Uri string
+ URI string
Username string
+ Header http.Header
Auth *authorization
Wa *wwwAuthenticate
+ CertVal bool
}
type DigestTransport struct {
@@ -26,6 +29,7 @@
func NewRequest(username, password, method, uri, body string) DigestRequest {
dr := DigestRequest{}
dr.UpdateRequest(username, password, method, uri, body)
+ dr.CertVal = true
return dr
}
@@ -43,8 +47,9 @@
dr.Body = body
dr.Method = method
dr.Password = password
- dr.Uri = uri
+ dr.URI = uri
dr.Username = username
+ dr.Header = make(map[string][]string)
return dr
}
@@ -74,13 +79,22 @@
}
var req *http.Request
- if req, err = http.NewRequest(dr.Method, dr.Uri, bytes.NewReader([]byte(dr.Body))); err != nil {
+ if req, err = http.NewRequest(dr.Method, dr.URI, bytes.NewReader([]byte(dr.Body))); err != nil {
return nil, err
}
+ req.Header = dr.Header
client := &http.Client{
Timeout: 30 * time.Second,
}
+
+ if !dr.CertVal {
+ tr := &http.Transport{
+ TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
+ }
+ client.Transport = tr
+ }
+
if resp, err = client.Do(req); err != nil {
return nil, err
}
@@ -135,15 +149,22 @@
func (dr *DigestRequest) executeRequest(authString string) (resp *http.Response, err error) {
var req *http.Request
- if req, err = http.NewRequest(dr.Method, dr.Uri, bytes.NewReader([]byte(dr.Body))); err != nil {
+ if req, err = http.NewRequest(dr.Method, dr.URI, bytes.NewReader([]byte(dr.Body))); err != nil {
return nil, err
}
-
+ req.Header = dr.Header
req.Header.Add("Authorization", authString)
client := &http.Client{
Timeout: 30 * time.Second,
}
+ if !dr.CertVal {
+ tr := &http.Transport{
+ TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
+ }
+ client.Transport = tr
+ }
+
return client.Do(req)
}