-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for bearer_token auth (#239)
- Loading branch information
1 parent
fac5634
commit 483d7a5
Showing
10 changed files
with
210 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ repos/ | |
sonar-agent.key | ||
.id_rsa | ||
.vault-token | ||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package roundtrippers | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
type bearerTokenRoundTripper struct { | ||
token string | ||
rt http.RoundTripper | ||
} | ||
|
||
// RoundTrip implements http.RoundTripper's interface | ||
func (rt *bearerTokenRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) { | ||
if len(req.Header.Get("Authorization")) == 0 { | ||
req = cloneRequest(req) | ||
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", rt.token)) | ||
} | ||
return rt.rt.RoundTrip(req) | ||
} | ||
|
||
// NewBearerToken returns an http.RoundTripper that adds the bearer token to a request's header | ||
func NewBearerToken(token string, rt http.RoundTripper) http.RoundTripper { | ||
return &bearerTokenRoundTripper{token, rt} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package roundtrippers | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"strings" | ||
) | ||
|
||
type bearerTokenFileRoundTripper struct { | ||
tokenFile string | ||
rt http.RoundTripper | ||
} | ||
|
||
// RoundTrip implements http.RoundTripper's interface | ||
func (rt *bearerTokenFileRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) { | ||
t, err := ioutil.ReadFile(rt.tokenFile) | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to read bearer token file %s: %s", rt.tokenFile, err) | ||
} | ||
|
||
token := strings.TrimSpace(string(t)) | ||
|
||
req = cloneRequest(req) | ||
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token)) | ||
|
||
return rt.rt.RoundTrip(req) | ||
} | ||
|
||
// NewBearerTokenFile returns an http.RoundTripper that adds the bearer token from a file to a request's header | ||
func NewBearerTokenFile(tokenFile string, rt http.RoundTripper) http.RoundTripper { | ||
return &bearerTokenFileRoundTripper{tokenFile, rt} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package roundtrippers | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
) | ||
|
||
const ( | ||
tokenPath = "testdata/token" | ||
invalidTokenPath = "testdata/missingToken" | ||
) | ||
|
||
func Test_bearerTokenFileRoundTripper_RoundTrip_Happy_Path(t *testing.T) { | ||
rt := NewBearerTokenFile(tokenPath, http.DefaultTransport) | ||
|
||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
expectedHeader := fmt.Sprintf("Bearer %s", token) | ||
if r.Header.Get("Authorization") != expectedHeader { | ||
t.Errorf("Header.Authorization = %s, want %s", r.Header.Get("Authorization"), expectedHeader) | ||
} | ||
})) | ||
defer ts.Close() | ||
|
||
_, err := rt.RoundTrip(httptest.NewRequest(http.MethodGet, ts.URL, nil)) | ||
if err != nil { | ||
t.Errorf("Unexpected error: %v", err) | ||
} | ||
} | ||
|
||
func Test_bearerTokenFileRoundTripper_RoundTrip_Missing_File(t *testing.T) { | ||
rt := NewBearerTokenFile(invalidTokenPath, http.DefaultTransport) | ||
|
||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
return | ||
})) | ||
defer ts.Close() | ||
|
||
_, err := rt.RoundTrip(httptest.NewRequest(http.MethodGet, ts.URL, nil)) | ||
if err == nil { | ||
t.Errorf("Expected error, got none") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package roundtrippers | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
) | ||
|
||
const ( | ||
token = "test-token-value" | ||
) | ||
|
||
func TestBearerTokenRoundTripper_RoundTrip_Happy_Path(t *testing.T) { | ||
rt := NewBearerToken(token, http.DefaultTransport) | ||
|
||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
expectedHeader := fmt.Sprintf("Bearer %s", token) | ||
if r.Header.Get("Authorization") != expectedHeader { | ||
t.Errorf("Header.Authorization = %s, want %s", r.Header.Get("Authorization"), expectedHeader) | ||
} | ||
})) | ||
defer ts.Close() | ||
|
||
_, err := rt.RoundTrip(httptest.NewRequest(http.MethodGet, ts.URL, nil)) | ||
if err != nil { | ||
t.Errorf("Unexpected error: %v", err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
test-token-value |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package roundtrippers | ||
|
||
import "net/http" | ||
|
||
func cloneRequest(req *http.Request) *http.Request { | ||
r := new(http.Request) | ||
|
||
// shallow clone | ||
*r = *req | ||
|
||
// deep copy headers | ||
r.Header = make(http.Header) | ||
for k, v := range req.Header { | ||
r.Header[k] = v | ||
} | ||
|
||
return r | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters