2017 © Pedro Peláez
 

project simple-rest-auth

Authentification for API REST

image

mickaelbaudoin/simple-rest-auth

Authentification for API REST

  • Wednesday, August 17, 2016
  • by mickaelbaudoin
  • Repository
  • 1 Watchers
  • 1 Stars
  • 1 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

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') );
    }
}

The Versions

17/08 2016

dev-master

9999999-dev https://github.com/mickaelbaudoin/simple-rest-auth

Authentification for API REST

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires