Skip to content

Commit

Permalink
authentication in progress; sso with cas and some views implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
kanellov committed Jan 15, 2016
1 parent 07eb4fb commit 262ecc2
Show file tree
Hide file tree
Showing 22 changed files with 581 additions and 44 deletions.
67 changes: 61 additions & 6 deletions app/dependencies.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,77 @@
);
};

// Database

$container['db'] = function ($c) {
$settings = $c->get('settings');
try {
$pdo = new \PDO(
$settings['db']['dsn'],
$settings['db']['user'],
$settings['db']['pass'],
$settings['db']['options']
);

return $pdo;
} catch (\PDOException $e) {
$c->get('logger')->error($e->getMessage());

return;
}
};

// Authentication service

$container['Service\\Authentication\\Adapter'] = function ($c) {
return new \GrEduLabs\Authentication\Adapter\Events($c->get('events'));
$container['Service\\Authentication\\DbAdapter'] = function ($c) {
return new \GrEduLabs\Authentication\Adapter\Pdo($c->get('db'));
};

$container['Service\\Authentication\\CasAdapter'] = function ($c) {
$settings = $c->get('settings');

return new GrEduLabs\Authentication\Adapter\Cas($settings['phpcas']);
};


$container['Service\\Authentication\\Storage'] = function ($c) {
return new \GrEduLabs\Authentication\Storage\PhpSession($_SESSION);
};

$container['Service\\Authentication'] = function ($c) {
return new \Zend\Authentication\AuthenticationService(
$c->get('Service\\Authentication\\Storage')
);
};

// Actions

$service = new \Zend\Authentication\AuthenticationService(
$c->get('Service\\Authentication\\Storage'),
$c->get('Service\\Authentication\\Adapter')
$container['GrEduLabs\\Action\\User\\Login'] = function ($c) {
return new GrEduLabs\Action\User\Login(
$c->get('view'),
function ($identity, $credential) use ($c) {
$service = $c->get('Service\\Authentication');
$adapter = $c->get('Service\\Authentication\\DbAdapter');
$adapter->setIdentity($identity)
->setCredential($credential);

return $service->authenticate($adapter);
}
);
};

return $service;
$container['GrEduLabs\\Action\\User\\LoginSso'] = function ($c) {
return new GrEduLabs\Action\User\LoginSso(function () use ($c) {
$service = $c->get('Service\\Authentication');
$adapter = $c->get('Service\\Authentication\\CasAdapter');

return $service->authenticate($adapter);
});
};

$container['GrEduLabs\\Action\\User\\Logout'] = function ($c) {
return new GrEduLabs\Action\User\Logout(
$c->get('Service\\Authentication'),
$c->get('router')->pathFor('index')
);
};
9 changes: 9 additions & 0 deletions app/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,13 @@
$view->render($response, 'home.twig');

return $response;
})->setName('index');

// authentication

$app->group('/user', function () {
$this->map(['GET', 'POST'], '/login', 'GrEduLabs\\Action\\User\\Login')->setName('user.login');
$this->get('/login-sso', 'GrEduLabs\\Action\\User\\LoginSso')->setName('user.loginSso');
$this->get('/logout', 'GrEduLabs\\Action\\User\\Logout')->setName('user.logout');
$this->get('/profile', 'GrEduLabs\\Action\\User\\Profile')->setName('user.profile');
});
53 changes: 53 additions & 0 deletions app/src/Action/User/Login.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?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\Action\User;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Slim\Views\Twig;

class Login
{
/**
* @var Twig
*/
protected $view;

/**
* @var callable
*/
protected $authenticate;

/**
* Constructor
* @param Twig $view
*/
public function __construct(
Twig $view,
callable $authenticate
) {
$this->view = $view;
$this->authenticate = $authenticate;
}

public function __invoke(ServerRequestInterface $req, ResponseInterface $res, array $args = [])
{
if ($req->isPost()) {
$authenticate = $this->authenticate;
$result = $authenticate(
$req->getParam('email'),
$req->getParam('password')
);
var_dump($result);
}

return $this->view->render($res, 'user/login.twig');
}
}
42 changes: 42 additions & 0 deletions app/src/Action/User/LoginSso.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?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\Action\User;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Slim\Views\Twig;

