Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented salt support for password hashing #92

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -72,19 +72,26 @@ class SchemaProcessor extends AbstractPersistenceProcessor implements SchemaProc
* @var array
*/
protected $users = array(
array('appserver', 'appserver.i0', array('Customer')),
array('appserver_01', 'appserver.i0', array('Customer')),
array('appserver_02', 'appserver.i0', array('Customer')),
array('appserver_03', 'appserver.i0', array('Customer')),
array('appserver_04', 'appserver.i0', array('Customer')),
array('appserver_05', 'appserver.i0', array('Customer')),
array('appserver_06', 'appserver.i0', array('Customer')),
array('appserver_07', 'appserver.i0', array('Customer')),
array('appserver_08', 'appserver.i0', array('Customer')),
array('appserver_09', 'appserver.i0', array('Customer')),
array('appserver', 'appserver.i0', 'salt', array('Customer')),
array('appserver_01', 'appserver.i0', 'salt01', array('Customer')),
array('appserver_02', 'appserver.i0', 'salt02', array('Customer')),
array('appserver_03', 'appserver.i0', 'salt03', array('Customer')),
array('appserver_04', 'appserver.i0', 'salt04', array('Customer')),
array('appserver_05', 'appserver.i0', 'salt05', array('Customer')),
array('appserver_06', 'appserver.i0', 'salt06', array('Customer')),
array('appserver_07', 'appserver.i0', 'salt07', array('Customer')),
array('appserver_08', 'appserver.i0', 'salt08', array('Customer')),
array('appserver_09', 'appserver.i0', 'salt09', array('Customer')),
array('guest', 'appserver.i0', array('Guest'))
);

/**
* The hash algorithm to hash the passwords with
*
* @var string
*/
protected $hashAlgorithm;

/**
* Example method that should be invoked after constructor.
*
Expand All @@ -96,6 +103,7 @@ public function initialize()
$this->getSystemLogger()->info(
sprintf('%s has successfully been invoked by @PostConstruct annotation', __METHOD__)
);
$this->hashAlgorithm = 'sha512';
}

/**
Expand Down Expand Up @@ -232,7 +240,7 @@ public function createDefaultCredentials()
// create the default credentials
foreach ($this->users as $userData) {
// extract the user data
list ($username, $password, $roleNames) = $userData;
list ($username, $password, $salt, $roleNames) = $userData;

// query whether or not, the user has already been created
if ($repository->findOneByUsername($username)) {
Expand All @@ -244,7 +252,8 @@ public function createDefaultCredentials()
$user->setEmail(sprintf('%[email protected]', $username));
$user->setUsername($username);
$user->setUserLocale('en_US');
$user->setPassword(md5($password));
$user->setPassword(hash($this->hashAlgorithm, $salt . $password));
$user->setSalt($salt);
$user->setEnabled(true);
$user->setRate(1000);
$user->setContractedHours(160);
Expand Down
5 changes: 3 additions & 2 deletions src/META-INF/context.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
<param name="lookupName" type="string">php:env/${container.name}/ds/appserver.io-example-application</param>
<param name="principalsQuery" type="string">select password from user where username = ?</param>
<param name="rolesQuery" type="string">select r.name, 'Roles' from role r inner join user p on r.userIdFk = p.userId where p.username = ?</param>
<param name="hashAlgorithm" type="string">SHA-512</param>
<param name="saltQuery" type="string">select salt from user where username = ?</param>
<param name="hashAlgorithm" type="string">sha512</param>
<param name="hashEncoding" type="string">hex</param>
<param name="password-stacking" type="string">useFirstPass</param>
</params>
Expand All @@ -40,4 +41,4 @@
</manager>
</managers>

</context>
</context>
29 changes: 29 additions & 0 deletions src/common/classes/AppserverIo/Apps/Example/Entities/Impl/User.php
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ class User
*/
protected $password;

/**
* @var string
*
* @ORM\Column(type="string")
*/
protected $salt;

/**
* @var boolean
*
Expand Down Expand Up @@ -397,4 +404,26 @@ public function getRoles()
{
return $this->roles;
}

/**
* Returns the value of the class member salt.
*
* @return string Holds the value of the class member salt
*/
public function getSalt()
{
return $this->salt;
}

/**
* Sets the value for the class member salt.
*
* @param string $salt Holds the value for the class member salt
*
* @return void
*/
public function setSalt($salt)
{
$this->salt = $salt;
}
}