Skip to content

Commit

Permalink
updated digestAuthHeader to return ErrWWWAuthenticateHeader* instead …
Browse files Browse the repository at this point in the history
…of calling fmt.Errorf
  • Loading branch information
opalmer committed Oct 15, 2016
1 parent da1df7e commit 2b0558c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ first. For more complete details see
* Fixed documentation for NewClient and moved fmt.Errorf call from
inside the function to a `ErrNoInstanceGiven` variable so it's
easier to compare against.
* Updated internal function digestAuthHeader to return exported errors
(ErrWWWAuthenticateHeader*) rather than calling fmt.Errorf. This makes
it easier to test against externally and also fixes a lint issue too.

### 0.1.0

Expand Down
18 changes: 15 additions & 3 deletions authentication.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,24 @@ import (
"crypto/md5"
"crypto/rand"
"encoding/base64"
"errors"
"fmt"
"io"
"net/http"
"strings"
)

var (
// Returned by digestAuthHeader when the WWW-Authenticate header is missing
ErrWWWAuthenticateHeaderMissing = errors.New("WWW-Authenticate header is missing")

// Returned by digestAuthHeader when the WWW-Authenticate invalid
ErrWWWAuthenticateHeaderInvalid = errors.New("WWW-Authenticate header is invalid")

// Returned by digestAuthHeader when the WWW-Authenticate header is not 'Digest'
ErrWWWAuthenticateHeaderNotDigest = errors.New("WWW-Authenticate header type is not Digest")
)

const (
// HTTP Basic Authentication
authTypeBasic = 1
Expand Down Expand Up @@ -54,16 +66,16 @@ func (s *AuthenticationService) SetDigestAuth(username, password string) {
func (s *AuthenticationService) digestAuthHeader(response *http.Response) (string, error) {
authenticateHeader := response.Header.Get("WWW-Authenticate")
if authenticateHeader == "" {
return "", fmt.Errorf("WWW-Authenticate header is missing")
return "", ErrWWWAuthenticateHeaderMissing
}

split := strings.SplitN(authenticateHeader, " ", 2)
if len(split) != 2 {
return "", fmt.Errorf("WWW-Authenticate header is invalid")
return "", ErrWWWAuthenticateHeaderInvalid
}

if split[0] != "Digest" {
return "", fmt.Errorf("WWW-Authenticate header type is not Digest")
return "", ErrWWWAuthenticateHeaderNotDigest
}

// Iterate over all the fields from the WWW-Authenticate header
Expand Down

0 comments on commit 2b0558c

Please sign in to comment.