Skip to content

Commit

Permalink
Fix formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
philmcmahon committed Jun 20, 2024
1 parent 16c7086 commit 9533976
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/patterns/ec2-app/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import {
import { AppAccess } from "../../types";
import type { GuAsgCapacity, GuDomainName } from "../../types";
import type { AmigoProps } from "../../types/amigo";
import {getUserPoolDomainPrefix} from "../../utils/cognito/cognito";
import { getUserPoolDomainPrefix } from "../../utils/cognito/cognito";

export interface AccessLoggingProps {
/**
Expand Down
23 changes: 11 additions & 12 deletions src/utils/cognito/cognito.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,29 @@ describe("getUserPoolDomainPrefix", () => {
const shortPrefix = "hello";
const domainPrefix = getUserPoolDomainPrefix(shortPrefix);

expect(domainPrefix.length).toEqual(32+shortPrefix.length+1)
expect(domainPrefix.length).toEqual(32 + shortPrefix.length + 1);
});

it("should include the full prefix where there is space for a 5 character hash", () => {
const prefix55 = new Array(55 + 1).join( "#" );
const prefix55 = new Array(55 + 1).join("#");
const domainPrefix = getUserPoolDomainPrefix(prefix55);

expect(domainPrefix.includes(prefix55)).toEqual(true)
expect(domainPrefix.length).toEqual(63)
expect(domainPrefix.includes(prefix55)).toEqual(true);
expect(domainPrefix.length).toEqual(63);
});

it("should not include the full prefix where there is not space for a 5 character hash", () => {
const prefix56 = new Array(58 + 1).join( "#" );
const prefix56 = new Array(58 + 1).join("#");
const domainPrefix = getUserPoolDomainPrefix(prefix56);

expect(domainPrefix.includes(prefix56)).toEqual(false)
expect(domainPrefix.length).toEqual(63)
})
expect(domainPrefix.includes(prefix56)).toEqual(false);
expect(domainPrefix.length).toEqual(63);
});

it("should be 63 characters long even with a very long prefix", () => {
const prefix = new Array(63 + 1).join( "#" );
const prefix = new Array(63 + 1).join("#");
const domainPrefix = getUserPoolDomainPrefix(prefix);

expect(domainPrefix.length).toEqual(63)
})

expect(domainPrefix.length).toEqual(63);
});
});
6 changes: 3 additions & 3 deletions src/utils/cognito/cognito.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import crypto from "crypto";
export const getUserPoolDomainPrefix = (prefix: string): string => {
// the user pool domain prefix has a max length of 63 characters. We want them to be unique so we add a hash to the end
// here we hope that a 5 character hash is unique enough to avoid collisions
const maxLength = 63
const prefixLengthWithHashSpace = maxLength - 6 // 6 = 5 char hash, 1 hyphen
const maxLength = 63;
const prefixLengthWithHashSpace = maxLength - 6; // 6 = 5 char hash, 1 hyphen
const suffix = crypto.createHash("md5").update(prefix).digest("hex");

// make space in prefix for hash
const prefixTrimmed = prefix.slice(0, prefixLengthWithHashSpace);

return `${prefixTrimmed}-${suffix}`.slice(0, maxLength);
}
};

0 comments on commit 9533976

Please sign in to comment.