Skip to content
Browse files

Updated StrongAuth extra to use new Strong Lib

  • Loading branch information...
1 parent 2b99757 commit a022ed23dae94e164000acd891e3394d903f9623 @silentworks silentworks committed Jan 7, 2013
Showing with 29 additions and 40 deletions.
  1. +17 −19 Middleware/README.markdown
  2. +12 −21 Middleware/StrongAuth.php
View
36 Middleware/README.markdown
@@ -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
@@ -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
@@ -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));
View
33 Middleware/StrongAuth.php
@@ -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
*
@@ -40,34 +40,25 @@
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);
}
/**
@@ -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) === '/') {
@@ -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');

0 comments on commit a022ed2

Please sign in to comment.
Something went wrong with that request. Please try again.