Skip to content

Commit e1e8d57

Browse files
authored
fix: replace escaped newlines with actual newlines in privateKey values (#96)
1 parent cb3cb17 commit e1e8d57

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ For a complete implementation of GitHub App authentication strategies, see [`@oc
101101
<code>string</code>
102102
</th>
103103
<td>
104-
<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.
104+
<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.
105105
</td>
106106
</tr>
107107
<tr>

index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ export default async function githubAppJwt({
1212
privateKey,
1313
now = Math.floor(Date.now() / 1000),
1414
}) {
15+
// Private keys are often times configured as environment variables, in which case line breaks are escaped using `\\n`.
16+
// Replace these here for convenience.
17+
const privateKeyWithNewlines = privateKey.replace(/\\n/g, '\n');
18+
1519
// When creating a JSON Web Token, it sets the "issued at time" (iat) to 30s
1620
// in the past as we have seen people running situations where the GitHub API
1721
// claimed the iat would be in future. It turned out the clocks on the
@@ -26,7 +30,7 @@ export default async function githubAppJwt({
2630
};
2731

2832
const token = await getToken({
29-
privateKey,
33+
privateKey: privateKeyWithNewlines,
3034
payload,
3135
});
3236

test/node.test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,3 +162,18 @@ test("Include the time difference in the expiration and issued_at field", async
162162
t.is(resultPayload.exp, 580);
163163
t.is(resultPayload.iat, -20);
164164
});
165+
166+
test("Replace escaped line breaks with actual linebreaks", async (t) => {
167+
MockDate.set(0);
168+
169+
const result = await githubAppJwt({
170+
id: APP_ID,
171+
privateKey: PRIVATE_KEY_PKCS8.replace(/\n/g, "\\n"),
172+
});
173+
174+
t.deepEqual(result, {
175+
appId: APP_ID,
176+
expiration: 570,
177+
token: BEARER,
178+
});
179+
});

0 commit comments

Comments
 (0)