Fixes after rebase (Login providers)

This commit is contained in:
Dmitry Khomutov 2016-07-19 15:34:36 +06:00
commit 9db4b09bd9
No known key found for this signature in database
GPG key ID: 7EB36C9576F9ECB9
6 changed files with 0 additions and 0 deletions

View file

@ -0,0 +1,41 @@
<?php
use Phinx\Db\Adapter\MysqlAdapter;
use Phinx\Migration\AbstractMigration;
class AddUserProviders extends AbstractMigration
{
/**
* Migrate Up.
*/
public function up()
{
// Add the provider columns
$this
->table('user')
// The provider name
->addColumn('provider_key', 'string', array(
'default' => 'internal',
'limit' => MysqlAdapter::TEXT_SMALL
))
// A data used by the provider
->addColumn('provider_data', 'string', array(
'null' => true,
'limit' => MysqlAdapter::TEXT_SMALL
))
->save();
}
/**
* Migrate Down.
*/
public function down()
{
// Remove the provider columns
$this
->table('user')
->removeColumn('provider_key')
->removeColumn('provider_data')
->save();
}
}

View file

@ -0,0 +1,30 @@
<?php
/**
* PHPCI - Continuous Integration for PHP
*
* @copyright Copyright 2015, Block 8 Limited.
* @license https://github.com/Block8/PHPCI/blob/master/LICENSE.md
* @link https://www.phptesting.org/
*/
namespace PHPCI\Security\Authentication;
use PHPCI\Model\User;
/**
* User provider which authenticiation using a password.
*
* @author Adirelle <adirelle@gmail.com>
*/
interface LoginPasswordProvider extends UserProvider
{
/** Verify if the supplied password matches the user's one.
*
* @param User $user
* @param string $password
*
* @return bool
*/
public function verifyPassword(User $user, $password);
}

View file

@ -0,0 +1,103 @@
<?php
/**
* PHPCI - Continuous Integration for PHP
*
* @copyright Copyright 2015, Block 8 Limited.
* @license https://github.com/Block8/PHPCI/blob/master/LICENSE.md
* @link https://www.phptesting.org/
*/
namespace PHPCI\Security\Authentication;
use b8\Config;
/**
* Authentication facade.
*
* @author Adirelle <adirelle@gmail.com>
*/
class Service
{
/**
*
* @var Service
*/
static private $instance;
/** Return the service singletion.
*
* @return Service
*/
public static function getInstance()
{
if (self::$instance === null) {
$config = Config::getInstance()->get(
'phpci.security.authentication',
['internal' => ['type' => 'internal']]
);
$providers = [];
foreach ($config as $key => $providerConfig) {
$providers[$key] = self::buildProvider($key, $providerConfig);
}
self::$instance = new self($providers);
}
return self::$instance;
}
/** Create a provider from a given configuration.
*
* @param string $key
* @param string|array $config
* @return UserProvider
*/
public static function buildProvider($key, $config)
{
$class = ucfirst($config['type']);
if (class_exists('\\PHPCI\\Security\\Authentication\\UserProvider\\' . $class)) {
$class = '\\PHPCI\\Security\\Authentication\\UserProvider\\' . $class;
}
return new $class($key, $config);
}
/** The table of providers.
*
* @var array
*/
private $providers;
/** Initialize the service.
*
* @param array $providers
*/
public function __construct(array $providers)
{
$this->providers = $providers;
}
/** Return all providers.
*
* @return UserProvider[]
*/
public function getProviders()
{
return $this->providers;
}
/** Return the user providers that allows password authentication.
*
* @return LoginPasswordProvider[]
*/
public function getLoginPasswordProviders()
{
$providers = [];
foreach ($this->providers as $key => $provider) {
if ($provider instanceof LoginPasswordProvider) {
$providers[$key] = $provider;
}
}
return $providers;
}
}

View file

@ -0,0 +1,36 @@
<?php
/**
* PHPCI - Continuous Integration for PHP
*
* @copyright Copyright 2015, Block 8 Limited.
* @license https://github.com/Block8/PHPCI/blob/master/LICENSE.md
* @link https://www.phptesting.org/
*/
namespace PHPCI\Security\Authentication;
use PHPCI\Model\User;
/**
* User provider interface.
*
* @author Adirelle <adirelle@gmail.com>
*/
interface UserProvider
{
/** Check if all software requirements are met (libraries, extensions, ...)
*
* @throws Exception
*/
public function checkRequirements();
/** Provision an new user for the given identifier.
*
* @param string $identifier The user identifier.
*
* @return User|null The new user or null if the provider does not know the user.
*/
public function provisionUser($identifier);
}

View file

@ -0,0 +1,40 @@
<?php
/**
* PHPCI - Continuous Integration for PHP
*
* @copyright Copyright 2014, Block 8 Limited.
* @license https://github.com/Block8/PHPCI/blob/master/LICENSE.md
* @link https://www.phptesting.org/
*/
namespace PHPCI\Security\Authentication\UserProvider;
use PHPCI\Security\Authentication\UserProvider;
/**
* Abstract user provider.
*
* @author Adirelle <adirelle@gmail.com>
*/
abstract class AbstractProvider implements UserProvider
{
/**
* @var string
*/
private $key;
public function __construct($key)
{
$this->key = $key;
}
/**
*
* @return string
*/
public function getKey()
{
return $this->key;
}
}

View file

@ -0,0 +1,37 @@
<?php
/**
* PHPCI - Continuous Integration for PHP
*
* @copyright Copyright 2014, Block 8 Limited.
* @license https://github.com/Block8/PHPCI/blob/master/LICENSE.md
* @link https://www.phptesting.org/
*/
namespace PHPCI\Security\Authentication\UserProvider;
use PHPCI\Model\User;
use PHPCI\Security\Authentication\LoginPasswordProvider;
/**
* Internal user provider.
* @author Adirelle <adirelle@gmail.com>
*/
class Internal extends AbstractProvider implements LoginPasswordProvider
{
public function verifyPassword(User $user, $password)
{
return password_verify($password, $user->getHash());
}
public function checkRequirements()
{
// Always fine
}
public function provisionUser($identifier)
{
return null;
}
}