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`
This commit is contained in:
Kevin Ansfield 2019-06-17 11:41:04 +01:00
parent 9354d3e20d
commit fbda677944
5 changed files with 94 additions and 29 deletions

View File

@ -1,7 +1,7 @@
/* eslint-disable ghost/ember/alias-model-in-controller */ /* eslint-disable ghost/ember/alias-model-in-controller */
import $ from 'jquery'; import $ from 'jquery';
import Controller from '@ember/controller'; 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 validator from 'validator';
import { import {
IMAGE_EXTENSIONS, IMAGE_EXTENSIONS,
@ -14,6 +14,13 @@ import {task} from 'ember-concurrency';
const ICON_EXTENSIONS = ['ico', 'png']; 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({ export default Controller.extend({
config: service(), config: service(),
ghostPaths: service(), ghostPaths: service(),

View File

@ -0,0 +1,86 @@
/*
* copied from https://github.com/bermi/password-generator
* Copyright(c) 2011-2015 Bermi Ferrer <bermi@bermilabs.com>
* 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;
}

View File

@ -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;
}

View File

@ -114,7 +114,6 @@
"markdown-it-mark": "2.0.0", "markdown-it-mark": "2.0.0",
"matchdep": "2.0.0", "matchdep": "2.0.0",
"normalize.css": "3.0.3", "normalize.css": "3.0.3",
"password-generator": "2.2.0",
"postcss-color-mod-function": "3.0.3", "postcss-color-mod-function": "3.0.3",
"postcss-custom-media": "7.0.8", "postcss-custom-media": "7.0.8",
"postcss-custom-properties": "8.0.10", "postcss-custom-properties": "8.0.10",

View File

@ -3013,11 +3013,6 @@ camelcase@^2.0.0:
resolved "https://registry.yarnpkg.com/camelcase/-/camelcase-2.1.1.tgz#7c1d16d679a1bbe59ca02cacecfb011e201f5a1f" resolved "https://registry.yarnpkg.com/camelcase/-/camelcase-2.1.1.tgz#7c1d16d679a1bbe59ca02cacecfb011e201f5a1f"
integrity sha1-fB0W1nmhu+WcoCys7PsBHiAfWh8= 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: can-symlink@^1.0.0:
version "1.0.0" version "1.0.0"
resolved "https://registry.yarnpkg.com/can-symlink/-/can-symlink-1.0.0.tgz#97b607d8a84bb6c6e228b902d864ecb594b9d219" 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" resolved "https://registry.yarnpkg.com/pascalcase/-/pascalcase-0.1.1.tgz#b363e55e8006ca6fe21784d2db22bd15d7917f14"
integrity sha1-s2PlXoAGym/iF4TS2yK9FdeRfxQ= 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: path-browserify@0.0.0:
version "0.0.0" version "0.0.0"
resolved "https://registry.yarnpkg.com/path-browserify/-/path-browserify-0.0.0.tgz#a0b870729aae214005b7d5032ec2cbbb0fb4451a" 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" fs-extra "^4.0.2"
lodash.merge "^4.6.0" 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: yeast@0.1.2:
version "0.1.2" version "0.1.2"
resolved "https://registry.yarnpkg.com/yeast/-/yeast-0.1.2.tgz#008e06d8094320c372dbc2f8ed76a0ca6c8ac419" resolved "https://registry.yarnpkg.com/yeast/-/yeast-0.1.2.tgz#008e06d8094320c372dbc2f8ed76a0ca6c8ac419"