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

Commit

Permalink
Merge pull request #6 from paul-hoehne/master
Browse files Browse the repository at this point in the history
Adding headers support & rename
  • Loading branch information
xinsnake authored May 8, 2018
2 parents 3667601 + 11167a6 commit 9da83de
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 13 deletions.
16 changes: 8 additions & 8 deletions authorization.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type authorization struct {
Qop string // unquoted
Realm string // quoted
Response string // quoted
Uri string // quoted
URI string // quoted
Userhash bool // quoted
Username string // quoted
Username_ string // quoted
Expand All @@ -40,7 +40,7 @@ func newAuthorization(dr *DigestRequest) (*authorization, error) {
Qop: "",
Realm: dr.Wa.Realm,
Response: "",
Uri: "",
URI: "",
Userhash: dr.Wa.Userhash,
Username: "",
Username_: "", // TODO
Expand All @@ -61,12 +61,12 @@ func (ah *authorization) refreshAuthorization(dr *DigestRequest) (*authorization

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
Expand Down Expand Up @@ -98,12 +98,12 @@ func (ah *authorization) computeA2(dr *DigestRequest) string {

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 ""
Expand Down Expand Up @@ -162,8 +162,8 @@ func (ah *authorization) toString() string {
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 {
Expand Down
31 changes: 26 additions & 5 deletions digest_auth_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package digest_auth_client

import (
"bytes"
"crypto/tls"
"fmt"
"net/http"
"time"
Expand All @@ -11,10 +12,12 @@ type DigestRequest struct {
Body string
Method string
Password string
Uri string
URI string
Username string
Header http.Header
Auth *authorization
Wa *wwwAuthenticate
CertVal bool
}

type DigestTransport struct {
Expand All @@ -26,6 +29,7 @@ type DigestTransport struct {
func NewRequest(username, password, method, uri, body string) DigestRequest {
dr := DigestRequest{}
dr.UpdateRequest(username, password, method, uri, body)
dr.CertVal = true
return dr
}

Expand All @@ -43,8 +47,9 @@ func (dr *DigestRequest) UpdateRequest(username, password, method, uri, body str
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
}

Expand Down Expand Up @@ -74,13 +79,22 @@ func (dr *DigestRequest) Execute() (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

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
}
Expand Down Expand Up @@ -135,15 +149,22 @@ func (dr *DigestRequest) executeExistingDigest() (resp *http.Response, err error
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)
}

0 comments on commit 9da83de

Please sign in to comment.