@lowdefy/node-utils 4.0.0-rc.9 → 4.0.0

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.
@@ -1,5 +1,5 @@
1
1
  /*
2
- Copyright 2020-2023 Lowdefy, Inc
2
+ Copyright 2020-2024 Lowdefy, Inc
3
3
 
4
4
  Licensed under the Apache License, Version 2.0 (the "License");
5
5
  you may not use this file except in compliance with the License.
@@ -1,5 +1,5 @@
1
1
  /*
2
- Copyright 2020-2023 Lowdefy, Inc
2
+ Copyright 2020-2024 Lowdefy, Inc
3
3
 
4
4
  Licensed under the Apache License, Version 2.0 (the "License");
5
5
  you may not use this file except in compliance with the License.
@@ -1,5 +1,5 @@
1
1
  /*
2
- Copyright 2020-2023 Lowdefy, Inc
2
+ Copyright 2020-2024 Lowdefy, Inc
3
3
 
4
4
  Licensed under the Apache License, Version 2.0 (the "License");
5
5
  you may not use this file except in compliance with the License.
@@ -1,5 +1,5 @@
1
1
  /*
2
- Copyright 2020-2023 Lowdefy, Inc
2
+ Copyright 2020-2024 Lowdefy, Inc
3
3
 
4
4
  Licensed under the Apache License, Version 2.0 (the "License");
5
5
  you may not use this file except in compliance with the License.
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*
2
- Copyright 2020-2023 Lowdefy, Inc
2
+ Copyright 2020-2024 Lowdefy, Inc
3
3
 
4
4
  Licensed under the Apache License, Version 2.0 (the "License");
5
5
  you may not use this file except in compliance with the License.
@@ -16,7 +16,8 @@
16
16
  import copyFileOrDirectory from './copyFileOrDirectory.js';
17
17
  import getFileExtension, { getFileSubExtension } from './getFileExtension.js';
18
18
  import getSecretsFromEnv from './getSecretsFromEnv.js';
19
+ import keygenValidateLicense from './keygenValidateLicense.js';
19
20
  import spawnProcess from './spawnProcess.js';
20
21
  import readFile from './readFile.js';
21
22
  import writeFile from './writeFile.js';
22
- export { cleanDirectory, copyFileOrDirectory, getFileExtension, getFileSubExtension, getSecretsFromEnv, spawnProcess, readFile, writeFile };
23
+ export { cleanDirectory, copyFileOrDirectory, getFileExtension, getFileSubExtension, getSecretsFromEnv, keygenValidateLicense, spawnProcess, readFile, writeFile };
@@ -0,0 +1,107 @@
1
+ /*
2
+ Copyright 2020-2024 Lowdefy, Inc
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */ import crypto from 'crypto';
16
+ import keygenValidateLicenseOffline from './keygenValidateLicenseOffline.js';
17
+ import keygenVerifyApiSignature from './keygenVerifyApiSignature.js';
18
+ async function keygenValidateLicense({ config }) {
19
+ const licenseKey = process.env.LOWDEFY_LICENSE_KEY;
20
+ let entitlements = [];
21
+ if (!config) {
22
+ throw new Error('License server config is not defined.');
23
+ }
24
+ // TODO: Return this or undefined/null?
25
+ if (!licenseKey) {
26
+ return {
27
+ id: 'NO_LICENSE',
28
+ code: 'NO_LICENSE',
29
+ entitlements,
30
+ metadata: {},
31
+ timestamp: new Date()
32
+ };
33
+ }
34
+ if (licenseKey.startsWith('key/')) {
35
+ const offlineLicense = await keygenValidateLicenseOffline({
36
+ config,
37
+ licenseKey
38
+ });
39
+ if (offlineLicense.entitlements.includes('OFFLINE')) {
40
+ return offlineLicense;
41
+ }
42
+ }
43
+ const nonce = crypto.randomInt(1000000000000);
44
+ const res = await fetch(`https://api.keygen.sh/v1/accounts/${config.accountId}/licenses/actions/validate-key`, {
45
+ method: 'POST',
46
+ headers: {
47
+ 'Content-Type': 'application/vnd.api+json',
48
+ Accept: 'application/vnd.api+json'
49
+ },
50
+ body: JSON.stringify({
51
+ meta: {
52
+ key: licenseKey,
53
+ nonce,
54
+ scope: {
55
+ product: config.productId
56
+ }
57
+ }
58
+ })
59
+ });
60
+ const body = await res.text();
61
+ const { meta, data, errors } = JSON.parse(body);
62
+ if (meta.nonce !== nonce) {
63
+ return {
64
+ id: 'INVALID_LICENSE',
65
+ code: 'INVALID_LICENSE',
66
+ entitlements,
67
+ metadata: {},
68
+ timestamp: new Date()
69
+ };
70
+ }
71
+ if (errors) {
72
+ return {
73
+ id: 'INVALID_LICENSE',
74
+ code: 'INVALID_LICENSE',
75
+ entitlements,
76
+ metadata: {},
77
+ timestamp: new Date()
78
+ };
79
+ }
80
+ await keygenVerifyApiSignature({
81
+ body,
82
+ config,
83
+ date: res.headers.get('date'),
84
+ signatureHeader: res.headers.get('keygen-signature'),
85
+ target: `post /v1/accounts/${config.accountId}/licenses/actions/validate-key`
86
+ });
87
+ if (data?.relationships?.entitlements?.links?.related) {
88
+ const entitlementResponse = await (await fetch(`https://api.keygen.sh/${data.relationships.entitlements.links.related}`, {
89
+ method: 'GET',
90
+ headers: {
91
+ 'Content-Type': 'application/vnd.api+json',
92
+ Accept: 'application/vnd.api+json',
93
+ Authorization: `License ${licenseKey}`
94
+ }
95
+ })).json();
96
+ entitlements = (entitlementResponse?.data ?? []).map((ent)=>ent?.attributes?.code);
97
+ }
98
+ return {
99
+ id: data?.id,
100
+ code: meta?.code,
101
+ entitlements,
102
+ expiry: data?.attributes?.expiry ? new Date(data?.attributes?.expiry) : undefined,
103
+ metadata: data?.attributes?.metadata,
104
+ timestamp: new Date()
105
+ };
106
+ }
107
+ export default keygenValidateLicense;
@@ -0,0 +1,53 @@
1
+ /*
2
+ Copyright 2020-2024 Lowdefy, Inc
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */ import crypto from 'crypto';
16
+ async function keygenValidateLicenseOffline({ config, licenseKey }) {
17
+ const [data, signature] = licenseKey.split('.');
18
+ const [prefix, enc] = data.split('/');
19
+ if (prefix !== 'key') {
20
+ throw new Error(`Unsupported prefix '${prefix}'`);
21
+ }
22
+ const verifyKey = crypto.createPublicKey({
23
+ format: 'der',
24
+ type: 'spki',
25
+ key: Buffer.from(config.publicKey, 'base64')
26
+ });
27
+ const signatureBytes = Buffer.from(signature, 'base64');
28
+ const dataBytes = Buffer.from(data);
29
+ const ok = crypto.verify(null, dataBytes, verifyKey, signatureBytes);
30
+ if (!ok) {
31
+ throw new Error('TODO');
32
+ }
33
+ const decoded = JSON.parse(Buffer.from(enc, 'base64'));
34
+ const expiry = new Date(decoded.expiry);
35
+ if (decoded.product !== config.productId) {
36
+ return {
37
+ id: 'INVALID_LICENSE',
38
+ code: 'INVALID_LICENSE',
39
+ entitlements: [],
40
+ metadata: {},
41
+ timestamp: new Date()
42
+ };
43
+ }
44
+ return {
45
+ id: decoded.id,
46
+ code: expiry.valueOf() < Date.now() ? 'EXPIRED' : 'VALID',
47
+ entitlements: decoded.entitlements,
48
+ expiry: expiry,
49
+ metadata: decoded.metadata,
50
+ timestamp: new Date()
51
+ };
52
+ }
53
+ export default keygenValidateLicenseOffline;
@@ -0,0 +1,45 @@
1
+ /*
2
+ Copyright 2020-2024 Lowdefy, Inc
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */ import crypto from 'crypto';
16
+ async function keygenVerifyApiSignature({ body, config, date, signatureHeader, target }) {
17
+ if (signatureHeader == null) {
18
+ throw new Error('Signature was expected but is missing'); // TODO
19
+ }
20
+ const [, signature] = signatureHeader.match(/signature="(.*)",/i);
21
+ const sha256 = crypto.createHash('sha256').update(body);
22
+ const digest = `sha-256=${sha256.digest('base64')}`;
23
+ // Rebuild the signing data
24
+ const data = [
25
+ `(request-target): ${target}`,
26
+ `host: api.keygen.sh`,
27
+ `date: ${date}`,
28
+ `digest: ${digest}`
29
+ ].join('\n');
30
+ // Decode DER verify key
31
+ const verifyKey = crypto.createPublicKey({
32
+ key: Buffer.from(config.publicKey, 'base64'),
33
+ format: 'der',
34
+ type: 'spki'
35
+ });
36
+ // Convert into bytes
37
+ const signatureBytes = Buffer.from(signature, 'base64');
38
+ const dataBytes = Buffer.from(data);
39
+ // Cryptographically verify data against the signature
40
+ const ok = crypto.verify(null, dataBytes, verifyKey, signatureBytes);
41
+ if (!ok) {
42
+ throw new Error(`Signature does not match: ${signature}`); // TODO
43
+ }
44
+ }
45
+ export default keygenVerifyApiSignature;
package/dist/readFile.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*
2
- Copyright 2020-2023 Lowdefy, Inc
2
+ Copyright 2020-2024 Lowdefy, Inc
3
3
 
