-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Adds access token authentication to client (#166)
- Loading branch information
1 parent
82df440
commit 7ba6d05
Showing
5 changed files
with
77 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
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,48 @@ | ||
import { ClientConfiguration } from "../clientConfiguration"; | ||
import { createRequestHeaders } from "./createRequestHeaders"; | ||
|
||
describe("createRequestHeaders", () => { | ||
test("when api key is provided it is put into the correct request header", () => { | ||
const configuration: ClientConfiguration = { | ||
instanceURL: "https://my.octopus.app", | ||
userAgentApp: "createRequestHeader-tests", | ||
apiKey: "api-key", | ||
}; | ||
|
||
const headers = createRequestHeaders(configuration); | ||
|
||
expect(headers).toEqual({ | ||
"Accept-Encoding": "gzip,deflate,compress", | ||
"X-Octopus-ApiKey": "api-key", | ||
}); | ||
}); | ||
|
||
test("when an access token is provided it is put into the correct request header", () => { | ||
const configuration: ClientConfiguration = { | ||
instanceURL: "https://my.octopus.app", | ||
userAgentApp: "createRequestHeader-tests", | ||
accessToken: "access-token", | ||
}; | ||
|
||
const headers = createRequestHeaders(configuration); | ||
|
||
expect(headers).toEqual({ | ||
"Accept-Encoding": "gzip,deflate,compress", | ||
Authorization: "Bearer access-token", | ||
}); | ||
}); | ||
|
||
test("when neither an access token or api key is provided then the correct api key header is filled in for backward compatibility", () => { | ||
const configuration: ClientConfiguration = { | ||
instanceURL: "https://my.octopus.app", | ||
userAgentApp: "createRequestHeader-tests", | ||
}; | ||
|
||
const headers = createRequestHeaders(configuration); | ||
|
||
expect(headers).toEqual({ | ||
"Accept-Encoding": "gzip,deflate,compress", | ||
"X-Octopus-ApiKey": "", | ||
}); | ||
}); | ||
}); |
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,20 @@ | ||
import type { RawAxiosRequestHeaders } from "axios"; | ||
import { ClientConfiguration } from "../clientConfiguration"; | ||
|
||
export function createRequestHeaders(configuration: ClientConfiguration): RawAxiosRequestHeaders { | ||
const headers: RawAxiosRequestHeaders = { | ||
"Accept-Encoding": "gzip,deflate,compress", // HACK: required for https://github.com/axios/axios/issues/5346 -- this line can be removed once this bug has been fixed | ||
}; | ||
if (configuration.apiKey) { | ||
headers["X-Octopus-ApiKey"] = configuration.apiKey; | ||
} | ||
if (configuration.accessToken) { | ||
headers["Authorization"] = `Bearer ${configuration.accessToken}`; | ||
} | ||
if (!configuration.accessToken && !configuration.apiKey) { | ||
// Backward compatibility: Add the api key header in with a blank value | ||
headers["X-Octopus-ApiKey"] = ""; | ||
} | ||
|
||
return headers; | ||
} |
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