class LoginSso
{

/**
* @var callable
*/
protected $authenticate;

/**
* Constructor
* @param Twig $view
*/
public function __construct(callable $authenticate)
{
$this->authenticate = $authenticate;
}

public function __invoke(ServerRequestInterface $req, ResponseInterface $res, array $args = [])
{
$authenticate = $this->authenticate;
$result = $authenticate();

var_dump($result);

return $res;
}
}
45 changes: 45 additions & 0 deletions app/src/Action/User/Logout.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?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\Action\User;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Zend\Authentication\AuthenticationServiceInterface;

class Logout
{
/**
* @var AuthenticationServiceInterface
*/
protected $authService;

/**
* @var string
*/
protected $redirectUrl;

public function __construct(
AuthenticationServiceInterface $authService,
$redirectUrl
) {
$this->authService = $authService;
$this->router = $router;
$this->redirectUrl = $redirectUrl;
}

public function __invoke(ServerRequestInterface $req, ResponseInterface $res, array $args = [])
{
if ($this->authService->hasIdentity()) {
$this->authService->clearIdentity();
}

return $res->withRedirect($this->redirectUrl);
}
}
Empty file added app/src/Action/User/Profile.php
Empty file.
86 changes: 86 additions & 0 deletions app/src/Authentication/Adapter/Cas.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?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\Adapter;

use Exception;
use GrEduLabs\Authentication\Identity;
use phpCAS;
use Zend\Authentication\Adapter\AdapterInterface;
use Zend\Authentication\Result;

class Cas implements AdapterInterface
{
public function __construct(array $settings = [])
{
phpCAS::client(
$settings['serverVersion'],
$settings['serverHostname'],
$settings['serverPort'],
$settings['serverUri'],
$settings['changeSessionId']
);

if (($casServerCaCert = $settings['casServerCaCert'])) {
if ($settings['casServerCnValidate']) {
phpCAS::setCasServerCACert($casServerCaCert, true);
} else {
phpCAS::setCasServerCACert($casServerCaCert, false);
}
}

if ($settings['noCasServerValidation']) {
phpCAS::setNoCasServerValidation();
}
}

public function authenticate()
{
try {
phpCAS::handleLogoutRequests();
phpCAS::forceAuthentication();
if (!phpCAS::isAuthenticated()) {
return new Result(Result::FAILURE, null, ['Authentication failure']);
}

return new Result(
Result::SUCCESS,
self::identityFormCasAttributes(),
['Authentication success']
);
} catch (Exception $e) {
return new Result(Result::FAILURE_UNCATEGORIZED, null, [$e->getMessage()]);
}
}

private static function identityFormCasAttributes()
{
$attributes = phpCAS::getAttributes();
$identity = phpCAS::getUser();

$filterAttribute = function ($attribute) use ($attributes) {
if (!isset($attributes[$attribute])) {
return;
}

if (is_array($attributes[$attribute])) {
return $attributes[$attribute];
}

return $attributes[$attribute];
};

return new Identity(
$identity,
$filterAttribute('mail'),
$filterAttribute('cn'),
$filterAttribute('ou')
);
}
}
39 changes: 39 additions & 0 deletions app/src/Authentication/Adapter/Pdo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?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\Adapter;

use PDO as PDOConnection;
use Zend\Authentication\Adapter\AbstractAdapter;
use Zend\Authentication\Result;

class Pdo extends AbstractAdapter
{

/**
* @var EventManagerInterface
*/
protected $events;

/**
* Construct adapter
*
* @param PDOConnection $db
*/
public function __construct(PDOConnection $db)
{
$this->db = $db;
}


public function authenticate()
{
return new Result(Result::FAILURE, null, ['Authentication failure']);
}
}
Loading

0 comments on commit 262ecc2

Please sign in to comment.