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

fix: replace escaped newlines with actual newlines in privateKey values #96

Merged
merged 2 commits into from
Apr 29, 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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ For a complete implementation of GitHub App authentication strategies, see [`@oc
<code>string</code>
</th>
<td>
<strong>Required</strong>. Content of the <code>*.pem</code> file you downloaded from the app’s about page. You can generate a new private key if needed. Make sure to preserve the line breaks.
<strong>Required</strong>. Content of the <code>*.pem</code> file you downloaded from the app’s about page. You can generate a new private key if needed. Make sure to preserve the line breaks. If your private key contains escaped newlines (`\\n`), they will be automatically replaced with actual newlines.
</td>
</tr>
<tr>
Expand Down
6 changes: 5 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ export default async function githubAppJwt({
privateKey,
now = Math.floor(Date.now() / 1000),
}) {
// Private keys are often times configured as environment variables, in which case line breaks are escaped using `\\n`.
// Replace these here for convenience.
const privateKeyWithNewlines = privateKey.replace(/\\n/g, '\n');

// When creating a JSON Web Token, it sets the "issued at time" (iat) to 30s
// in the past as we have seen people running situations where the GitHub API
// claimed the iat would be in future. It turned out the clocks on the
Expand All @@ -26,7 +30,7 @@ export default async function githubAppJwt({
};

const token = await getToken({
privateKey,
privateKey: privateKeyWithNewlines,
payload,
});

Expand Down
15 changes: 15 additions & 0 deletions test/node.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,18 @@ test("Include the time difference in the expiration and issued_at field", async
t.is(resultPayload.exp, 580);
t.is(resultPayload.iat, -20);
});

test("Replace escaped line breaks with actual linebreaks", async (t) => {
MockDate.set(0);

const result = await githubAppJwt({
id: APP_ID,
privateKey: PRIVATE_KEY_PKCS8.replace(/\n/g, "\\n"),
});

t.deepEqual(result, {
appId: APP_ID,
expiration: 570,
token: BEARER,
});
});