CakePHP 2: Override AuthComponent's “password” method

拥有回忆 提交于 2019-12-03 02:50:36

You could probably create a custom auth object and hash the password however you like. Take a look at the existing auth objects to get the general idea of how they work.

Have you considered not using Auth->login() call but rather using the code from the current implementation in your model? (http://api20.cakephp.org/view_source/auth-component#line-506) You could rewrite this to suit your needs.

For anyone wanting more information on why salting each password is the right way to hash passwords (w/code examples), visit here: http://crackstation.net/hashing-security.htm.

Perhaps a slight improvement to the code posted here is to take the advice of the article I just linked to, and to generate a "new random salt" ... "each time a user creates an account or changes their password."

The implementation posted here uses a combination of the original Auth's hard-coded static salt plus the user ID as the salt which means that the same salt gets re-used for each user whenever they change their password. So if you want to follow the recommendations of this hashing guide, you need to generate a new random salt each time the user creates/changes their password, and must store that unique salt in the users table along with the hashed password.

You could use their random salt generator:

define("PBKDF2_SALT_BYTES", 24);
$salt = base64_encode(mcrypt_create_iv(PBKDF2_SALT_BYTES, MCRYPT_DEV_URANDOM));

and by convention, store it in the users table in a new field named 'salt'. Since the code already gives you the user id, you can always store/lookup the salt as needed.

Also mentioned in the article is a section on "Slow Hash Functions" using a technique known as "key stretching" and how to implement using standard algorithm like PBKDF2 or bcrypt. PHP code examples are provided which can be copied and pasted into your custom Auth implementation for added security.

CakePHP developer Mark Story has posted a blog entry on how to implement bcrypt in CakePHP's Auth

In the comments section, Mark Story commented that CakePHP 2.3 will have some new built-in features to generate bcrypt hashes.

Atleast in cake 2.3 a unique salt is already used, even though the salt in your config value is always the same. I'm not sure if this is true for older versions as well.

You could also just change the salt in your beforeSave() function in the User model by using Configure::write("Security.salt", $superAwesomeUserSpecificSalt);

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!