Skip to content

Commit 9be9e42

Browse files
committed
Fixed to account for GovCloud and some minor feedback fixes
1 parent d914d8a commit 9be9e42

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

src/cognito/index.test.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ describe("AuthHelper for Cognito", () => {
1313
const region = "us-west-2";
1414
const cognitoIdentityPoolId = `${region}:TEST-IDENTITY-POOL-ID`;
1515
const url = "https://maps.geo.us-west-2.amazonaws.com/";
16+
const govCloudUrl = "https://maps.geo-fips.us-gov-west-1.amazonaws.com/";
1617
const nonAWSUrl = "https://example.com/";
1718
const nonLocationAWSUrl = "https://my.cool.service.us-west-2.amazonaws.com/";
1819
const mockedCredentials = {
@@ -163,6 +164,35 @@ describe("AuthHelper for Cognito", () => {
163164
expect(credential).toContain(mockedCredentials.accessKeyId);
164165
});
165166

167+
it("getMapAuthenticationOptions should contain transformRequest function to sign the AWS GovCloud Urls using our custom signer", async () => {
168+
const authHelper = await withIdentityPoolId(cognitoIdentityPoolId);
169+
const transformRequest = authHelper.getMapAuthenticationOptions().transformRequest;
170+
const originalUrl = new URL(govCloudUrl);
171+
const signedUrl = new URL(transformRequest(govCloudUrl).url);
172+
173+
// Host and pathname should still be the same
174+
expect(signedUrl.hostname).toStrictEqual(originalUrl.hostname);
175+
expect(signedUrl.pathname).toStrictEqual(originalUrl.pathname);
176+
177+
const searchParams = signedUrl.searchParams;
178+
expect(searchParams.size).toStrictEqual(6);
179+
180+
// Verify these search params exist on the signed url
181+
// We don't need to test the actual values since they are non-deterministic or constants
182+
const expectedSearchParams = ["X-Amz-Algorithm", "X-Amz-Date", "X-Amz-SignedHeaders", "X-Amz-Signature"];
183+
expectedSearchParams.forEach((value) => {
184+
expect(searchParams.has(value)).toStrictEqual(true);
185+
});
186+
187+
// We can expect the session token to match exactly as passed in
188+
const securityToken = searchParams.get("X-Amz-Security-Token");
189+
expect(securityToken).toStrictEqual(mockedCredentials.sessionToken);
190+
191+
// The credential starts with our access key, the rest is generated
192+
const credential = searchParams.get("X-Amz-Credential");
193+
expect(credential).toContain(mockedCredentials.accessKeyId);
194+
});
195+
166196
it("getMapAuthenticationOptions transformRequest function should pass-through non AWS Urls unchanged", async () => {
167197
const authHelper = await withIdentityPoolId(cognitoIdentityPoolId);
168198
const transformRequest = authHelper.getMapAuthenticationOptions().transformRequest;

src/cognito/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ export async function withIdentityPoolId(
4242
return {
4343
getMapAuthenticationOptions: () => ({
4444
transformRequest: (url: string) => {
45-
// Only sign aws location service URLs
46-
if (url.match("(http|https)://(.*).geo.(.*).amazonaws.com")) {
45+
// Only sign Amazon Location Service URLs
46+
if (url.match("https://maps.(geo|geo-fips).(.*).amazonaws.com")) {
4747
return {
4848
url: Signer.signUrl(url, region, {
4949
access_key: credentials.accessKeyId,

0 commit comments

Comments
 (0)