Skip to content

Commit

Permalink
Rename error vars
Browse files Browse the repository at this point in the history
  • Loading branch information
segevda committed Mar 12, 2023
1 parent 3b6111c commit 59aaee7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
14 changes: 7 additions & 7 deletions client_test.go
Expand Up @@ -127,13 +127,13 @@ func TestClientDigestErrors(t *testing.T) {
expect error
}
tests := []test{
{mutateConf: func(c *digestServerConfig) { c.algo = "BAD_ALGO" }, expect: ErrAlgNotSupported},
{mutateConf: func(c *digestServerConfig) { c.qop = "bad-qop" }, expect: ErrQopNotSupported},
{mutateConf: func(c *digestServerConfig) { c.qop = "" }, expect: ErrNoQop},
{mutateConf: func(c *digestServerConfig) { c.charset = "utf-16" }, expect: ErrCharset},
{mutateConf: func(c *digestServerConfig) { c.uri = "/bad" }, expect: ErrBadChallenge},
{mutateConf: func(c *digestServerConfig) { c.uri = "/unknown_param" }, expect: ErrBadChallenge},
{mutateConf: func(c *digestServerConfig) { c.uri = "/no_challenge" }, expect: ErrBadChallenge},
{mutateConf: func(c *digestServerConfig) { c.algo = "BAD_ALGO" }, expect: ErrDigestAlgNotSupported},
{mutateConf: func(c *digestServerConfig) { c.qop = "bad-qop" }, expect: ErrDigestQopNotSupported},
{mutateConf: func(c *digestServerConfig) { c.qop = "" }, expect: ErrDigestNoQop},
{mutateConf: func(c *digestServerConfig) { c.charset = "utf-16" }, expect: ErrDigestCharset},
{mutateConf: func(c *digestServerConfig) { c.uri = "/bad" }, expect: ErrDigestBadChallenge},
{mutateConf: func(c *digestServerConfig) { c.uri = "/unknown_param" }, expect: ErrDigestBadChallenge},
{mutateConf: func(c *digestServerConfig) { c.uri = "/no_challenge" }, expect: ErrDigestBadChallenge},
{mutateConf: func(c *digestServerConfig) { c.uri = "/status_500" }, expect: nil},
}

Expand Down
24 changes: 12 additions & 12 deletions digest.go
Expand Up @@ -14,11 +14,11 @@ import (
)

var (
ErrBadChallenge = errors.New("challenge is bad")
ErrCharset = errors.New("unsupported charset")
ErrAlgNotSupported = errors.New("algorithm is not supported")
ErrQopNotSupported = errors.New("no supported qop in list")
ErrNoQop = errors.New("qop must be specified")
ErrDigestBadChallenge = errors.New("digest: challenge is bad")
ErrDigestCharset = errors.New("digest: unsupported charset")
ErrDigestAlgNotSupported = errors.New("digest: algorithm is not supported")
ErrDigestQopNotSupported = errors.New("digest: no supported qop in list")
ErrDigestNoQop = errors.New("digest: qop must be specified")
)

var hashFuncs = map[string]func() hash.Hash{
Expand Down Expand Up @@ -56,7 +56,7 @@ func (dt *digestTransport) RoundTrip(req *http.Request) (*http.Response, error)
}
chal := resp.Header.Get(hdrWwwAuthenticateKey)
if chal == "" {
return resp, ErrBadChallenge
return resp, ErrDigestBadChallenge
}

c, err := parseChallenge(chal)
Expand Down Expand Up @@ -113,7 +113,7 @@ func parseChallenge(input string) (*challenge, error) {
const qs = `"`
s := strings.Trim(input, ws)
if !strings.HasPrefix(s, "Digest ") {
return nil, ErrBadChallenge
return nil, ErrDigestBadChallenge
}
s = strings.Trim(s[7:], ws)
sl := strings.Split(s, ", ")
Expand All @@ -138,12 +138,12 @@ func parseChallenge(input string) (*challenge, error) {
c.qop = strings.Trim(r[1], qs)
case "charset":
if strings.ToUpper(strings.Trim(r[1], qs)) != "UTF-8" {
return nil, ErrCharset
return nil, ErrDigestCharset
}
case "userhash":
c.userhash = strings.Trim(r[1], qs)
default:
return nil, ErrBadChallenge
return nil, ErrDigestBadChallenge
}
}
return c, nil
Expand All @@ -167,7 +167,7 @@ type credentials struct {

func (c *credentials) authorize() (string, error) {
if _, ok := hashFuncs[c.algorithm]; !ok {
return "", ErrAlgNotSupported
return "", ErrDigestAlgNotSupported
}

if err := c.validateQop(); err != nil {
Expand Down Expand Up @@ -208,7 +208,7 @@ func (c *credentials) validateQop() error {
// NOTE: cURL support auth-int qop for requests other than POST and PUT (i.e. w/o body) by hashing an empty string
// is this applicable for resty? see: https://github.com/curl/curl/blob/307b7543ea1e73ab04e062bdbe4b5bb409eaba3a/lib/vauth/digest.c#L774
if c.messageQop == "" {
return ErrNoQop
return ErrDigestNoQop
}
possibleQops := strings.Split(c.messageQop, ", ")
var authSupport bool
Expand All @@ -219,7 +219,7 @@ func (c *credentials) validateQop() error {
}
}
if !authSupport {
return ErrQopNotSupported
return ErrDigestQopNotSupported
}

c.messageQop = "auth"
Expand Down

0 comments on commit 59aaee7

Please sign in to comment.