Skip to content
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

feat(get-app-authentication): improve error message when privateKey is incomplete #312

Merged
merged 4 commits into from
Aug 1, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
32 changes: 21 additions & 11 deletions src/get-app-authentication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,26 @@ export async function getAppAuthentication({
privateKey,
timeDifference,
}: State & { timeDifference?: number }): Promise<AppAuthentication> {
const appAuthentication = await githubAppJwt({
id: +appId,
privateKey,
now: timeDifference && Math.floor(Date.now() / 1000) + timeDifference,
});
try {
const appAuthentication = await githubAppJwt({
id: +appId,
privateKey,
now: timeDifference && Math.floor(Date.now() / 1000) + timeDifference,
});

return {
type: "app",
token: appAuthentication.token,
appId: appAuthentication.appId,
expiresAt: new Date(appAuthentication.expiration * 1000).toISOString(),
};
return {
type: "app",
token: appAuthentication.token,
appId: appAuthentication.appId,
expiresAt: new Date(appAuthentication.expiration * 1000).toISOString(),
};
} catch (error) {
if (privateKey === "-----BEGIN RSA PRIVATE KEY-----") {
throw new Error(
"Private key is incomplete. Make sure it is a single line String and newlines have been replaced by '\n'"
);
} else {
throw error;
}
}
}
30 changes: 30 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,36 @@ test("throws if invalid 'type' is provided", async () => {
);
});

test("throws if invalid Private Key is provided", async () => {
const auth = createAppAuth({
appId: APP_ID,
privateKey: "INVALID_PRIVATE_KEY",
});

const expectedError = new Error();
Object.assign(expectedError, {
code: "ERR_OSSL_PEM_NO_START_LINE",
function: "get_name",
library: "PEM routines",
reason: "no start line",
});

await expect(auth({ type: "app" })).rejects.toEqual(expectedError);
});

test("throws if incomplete Private Key is provided", async () => {
const auth = createAppAuth({
appId: APP_ID,
privateKey: "-----BEGIN RSA PRIVATE KEY-----",
});

await expect(auth({ type: "app" })).rejects.toEqual(
new Error(
"Private key is incomplete. Make sure it is a single line String and newlines have been replaced by '\n'"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! I suggest we slightly change the error message to

Suggested change
"Private key is incomplete. Make sure it is a single line String and newlines have been replaced by '\n'"
"The 'privateKey` option contains only the first line '-----BEGIN RSA PRIVATE KEY-----'. If you are setting it using a `.env` file, make sure it is set on a single line with newlines replaced by '\n'"

what do you think?

)
);
});

test("README example for installation auth", async () => {
const matchCreateInstallationAccessToken: MockMatcherFunction = (
url,
Expand Down