@lighthouse/common 4.37.0-canary-4 → 4.37.0-canary-5
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/dist/helpers/fetch-image/index.js +1 -1
- package/dist/helpers/get-cloudfront-cookies/index.js +14 -17
- package/dist/helpers/get-secret-value/index.js +2 -2
- package/lib/helpers/fetch-image/index.js +1 -1
- package/lib/helpers/fetch-image/index.js.map +1 -1
- package/lib/helpers/get-cloudfront-cookies/index.js +10 -16
- package/lib/helpers/get-cloudfront-cookies/index.js.map +1 -1
- package/lib/helpers/get-secret-value/index.js +2 -2
- package/lib/helpers/get-secret-value/index.js.map +1 -1
- package/package.json +2 -3
|
@@ -50,7 +50,7 @@ function fetchImage(url, options = {}) {
|
|
|
50
50
|
};
|
|
51
51
|
const applicationId = options.applicationId;
|
|
52
52
|
|
|
53
|
-
if (url.
|
|
53
|
+
if (url && url.includes('.cloudfront.net')) {
|
|
54
54
|
fetchOptions.credentials = 'include'; // Ensure cookies are sent with the request
|
|
55
55
|
|
|
56
56
|
const signedCookies = (0, _getCloudfrontCookies.generateCloudFrontCookies)({
|
|
@@ -1,19 +1,18 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
|
|
3
5
|
Object.defineProperty(exports, "__esModule", {
|
|
4
6
|
value: true
|
|
5
7
|
});
|
|
6
8
|
exports.generateCloudFrontCookies = generateCloudFrontCookies;
|
|
7
9
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
10
|
+
var _awsSdk = _interopRequireDefault(require("aws-sdk"));
|
|
11
|
+
|
|
12
|
+
var _getSecretValue = require("../get-secret-value");
|
|
11
13
|
|
|
12
|
-
const
|
|
13
|
-
getSecretValue
|
|
14
|
-
} = require('../get-secret-value');
|
|
14
|
+
const getSignedCookies = _awsSdk.default.CloudFront.Signer.getSignedCookies;
|
|
15
15
|
|
|
16
|
-
const logger = require('../../logger');
|
|
17
16
|
/**
|
|
18
17
|
* Generate CloudFront signed cookies for authenticated users
|
|
19
18
|
* @param {Object} options - Configuration options
|
|
@@ -27,15 +26,13 @@ const logger = require('../../logger');
|
|
|
27
26
|
* "CloudFront-Policy"?: string
|
|
28
27
|
* } | null>} Signed cookies object or null if disabled/failed
|
|
29
28
|
*/
|
|
30
|
-
|
|
31
|
-
|
|
32
29
|
async function generateCloudFrontCookies({
|
|
33
30
|
userId,
|
|
34
31
|
applicationId
|
|
35
32
|
}) {
|
|
36
33
|
// Early return if CloudFront is not configured
|
|
37
34
|
if (!process.env.CLOUDFRONT_DOMAIN || !process.env.CLOUDFRONT_KEY_PAIR_ID) {
|
|
38
|
-
|
|
35
|
+
console.debug('CloudFront cookie generation skipped - not configured');
|
|
39
36
|
return null;
|
|
40
37
|
}
|
|
41
38
|
|
|
@@ -44,7 +41,7 @@ async function generateCloudFrontCookies({
|
|
|
44
41
|
const privateKey = await getPrivateKey();
|
|
45
42
|
|
|
46
43
|
if (!privateKey) {
|
|
47
|
-
|
|
44
|
+
console.warn('CloudFront private key not available');
|
|
48
45
|
return null;
|
|
49
46
|
} // Set expiration time (14 days from now)
|
|
50
47
|
|
|
@@ -63,7 +60,7 @@ async function generateCloudFrontCookies({
|
|
|
63
60
|
dateLessThan: expiration.getTime() // Use epoch timestamp in milliseconds
|
|
64
61
|
|
|
65
62
|
});
|
|
66
|
-
|
|
63
|
+
console.info('CloudFront cookies generated successfully', {
|
|
67
64
|
userId,
|
|
68
65
|
applicationId,
|
|
69
66
|
resourcePath,
|
|
@@ -71,7 +68,7 @@ async function generateCloudFrontCookies({
|
|
|
71
68
|
});
|
|
72
69
|
return signedCookies;
|
|
73
70
|
} catch (error) {
|
|
74
|
-
|
|
71
|
+
console.error('Failed to generate CloudFront cookies', {
|
|
75
72
|
userId,
|
|
76
73
|
applicationId,
|
|
77
74
|
error: {
|
|
@@ -95,18 +92,18 @@ async function getPrivateKey() {
|
|
|
95
92
|
}
|
|
96
93
|
|
|
97
94
|
if (!process.env.CLOUDFRONT_PRIVATE_KEY_SECRET_ID) {
|
|
98
|
-
|
|
95
|
+
console.warn('CLOUDFRONT_PRIVATE_KEY_SECRET_ID not configured');
|
|
99
96
|
return null;
|
|
100
97
|
}
|
|
101
98
|
|
|
102
99
|
try {
|
|
103
|
-
const privateKey = await getSecretValue(process.env.CLOUDFRONT_PRIVATE_KEY_SECRET_ID, 'CLOUDFRONT_PRIVATE_KEY');
|
|
100
|
+
const privateKey = await (0, _getSecretValue.getSecretValue)(process.env.CLOUDFRONT_PRIVATE_KEY_SECRET_ID, 'CLOUDFRONT_PRIVATE_KEY');
|
|
104
101
|
privateKeyCache = privateKey;
|
|
105
102
|
cacheExpiry = now + CACHE_TTL;
|
|
106
|
-
|
|
103
|
+
console.debug('CloudFront private key retrieved and cached');
|
|
107
104
|
return privateKey;
|
|
108
105
|
} catch (error) {
|
|
109
|
-
|
|
106
|
+
console.error('Failed to retrieve CloudFront private key', {
|
|
110
107
|
secretId: process.env.CLOUDFRONT_PRIVATE_KEY_SECRET_ID,
|
|
111
108
|
error: {
|
|
112
109
|
message: error.message,
|
|
@@ -7,7 +7,7 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
7
7
|
});
|
|
8
8
|
exports.default = getSecretValue;
|
|
9
9
|
|
|
10
|
-
var
|
|
10
|
+
var _awsSdk = _interopRequireDefault(require("aws-sdk"));
|
|
11
11
|
|
|
12
12
|
var _lodash = require("lodash");
|
|
13
13
|
|
|
@@ -17,7 +17,7 @@ function getSecretValue(secretId, secretKey) {
|
|
|
17
17
|
} // TODO: update these credentials to specific values for service
|
|
18
18
|
|
|
19
19
|
|
|
20
|
-
const secretsClient = new
|
|
20
|
+
const secretsClient = new _awsSdk.default.SecretsManager({
|
|
21
21
|
accessKeyId: process.env.AWS_KEY,
|
|
22
22
|
region: process.env.AWS_SECRET_MANAGER_REGION,
|
|
23
23
|
secretAccessKey: process.env.AWS_SECRET
|
|
@@ -40,7 +40,7 @@ export function fetchImage(url) {
|
|
|
40
40
|
|
|
41
41
|
var applicationId = options.applicationId;
|
|
42
42
|
|
|
43
|
-
if (url.
|
|
43
|
+
if (url && url.includes('.cloudfront.net')) {
|
|
44
44
|
fetchOptions.credentials = 'include'; // Ensure cookies are sent with the request
|
|
45
45
|
|
|
46
46
|
var signedCookies = generateCloudFrontCookies({
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/helpers/fetch-image/index.js"],"names":["atob","btoa","fetchPonyfill","Promise","LIGHTHOUSE_LOGO_URL","imageNotFound","generateCloudFrontCookies","fetch","self","contentTypes","defaultOptions","cache","fetchImage","url","options","encodedUrl","encodeURI","fetchOptions","applicationId","contains","credentials","signedCookies","userId","headers","Cookie","Object","entries","map","key","value","join","isHeader","then","response","contentHeader","get","contentType","reject","Error","ok","imageType","arrayBuffer","buffer","base64Flag","imageStr","arrayBufferToBase64","base64","isValid","validateBase64Image","catch","error","console","binary","bytes","slice","call","Uint8Array","forEach","b","String","fromCharCode","base64String","isJpeg","startsWith","validateJpegImage","isPng","validatePngImage","base64string","src","imageData","from","replace","c","charCodeAt","imageCorrupted","length","sequence","i"],"mappings":";;;;;;;;AAAA,SAASA,IAAT,EAAeC,IAAf,QAA2B,kBAA3B;AACA,OAAOC,aAAP,MAA0B,gBAA1B;AACA,OAAOC,OAAP,MAAoB,UAApB;AACA,SAASC,mBAAT,QAAoC,iBAApC;AACA,SAASC,aAAT,QAA8B,cAA9B;AACA,SAASC,yBAAT,QAA0C,2BAA1C,C,CACA;AACA;AACA;;AACA,IAAMC,KAAK,GACR,QAAOC,IAAP,yCAAOA,IAAP,OAAgB,QAAhB,IAA4BA,IAAI,CAACD,KAAlC,IAA4CL,aAAa,CAAC;AAAEC,EAAAA,OAAO,EAAPA;AAAF,CAAD,CAAb,CAA2BI,KADzE;AAGA,IAAME,YAAY,GAAG;AACnB,eAAa,KADM;AAEnB,gBAAc;AAFK,CAArB;A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AME,SAAS,GAAGb,UAAU,CAACc,IAAX,CAChBtE,IAAI,CAACoE,GAAG,CAACG,OAAJ,CAAY,wBAAZ,EAAsC,EAAtC,CAAD,CADY,EAEhB,UAAAC,CAAC;AAAA,WAAIA,CAAC,CAACC,UAAF,CAAa,CAAb,CAAJ;AAAA,GAFe,CAAlB;AAIA,MAAMG,QAAQ,GAAG,CAAC,CAAD,EAAI,CAAJ,EAAO,CAAP,EAAU,CAAV,EAAa,EAAb,EAAiB,EAAjB,EAAqB,EAArB,EAAyB,EAAzB,EAA6B,GAA7B,EAAkC,EAAlC,EAAsC,EAAtC,EAA0C,GAA1C,CAAjB,CAN6C,CAMmB;AAEhE;;AACA,OAAK,IAAIC,CAAC,GAAG,EAAb,EAAiBA,CAAC,GAAG,CAArB,EAAwBA,CAAC,EAAzB,EAA6B;AAC3B,QAAIR,SAAS,CAACA,SAAS,CAACM,MAAV,GAAmBE,CAApB,CAAT,KAAoCD,QAAQ,CAAC,KAAKC,CAAN,CAAhD,EAA0D;AACxD,aAAO,KAAP;AACD;AACF;;AAED,SAAO,IAAP;AACD","sourcesContent":["import { atob, btoa } from '@lighthouse/abab'\nimport fetchPonyfill from 'fetch-ponyfill'\nimport Promise from 'bluebird'\nimport { LIGHTHOUSE_LOGO_URL } from '../../constants'\nimport { imageNotFound } from '../../images'\nimport { generateCloudFrontCookies } from '../get-cloudfront-cookies'\n// NOTE use the native fetch if it's available in the browser, because the\n// ponyfill (which actually uses the github polyfill) does not support all the\n// same options as native fetch\nconst fetch =\n (typeof self === 'object' && self.fetch) || fetchPonyfill({ Promise }).fetch\n\nconst contentTypes = {\n 'image/png': 'png',\n 'image/jpeg': 'jpeg',\n}\n\nconst defaultOptions = {\n // NOTE The cache: no-cache option is important to avoid an issue with CORS\n // and caching on Chrome. Here's a good explanation of the issue:\n // https://stackoverflow.com/a/37455118\n // In our case, when loading the web version of a form, the signature image is\n // cached without the correct CORS headers. If the pdf is then generated,\n // there's a mismatch between the cached image headers and the CORS headers\n // sent from the fetch request, causing an error\n cache: 'no-cache',\n}\n\nexport function fetchImage(url, options = {}) {\n const encodedUrl = encodeURI(url)\n const fetchOptions = {\n ...defaultOptions,\n ...options,\n }\n\n const applicationId = options.applicationId\n\n if (url.contains('.cloudfront.net')) {\n fetchOptions.credentials = 'include' // Ensure cookies are sent with the request\n\n const signedCookies = generateCloudFrontCookies({\n userId: 'anonymous',\n applicationId,\n })\n\n fetchOptions.headers.Cookie = Object.entries(signedCookies)\n .map(([key, value]) => `${key}=${value}`)\n .join('; ')\n }\n\n const { isHeader = false } = options\n\n return fetch(encodedUrl, fetchOptions)\n .then(response => {\n const contentHeader = response.headers.get('content-length')\n const contentType = response.headers.get('content-type')\n\n // NOTE: the response will be ok but we won't be able to render any\n // image meaning pdfmake will error. Raise error here and return early.\n if (contentHeader === '0') {\n return Promise.reject(\n new Error(`Failed to fetch image as no content length: ${encodedUrl}`)\n )\n }\n\n if (!response.ok) {\n return Promise.reject(new Error(`Failed to fetch image: ${encodedUrl}`))\n }\n\n const imageType = contentTypes[contentType]\n\n return response.arrayBuffer().then(buffer => ({\n buffer,\n imageType,\n }))\n })\n .then(({ buffer, imageType }) => {\n const base64Flag = `data:image/${imageType};base64,`\n const imageStr = arrayBufferToBase64(buffer)\n\n const base64 = `${base64Flag}${imageStr}`\n const isValid = validateBase64Image(base64)\n\n if (!isValid) {\n return Promise.reject(new Error('InvalidImageError'))\n }\n\n return base64\n })\n .catch(error => {\n if (isHeader) {\n // NOTE: Replace failed headers with LH logo\n console.error('FetchImageHeaderError', error)\n return fetchImage(LIGHTHOUSE_LOGO_URL, defaultOptions)\n }\n\n console.error(error)\n return imageNotFound\n })\n}\n\nfunction arrayBufferToBase64(buffer) {\n let binary = ''\n const bytes = [].slice.call(new Uint8Array(buffer))\n\n bytes.forEach(b => (binary += String.fromCharCode(b)))\n\n return btoa(binary)\n}\n\nexport function validateBase64Image(base64String) {\n const isJpeg = base64String.startsWith('data:image/jpeg;base64,')\n\n if (isJpeg) return validateJpegImage(base64String)\n\n const isPng = base64String.startsWith('data:image/png;base64,')\n\n if (isPng) return validatePngImage(base64String)\n\n return false\n}\n\n// See SO for more info: https://stackoverflow.com/a/41635312\n// Fiddle: https://jsfiddle.net/Lnyxuchw/\nexport function validateJpegImage(base64string) {\n const src = base64string\n const imageData = Uint8Array.from(\n atob(src.replace('data:image/jpeg;base64,', '')),\n c => c.charCodeAt(0)\n )\n const imageCorrupted =\n imageData[imageData.length - 1] === 217 &&\n imageData[imageData.length - 2] === 255\n\n return imageCorrupted\n}\n\n// See SO for more info: https://stackoverflow.com/a/41635312\n// Fiddle: https://jsfiddle.net/Lnyxuchw/\nexport function validatePngImage(base64string) {\n const src = base64string\n const imageData = Uint8Array.from(\n atob(src.replace('data:image/png;base64,', '')),\n c => c.charCodeAt(0)\n )\n const sequence = [0, 0, 0, 0, 73, 69, 78, 68, 174, 66, 96, 130] // in hex:\n\n //check last 12 elements of array so they contains needed values\n for (let i = 12; i > 0; i--) {\n if (imageData[imageData.length - i] !== sequence[12 - i]) {\n return false\n }\n }\n\n return true\n}\n"],"file":"index.js"}
|
|
1
|
+
{"version":3,"sources":["../../../src/helpers/fetch-image/index.js"],"names":["atob","btoa","fetchPonyfill","Promise","LIGHTHOUSE_LOGO_URL","imageNotFound","generateCloudFrontCookies","fetch","self","contentTypes","defaultOptions","cache","fetchImage","url","options","encodedUrl","encodeURI","fetchOptions","applicationId","includes","credentials","signedCookies","userId","headers","Cookie","Object","entries","map","key","value","join","isHeader","then","response","contentHeader","get","contentType","reject","Error","ok","imageType","arrayBuffer","buffer","base64Flag","imageStr","arrayBufferToBase64","base64","isValid","validateBase64Image","catch","error","console","binary","bytes","slice","call","Uint8Array","forEach","b","String","fromCharCode","base64String","isJpeg","startsWith","validateJpegImage","isPng","validatePngImage","base64string","src","imageData","from","replace","c","charCodeAt","imageCorrupted","length","sequence","i"],"mappings":";;;;;;;;AAAA,SAASA,IAAT,EAAeC,IAAf,QAA2B,kBAA3B;AACA,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AG,CAACG,OAAJ,CAAY,yBAAZ,EAAuC,EAAvC,CAAD,CADY,EAEhB,UAAAC,CAAC;AAAA,WAAIA,CAAC,CAACC,UAAF,CAAa,CAAb,CAAJ;AAAA,GAFe,CAAlB;AAIA,MAAMC,cAAc,GAClBL,SAAS,CAACA,SAAS,CAACM,MAAV,GAAmB,CAApB,CAAT,KAAoC,GAApC,IACAN,SAAS,CAACA,SAAS,CAACM,MAAV,GAAmB,CAApB,CAAT,KAAoC,GAFtC;AAIA,SAAOD,cAAP;AACD,C,CAED;AACA;;AACA,OAAO,SAASR,gBAAT,CAA0BC,YAA1B,EAAwC;AAC7C,MAAMC,GAAG,GAAGD,YAAZ;AACA,MAAME,SAAS,GAAGb,UAAU,CAACc,IAAX,CAChBtE,IAAI,CAACoE,GAAG,CAACG,OAAJ,CAAY,wBAAZ,EAAsC,EAAtC,CAAD,CADY,EAEhB,UAAAC,CAAC;AAAA,WAAIA,CAAC,CAACC,UAAF,CAAa,CAAb,CAAJ;AAAA,GAFe,CAAlB;AAIA,MAAMG,QAAQ,GAAG,CAAC,CAAD,EAAI,CAAJ,EAAO,CAAP,EAAU,CAAV,EAAa,EAAb,EAAiB,EAAjB,EAAqB,EAArB,EAAyB,EAAzB,EAA6B,GAA7B,EAAkC,EAAlC,EAAsC,EAAtC,EAA0C,GAA1C,CAAjB,CAN6C,CAMmB;AAEhE;;AACA,OAAK,IAAIC,CAAC,GAAG,EAAb,EAAiBA,CAAC,GAAG,CAArB,EAAwBA,CAAC,EAAzB,EAA6B;AAC3B,QAAIR,SAAS,CAACA,SAAS,CAACM,MAAV,GAAmBE,CAApB,CAAT,KAAoCD,QAAQ,CAAC,KAAKC,CAAN,CAAhD,EAA0D;AACxD,aAAO,KAAP;AACD;AACF;;AAED,SAAO,IAAP;AACD","sourcesContent":["import { atob, btoa } from '@lighthouse/abab'\nimport fetchPonyfill from 'fetch-ponyfill'\nimport Promise from 'bluebird'\nimport { LIGHTHOUSE_LOGO_URL } from '../../constants'\nimport { imageNotFound } from '../../images'\nimport { generateCloudFrontCookies } from '../get-cloudfront-cookies'\n// NOTE use the native fetch if it's available in the browser, because the\n// ponyfill (which actually uses the github polyfill) does not support all the\n// same options as native fetch\nconst fetch =\n (typeof self === 'object' && self.fetch) || fetchPonyfill({ Promise }).fetch\n\nconst contentTypes = {\n 'image/png': 'png',\n 'image/jpeg': 'jpeg',\n}\n\nconst defaultOptions = {\n // NOTE The cache: no-cache option is important to avoid an issue with CORS\n // and caching on Chrome. Here's a good explanation of the issue:\n // https://stackoverflow.com/a/37455118\n // In our case, when loading the web version of a form, the signature image is\n // cached without the correct CORS headers. If the pdf is then generated,\n // there's a mismatch between the cached image headers and the CORS headers\n // sent from the fetch request, causing an error\n cache: 'no-cache',\n}\n\nexport function fetchImage(url, options = {}) {\n const encodedUrl = encodeURI(url)\n const fetchOptions = {\n ...defaultOptions,\n ...options,\n }\n\n const applicationId = options.applicationId\n\n if (url && url.includes('.cloudfront.net')) {\n fetchOptions.credentials = 'include' // Ensure cookies are sent with the request\n\n const signedCookies = generateCloudFrontCookies({\n userId: 'anonymous',\n applicationId,\n })\n\n fetchOptions.headers.Cookie = Object.entries(signedCookies)\n .map(([key, value]) => `${key}=${value}`)\n .join('; ')\n }\n\n const { isHeader = false } = options\n\n return fetch(encodedUrl, fetchOptions)\n .then(response => {\n const contentHeader = response.headers.get('content-length')\n const contentType = response.headers.get('content-type')\n\n // NOTE: the response will be ok but we won't be able to render any\n // image meaning pdfmake will error. Raise error here and return early.\n if (contentHeader === '0') {\n return Promise.reject(\n new Error(`Failed to fetch image as no content length: ${encodedUrl}`)\n )\n }\n\n if (!response.ok) {\n return Promise.reject(new Error(`Failed to fetch image: ${encodedUrl}`))\n }\n\n const imageType = contentTypes[contentType]\n\n return response.arrayBuffer().then(buffer => ({\n buffer,\n imageType,\n }))\n })\n .then(({ buffer, imageType }) => {\n const base64Flag = `data:image/${imageType};base64,`\n const imageStr = arrayBufferToBase64(buffer)\n\n const base64 = `${base64Flag}${imageStr}`\n const isValid = validateBase64Image(base64)\n\n if (!isValid) {\n return Promise.reject(new Error('InvalidImageError'))\n }\n\n return base64\n })\n .catch(error => {\n if (isHeader) {\n // NOTE: Replace failed headers with LH logo\n console.error('FetchImageHeaderError', error)\n return fetchImage(LIGHTHOUSE_LOGO_URL, defaultOptions)\n }\n\n console.error(error)\n return imageNotFound\n })\n}\n\nfunction arrayBufferToBase64(buffer) {\n let binary = ''\n const bytes = [].slice.call(new Uint8Array(buffer))\n\n bytes.forEach(b => (binary += String.fromCharCode(b)))\n\n return btoa(binary)\n}\n\nexport function validateBase64Image(base64String) {\n const isJpeg = base64String.startsWith('data:image/jpeg;base64,')\n\n if (isJpeg) return validateJpegImage(base64String)\n\n const isPng = base64String.startsWith('data:image/png;base64,')\n\n if (isPng) return validatePngImage(base64String)\n\n return false\n}\n\n// See SO for more info: https://stackoverflow.com/a/41635312\n// Fiddle: https://jsfiddle.net/Lnyxuchw/\nexport function validateJpegImage(base64string) {\n const src = base64string\n const imageData = Uint8Array.from(\n atob(src.replace('data:image/jpeg;base64,', '')),\n c => c.charCodeAt(0)\n )\n const imageCorrupted =\n imageData[imageData.length - 1] === 217 &&\n imageData[imageData.length - 2] === 255\n\n return imageCorrupted\n}\n\n// See SO for more info: https://stackoverflow.com/a/41635312\n// Fiddle: https://jsfiddle.net/Lnyxuchw/\nexport function validatePngImage(base64string) {\n const src = base64string\n const imageData = Uint8Array.from(\n atob(src.replace('data:image/png;base64,', '')),\n c => c.charCodeAt(0)\n )\n const sequence = [0, 0, 0, 0, 73, 69, 78, 68, 174, 66, 96, 130] // in hex:\n\n //check last 12 elements of array so they contains needed values\n for (let i = 12; i > 0; i--) {\n if (imageData[imageData.length - i] !== sequence[12 - i]) {\n return false\n }\n }\n\n return true\n}\n"],"file":"index.js"}
|
|
@@ -1,13 +1,8 @@
|
|
|
1
1
|
import _regeneratorRuntime from "@babel/runtime/regenerator";
|
|
2
2
|
import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
|
|
3
|
-
|
|
4
|
-
var
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
var _require2 = require('../get-secret-value'),
|
|
8
|
-
getSecretValue = _require2.getSecretValue;
|
|
9
|
-
|
|
10
|
-
var logger = require('../../logger');
|
|
3
|
+
import AWS from 'aws-sdk';
|
|
4
|
+
var getSignedCookies = AWS.CloudFront.Signer.getSignedCookies;
|
|
5
|
+
import { getSecretValue } from '../get-secret-value';
|
|
11
6
|
/**
|
|
12
7
|
* Generate CloudFront signed cookies for authenticated users
|
|
13
8
|
* @param {Object} options - Configuration options
|
|
@@ -22,7 +17,6 @@ var logger = require('../../logger');
|
|
|
22
17
|
* } | null>} Signed cookies object or null if disabled/failed
|
|
23
18
|
*/
|
|
24
19
|
|
|
25
|
-
|
|
26
20
|
export function generateCloudFrontCookies(_x) {
|
|
27
21
|
return _generateCloudFrontCookies.apply(this, arguments);
|
|
28
22
|
}
|
|
@@ -41,7 +35,7 @@ function _generateCloudFrontCookies() {
|
|
|
41
35
|
break;
|
|
42
36
|
}
|
|
43
37
|
|
|
44
|
-
|
|
38
|
+
console.debug('CloudFront cookie generation skipped - not configured');
|
|
45
39
|
return _context.abrupt("return", null);
|
|
46
40
|
|
|
47
41
|
case 4:
|
|
@@ -57,7 +51,7 @@ function _generateCloudFrontCookies() {
|
|
|
57
51
|
break;
|
|
58
52
|
}
|
|
59
53
|
|
|
60
|
-
|
|
54
|
+
console.warn('CloudFront private key not available');
|
|
61
55
|
return _context.abrupt("return", null);
|
|
62
56
|
|
|
63
57
|
case 11:
|
|
@@ -76,7 +70,7 @@ function _generateCloudFrontCookies() {
|
|
|
76
70
|
dateLessThan: expiration.getTime() // Use epoch timestamp in milliseconds
|
|
77
71
|
|
|
78
72
|
});
|
|
79
|
-
|
|
73
|
+
console.info('CloudFront cookies generated successfully', {
|
|
80
74
|
userId: userId,
|
|
81
75
|
applicationId: applicationId,
|
|
82
76
|
resourcePath: resourcePath,
|
|
@@ -87,7 +81,7 @@ function _generateCloudFrontCookies() {
|
|
|
87
81
|
case 21:
|
|
88
82
|
_context.prev = 21;
|
|
89
83
|
_context.t0 = _context["catch"](4);
|
|
90
|
-
|
|
84
|
+
console.error('Failed to generate CloudFront cookies', {
|
|
91
85
|
userId: userId,
|
|
92
86
|
applicationId: applicationId,
|
|
93
87
|
error: {
|
|
@@ -137,7 +131,7 @@ function _getPrivateKey() {
|
|
|
137
131
|
break;
|
|
138
132
|
}
|
|
139
133
|
|
|
140
|
-
|
|
134
|
+
console.warn('CLOUDFRONT_PRIVATE_KEY_SECRET_ID not configured');
|
|
141
135
|
return _context2.abrupt("return", null);
|
|
142
136
|
|
|
143
137
|
case 6:
|
|
@@ -149,13 +143,13 @@ function _getPrivateKey() {
|
|
|
149
143
|
privateKey = _context2.sent;
|
|
150
144
|
privateKeyCache = privateKey;
|
|
151
145
|
cacheExpiry = now + CACHE_TTL;
|
|
152
|
-
|
|
146
|
+
console.debug('CloudFront private key retrieved and cached');
|
|
153
147
|
return _context2.abrupt("return", privateKey);
|
|
154
148
|
|
|
155
149
|
case 16:
|
|
156
150
|
_context2.prev = 16;
|
|
157
151
|
_context2.t0 = _context2["catch"](6);
|
|
158
|
-
|
|
152
|
+
console.error('Failed to retrieve CloudFront private key', {
|
|
159
153
|
secretId: process.env.CLOUDFRONT_PRIVATE_KEY_SECRET_ID,
|
|
160
154
|
error: {
|
|
161
155
|
message: _context2.t0.message,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/helpers/get-cloudfront-cookies/index.js"],"names":["
|
|
1
|
+
{"version":3,"sources":["../../../src/helpers/get-cloudfront-cookies/index.js"],"names":["AWS","getSignedCookies","CloudFront","Signer","getSecretValue","generateCloudFrontCookies","userId","applicationId","process","env","CLOUDFRONT_DOMAIN","CLOUDFRONT_KEY_PAIR_ID","console","debug","getPrivateKey","privateKey","warn","expiration","Date","setTime","getTime","distributionDomain","resourcePath","urlPattern","signedCookies","url","keyPairId","dateLessThan","info","toISOString","error","message","code","privateKeyCache","cacheExpiry","CACHE_TTL","now","CLOUDFRONT_PRIVATE_KEY_SECRET_ID","secretId"],"mappings":";;AAAA,OAAOA,GAAP,MAAgB,SAAhB;AACA,IAAMC,gBAAgB,GAAGD,GAAG,CAACE,UAAJ,CAAeC,MAAf,CAAsBF,gBAA/C;AACA,SAASG,cAAT,QAA+B,qBAA/B;AAEA;;;;;;;;;;;;;;AAcA,gBAAsBC,yBAAtB;AAAA;AAAA;;;wFAAO;AAAA;AAAA;AAAA;AAAA;AAAA;AAA2CC,YAAAA,MAA3C,QAA2CA,MAA3C,EAAmDC,aAAnD,QAAmDA,aAAnD;;AAAA,kBAED,CAACC,OAAO,CAACC,GAAR,CAAYC,iBAAb,IAAkC,CAACF,OAAO,CAACC,GAAR,CAAYE,sBAF9C;AAAA;AAAA;AAAA;;AAGHC,YAAAA,OAAO,CAACC,KAAR,CAAc,uDAAd;AAHG,6CAII,IAJJ;;AAAA;AAAA;AAAA;AAAA,mBASsBC,aAAa,EATnC;;AAAA;AASGC,YAAAA,UATH;;AAAA,gBAUEA,UAVF;AAAA;AAAA;AAAA;;AAWDH,YAAAA,OAAO,CAACI,IAAR,CAAa,sCAAb;AAXC,6CAYM,IAZN;;AAAA;AAeH;AACMC,YAAAA,UAhBH,GAgBgB,IAAIC,IAAJ,EAhBhB;AAiBHD,YAAAA,UAAU,CAACE,OAAX,CAAmBF,UAAU,CAACG,OAAX,KAAuB,KAAK,EAAL,GAAU,EAAV,GAAe,EAAf,GAAoB,IAA9D,EAjBG,CAmBH;;AACMC,YAAAA,kBApBH,qBAoBmCb,OAAO,CAACC,GAAR,CAAYC,iBApB/C;AAqBGY,YAAAA,YArBH,GAqBkBf,aAAa,aAAMA,aAAN,UAA0B,GArBzD;AAsBGgB,YAAAA,UAtBH,aAsBmBF,kBAtBnB,cAsByCC,YAtBzC,GAwBH;;AACME,YAAAA,aAzBH,GAyBmBvB,gBAAgB,CAAC;AACrCwB,cAAAA,GAAG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AAb;AARJ,8CASW,IATX;;AAAA;AAAA;AAAA;AAAA,mBAa6BZ,cAAc,CACrCI,OAAO,CAACC,GAAR,CAAY4B,gCADyB,EAErC,wBAFqC,CAb3C;;AAAA;AAaUtB,YAAAA,UAbV;AAkBIkB,YAAAA,eAAe,GAAGlB,UAAlB;AACAmB,YAAAA,WAAW,GAAGE,GAAG,GAAGD,SAApB;AAEAvB,YAAAA,OAAO,CAACC,KAAR,CAAc,6CAAd;AArBJ,8CAsBWE,UAtBX;;AAAA;AAAA;AAAA;AAwBIH,YAAAA,OAAO,CAACkB,KAAR,CAAc,2CAAd,EAA2D;AACzDQ,cAAAA,QAAQ,EAAE9B,OAAO,CAACC,GAAR,CAAY4B,gCADmC;AAEzDP,cAAAA,KAAK,EAAE;AACLC,gBAAAA,OAAO,EAAE,aAAMA,OADV;AAELC,gBAAAA,IAAI,EAAE,aAAMA;AAFP;AAFkD,aAA3D;AAxBJ,8CA+BW,IA/BX;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,G","sourcesContent":["import AWS from 'aws-sdk'\nconst getSignedCookies = AWS.CloudFront.Signer.getSignedCookies\nimport { getSecretValue } from '../get-secret-value'\n\n/**\n * Generate CloudFront signed cookies for authenticated users\n * @param {Object} options - Configuration options\n * @param {string} options.userId - User ID for logging purposes\n * @param {string} options.applicationId - Application ID for resource scoping\n * Return Type: https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-aws-sdk-cloudfront-signer/Interface/CloudfrontSignedCookiesOutput/\n * @returns {Promise<{\n * \"CloudFront-Key-Pair-Id\": string,\n * \"CloudFront-Signature\": string,\n * \"CloudFront-Expires\"?: number,\n * \"CloudFront-Policy\"?: string\n * } | null>} Signed cookies object or null if disabled/failed\n */\n\nexport async function generateCloudFrontCookies({ userId, applicationId }) {\n // Early return if CloudFront is not configured\n if (!process.env.CLOUDFRONT_DOMAIN || !process.env.CLOUDFRONT_KEY_PAIR_ID) {\n console.debug('CloudFront cookie generation skipped - not configured')\n return null\n }\n\n try {\n // Get private key from AWS Secrets Manager\n const privateKey = await getPrivateKey()\n if (!privateKey) {\n console.warn('CloudFront private key not available')\n return null\n }\n\n // Set expiration time (14 days from now)\n const expiration = new Date()\n expiration.setTime(expiration.getTime() + 14 * 24 * 60 * 60 * 1000)\n\n // Generate resource URL pattern for wildcard access\n const distributionDomain = `https://${process.env.CLOUDFRONT_DOMAIN}`\n const resourcePath = applicationId ? `${applicationId}/*` : '*'\n const urlPattern = `${distributionDomain}/${resourcePath}`\n\n // Generate signed cookies\n const signedCookies = getSignedCookies({\n url: urlPattern,\n keyPairId: process.env.CLOUDFRONT_KEY_PAIR_ID,\n privateKey: privateKey,\n dateLessThan: expiration.getTime(), // Use epoch timestamp in milliseconds\n })\n\n console.info('CloudFront cookies generated successfully', {\n userId,\n applicationId,\n resourcePath,\n expiration: expiration.toISOString(),\n })\n\n return signedCookies\n } catch (error) {\n console.error('Failed to generate CloudFront cookies', {\n userId,\n applicationId,\n error: {\n message: error.message,\n code: error.code,\n },\n })\n return null\n }\n}\n\nlet privateKeyCache = null\nlet cacheExpiry = null\nconst CACHE_TTL = 5 * 60 * 1000 // 5 minutes\n\nasync function getPrivateKey() {\n const now = Date.now()\n\n if (privateKeyCache && cacheExpiry && now < cacheExpiry) {\n return privateKeyCache\n }\n\n if (!process.env.CLOUDFRONT_PRIVATE_KEY_SECRET_ID) {\n console.warn('CLOUDFRONT_PRIVATE_KEY_SECRET_ID not configured')\n return null\n }\n\n try {\n const privateKey = await getSecretValue(\n process.env.CLOUDFRONT_PRIVATE_KEY_SECRET_ID,\n 'CLOUDFRONT_PRIVATE_KEY'\n )\n\n privateKeyCache = privateKey\n cacheExpiry = now + CACHE_TTL\n\n console.debug('CloudFront private key retrieved and cached')\n return privateKey\n } catch (error) {\n console.error('Failed to retrieve CloudFront private key', {\n secretId: process.env.CLOUDFRONT_PRIVATE_KEY_SECRET_ID,\n error: {\n message: error.message,\n code: error.code,\n },\n })\n return null\n }\n}\n"],"file":"index.js"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import AWS from 'aws-sdk';
|
|
2
2
|
import { attempt, isError } from 'lodash';
|
|
3
3
|
export default function getSecretValue(secretId, secretKey) {
|
|
4
4
|
if (!secretId) {
|
|
@@ -6,7 +6,7 @@ export default function getSecretValue(secretId, secretKey) {
|
|
|
6
6
|
} // TODO: update these credentials to specific values for service
|
|
7
7
|
|
|
8
8
|
|
|
9
|
-
var secretsClient = new SecretsManager({
|
|
9
|
+
var secretsClient = new AWS.SecretsManager({
|
|
10
10
|
accessKeyId: process.env.AWS_KEY,
|
|
11
11
|
region: process.env.AWS_SECRET_MANAGER_REGION,
|
|
12
12
|
secretAccessKey: process.env.AWS_SECRET
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/helpers/get-secret-value/index.js"],"names":["
|
|
1
|
+
{"version":3,"sources":["../../../src/helpers/get-secret-value/index.js"],"names":["AWS","attempt","isError","getSecretValue","secretId","secretKey","Promise","reject","Error","secretsClient","SecretsManager","accessKeyId","process","env","AWS_KEY","region","AWS_SECRET_MANAGER_REGION","secretAccessKey","AWS_SECRET","SecretId","promise","then","payload","secret","parseSecretString","secretValue","catch","err","code","message","secretString","SecretString","parsed","JSON","parse"],"mappings":"AAAA,OAAOA,GAAP,MAAgB,SAAhB;AACA,SAASC,OAAT,EAAkBC,OAAlB,QAAiC,QAAjC;AAEA,eAAe,SAASC,cAAT,CAAwBC,QAAxB,EAAkCC,SAAlC,EAA6C;AAC1D,MAAI,CAACD,QAAL,EAAe;AACb,WAAOE,OAAO,CAACC,MAAR,CACL,IAAIC,KAAJ,4CAA8CJ,QAA9C,EADK,CAAP;AAGD,GALyD,CAO1D;;;AACA,MAAMK,aAAa,GAAG,IAAIT,GAAG,CAACU,cAAR,CAAuB;AAC3CC,IAAAA,WAAW,EAAEC,OAAO,CAACC,GAAR,CAAYC,OADkB;AAE3CC,IAAAA,MAAM,EAAEH,OAAO,CAACC,GAAR,CAAYG,yBAFuB;AAG3CC,IAAAA,eAAe,EAAEL,OAAO,CAACC,GAAR,CAAYK;AAHc,GAAvB,CAAtB;AAMA,SAAOT,aAAa,CACjBN,cADI,CACW;AAAEgB,IAAAA,QAAQ,EAAEf;AAAZ,GADX,EAEJgB,OAFI,GAGJC,IAHI,CAGC,UAAAC,OAAO,EAAI;AACf,QAAMC,MAAM,GAAGC,iBAAiB,CAACF,OAAD,CAAhC,CADe,CAGf;;AACA,QAAI,CAACjB,SAAL,EAAgB,OAAOkB,MAAP;AAEhB,QAAME,WAAW,GAAGF,MAAM,CAAClB,SAAD,CAA1B;;AAEA,QAAI,CAACoB,WAAL,EAAkB;AAChB,YAAM,IAAIjB,KAAJ,CAAU,iCAAV,CAAN;AACD;;AAED,WAAOiB,WAAP;AACD,GAhBI,EAiBJC,KAjBI,CAiBE,UAAAC,GAAG,EAAI;AACZ,UAAM,IAAInB,KAAJ,gCAAkCmB,GAAG,CAACC,IAAtC,eAA+CD,GAAG,CAACE,OAAnD,EAAN;AACD,GAnBI,CAAP;AAoBD;;AAED,SAASL,iBAAT,CAA2BF,OAA3B,EAAoC;AAClC,MAAMQ,YAAY,GAAGR,OAAO,CAACS,YAAR,IAAwB,EAA7C;AAEA,MAAMC,MAAM,GAAG/B,OAAO,CAACgC,IAAI,CAACC,KAAN,EAAaJ,YAAb,CAAtB;AAEA,SAAO5B,OAAO,CAAC8B,MAAD,CAAP,GAAkB,EAAlB,GAAuBA,MAA9B;AACD","sourcesContent":["import AWS from 'aws-sdk'\nimport { attempt, isError } from 'lodash'\n\nexport default function getSecretValue(secretId, secretKey) {\n if (!secretId) {\n return Promise.reject(\n new Error(`Missing required param: secretId:${secretId}`)\n )\n }\n\n // TODO: update these credentials to specific values for service\n const secretsClient = new AWS.SecretsManager({\n accessKeyId: process.env.AWS_KEY,\n region: process.env.AWS_SECRET_MANAGER_REGION,\n secretAccessKey: process.env.AWS_SECRET,\n })\n\n return secretsClient\n .getSecretValue({ SecretId: secretId })\n .promise()\n .then(payload => {\n const secret = parseSecretString(payload)\n\n // Return early if secretKey isn't defined (we want the full set of key/values)\n if (!secretKey) return secret\n\n const secretValue = secret[secretKey]\n\n if (!secretValue) {\n throw new Error('Secret value could not be found')\n }\n\n return secretValue\n })\n .catch(err => {\n throw new Error(`AWSSecretFetchError: ${err.code}, ${err.message}`)\n })\n}\n\nfunction parseSecretString(payload) {\n const secretString = payload.SecretString || ''\n\n const parsed = attempt(JSON.parse, secretString)\n\n return isError(parsed) ? {} : parsed\n}\n"],"file":"index.js"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lighthouse/common",
|
|
3
|
-
"version": "4.37.0-canary-
|
|
3
|
+
"version": "4.37.0-canary-5",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "lib/index.js",
|
|
@@ -38,9 +38,8 @@
|
|
|
38
38
|
},
|
|
39
39
|
"homepage": "https://github.com/Lighthouse-io/common#readme",
|
|
40
40
|
"dependencies": {
|
|
41
|
-
"@aws-sdk/client-secrets-manager": "^3.307.0",
|
|
42
|
-
"@aws-sdk/cloudfront-signer": "^3.307.0",
|
|
43
41
|
"@lighthouse/abab": "^0.0.6",
|
|
42
|
+
"aws-sdk": "2.1361.0",
|
|
44
43
|
"bluebird": "^3.7.2",
|
|
45
44
|
"fetch-ponyfill": "^6.1.0",
|
|
46
45
|
"lodash": "^4.17.15",
|