chg: use new Tool for random string generation

pull/1457/head
Andreas Ziegler 2016-08-25 20:19:16 +02:00
parent 72730e54ef
commit 7a5dad6598
6 changed files with 32 additions and 81 deletions

View File

@ -4,6 +4,7 @@ App::uses('AppModel', 'Model');
App::uses('Folder', 'Utility');
App::uses('File', 'Utility');
App::uses('FinancialTool', 'Tools');
App::uses('RandomTool', 'Tools');
class Attribute extends AppModel {
@ -1767,14 +1768,7 @@ class Attribute extends AppModel {
}
public function generateRandomFileName() {
$length = 12;
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charLen = strlen($characters) - 1;
$fn = '';
for ($p = 0; $p < $length; $p++) {
$fn .= $characters[rand(0, $charLen)];
}
return $fn;
return (new RandomTool())->random_str(FALSE, 12);
}
public function resolveHashType($hash) {

View File

@ -1,6 +1,7 @@
<?php
App::uses('AppModel', 'Model');
App::uses('CakeEmail', 'Network/Email');
App::uses('RandomTool', 'Tools');
Configure::load('config'); // This is needed to load GnuPG.bodyonlyencrypted
class Event extends AppModel {
@ -2544,14 +2545,7 @@ class Event extends AppModel {
}
public function generateRandomFileName() {
$length = 12;
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charLen = strlen($characters) - 1;
$fn = '';
for ($p = 0; $p < $length; $p++) {
$fn .= $characters[rand(0, $charLen)];
}
return $fn;
return (new RandomTool())->random_str(FALSE, 12);
}

View File

@ -1,5 +1,6 @@
<?php
App::uses('AppModel', 'Model');
App::uses('RandomTool', 'Tools');
class Sighting extends AppModel {
@ -140,13 +141,6 @@ class Sighting extends AppModel {
}
public function generateRandomFileName() {
$length = 12;
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charLen = strlen($characters) - 1;
$fn = '';
for ($p = 0; $p < $length; $p++) {
$fn .= $characters[rand(0, $charLen)];
}
return $fn;
return (new RandomTool())->random_str(FALSE, 12);
}
}

View File

@ -1,6 +1,7 @@
<?php
App::uses('AppModel', 'Model');
App::uses('RandomTool', 'Tools');
class Template extends AppModel {
@ -63,13 +64,6 @@ class Template extends AppModel {
}
public function generateRandomFileName() {
$length = 12;
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charLen = strlen($characters) - 1;
$fn = '';
for ($p = 0; $p < $length; $p++) {
$fn .= $characters[rand(0, $charLen)];
}
return $fn;
return (new RandomTool())->random_str(FALSE, 12);
}
}

View File

@ -1,6 +1,7 @@
<?php
App::uses('AppModel', 'Model');
App::uses('AuthComponent', 'Controller/Component');
App::uses('RandomTool', 'Tools');
class User extends AppModel {
@ -215,29 +216,10 @@ class User extends AppModel {
'Containable'
);
private function __generatePassword() {
$groups = array(
'0123456789',
'abcdefghijklmnopqrstuvwxyz',
'ABCDEFGHIJKLOMNOPQRSTUVWXYZ',
'!@#$%^&*()_-'
);
$passwordLength = Configure::read('Security.password_policy_length') ? Configure::read('Security.password_policy_length') : 12;
$pw = '';
for ($i = 0; $i < $passwordLength; $i++) {
$chars = implode('', $groups);
$pw .= $chars[mt_rand(0, strlen($chars)-1)];
}
foreach ($groups as &$group) {
$pw .= $group[mt_rand(0, strlen($group)-1)];
}
return $pw;
}
public function beforeValidate($options = array()) {
if (!isset($this->data['User']['id'])) {
if ((isset($this->data['User']['enable_password']) && (!$this->data['User']['enable_password'])) || (empty($this->data['User']['password']) && empty($this->data['User']['confirm_password']))) {
$this->data['User']['password'] = $this->__generatePassword();
$this->data['User']['password'] = $this->generateRandomPassword();
$this->data['User']['confirm_password'] = $this->data['User']['password'];
}
}
@ -385,24 +367,23 @@ class User extends AppModel {
}
public function generateAuthKey() {
$length = 40;
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charLen = strlen($characters) - 1;
$key = '';
for ($p = 0; $p < $length; $p++) {
$key .= $characters[rand(0, $charLen)];
}
return $key;
return (new RandomTool())->random_str(TRUE, 40);
}
public function generateRandomPassword($length = 12) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-+=!@#$%&*()<>/?';
$charLen = strlen($characters) - 1;
$key = '';
for ($p = 0; $p < $length; $p++) {
$key .= $characters[rand(0, $charLen)];
/**
* Generates a cryptographically secure password
*
* @param int $passwordLength
* @return string
*/
public function generateRandomPassword($passwordLength = 40) {
// makes sure, the password policy isn't undermined by setting a manual passwordLength
$policyPasswordLength = Configure::read('Security.password_policy_length') ? Configure::read('Security.password_policy_length') : false;
if (is_int($policyPasswordLength) && $policyPasswordLength > $passwordLength) {
$passwordLength = $policyPasswordLength;
}
return $key;
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-+=!@#$%^&*()<>/?';
return (new RandomTool())->random_str(TRUE, $passwordLength, $characters);
}

View File

@ -1,6 +1,8 @@
<?php
App::uses('BaseAuthenticate', 'Controller/Component/Auth');
App::uses('RandomTool', 'Tools');
session_start();
session_regenerate_id();
/*
@ -108,8 +110,11 @@ class ApacheShibbAuthenticate extends BaseAuthenticate {
CakeLog::write('info', "User ${mispUsername} not found in database.");
//Insert user in database if not existent
//Generate random password
$password = $this->randPasswordGen(40);
// Generate random password
$password = $userModel->generateRandomPassword();
// Generate random auth key
$authKey = $userModel->generateAuthKey();
// get maximum nids value
$nidsMax = $userModel->find('all', array(
'fields' => array('MAX(User.nids_sid) AS nidsMax'),
@ -121,7 +126,7 @@ class ApacheShibbAuthenticate extends BaseAuthenticate {
'org_id' => $org,
'password' => $password, //Since it is done via shibboleth the password will be a random 40 character string
'confirm_password' => $password,
'authkey' => $userModel->generateAuthKey(),
'authkey' => $authKey,
'nids_sid' => ((int)$nidsMax[0][0]['nidsMax'])+1,
'newsread' => date('Y-m-d'),
'role_id' => $roleId,
@ -137,17 +142,6 @@ class ApacheShibbAuthenticate extends BaseAuthenticate {
);
}
private function randPasswordGen($len) {
$result = "";
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\$_?!-0123456789";
$charArray = str_split($chars);
for ($i = 0; $i < $len; $i++) {
$randItem = array_rand($charArray);
$result .= "".$charArray[$randItem];
}
return $result;
}
/**
* @param $roleChanged
* @param $user