Skip to content

Commit

Permalink
Use correct header when using UAA token in requests [#146297845]
Browse files Browse the repository at this point in the history
Signed-off-by: Danny Sullivan <[email protected]>
  • Loading branch information
pivotal-saman-alvi authored and Danny Sullivan committed Jun 28, 2017
1 parent 0e2e68a commit d298c1a
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 18 deletions.
9 changes: 8 additions & 1 deletion pivnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type Client struct {
token string
userAgent string
logger logger.Logger
usingUAAToken bool

HTTP *http.Client

Expand All @@ -51,6 +52,7 @@ type ClientConfig struct {
Token string
UserAgent string
SkipSSLValidation bool
UsingUAAToken bool
}

func NewClient(
Expand Down Expand Up @@ -90,6 +92,7 @@ func NewClient(
baseURL: baseURL,
token: config.Token,
userAgent: config.UserAgent,
usingUAAToken: config.UsingUAAToken,
logger: logger,
downloader: downloader,
HTTP: httpClient,
Expand Down Expand Up @@ -130,8 +133,12 @@ func (c Client) CreateRequest(
return nil, err
}

if c.usingUAAToken {
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", c.token))
} else {
req.Header.Add("Authorization", fmt.Sprintf("Token %s", c.token))
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Authorization", fmt.Sprintf("Token %s", c.token))
req.Header.Add("User-Agent", c.userAgent)

return req, nil
Expand Down
81 changes: 64 additions & 17 deletions pivnet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ var _ = Describe("PivnetClient", func() {
client pivnet.Client
token string
userAgent string
usingUAAToken bool

releases pivnet.ReleasesResponse

Expand All @@ -46,12 +47,14 @@ var _ = Describe("PivnetClient", func() {
server = ghttp.NewServer()
token = "my-auth-token"
userAgent = "pivnet-resource/0.1.0 (some-url)"
usingUAAToken = false

fakeLogger = &loggerfakes.FakeLogger{}
newClientConfig = pivnet.ClientConfig{
Host: server.URL(),
Token: token,
UserAgent: userAgent,
UsingUAAToken: usingUAAToken,
}
client = pivnet.NewClient(newClientConfig, fakeLogger)
})
Expand All @@ -60,25 +63,69 @@ var _ = Describe("PivnetClient", func() {
server.Close()
})

It("has authenticated headers for each request", func() {
server.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyRequest(
"GET",
fmt.Sprintf("%s/foo", apiPrefix),
Context("when using a pivnet API token", func(){
BeforeEach(func() {
newClientConfig = pivnet.ClientConfig{
Host: server.URL(),
Token: token,
UserAgent: userAgent,
UsingUAAToken: false,
}
client = pivnet.NewClient(newClientConfig, fakeLogger)
})
It("uses token authentication header if configured with a pivnet api token", func() {
server.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyRequest(
"GET",
fmt.Sprintf("%s/foo", apiPrefix),
),
ghttp.VerifyHeaderKV("Authorization", fmt.Sprintf("Token %s", token)),
ghttp.RespondWithJSONEncoded(http.StatusOK, releases),
),
ghttp.VerifyHeaderKV("Authorization", fmt.Sprintf("Token %s", token)),
ghttp.RespondWithJSONEncoded(http.StatusOK, releases),
),
)
)

_, err := client.MakeRequest(
"GET",
"/foo",
http.StatusOK,
nil,
)
Expect(err).NotTo(HaveOccurred())
_, err := client.MakeRequest(
"GET",
"/foo",
http.StatusOK,
nil,
)
Expect(err).NotTo(HaveOccurred())
})
})

Context("when using a UAA token", func(){
BeforeEach(func() {
newClientConfig = pivnet.ClientConfig{
Host: server.URL(),
Token: token,
UserAgent: userAgent,
UsingUAAToken: true,
}
client = pivnet.NewClient(newClientConfig, fakeLogger)
})

It("uses bearer authentication header", func() {
server.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyRequest(
"GET",
fmt.Sprintf("%s/foo", apiPrefix),
),
ghttp.VerifyHeaderKV("Authorization", fmt.Sprintf("Bearer %s", token)),
ghttp.RespondWithJSONEncoded(http.StatusOK, releases),
),
)

_, err := client.MakeRequest(
"GET",
"/foo",
http.StatusOK,
nil,
)
Expect(err).NotTo(HaveOccurred())
})
})

It("sets custom user agent", func() {
Expand Down

0 comments on commit d298c1a

Please sign in to comment.