@terrafirmagreg-team/data 1.2.1 → 1.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/lib/app-token.ts +10 -20
package/package.json
CHANGED
package/src/lib/app-token.ts
CHANGED
|
@@ -1,16 +1,17 @@
|
|
|
1
|
+
import { createPrivateKey, sign } from 'node:crypto';
|
|
1
2
|
import { env } from './env';
|
|
2
3
|
import { githubFetch } from './github';
|
|
3
4
|
|
|
4
5
|
const TOKEN_TTL_MS = 50 * 60 * 1000;
|
|
5
6
|
const cache = new Map<string, { token: Promise<string>; exp: number }>();
|
|
6
7
|
|
|
7
|
-
function
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
return
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
)
|
|
8
|
+
function privateKey(pem: string) {
|
|
9
|
+
const normalized = pem.replace(/\\n/g, '\n');
|
|
10
|
+
if (normalized.includes('-----BEGIN')) return createPrivateKey(normalized);
|
|
11
|
+
return createPrivateKey({
|
|
12
|
+
key: Buffer.from(normalized.trim(), 'base64'),
|
|
13
|
+
format: 'der',
|
|
14
|
+
});
|
|
14
15
|
}
|
|
15
16
|
|
|
16
17
|
async function appJwt(): Promise<string> {
|
|
@@ -22,19 +23,8 @@ async function appJwt(): Promise<string> {
|
|
|
22
23
|
const pack = (value: object) =>
|
|
23
24
|
Buffer.from(JSON.stringify(value)).toString('base64url');
|
|
24
25
|
const payload = `${pack({ alg: 'RS256', typ: 'JWT' })}.${pack({ iat: now - 60, exp: now + 540, iss: appId })}`;
|
|
25
|
-
const
|
|
26
|
-
|
|
27
|
-
pemToDer(pem),
|
|
28
|
-
{ name: 'RSASSA-PKCS1-v1_5', hash: 'SHA-256' },
|
|
29
|
-
false,
|
|
30
|
-
['sign'],
|
|
31
|
-
);
|
|
32
|
-
const signature = await crypto.subtle.sign(
|
|
33
|
-
'RSASSA-PKCS1-v1_5',
|
|
34
|
-
key,
|
|
35
|
-
new TextEncoder().encode(payload),
|
|
36
|
-
);
|
|
37
|
-
return `${payload}.${Buffer.from(signature).toString('base64url')}`;
|
|
26
|
+
const signature = sign('sha256', Buffer.from(payload), privateKey(pem));
|
|
27
|
+
return `${payload}.${signature.toString('base64url')}`;
|
|
38
28
|
}
|
|
39
29
|
|
|
40
30
|
async function mintInstallationToken(repo: string): Promise<string> {
|