Synospis
Authentication API REST, (*1)
Code example
Step 1 - Create UserEntity and UserService
namespace Foo\Entity;
class UserEntity implements \MB\SimpleRestAuth\IUser{
public function getGroups() {
return [];
}
public function getLogin() {
return 'test';
}
public function getToken() {
return 'DKS827HDKLSC782';
}
public function getTokenDateExpired() {
}
public function getUserId() {
return 1;
}
public function setToken($token) {
}
}
namespace Foo\Service;
class UserService implements \MB\SimpleRestAuth\IUserService{
private $login = "test";
private $password = "123456";
public function findUserByFilters(\Psr\Http\Message\ServerRequestInterface $request) {
$login = $request->getAttribute('login');
$password = $request->getAttribute('password');
if($login == $this->login && $password == $this->password){
$user = new UserEntity();
return $user;
}
return null;
}
public function generateToken(\MB\SimpleRestAuth\IUser $user) {
return $user;
}
}
Step 2 - Configuring Factories and Middlewares
With zend-expressive
index.php, (*2)
<?php
use Zend\ServiceManager\Config;
use Zend\ServiceManager\ServiceManager;
// Load configuration
$config = require __DIR__ . '/config.php';
// Build container
$container = new ServiceManager();
//Authentification
$container->setService("IUserService", new \Lib\AuthImpl\UserService());
//Config
(new Config($config['dependencies']))->configureServiceManager($container);
// Inject config
$container->setService('config', $config);
return $container;
routes.global.php, (*3)
<?php
use Zend\Expressive\Container\ApplicationFactory;
use Zend\Expressive\Helper;
return [
// Map middleware -> factories here
'factories' => [
'MB\AuthorizationMiddleware' => 'Foo\Factories\AuthentificationFactory',
],
.
.
.
'routes' : [
[
'name' => 'auth',
'path' => "/auth/login/{login:\w+}/password/{password:\w+}",
'middleware' => MB\AuthentificationMiddleware::class,
'allowed_methods' => ['POST'],
],
]
.
.
Step 3 - Create AuthentificationFactory
Create AuthentificationFactory for injected IUserService, (*4)
<?php
namespace Foo\Factories;
use Interop\Container\ContainerInterface;
use MB\AuthentificationMidleware;
/**
* Description of AuthentificationFactory
*/
class AuthentificationFactory {
public function __invoke(ContainerInterface $container)
{
return new AuthentificationMidleware( $container->get('IUserService') );
}
}