Skip to content

Commit

Permalink
feat(get-app-authentication): improve error message when privateKey i…
Browse files Browse the repository at this point in the history
…s incomplete (#312)
  • Loading branch information
oscard0m authored Aug 1, 2021
1 parent 348568d commit e0dc894
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 11 deletions.
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(
"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'"
);
} else {
throw error;
}
}
}
22 changes: 22 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,28 @@ 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",
});

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

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(
"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'"
)
);
});

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

0 comments on commit e0dc894

Please sign in to comment.