Skip to content

Commit 4eae633

Browse files
authored
fix: auth scheme override with empty value #954 (#956)
1 parent 414b364 commit 4eae633

File tree

4 files changed

+33
-24
lines changed

4 files changed

+33
-24
lines changed

client.go

+3
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,8 @@ func (c *Client) R() *Request {
450450
PathParams: map[string]string{},
451451
RawPathParams: map[string]string{},
452452
Debug: c.Debug,
453+
AuthScheme: c.AuthScheme,
454+
Token: c.Token,
453455

454456
client: c,
455457
multipartFiles: []*File{},
@@ -1464,6 +1466,7 @@ func createClient(hc *http.Client) *Client {
14641466
XMLMarshal: xml.Marshal,
14651467
XMLUnmarshal: xml.Unmarshal,
14661468
HeaderAuthorizationKey: http.CanonicalHeaderKey("Authorization"),
1469+
AuthScheme: "Bearer",
14671470

14681471
jsonEscapeHTML: true,
14691472
httpClient: hc,

middleware.go

+3-15
Original file line numberDiff line numberDiff line change
@@ -298,21 +298,9 @@ func addCredentials(c *Client, r *Request) error {
298298
}
299299
}
300300

301-
// Set the Authorization Header Scheme
302-
var authScheme string
303-
if !IsStringEmpty(r.AuthScheme) {
304-
authScheme = r.AuthScheme
305-
} else if !IsStringEmpty(c.AuthScheme) {
306-
authScheme = c.AuthScheme
307-
} else {
308-
authScheme = "Bearer"
309-
}
310-
311-
// Build the Token Auth header
312-
if !IsStringEmpty(r.Token) { // takes precedence
313-
r.RawRequest.Header.Set(c.HeaderAuthorizationKey, authScheme+" "+r.Token)
314-
} else if !IsStringEmpty(c.Token) {
315-
r.RawRequest.Header.Set(c.HeaderAuthorizationKey, authScheme+" "+c.Token)
301+
// Build the token Auth header
302+
if !IsStringEmpty(r.Token) {
303+
r.RawRequest.Header.Set(c.HeaderAuthorizationKey, strings.TrimSpace(r.AuthScheme+" "+r.Token))
316304
}
317305

318306
return nil

request_test.go

+24-6
Original file line numberDiff line numberDiff line change
@@ -681,13 +681,31 @@ func TestRequestAuthScheme(t *testing.T) {
681681
SetAuthScheme("OAuth").
682682
SetAuthToken("004DDB79-6801-4587-B976-F093E6AC44FF")
683683

684-
resp, err := c.R().
685-
SetAuthScheme("Bearer").
686-
SetAuthToken("004DDB79-6801-4587-B976-F093E6AC44FF-Request").
687-
Get(ts.URL + "/profile")
684+
t.Run("override auth scheme", func(t *testing.T) {
685+
resp, err := c.R().
686+
SetAuthScheme("Bearer").
687+
SetAuthToken("004DDB79-6801-4587-B976-F093E6AC44FF-Request").
688+
Get(ts.URL + "/profile")
689+
690+
assertError(t, err)
691+
assertEqual(t, http.StatusOK, resp.StatusCode())
692+
})
693+
694+
t.Run("empty auth scheme GH954", func(t *testing.T) {
695+
tokenValue := "004DDB79-6801-4587-B976-F093E6AC44FF"
696+
697+
// set client level
698+
c.SetAuthScheme("").
699+
SetAuthToken(tokenValue)
700+
701+
resp, err := c.R().
702+
Get(ts.URL + "/profile")
703+
704+
assertError(t, err)
705+
assertEqual(t, http.StatusOK, resp.StatusCode())
706+
assertEqual(t, tokenValue, resp.Request.Header.Get(hdrAuthorizationKey))
707+
})
688708

689-
assertError(t, err)
690-
assertEqual(t, http.StatusOK, resp.StatusCode())
691709
}
692710

693711
func TestRequestDigestAuth(t *testing.T) {

resty_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -501,19 +501,19 @@ func createAuthServerTLSOptional(t *testing.T, useTLS bool) *httptest.Server {
501501
if r.URL.Path == "/profile" {
502502
// 004DDB79-6801-4587-B976-F093E6AC44FF
503503
auth := r.Header.Get("Authorization")
504-
t.Logf("Bearer Auth: %v", auth)
504+
t.Logf("Auth Header: %v", auth)
505505

506506
w.Header().Set(hdrContentTypeKey, "application/json; charset=utf-8")
507507

508-
if !strings.HasPrefix(auth, "Bearer ") {
508+
if strings.HasPrefix(auth, "Basic ") {
509509
w.Header().Set("Www-Authenticate", "Protected Realm")
510510
w.WriteHeader(http.StatusUnauthorized)
511511
_, _ = w.Write([]byte(`{ "id": "unauthorized", "message": "Invalid credentials" }`))
512512

513513
return
514514
}
515515

516-
if auth[7:] == "004DDB79-6801-4587-B976-F093E6AC44FF" || auth[7:] == "004DDB79-6801-4587-B976-F093E6AC44FF-Request" {
516+
if strings.Contains(auth, "004DDB79-6801-4587-B976-F093E6AC44FF") {
517517
_, _ = w.Write([]byte(`{ "id": "success", "message": "login successful" }`))
518518
}
519519
}

0 commit comments

Comments
 (0)