grav/system/src/Grav/Common/User/Authentication.php
Djamil Legato 230a2b594e Source
2014-08-02 12:12:32 -07:00

47 lines
1.2 KiB
PHP

<?php
namespace Grav\Common\User;
/**
* User authentication
*
* @author RocketTheme
* @license MIT
*/
abstract class Authentication
{
/**
* Create password hash from plaintext password.
*
* @param string $password Plaintext password.
* @return string|bool
*/
static public function create($password)
{
return password_hash($password, PASSWORD_DEFAULT);
}
/**
* Verifies that a password matches a hash.
*
* @param string $password Plaintext password.
* @param string $hash Hash to verify against.
* @return int Returns 0 if the check fails, 1 if password matches, 2 if hash needs to be updated.
*/
static public function verify($password, $hash)
{
// Always accept plaintext passwords (needs an update).
// FIXME: not safe to do this...
if ($password && $password == $hash) {
return 2;
}
// Fail if hash doesn't match.
if (!$password || !password_verify($password, $hash)) {
return 0;
}
// Otherwise check if hash needs an update.
return password_needs_rehash($hash, PASSWORD_DEFAULT) ? 2 : 1;
}
}