Skip to content

Commit

Permalink
fix: auth scheme and token from client level #959
Browse files Browse the repository at this point in the history
  • Loading branch information
jeevatkm committed Jan 22, 2025
1 parent 7ad1178 commit 43db49c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
2 changes: 0 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,8 +450,6 @@ func (c *Client) R() *Request {
PathParams: map[string]string{},
RawPathParams: map[string]string{},
Debug: c.Debug,
AuthScheme: c.AuthScheme,
Token: c.Token,

client: c,
multipartFiles: []*File{},
Expand Down
9 changes: 8 additions & 1 deletion middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,9 +298,16 @@ func addCredentials(c *Client, r *Request) error {
}
}

authScheme := c.AuthScheme
if !IsStringEmpty(r.AuthScheme) {
authScheme = r.AuthScheme
}

// Build the token Auth header
if !IsStringEmpty(r.Token) {
r.RawRequest.Header.Set(c.HeaderAuthorizationKey, strings.TrimSpace(r.AuthScheme+" "+r.Token))
r.RawRequest.Header.Set(c.HeaderAuthorizationKey, strings.TrimSpace(authScheme+" "+r.Token))
} else if !IsStringEmpty(c.Token) {
r.RawRequest.Header.Set(c.HeaderAuthorizationKey, strings.TrimSpace(authScheme+" "+c.Token))
}

return nil
Expand Down
15 changes: 15 additions & 0 deletions request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,21 @@ func TestRequestAuthScheme(t *testing.T) {
assertEqual(t, tokenValue, resp.Request.Header.Get(hdrAuthorizationKey))
})

t.Run("only client level auth token GH959", func(t *testing.T) {
tokenValue := "004DDB79-6801-4587-B976-F093E6AC44FF"

c := dc().
SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true}).
SetAuthToken(tokenValue)

resp, err := c.R().
Get(ts.URL + "/profile")

assertError(t, err)
assertEqual(t, http.StatusOK, resp.StatusCode())
assertEqual(t, "Bearer "+tokenValue, resp.Request.Header.Get(hdrAuthorizationKey))
})

}

func TestRequestDigestAuth(t *testing.T) {
Expand Down

0 comments on commit 43db49c

Please sign in to comment.