Skip to content

Commit

Permalink
Updated StrongAuth extra to use new Strong Lib
Browse files Browse the repository at this point in the history
  • Loading branch information
silentworks committed Jan 7, 2013
1 parent 2b99757 commit a022ed2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 40 deletions.
36 changes: 17 additions & 19 deletions Middleware/README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ This is used to protect your website from CSRF attacks.
use \Slim\Slim;
use \Slim\Extras\Middleware\CsrfGuard;

$app = new Slim();
$app->add(new CsrfGuard());
$app = new Slim();
$app->add(new CsrfGuard());

In your view template add this to any web forms you have created.

<input type="hidden" name="<?php echo $csrf_key; ?>" value="<?php echo $csrf_token; ?>">
<input type="hidden" name="<?php echo $csrf_key; ?>" value="<?php echo $csrf_token; ?>">

## HttpBasic

Expand All @@ -25,8 +25,8 @@ This will provide you with basic user Authentication based on username and passw
use \Slim\Slim;
use \Slim\Extras\Middleware\HttpBasicAuth;

$app = new Slim();
$app->add(new HttpBasicAuth('theUsername', 'thePassword'));
$app = new Slim();
$app->add(new HttpBasicAuth('theUsername', 'thePassword'));


## Strong
Expand All @@ -42,17 +42,15 @@ Here is some sample code for using PDO provider and securing some routes using r
use \Slim\Extras\Middleware\StrongAuth;

$app = new Slim();
$config = array(
'provider' => 'PDO',
'dsn' => 'mysql:host=localhost;dbname=slimdev',
'dbuser' => 'serverside',
'dbpass' => 'password',
'auth.type' => 'form',
'login.url' => '/',
'security.urls' => array(
array('path' => '/test'),
array('path' => '/about/.+'),
),
);

$app->add(new StrongAuth($config));
$config = array(
'provider' => 'PDO',
'pdo' => new PDO('mysql:host=localhost;dbname=database_name', 'username', 'password'),
'auth.type' => 'form',
'login.url' => '/',
'security.urls' => array(
array('path' => '/test'),
array('path' => '/about/.+'),
),
);

$app->add(new StrongAuth($config));
33 changes: 12 additions & 21 deletions Middleware/StrongAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* USAGE
*
* $app = new \Slim\Slim();
* $app->add(new \Slim\Extras\Middleware\StrongAuth(array('provider' => 'PDO', 'dsn' => 'sqlite:memory')));
* $app->add(new \Slim\Extras\Middleware\StrongAuth(array('provider' => 'PDO', 'pdo' => new PDO('sqlite:memory'))));
*
* MIT LICENSE
*
Expand All @@ -39,35 +39,26 @@

class StrongAuth extends \Slim\Middleware
{
/**
* @var string
*/
protected $username;

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

/**
* @var array
*/
protected $settings = array(
'login.url' => '/',
'auth.type' => 'http',
'realm' => 'Protected Area',
);

/**
* Constructor
*
* @param array $config Configuration for Strong and Login Details
* @param \Strong $strong
* @param \Strong\Strong $strong
* @return void
*/
public function __construct(array $config = array(), \Strong $strong = null)
public function __construct(array $config = array(), \Strong\Strong $strong = null)
{
$this->config = array_merge($this->settings, $config);
$this->auth = (!empty($strong)) ? $strong : \Strong::factory($this->config);
$this->auth = (!empty($strong)) ? $strong : \Strong\Strong::factory($this->config);
}

/**
Expand All @@ -82,26 +73,26 @@ public function call()
// Authentication Initialised
switch ($this->config['auth.type']) {
case 'form':
$this->formauth($this->auth, $req);
$this->formAuth($this->auth, $req);
break;
default:
$this->httpauth($this->auth, $req);
$this->httpAuth($this->auth, $req);
break;
}
}

/**
* Form based authentication
*
* @param \Strong $auth
* @param \Strong\Strong $auth
* @param object $req
*/
private function formauth(\Strong $auth, $req)
private function formAuth($auth, $req)
{
$app = $this->app;
$config = $this->config;
$this->app->hook('slim.before.router', function () use ($app, $auth, $req, $config) {
$secured_urls = isset($config['security.urls']) ? $config['security.urls'] : array();
$secured_urls = isset($config['security.urls']) && is_array($config['security.urls']) ? $config['security.urls'] : array();
foreach ($secured_urls as $surl) {
$patternAsRegex = $surl['path'];
if (substr($surl['path'], -1) === '/') {
Expand Down Expand Up @@ -129,10 +120,10 @@ private function formauth(\Strong $auth, $req)
* the request has already authenticated, the next middleware is called. Otherwise,
* a 401 Authentication Required response is returned to the client.
*
* @param \Strong $auth
* @param \Strong\Strong $auth
* @param object $req
*/
private function httpauth(\Strong $auth, $req)
private function httpAuth($auth, $req)
{
$res = $this->app->response();
$authUser = $req->headers('PHP_AUTH_USER');
Expand Down

0 comments on commit a022ed2

Please sign in to comment.