$success_url = $this->container['router']->reverse(array('IPF_Admin_Dashboard_Controller', 'index'));
$auth_app = $this->authApp();
+ $auth_serice = $this->container['auth_service'];
if ($this->request->method == 'POST') {
- $form = new IPF_Admin_Forms_Login($this->request->POST, array('auth_app' => $auth_app));
+ $form = new IPF_Admin_Forms_Login($this->request->POST, array('auth_app' => $auth_app, 'auth_service' => $auth_serice));
if ($form->isValid()) {
$auth_app->login($this->request, $form->user);
return new IPF_HTTP_Response_Redirect($success_url);
}
} else {
- $form = new IPF_Admin_Forms_Login(array('next'=>$success_url), array('auth_app' => $auth_app));
+ $form = new IPF_Admin_Forms_Login(array('next'=>$success_url), array('auth_app' => $auth_app, 'auth_service' => $auth_serice));
}
$context = array(
/** @var IPF_Auth_App */
private $auth_app;
+ /** @var \IPF\Auth\AuthService */
+ private $auth_service;
protected function initFields($extra=array())
{
$this->auth_app = $extra['auth_app'];
+ $this->auth_service = $extra['auth_service'];
$this->fields['username'] = new IPF_Form_Field_Varchar(array('required'=>true));
$this->fields['password'] = new IPF_Form_Field_Varchar(array('required'=>true,'widget'=>'IPF_Form_Widget_PasswordInput'));
$data = parent::clean();
$user = $this->auth_app->findUserByUsername($data['username']);
- if (!$user || !$user->checkPassword($data['password']))
+ if ($this->auth_service->checkPassword($user, $data['password'])) {
+ $this->user = $user;
+ return $data;
+ } else {
throw new IPF_Exception_Form(__('The login or the password is not valid. The login and the password are case sensitive.'));
-
- $this->user = $user;
- return $data;
+ }
}
public function render()
namespace IPF\Auth\Admin;
use Doctrine\DBAL\Connection;
+use IPF\Auth\AuthService;
use IPF\Auth\Permission as Permission;
use IPF\Auth\Role as Role;
use IPF_Admin_App;
{
/** @var Container */
protected $container;
+ /** @var AuthService */
+ protected $auth_service;
- function __construct(Container $container, IPF_Auth_App $auth_app)
+ function __construct(Container $container, IPF_Auth_App $auth_app, AuthService $auth_service)
{
$this->auth_app = $auth_app;
$this->container = $container;
+ $this->auth_service = $auth_service;
}
/**
$user = $this->authApp()->createUser();
$form->toObject($user);
- if ($form->cleaned_data['password1'])
- $user->setPassword($form->cleaned_data['password1']);
+ if ($form->cleaned_data['password1']) {
+ $this->auth_service->setPassword($user, $form->cleaned_data['password1']);
+ }
$user->save($connection);
if ($this->authApp()->arePermissionsEnabled()) {
{
/** @var IPF_Auth_App $app */
+ $auth_service = $container['auth_service'];
+
$components = array(
- new AdminUser($container, $app),
+ new AdminUser($container, $app, $auth_service),
);
if ($app->arePermissionsEnabled()) {
- $components[] = new AdminRole($container, $app);
+ $components[] = new AdminRole($container, $app, $auth_service);
}
return $components;
<?php
use Doctrine\DBAL\Connection;
+use IPF\Auth\AuthServiceDefault;
use IPF\Auth\User;
use Pimple\Container;
*/
public function configure(Container $container)
{
+ $container['auth_service'] = function ($c) {
+ return new AuthServiceDefault();
+ };
+
$this->container = $container;
$this->userModel = $container['settings']->get('auth_user_model', User::class);
}
public function commands()
{
return array(
- new \IPF\Auth\Commands\CreateSuperUser($this),
+ new \IPF\Auth\Commands\CreateSuperUser($this, $this->container['auth_service']),
);
}
--- /dev/null
+<?php
+
+namespace IPF\Auth;
+
+use IPF_Utils;
+
+interface AuthService
+{
+ function setPassword($user, $raw_password);
+
+ function checkPassword($user, $raw_password);
+}
+
+class AuthServiceDefault implements AuthService
+{
+ function setPassword($user, $raw_password)
+ {
+ if ($raw_password) {
+ $salt = IPF_Utils::randomString(5);
+ $user->password = 'sha1:' . $salt . ':' . sha1($salt . $raw_password);
+ } else {
+ $user->password = '';
+ }
+ }
+
+ function checkPassword($user, $raw_password)
+ {
+ if ($raw_password &&
+ $user &&
+ $user->is_active &&
+ $user->password &&
+ strpos($user->password, ':') !== false
+ ) {
+ list($algo, $salt, $hash) = explode(':', $user->password);
+ return $hash === $algo($salt . $raw_password);
+ } else {
+ return false;
+ }
+ }
+}
namespace IPF\Auth\Commands;
+use IPF\Auth\AuthService;
+
class CreateSuperUser
{
public $command = 'createsuperuser';
/** @var \IPF_Auth_App */
private $auth_app;
+ /** @var AuthService */
+ private $auth_service;
- public function __construct(\IPF_Auth_App $auth_app)
+ public function __construct(\IPF_Auth_App $auth_app, AuthService $auth_service)
{
$this->auth_app = $auth_app;
+ $this->auth_service = $auth_service;
}
public function run($args=null)
$su->is_staff = true;
$su->is_active = true;
$su->is_superuser = true;
- $su->setPassword($password);
+ $this->auth_service->setPassword($su, $password);
$su->save();
print "Done\n";
}
return $s;
}
- function setPassword($raw_password)
- {
- if ($raw_password)
- $this->password = \IPF_Crypto::hashPassword($raw_password);
- else
- $this->password = '';
- }
-
- function checkPassword($raw_password)
- {
- return $this->is_active && \IPF_Crypto::checkPassword($raw_password, $this->password);
- }
-
function isAnonymous()
{
return false;
return hash_hmac('sha1', $string, self::get_key());
}
- public static function hashPassword($rawPassword)
- {
- $salt = IPF_Utils::randomString(5);
- return 'sha1:'.$salt.':'.sha1($salt . $rawPassword);
- }
-
- public static function checkPassword($rawPassword, $hashedPassword)
- {
- if (!$rawPassword || !$hashedPassword || strpos($hashedPassword, ':') === false)
- return false;
- list($algo, $salt, $hash) = explode(':', $hashedPassword);
- return $hash === $algo($salt . $rawPassword);
- }
-
private static function get_key()
{
$secret = self::secretKey();
throw new Exception('Security error: "secret_key" is not set in the configuration file.');
}
}
-
+++ /dev/null
-<?php
-
-class Crypto_Test extends PHPUnit_Framework_TestCase
-{
- public function testRecording()
- {
- $this->assertFalse(IPF_Crypto::checkPassword('secret', 'badhash'));
- }
-}