Skip to content

Commit

Permalink
Fixed to account for GovCloud and some minor feedback fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
cgalvan committed Jul 17, 2024
1 parent d914d8a commit 9be9e42
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
30 changes: 30 additions & 0 deletions src/cognito/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ describe("AuthHelper for Cognito", () => {
const region = "us-west-2";
const cognitoIdentityPoolId = `${region}:TEST-IDENTITY-POOL-ID`;
const url = "https://maps.geo.us-west-2.amazonaws.com/";
const govCloudUrl = "https://maps.geo-fips.us-gov-west-1.amazonaws.com/";
const nonAWSUrl = "https://example.com/";
const nonLocationAWSUrl = "https://my.cool.service.us-west-2.amazonaws.com/";
const mockedCredentials = {
Expand Down Expand Up @@ -163,6 +164,35 @@ describe("AuthHelper for Cognito", () => {
expect(credential).toContain(mockedCredentials.accessKeyId);
});

it("getMapAuthenticationOptions should contain transformRequest function to sign the AWS GovCloud Urls using our custom signer", async () => {
const authHelper = await withIdentityPoolId(cognitoIdentityPoolId);
const transformRequest = authHelper.getMapAuthenticationOptions().transformRequest;
const originalUrl = new URL(govCloudUrl);
const signedUrl = new URL(transformRequest(govCloudUrl).url);

// Host and pathname should still be the same
expect(signedUrl.hostname).toStrictEqual(originalUrl.hostname);
expect(signedUrl.pathname).toStrictEqual(originalUrl.pathname);

const searchParams = signedUrl.searchParams;
expect(searchParams.size).toStrictEqual(6);

// Verify these search params exist on the signed url
// We don't need to test the actual values since they are non-deterministic or constants
const expectedSearchParams = ["X-Amz-Algorithm", "X-Amz-Date", "X-Amz-SignedHeaders", "X-Amz-Signature"];
expectedSearchParams.forEach((value) => {
expect(searchParams.has(value)).toStrictEqual(true);
});

// We can expect the session token to match exactly as passed in
const securityToken = searchParams.get("X-Amz-Security-Token");
expect(securityToken).toStrictEqual(mockedCredentials.sessionToken);

// The credential starts with our access key, the rest is generated
const credential = searchParams.get("X-Amz-Credential");
expect(credential).toContain(mockedCredentials.accessKeyId);
});

it("getMapAuthenticationOptions transformRequest function should pass-through non AWS Urls unchanged", async () => {
const authHelper = await withIdentityPoolId(cognitoIdentityPoolId);
const transformRequest = authHelper.getMapAuthenticationOptions().transformRequest;
Expand Down
4 changes: 2 additions & 2 deletions src/cognito/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ export async function withIdentityPoolId(
return {
getMapAuthenticationOptions: () => ({
transformRequest: (url: string) => {
// Only sign aws location service URLs
if (url.match("(http|https)://(.*).geo.(.*).amazonaws.com")) {
// Only sign Amazon Location Service URLs
if (url.match("https://maps.(geo|geo-fips).(.*).amazonaws.com")) {
return {
url: Signer.signUrl(url, region, {
access_key: credentials.accessKeyId,
Expand Down

0 comments on commit 9be9e42

Please sign in to comment.