From fbda6779441ae7755330ba5a7ce70c5bde78631c Mon Sep 17 00:00:00 2001 From: Kevin Ansfield Date: Mon, 17 Jun 2019 11:41:04 +0100 Subject: [PATCH] Inline password-generator to fix auto-import issues no issue - copies `password-generator` npm module functionality into a utility function because the way it was trying to use the `crypto` core library was tripping up webpack which no longer has `node: true` config set by `ember-auto-import` --- .../admin/app/controllers/settings/general.js | 9 +- ghost/admin/app/utils/password-generator.js | 86 +++++++++++++++++++ ghost/admin/app/utils/random-password.js | 8 -- ghost/admin/package.json | 1 - ghost/admin/yarn.lock | 19 ---- 5 files changed, 94 insertions(+), 29 deletions(-) create mode 100644 ghost/admin/app/utils/password-generator.js delete mode 100644 ghost/admin/app/utils/random-password.js diff --git a/ghost/admin/app/controllers/settings/general.js b/ghost/admin/app/controllers/settings/general.js index c9270671bd..9708501c5a 100644 --- a/ghost/admin/app/controllers/settings/general.js +++ b/ghost/admin/app/controllers/settings/general.js @@ -1,7 +1,7 @@ /* eslint-disable ghost/ember/alias-model-in-controller */ import $ from 'jquery'; import Controller from '@ember/controller'; -import randomPassword from 'ghost-admin/utils/random-password'; +import generatePassword from 'ghost-admin/utils/password-generator'; import validator from 'validator'; import { IMAGE_EXTENSIONS, @@ -14,6 +14,13 @@ import {task} from 'ember-concurrency'; const ICON_EXTENSIONS = ['ico', 'png']; +function randomPassword() { + let word = generatePassword(6); + let randomN = Math.floor(Math.random() * 1000); + + return word + randomN; +} + export default Controller.extend({ config: service(), ghostPaths: service(), diff --git a/ghost/admin/app/utils/password-generator.js b/ghost/admin/app/utils/password-generator.js new file mode 100644 index 0000000000..68e517ac6e --- /dev/null +++ b/ghost/admin/app/utils/password-generator.js @@ -0,0 +1,86 @@ +/* + * copied from https://github.com/bermi/password-generator + * Copyright(c) 2011-2015 Bermi Ferrer + * MIT Licensed + */ + +// copied from the NPM module because the switch away from polyfilling `global` +// via webpack in ember-auto-import resulted in an error finding 'crypto' +// Module not found: Error: Can't resolve 'crypto' + +const vowel = /[aeiou]$/i; +const consonant = /[bcdfghjklmnpqrstvwxyz]$/i; + +function rand(min, max) { + var key, value, arr = new Uint8Array(max); + getRandomValues(arr); + for (key in arr) { + if (arr.hasOwnProperty(key)) { + value = arr[key]; + if (value > min && value < max) { + return value; + } + } + } + return rand(min, max); +} + +function getRandomValues(buf) { + window.crypto.getRandomValues(buf); +} + +export default function generatePassword(length, memorable, pattern, prefix) { + var char = '', n, i, validChars = []; + if (length === null || typeof (length) === 'undefined') { + length = 10; + } + if (memorable === null || typeof (memorable) === 'undefined') { + memorable = true; + } + if (pattern === null || typeof (pattern) === 'undefined') { + pattern = /\w/; + } + if (prefix === null || typeof (prefix) === 'undefined') { + prefix = ''; + } + + // Non memorable passwords will pick characters from a pre-generated + // list of characters + if (!memorable) { + for (i = 33; 126 > i; i += 1) { + char = String.fromCharCode(i); + if (char.match(pattern)) { + validChars.push(char); + } + } + + if (!validChars.length) { + throw new Error('Could not find characters that match the ' + + 'password pattern ' + pattern + '. Patterns must match individual ' + + 'characters, not the password as a whole.'); + } + } + + while (prefix.length < length) { + if (memorable) { + if (prefix.match(consonant)) { + pattern = vowel; + } else { + pattern = consonant; + } + n = rand(33, 126); + char = String.fromCharCode(n); + } else { + char = validChars[rand(0, validChars.length)]; + } + + if (memorable) { + char = char.toLowerCase(); + } + if (char.match(pattern)) { + prefix = '' + prefix + char; + } + } + + return prefix; +} diff --git a/ghost/admin/app/utils/random-password.js b/ghost/admin/app/utils/random-password.js deleted file mode 100644 index 7de9a022a6..0000000000 --- a/ghost/admin/app/utils/random-password.js +++ /dev/null @@ -1,8 +0,0 @@ -import generatePassword from 'password-generator'; - -export default function () { - let word = generatePassword(6); - let randomN = Math.floor(Math.random() * 1000); - - return word + randomN; -} diff --git a/ghost/admin/package.json b/ghost/admin/package.json index 756eba9231..0e884daf5f 100644 --- a/ghost/admin/package.json +++ b/ghost/admin/package.json @@ -114,7 +114,6 @@ "markdown-it-mark": "2.0.0", "matchdep": "2.0.0", "normalize.css": "3.0.3", - "password-generator": "2.2.0", "postcss-color-mod-function": "3.0.3", "postcss-custom-media": "7.0.8", "postcss-custom-properties": "8.0.10", diff --git a/ghost/admin/yarn.lock b/ghost/admin/yarn.lock index e9ad564667..1f9d0fe227 100644 --- a/ghost/admin/yarn.lock +++ b/ghost/admin/yarn.lock @@ -3013,11 +3013,6 @@ camelcase@^2.0.0: resolved "https://registry.yarnpkg.com/camelcase/-/camelcase-2.1.1.tgz#7c1d16d679a1bbe59ca02cacecfb011e201f5a1f" integrity sha1-fB0W1nmhu+WcoCys7PsBHiAfWh8= -camelcase@^4.1.0: - version "4.1.0" - resolved "https://registry.yarnpkg.com/camelcase/-/camelcase-4.1.0.tgz#d545635be1e33c542649c69173e5de6acfae34dd" - integrity sha1-1UVjW+HjPFQmScaRc+Xeas+uNN0= - can-symlink@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/can-symlink/-/can-symlink-1.0.0.tgz#97b607d8a84bb6c6e228b902d864ecb594b9d219" @@ -9219,13 +9214,6 @@ pascalcase@^0.1.1: resolved "https://registry.yarnpkg.com/pascalcase/-/pascalcase-0.1.1.tgz#b363e55e8006ca6fe21784d2db22bd15d7917f14" integrity sha1-s2PlXoAGym/iF4TS2yK9FdeRfxQ= -password-generator@2.2.0: - version "2.2.0" - resolved "https://registry.yarnpkg.com/password-generator/-/password-generator-2.2.0.tgz#fc75cff795110923e054a5a71623433240bf5e49" - integrity sha1-/HXP95URCSPgVKWnFiNDMkC/Xkk= - dependencies: - yargs-parser "^8.0.0" - path-browserify@0.0.0: version "0.0.0" resolved "https://registry.yarnpkg.com/path-browserify/-/path-browserify-0.0.0.tgz#a0b870729aae214005b7d5032ec2cbbb0fb4451a" @@ -12076,13 +12064,6 @@ yam@^1.0.0: fs-extra "^4.0.2" lodash.merge "^4.6.0" -yargs-parser@^8.0.0: - version "8.1.0" - resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-8.1.0.tgz#f1376a33b6629a5d063782944da732631e966950" - integrity sha512-yP+6QqN8BmrgW2ggLtTbdrOyBNSI7zBa4IykmiV5R1wl1JWNxQvWhMfMdmzIYtKU7oP3OOInY/tl2ov3BDjnJQ== - dependencies: - camelcase "^4.1.0" - yeast@0.1.2: version "0.1.2" resolved "https://registry.yarnpkg.com/yeast/-/yeast-0.1.2.tgz#008e06d8094320c372dbc2f8ed76a0ca6c8ac419"