Skip to content

Commit

Permalink
[FIX] Random generated password not matching the Password Policy (#18475
Browse files Browse the repository at this point in the history
)
  • Loading branch information
rodrigok authored Aug 6, 2020
1 parent 37fe21f commit 3ce8e65
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
3 changes: 1 addition & 2 deletions app/lib/server/functions/saveUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { Accounts } from 'meteor/accounts-base';
import _ from 'underscore';
import s from 'underscore.string';
import { Gravatar } from 'meteor/jparker:gravatar';
import { Random } from 'meteor/random';

import * as Mailer from '../../../mailer';
import { getRoles, hasPermission } from '../../../authorization';
Expand Down Expand Up @@ -238,7 +237,7 @@ export const saveUser = function(userId, userData) {

if (userData.hasOwnProperty('setRandomPassword')) {
if (userData.setRandomPassword) {
userData.password = Random.id();
userData.password = passwordPolicy.generatePassword();
userData.requirePasswordChange = true;
sendPassword = true;
}
Expand Down
19 changes: 19 additions & 0 deletions app/lib/server/lib/PasswordPolicyClass.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Meteor } from 'meteor/meteor';
import { Random } from 'meteor/random';

class PasswordPolicy {
constructor({
Expand Down Expand Up @@ -123,6 +124,24 @@ class PasswordPolicy {
}
return data;
}

generatePassword() {
if (this.enabled) {
for (let i = 0; i < 10; i++) {
const password = this._generatePassword();
if (this.validate(password)) {
return password;
}
}
}

return Random.id();
}

_generatePassword() {
const length = Math.min(Math.max(this.minLength, 12), this.maxLength > 0 ? this.maxLength : Number.MAX_SAFE_INTEGER);
return new Array(length).fill().map(() => String.fromCharCode(Math.random() * 86 + 40)).join('');
}
}

export default PasswordPolicy;

0 comments on commit 3ce8e65

Please sign in to comment.