add authentication services and do some unit testing

parent 32c1ccc5
<?php
/**
* gredu_labs
*
* @link https://github.com/eellak/gredu_labs for the canonical source repository
* @copyright Copyright (c) 2008-2015 Greek Free/Open Source Software Society (https://gfoss.ellak.gr/)
* @license GNU GPLv3 http://www.gnu.org/licenses/gpl-3.0-standalone.html
*/
$container = $app->getContainer();
......@@ -10,9 +18,10 @@ $container['view'] = function ($c) {
$settings['view']['template_path'],
$settings['view']['twig']
);
$view->addExtension(
new Slim\Views\TwigExtension($c->get('router'), $c->get('request')->getUri())
);
$view->addExtension(new Slim\Views\TwigExtension(
$c->get('router'),
$c->get('request')->getUri()
));
$view->addExtension(new Twig_Extension_Debug());
return $view;
......@@ -21,7 +30,7 @@ $container['view'] = function ($c) {
// Flash messages
$container['flash'] = function ($c) {
return new \Slim\Flash\messages;
return new \Slim\Flash\Messages;
};
// Monolog
......@@ -37,3 +46,30 @@ $container['logger'] = function ($c) {
return $logger;
};
$container['events'] = function ($c) {
return new \Zend\EventManager\EventManager(
new \Zend\EventManager\SharedEventManager()
);
};
$container['Service\\Authentication\\Adapter'] = function ($c) use ($app) {
return new \Zend\Authentication\Adapter\Callback(function () use ($c) {
return $c->events->triggerUntil(
function (\Zend\Authentication\Result $result) {
return $result->isValid();
},
'authenticate', $app, func_get_args()
)->last();
});
};
$container['Service\\Authentication'] = function ($c) {
$service = new \Zend\Authentication\AuthenticationService(
new \GrEduLabs\Authentication\Storage\PhpSession($_SESSION),
$c->get('Service\\Authentication\\Adapter')
);
return $service;
};
<?php
/**
* gredu_labs
*
* @link https://github.com/eellak/gredu_labs for the canonical source repository
* @copyright Copyright (c) 2008-2015 Greek Free/Open Source Software Society (https://gfoss.ellak.gr/)
* @license GNU GPLv3 http://www.gnu.org/licenses/gpl-3.0-standalone.html
*/
// Application middleware
// e.g: $app->add(new \Slim\Csrf\Guard);
<?php
/**
* gredu_labs
*
* @link https://github.com/eellak/gredu_labs for the canonical source repository
* @copyright Copyright (c) 2008-2015 Greek Free/Open Source Software Society (https://gfoss.ellak.gr/)
* @license GNU GPLv3 http://www.gnu.org/licenses/gpl-3.0-standalone.html
*/
$app->get('/', function ($request, $response, $args) use ($app) {
$container = $app->getContainer();
......
<?php
/**
* gredu_labs
*
* @link https://github.com/eellak/gredu_labs for the canonical source repository
* @copyright Copyright (c) 2008-2015 Greek Free/Open Source Software Society (https://gfoss.ellak.gr/)
* @license GNU GPLv3 http://www.gnu.org/licenses/gpl-3.0-standalone.html
*/
namespace GrEduLabs\Authentication\Storage;
use Zend\Authentication\Storage\StorageInterface;
class PhpSession implements StorageInterface
{
/**
* Default session namespace
*/
const NAMESPAGE_DEFAULT = 'GrEduLabs_Auth';
/**
* Default session object member name
*/
const MEMBER_DEFAULT = 'storage';
/**
* Session array
*
* @var array
*/
protected $session;
/**
* Session namespace
*
* @var mixed
*/
protected $namespace = self::NAMESPAGE_DEFAULT;
/**
* Session object member
*
* @var mixed
*/
protected $member = self::MEMBER_DEFAULT;
/**
* Sets session storage options and initializes session namespace
*
* @param array $session
* @param mixed $namespace
* @param mixed $member
*/
public function __construct(array &$session, $namespace = null, $member = null)
{
if ($namespace !== null) {
$this->namespace = $namespace;
}
if ($member !== null) {
$this->member = $member;
}
$this->session =& $session;
$this->session[$this->namespace] = [];
}
/**
* Returns the session namespace
*
* @return string
*/
public function getNamespace()
{
return $this->namespace;
}
/**
* Returns the name of the session object member
*
* @return string
*/
public function getMember()
{
return $this->member;
}
/**
* Returns true if and only if storage is empty
*
* @throws \Zend\Authentication\Exception\ExceptionInterface If it is impossible to determine whether storage is empty
* @return bool
*/
public function isEmpty()
{
return !isset($this->session[$this->namespace][$this->member]);
}
/**
* Returns the contents of storage
*
* Behavior is undefined when storage is empty.
*
* @throws \Zend\Authentication\Exception\ExceptionInterface If reading contents from storage is impossible
* @return mixed
*/
public function read()
{
return $this->session[$this->namespace][$this->member];
}
/**
* Writes $contents to storage
*
* @param mixed $contents
* @throws \Zend\Authentication\Exception\ExceptionInterface If writing $contents to storage is impossible
* @return void
*/
public function write($contents)
{
$this->session[$this->namespace][$this->member] = $contents;
}
/**
* Clears contents from storage
*
* @throws \Zend\Authentication\Exception\ExceptionInterface If clearing contents from storage is impossible
* @return void
*/
public function clear()
{
unset($this->session[$this->namespace][$this->member]);
}
}
......@@ -22,7 +22,9 @@
"slim/twig-view": "^2.0",
"slim/flash": "^0.1.0",
"monolog/monolog": "^1.13",
"kanellov/config-merge": "dev-master"
"kanellov/config-merge": "dev-master",
"zendframework/zend-authentication": "^2.0",
"zendframework/zend-eventmanager": "^3.0"
},
"require-dev": {
"fabpot/php-cs-fixer": "1.*",
......@@ -32,11 +34,12 @@
},
"autoload": {
"psr-4": {
"GrEdu\\": "app/src"
"GrEduLabs\\": "app/src"
}
},
"autoload-dev": {
"psr-0": {
"psr-4": {
"GrEduLabsTest\\": "test/src"
}
}
}
......@@ -4,8 +4,8 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically"
],
"hash": "9a345fe353c41b2f1fb399680d646fe0",
"content-hash": "ab7cd1b922f8f7454f39339a431343f4",
"hash": "8f23b8ce2b3934db70f529ce01ff5b1d",
"content-hash": "335c05fc635c7a452b9fa6e390019a37",
"packages": [
{
"name": "container-interop/container-interop",
......@@ -557,6 +557,236 @@
"templating"
],
"time": "2016-01-11 14:02:19"
},
{
"name": "zendframework/zend-authentication",
"version": "2.5.2",
"source": {
"type": "git",
"url": "https://github.com/zendframework/zend-authentication.git",
"reference": "1002b40baa9acb3f875cc77f4a2b4fc238b62170"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/zendframework/zend-authentication/zipball/1002b40baa9acb3f875cc77f4a2b4fc238b62170",
"reference": "1002b40baa9acb3f875cc77f4a2b4fc238b62170",
"shasum": ""
},
"require": {
"php": ">=5.5",
"zendframework/zend-stdlib": "~2.5"
},
"require-dev": {
"fabpot/php-cs-fixer": "1.7.*",
"phpunit/phpunit": "~4.0",
"zendframework/zend-crypt": "~2.5",
"zendframework/zend-db": "~2.5",
"zendframework/zend-http": "~2.5",
"zendframework/zend-ldap": "~2.5",
"zendframework/zend-session": "~2.5",
"zendframework/zend-uri": "~2.5",
"zendframework/zend-validator": "~2.5"
},
"suggest": {
"zendframework/zend-crypt": "Zend\\Crypt component",
"zendframework/zend-db": "Zend\\Db component",
"zendframework/zend-http": "Zend\\Http component",
"zendframework/zend-ldap": "Zend\\Ldap component",
"zendframework/zend-session": "Zend\\Session component",
"zendframework/zend-uri": "Zend\\Uri component",
"zendframework/zend-validator": "Zend\\Validator component"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "2.5-dev",
"dev-develop": "2.6-dev"
}
},
"autoload": {
"psr-4": {
"Zend\\Authentication\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"BSD-3-Clause"
],
"description": "provides an API for authentication and includes concrete authentication adapters for common use case scenarios",
"homepage": "https://github.com/zendframework/zend-authentication",
"keywords": [
"Authentication",
"zf2"
],
"time": "2015-06-15 19:37:28"
},
{
"name": "zendframework/zend-eventmanager",
"version": "3.0.0",
"source": {
"type": "git",
"url": "https://github.com/zendframework/zend-eventmanager.git",
"reference": "8c9917f1595ff260f289439bdeb1f46500c65d62"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/zendframework/zend-eventmanager/zipball/8c9917f1595ff260f289439bdeb1f46500c65d62",
"reference": "8c9917f1595ff260f289439bdeb1f46500c65d62",
"shasum": ""
},
"require": {
"php": "^5.5 || ^7.0"
},
"require-dev": {
"athletic/athletic": "^0.1",
"container-interop/container-interop": "^1.1.0",
"phpunit/phpunit": "~4.0",
"squizlabs/php_codesniffer": "^2.0",
"zendframework/zend-stdlib": "^2.7.3"
},
"suggest": {
"container-interop/container-interop": "^1.1.0, to use the lazy listeners feature",
"zendframework/zend-stdlib": "^2.7.3, to use the FilterChain feature"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "3.0-dev",
"dev-develop": "3.1-dev"
}
},
"autoload": {
"psr-4": {
"Zend\\EventManager\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"BSD-3-Clause"
],
"description": "Trigger and listen to events within a PHP application",
"homepage": "https://github.com/zendframework/zend-eventmanager",
"keywords": [
"event",
"eventmanager",
"events",
"zf2"
],
"time": "2016-01-12 23:27:48"
},
{
"name": "zendframework/zend-hydrator",
"version": "1.0.0",
"source": {
"type": "git",
"url": "https://github.com/zendframework/zend-hydrator.git",
"reference": "f3ed8b833355140350bbed98d8a7b8b66875903f"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/zendframework/zend-hydrator/zipball/f3ed8b833355140350bbed98d8a7b8b66875903f",
"reference": "f3ed8b833355140350bbed98d8a7b8b66875903f",
"shasum": ""
},
"require": {
"php": ">=5.5",
"zendframework/zend-stdlib": "^2.5.1"
},
"require-dev": {
"phpunit/phpunit": "~4.0",
"squizlabs/php_codesniffer": "^2.0@dev",
"zendframework/zend-eventmanager": "^2.5.1",
"zendframework/zend-filter": "^2.5.1",
"zendframework/zend-inputfilter": "^2.5.1",
"zendframework/zend-serializer": "^2.5.1",
"zendframework/zend-servicemanager": "^2.5.1"
},
"suggest": {
"zendframework/zend-eventmanager": "^2.5.1, to support aggregate hydrator usage",
"zendframework/zend-filter": "^2.5.1, to support naming strategy hydrator usage",
"zendframework/zend-serializer": "^2.5.1, to use the SerializableStrategy",
"zendframework/zend-servicemanager": "^2.5.1, to support hydrator plugin manager usage"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.0-dev",
"dev-develop": "1.1-dev"
}
},
"autoload": {
"psr-4": {
"Zend\\Hydrator\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"BSD-3-Clause"
],
"homepage": "https://github.com/zendframework/zend-hydrator",
"keywords": [
"hydrator",
"zf2"
],
"time": "2015-09-17 14:06:43"
},
{
"name": "zendframework/zend-stdlib",
"version": "2.7.4",
"source": {
"type": "git",
"url": "https://github.com/zendframework/zend-stdlib.git",
"reference": "cae029346a33663b998507f94962eb27de060683"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/zendframework/zend-stdlib/zipball/cae029346a33663b998507f94962eb27de060683",
"reference": "cae029346a33663b998507f94962eb27de060683",
"shasum": ""
},
"require": {
"php": ">=5.5",
"zendframework/zend-hydrator": "~1.0"
},
"require-dev": {
"athletic/athletic": "~0.1",
"fabpot/php-cs-fixer": "1.7.*",
"phpunit/phpunit": "~4.0",
"zendframework/zend-config": "~2.5",
"zendframework/zend-eventmanager": "~2.5",
"zendframework/zend-filter": "~2.5",
"zendframework/zend-inputfilter": "~2.5",
"zendframework/zend-serializer": "~2.5",
"zendframework/zend-servicemanager": "~2.5"
},
"suggest": {
"zendframework/zend-eventmanager": "To support aggregate hydrator usage",
"zendframework/zend-filter": "To support naming strategy hydrator usage",
"zendframework/zend-serializer": "Zend\\Serializer component",
"zendframework/zend-servicemanager": "To support hydrator plugin manager usage"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "2.7-dev",
"dev-develop": "2.8-dev"
}
},
"autoload": {
"psr-4": {
"Zend\\Stdlib\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"BSD-3-Clause"
],
"homepage": "https://github.com/zendframework/zend-stdlib",
"keywords": [
"stdlib",
"zf2"
],
"time": "2015-10-15 15:57:32"
}
],
"packages-dev": [
......
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="./vendor/autoload.php"
colors="true"
verbose="true">
<testsuites>
<testsuite name="GrEduLabs Test Suite">
<directory suffix="Test.php">./test/</directory>
</testsuite>
</testsuites>
<groups>
<exclude>
<group>disable</group>
</exclude>
</groups>
<filter>
<whitelist addUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./app/</directory>
<exclude>
<directory>./app/templates</directory>
</exclude>
</whitelist>
</filter>
<logging>
<log type="coverage-text" target="php://stdout" showUncoveredFiles="true"/>
</logging>
<php>
<ini name="date.timezone" value="UTC"/>
</php>
</phpunit>
\ No newline at end of file
<?php
/**
* gredu_labs
*
* @link https://github.com/eellak/gredu_labs for the canonical source repository
* @copyright Copyright (c) 2008-2015 Greek Free/Open Source Software Society (https://gfoss.ellak.gr/)
* @license GNU GPLv3 http://www.gnu.org/licenses/gpl-3.0-standalone.html
*/
chdir(dirname(__DIR__));
if (PHP_SAPI === 'cli-server' && $_SERVER['SCRIPT_FILENAME'] !== __FILE__) {
......
<?php
/**
* gredu_labs
*
* @link https://github.com/eellak/gredu_labs for the canonical source repository
* @copyright Copyright (c) 2008-2015 Greek Free/Open Source Software Society (https://gfoss.ellak.gr/)
* @license GNU GPLv3 http://www.gnu.org/licenses/gpl-3.0-standalone.html
*/
class DependenciesTest extends \PHPUnit_Framework_TestCase
{
private static $container;
private static $settings = [
'settings' => [
'view' => [
'template_path' => __DIR__,
'twig' => [
'cache' => __DIR__,
'debug' => true,
'auto_reload' => true,
],
],
'logger' => [
'name' => 'app',
'path' => __DIR__,
],
],
];
public static function setUpBeforeClass()
{
@session_start();
$app = new \Slim\App(self::$settings);
require __DIR__ . '/../app/dependencies.php';
self::$container = $container;
@session_destroy();
}
public function testViewInContainer()
{
$view = self::$container->get('view');
$this->assertInstanceOf('\Slim\Views\Twig', $view);
}
public function testFlashInContainer()
{
$flash = self::$container->get('flash');
$this->assertInstanceOf('\Slim\Flash\Messages', $flash);
}
public function testLoggerInContainer()
{
$logger = self::$container->get('logger');
$this->assertInstanceOf('\Monolog\Logger', $logger);
}
public function testEventsInContainer()
{
$events = self::$container->get('events');
$this->assertInstanceOf('\Zend\EventManager\EventManagerInterface', $events);
}
}
<?php
/**
* gredu_labs
*
* @link https://github.com/eellak/gredu_labs for the canonical source repository
* @copyright Copyright (c) 2008-2015 Greek Free/Open Source Software Society (https://gfoss.ellak.gr/)
* @license GNU GPLv3 http://www.gnu.org/licenses/gpl-3.0-standalone.html
*/
namespace GrEduLabstest\Authentication\Storage;
use GrEduLabs\Authentication\Storage\PhpSession;
class PhpSessionTest extends \PHPUnit_Framework_TestCase
{
private $storage;
private $session;
protected function setUp()
{
$this->session = [];
$this->storage = new PhpSession($this->session);
}
public function testPassSessionArrayByRefernce()
{
$this->assertTrue(is_array($this->session[PhpSession::NAMESPAGE_DEFAULT]));
}
public function testSetingNamespaceAndMember()
{
$session = [];
$storage = new PhpSession($session, 'TEST_NS', 'TEST_MEMBER');
$this->assertAttributeSame('TEST_NS', 'namespace', $storage);
$this->assertAttributeSame('TEST_MEMBER', 'member', $storage);
}
public function testGetNamesapceMethod()
{
$session = [];
$storage = new PhpSession($session, 'TEST_NS');
$this->assertSame('TEST_NS', $storage->getNamespace());
}
public function testGetMemberMethod()
{
$session = [];
$storage = new PhpSession($session, 'TEST_NS', 'TEST_MEMBER');
$this->assertSame('TEST_MEMBER', $storage->getMember());
}
public function testIsEmptyMethodWhenSessionIsEmpty()
{
$this->assertTrue($this->storage->isEmpty());
}
public function testIsEmptyMethodWhenSessionNotEmpty()
{
$this->session[PhpSession::NAMESPAGE_DEFAULT][PhpSession::MEMBER_DEFAULT] = 'test';
$this->assertFalse($this->storage->isEmpty());
}
public function testReadMethodReturnCorrectResult()
{
$this->session[PhpSession::NAMESPAGE_DEFAULT][PhpSession::MEMBER_DEFAULT] = 'test';
$this->assertSame('test', $this->storage->read());
}
public function testWriteMethodSetsContents()
{
$this->storage->write('test');
$this->assertSame('test', $this->session[PhpSession::NAMESPAGE_DEFAULT][PhpSession::MEMBER_DEFAULT]);
}
public function testClearMethodUnsetSession()
{
$this->storage->write('test');
$this->storage->clear();
$this->assertFalse(array_key_exists(PhpSession::MEMBER_DEFAULT, $this->session[PhpSession::NAMESPAGE_DEFAULT]));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment