From de890ed2239a39beeeda55d9a761f3b43acf749f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Chopin?= Date: Fri, 23 Feb 2024 11:25:26 +0100 Subject: [PATCH] feat: generate NUXT_SESSION_PASSWORD and throw if not set in production --- package.json | 4 +++- pnpm-lock.yaml | 35 +++++++++++++++++++---------------- src/module.ts | 22 +++++++++++++++------- 3 files changed, 37 insertions(+), 24 deletions(-) diff --git a/package.json b/package.json index 9adf71bc..bc2cdfe3 100644 --- a/package.json +++ b/package.json @@ -34,7 +34,9 @@ "defu": "^6.1.4", "hookable": "^5.5.3", "ofetch": "^1.3.3", - "ohash": "^1.1.3" + "ohash": "^1.1.3", + "pathe": "^1.1.2", + "uncrypto": "^0.1.3" }, "devDependencies": { "@iconify-json/simple-icons": "^1.1.91", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 5a4d00e3..23b074b0 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -23,6 +23,12 @@ importers: ohash: specifier: ^1.1.3 version: 1.1.3 + pathe: + specifier: ^1.1.2 + version: 1.1.2 + uncrypto: + specifier: ^0.1.3 + version: 0.1.3 devDependencies: '@iconify-json/simple-icons': specifier: ^1.1.91 @@ -1768,7 +1774,7 @@ packages: ofetch: 1.3.3 ohash: 1.1.3 pacote: 17.0.4 - pathe: 1.1.1 + pathe: 1.1.2 perfect-debounce: 1.0.0 pkg-types: 1.0.3 rc9: 2.1.1 @@ -1959,7 +1965,7 @@ packages: consola: 3.2.3 mlly: 1.4.2 nuxi: 3.10.0 - pathe: 1.1.1 + pathe: 1.1.2 unbuild: 2.0.0(typescript@5.3.3) transitivePeerDependencies: - sass @@ -3061,14 +3067,14 @@ packages: dependencies: '@vitest/utils': 1.2.2 p-limit: 5.0.0 - pathe: 1.1.1 + pathe: 1.1.2 dev: true /@vitest/snapshot@1.2.2: resolution: {integrity: sha512-SmGY4saEw1+bwE1th6S/cZmPxz/Q4JWsl7LvbQIky2tKE35US4gd0Mjzqfr84/4OD0tikGWaWdMja/nWL5NIPA==} dependencies: magic-string: 0.30.5 - pathe: 1.1.1 + pathe: 1.1.2 pretty-format: 29.7.0 dev: true @@ -3730,7 +3736,7 @@ packages: jiti: 1.21.0 mlly: 1.4.2 ohash: 1.1.3 - pathe: 1.1.1 + pathe: 1.1.2 perfect-debounce: 1.0.0 pkg-types: 1.0.3 rc9: 2.1.1 @@ -3871,7 +3877,7 @@ packages: node-fetch-native: 1.4.1 ofetch: 1.3.3 open: 9.1.0 - pathe: 1.1.1 + pathe: 1.1.2 pkg-types: 1.0.3 scule: 1.1.0 semver: 7.5.4 @@ -4991,7 +4997,7 @@ packages: https-proxy-agent: 7.0.2 mri: 1.2.0 node-fetch-native: 1.4.1 - pathe: 1.1.1 + pathe: 1.1.2 tar: 6.2.0 transitivePeerDependencies: - supports-color @@ -5725,7 +5731,7 @@ packages: jiti: 1.21.0 mlly: 1.4.2 node-forge: 1.3.1 - pathe: 1.1.1 + pathe: 1.1.2 std-env: 3.7.0 ufo: 1.3.2 untun: 0.1.2 @@ -6042,7 +6048,7 @@ packages: jiti: 1.21.0 mlly: 1.4.2 mri: 1.2.0 - pathe: 1.1.1 + pathe: 1.1.2 postcss: 8.4.31 postcss-nested: 6.0.1(postcss@8.4.31) typescript: 5.3.3 @@ -6052,7 +6058,7 @@ packages: resolution: {integrity: sha512-i/Ykufi2t1EZ6NaPLdfnZk2AX8cs0d+mTzVKuPfqPKPatxLApaBoxJQ9x1/uckXtrS/U5oisPMDkNs0yQTaBRg==} dependencies: acorn: 8.11.2 - pathe: 1.1.1 + pathe: 1.1.2 pkg-types: 1.0.3 ufo: 1.3.2 @@ -6961,9 +6967,6 @@ packages: resolution: {integrity: sha512-5HviZNaZcfqP95rwpv+1HDgUamezbqdSYTyzjTvwtJSnIH+3vnbmWsItli8OFEndS984VT55M3jduxZbX351gg==} engines: {node: '>=12'} - /pathe@1.1.1: - resolution: {integrity: sha512-d+RQGp0MAYTIaDBIMmOfMwz3E+LOZnxx1HZd5R18mmCZY0QBlK0LDZfPc8FW8Ed2DlvsuE6PRjroDY+wg4+j/Q==} - /pathe@1.1.2: resolution: {integrity: sha512-whLdWMYL2TwI08hn8/ZqAbrVemu0LNaNNJZX73O6qaIdCTfXutsLhMkjdENX0qhsQ9uIimo4/aQOmXkoon2nDQ==} @@ -6996,7 +6999,7 @@ packages: dependencies: jsonc-parser: 3.2.0 mlly: 1.4.2 - pathe: 1.1.1 + pathe: 1.1.2 /portfinder@1.0.32: resolution: {integrity: sha512-on2ZJVVDXRADWE6jnQaX0ioEylzgBpQk8r55NE4wjXW1ZxO+BgDlY6DXwj20i0V8eB4SenDQ00WEaxfiIQPcxg==} @@ -8645,7 +8648,7 @@ packages: magic-string: 0.30.5 mkdist: 1.4.0(typescript@5.3.3) mlly: 1.4.2 - pathe: 1.1.1 + pathe: 1.1.2 pkg-types: 1.0.3 pretty-bytes: 6.1.1 rollup: 3.29.4 @@ -9347,7 +9350,7 @@ packages: execa: 8.0.1 local-pkg: 0.5.0 magic-string: 0.30.5 - pathe: 1.1.1 + pathe: 1.1.2 picocolors: 1.0.0 std-env: 3.7.0 strip-literal: 1.3.0 diff --git a/src/module.ts b/src/module.ts index 1de8f817..cf4c9e43 100644 --- a/src/module.ts +++ b/src/module.ts @@ -1,6 +1,8 @@ import { defineNuxtModule, addPlugin, createResolver, addImportsDir, addServerHandler } from '@nuxt/kit' -import { sha256 } from 'ohash' +import { join } from 'pathe' import { defu } from 'defu' +import { randomUUID } from 'uncrypto' +import { writeFile, readFile } from 'node:fs/promises' // Module options TypeScript interface definition export interface ModuleOptions {} @@ -12,14 +14,20 @@ export default defineNuxtModule({ }, // Default configuration options of the Nuxt module defaults: {}, - setup (options, nuxt) { + async setup (options, nuxt) { const resolver = createResolver(import.meta.url) - if (!process.env.NUXT_SESSION_PASSWORD && !nuxt.options._prepare) { - const randomPassword = sha256(`${Date.now()}${Math.random()}`).slice(0, 32) - process.env.NUXT_SESSION_PASSWORD = randomPassword - console.warn('No session password set, using a random password, please set NUXT_SESSION_PASSWORD in your .env file with at least 32 chars') - console.log(`NUXT_SESSION_PASSWORD=${randomPassword}`) + // Generate the session password + if (nuxt.options.dev && !process.env.NUXT_SESSION_PASSWORD) { + process.env.NUXT_SESSION_PASSWORD = randomUUID().replace(/-/g, '') + // Add it to .env + const envPath = join(nuxt.options.rootDir, '.env') + const envContent = await readFile(envPath, 'utf-8').catch(() => '') + if (!envContent.includes('NUXT_SESSION_PASSWORD')) { + await writeFile(envPath, `${envContent ? envContent + '\n' : envContent}NUXT_SESSION_PASSWORD=${process.env.NUXT_SESSION_PASSWORD}`, 'utf-8') + } + } else if (!nuxt.options._prepare && !process.env.NUXT_SESSION_PASSWORD) { + throw new Error('NUXT_SESSION_PASSWORD environment variable is not set') } nuxt.options.alias['#auth-utils'] = resolver.resolve('./runtime/types/index')