Skip to content

Commit

Permalink
Adding Security.cipherSeed generation to bake project task, closes #237
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Jan 24, 2010
1 parent c2d19c2 commit 0387907
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
30 changes: 30 additions & 0 deletions cake/console/libs/tasks/project.php
Expand Up @@ -90,6 +90,12 @@ function execute($project = null) {
$this->err(sprintf(__('Unable to generate random hash for \'Security.salt\', you should change it in %s', true), CONFIGS . 'core.php')); $this->err(sprintf(__('Unable to generate random hash for \'Security.salt\', you should change it in %s', true), CONFIGS . 'core.php'));
} }


if ($this->securityCipherSeed($path) === true ) {
$this->out(__('Random seed created for \'Security.cipherSeed\'', true));
} else {
$this->err(sprintf(__('Unable to generate random seed for \'Security.cipherSeed\', you should change it in %s', true), CONFIGS . 'core.php'));
}

$corePath = $this->corePath($path); $corePath = $this->corePath($path);
if ($corePath === true ) { if ($corePath === true ) {
$this->out(sprintf(__('CAKE_CORE_INCLUDE_PATH set to %s in webroot/index.php', true), CAKE_CORE_INCLUDE_PATH)); $this->out(sprintf(__('CAKE_CORE_INCLUDE_PATH set to %s in webroot/index.php', true), CAKE_CORE_INCLUDE_PATH));
Expand Down Expand Up @@ -215,6 +221,30 @@ function securitySalt($path) {
return false; return false;
} }


/**
* Generates and writes 'Security.cipherSeed'
*
* @param string $path Project path
* @return boolean Success
* @access public
*/
function securityCipherSeed($path) {
$File =& new File($path . 'config' . DS . 'core.php');
$contents = $File->read();
if (preg_match('/([\\t\\x20]*Configure::write\\(\\\'Security.cipherSeed\\\',[\\t\\x20\'A-z0-9]*\\);)/', $contents, $match)) {
if (!class_exists('Security')) {
require LIBS . 'security.php';
}
$string = substr(bin2hex(Security::generateAuthKey()), 0, 30);
$result = str_replace($match[0], "\t" . 'Configure::write(\'Security.cipherSeed\', \''.$string.'\');', $contents);
if ($File->write($result)) {
return true;
}
return false;
}
return false;
}

/** /**
* Generates and writes CAKE_CORE_INCLUDE_PATH * Generates and writes CAKE_CORE_INCLUDE_PATH
* *
Expand Down
18 changes: 18 additions & 0 deletions cake/tests/cases/console/libs/tasks/project.test.php
Expand Up @@ -173,6 +173,24 @@ function testSecuritySaltGeneration() {
$this->assertNoPattern('/DYhG93b0qyJfIxfs2guVoUubWwvniR2G0FgaC9mi/', $contents, 'Default Salt left behind. %s'); $this->assertNoPattern('/DYhG93b0qyJfIxfs2guVoUubWwvniR2G0FgaC9mi/', $contents, 'Default Salt left behind. %s');
} }


/**
* test generation of Security.salt
*
* @return void
* @access public
*/
function testSecurityCipherSeedGeneration() {
$this->_setupTestProject();

$path = $this->Task->path . 'bake_test_app' . DS;
$result = $this->Task->securityCipherSeed($path);
$this->assertTrue($result);

$file =& new File($path . 'config' . DS . 'core.php');
$contents = $file->read();
$this->assertNoPattern('/76859309657453542496749683645/', $contents, 'Default Salt left behind. %s');
}

/** /**
* Test that index.php is generated correctly. * Test that index.php is generated correctly.
* *
Expand Down

0 comments on commit 0387907

Please sign in to comment.