Skip to content

Refine the transform interceptor to only match amazon location service URLs #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions src/cognito/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ 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 = {
identityId: "identityId",
accessKeyId: "accessKeyId",
Expand Down Expand Up @@ -162,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 All @@ -171,6 +202,15 @@ describe("AuthHelper for Cognito", () => {
});
});

it("getMapAuthenticationOptions transformRequest function should pass-through AWS Urls that aren't for the Amazon Location Service unchanged", async () => {
const authHelper = await withIdentityPoolId(cognitoIdentityPoolId);
const transformRequest = authHelper.getMapAuthenticationOptions().transformRequest;

expect(transformRequest(nonLocationAWSUrl)).toStrictEqual({
url: nonLocationAWSUrl,
});
});

it("getLocationClientConfig should provide credentials from cognito", async () => {
const authHelper = await withIdentityPoolId(cognitoIdentityPoolId);
const additionalLocationClientConfig = authHelper.getLocationClientConfig();
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 URLs
if (url.includes("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
Loading