4
4
  Licensed under the Apache License, Version 2.0 (the "License");
5
5
  you may not use this file except in compliance with the License.
@@ -1,5 +1,5 @@
1
1
  /*
2
- Copyright 2020-2023 Lowdefy, Inc
2
+ Copyright 2020-2024 Lowdefy, Inc
3
3
 
4
4
  Licensed under the Apache License, Version 2.0 (the "License");
5
5
  you may not use this file except in compliance with the License.
@@ -13,7 +13,7 @@
13
13
  See the License for the specific language governing permissions and
14
14
  limitations under the License.
15
15
  */ import { spawn } from 'child_process';
16
- function createStdIOHandler({ lineHandler }) {
16
+ function createStdIOHandler({ lineHandler }) {
17
17
  function handler(data) {
18
18
  data.toString('utf8').split('\n').forEach((line)=>{
19
19
  if (line) {
@@ -23,7 +23,7 @@ function createStdIOHandler({ lineHandler }) {
23
23
  }
24
24
  return handler;
25
25
  }
26
- function spawnProcess({ args , command , processOptions , returnProcess , stdErrLineHandler , stdOutLineHandler =()=>{} }) {
26
+ function spawnProcess({ args, command, processOptions, returnProcess, stdErrLineHandler, stdOutLineHandler = ()=>{} }) {
27
27
  if (!stdErrLineHandler) {
28
28
  stdErrLineHandler = stdOutLineHandler;
29
29
  }
package/dist/writeFile.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*
2
- Copyright 2020-2023 Lowdefy, Inc
2
+ Copyright 2020-2024 Lowdefy, Inc
3
3
 
4
4
  Licensed under the Apache License, Version 2.0 (the "License");
5
5
  you may not use this file except in compliance with the License.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lowdefy/node-utils",
3
- "version": "4.0.0-rc.9",
3
+ "version": "4.0.0",
4
4
  "license": "Apache-2.0",
5
5
  "description": "",
6
6
  "homepage": "https://lowdefy.com",
@@ -33,24 +33,22 @@
33
33
  "files": [
34
34
  "dist/*"
35
35
  ],
36
- "scripts": {
37
- "build": "swc src --out-dir dist --config-file ../../../.swcrc --delete-dir-on-start",
38
- "clean": "rm -rf dist",
39
- "prepublishOnly": "pnpm build",
40
- "test": "node --experimental-vm-modules node_modules/jest/bin/jest.js"
41
- },
42
36
  "dependencies": {
43
- "@lowdefy/helpers": "4.0.0-rc.9",
44
- "fs-extra": "11.1.0"
37
+ "@lowdefy/helpers": "4.0.0",
38
+ "fs-extra": "11.1.1"
45
39
  },
46
40
  "devDependencies": {
47
- "@swc/cli": "0.1.59",
48
- "@swc/core": "1.3.24",
49
- "@swc/jest": "0.2.24",
50
- "jest": "28.1.0"
41
+ "@swc/cli": "0.1.63",
42
+ "@swc/core": "1.3.99",
43
+ "@swc/jest": "0.2.29",
44
+ "jest": "28.1.3"
51
45
  },
52
46
  "publishConfig": {
53
47
  "access": "public"
54
48
  },
55
- "gitHead": "d20e6ac424643feca527a732dc2b0710713c8243"
56
- }
49
+ "scripts": {
50
+ "build": "swc src --out-dir dist --config-file ../../../.swcrc --delete-dir-on-start",
51
+ "clean": "rm -rf dist",
52
+ "test": "node --experimental-vm-modules node_modules/jest/bin/jest.js"
53
+ }
54
+ }