@terrafirmagreg-team/data 1.2.0 → 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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@terrafirmagreg-team/data",
3
- "version": "1.2.0",
3
+ "version": "1.2.2",
4
4
  "type": "module",
5
5
  "description": "Storage drivers and shared helpers",
6
6
  "license": "LGPL-3.0-only",
@@ -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 pemToDer(pem: string): ArrayBuffer {
8
- const b64 = pem.replace(/\\n/g, '\n').replace(/-----(?:BEGIN|END)[^\n-]*-----|\s/g, '');
9
- const bin = Buffer.from(b64, 'base64');
10
- return bin.buffer.slice(
11
- bin.byteOffset,
12
- bin.byteOffset + bin.byteLength,
13
- ) as ArrayBuffer;
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 key = await crypto.subtle.importKey(
26
- 'pkcs8',
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> {