Skip to content

Commit a022ed2

Browse files
committed
Updated StrongAuth extra to use new Strong Lib
1 parent 2b99757 commit a022ed2

File tree

2 files changed

+29
-40
lines changed

2 files changed

+29
-40
lines changed

Middleware/README.markdown

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ This is used to protect your website from CSRF attacks.
99
use \Slim\Slim;
1010
use \Slim\Extras\Middleware\CsrfGuard;
1111

12-
$app = new Slim();
13-
$app->add(new CsrfGuard());
12+
$app = new Slim();
13+
$app->add(new CsrfGuard());
1414

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

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

1919
## HttpBasic
2020

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

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

3131

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

4444
$app = new Slim();
45-
$config = array(
46-
'provider' => 'PDO',
47-
'dsn' => 'mysql:host=localhost;dbname=slimdev',
48-
'dbuser' => 'serverside',
49-
'dbpass' => 'password',
50-
'auth.type' => 'form',
51-
'login.url' => '/',
52-
'security.urls' => array(
53-
array('path' => '/test'),
54-
array('path' => '/about/.+'),
55-
),
56-
);
57-
58-
$app->add(new StrongAuth($config));
45+
$config = array(
46+
'provider' => 'PDO',
47+
'pdo' => new PDO('mysql:host=localhost;dbname=database_name', 'username', 'password'),
48+
'auth.type' => 'form',
49+
'login.url' => '/',
50+
'security.urls' => array(
51+
array('path' => '/test'),
52+
array('path' => '/about/.+'),
53+
),
54+
);
55+
56+
$app->add(new StrongAuth($config));

Middleware/StrongAuth.php

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* USAGE
1313
*
1414
* $app = new \Slim\Slim();
15-
* $app->add(new \Slim\Extras\Middleware\StrongAuth(array('provider' => 'PDO', 'dsn' => 'sqlite:memory')));
15+
* $app->add(new \Slim\Extras\Middleware\StrongAuth(array('provider' => 'PDO', 'pdo' => new PDO('sqlite:memory'))));
1616
*
1717
* MIT LICENSE
1818
*
@@ -39,35 +39,26 @@
3939

4040
class StrongAuth extends \Slim\Middleware
4141
{
42-
/**
43-
* @var string
44-
*/
45-
protected $username;
46-
47-
/**
48-
* @var string
49-
*/
50-
protected $password;
51-
5242
/**
5343
* @var array
5444
*/
5545
protected $settings = array(
5646
'login.url' => '/',
5747
'auth.type' => 'http',
48+
'realm' => 'Protected Area',
5849
);
5950

6051
/**
6152
* Constructor
6253
*
6354
* @param array $config Configuration for Strong and Login Details
64-
* @param \Strong $strong
55+
* @param \Strong\Strong $strong
6556
* @return void
6657
*/
67-
public function __construct(array $config = array(), \Strong $strong = null)
58+
public function __construct(array $config = array(), \Strong\Strong $strong = null)
6859
{
6960
$this->config = array_merge($this->settings, $config);
70-
$this->auth = (!empty($strong)) ? $strong : \Strong::factory($this->config);
61+
$this->auth = (!empty($strong)) ? $strong : \Strong\Strong::factory($this->config);
7162
}
7263

7364
/**
@@ -82,26 +73,26 @@ public function call()
8273
// Authentication Initialised
8374
switch ($this->config['auth.type']) {
8475
case 'form':
85-
$this->formauth($this->auth, $req);
76+
$this->formAuth($this->auth, $req);
8677
break;
8778
default:
88-
$this->httpauth($this->auth, $req);
79+
$this->httpAuth($this->auth, $req);
8980
break;
9081
}
9182
}
9283

9384
/**
9485
* Form based authentication
9586
*
96-
* @param \Strong $auth
87+
* @param \Strong\Strong $auth
9788
* @param object $req
9889
*/
99-
private function formauth(\Strong $auth, $req)
90+
private function formAuth($auth, $req)
10091
{
10192
$app = $this->app;
10293
$config = $this->config;
10394
$this->app->hook('slim.before.router', function () use ($app, $auth, $req, $config) {
104-
$secured_urls = isset($config['security.urls']) ? $config['security.urls'] : array();
95+
$secured_urls = isset($config['security.urls']) && is_array($config['security.urls']) ? $config['security.urls'] : array();
10596
foreach ($secured_urls as $surl) {
10697
$patternAsRegex = $surl['path'];
10798
if (substr($surl['path'], -1) === '/') {
@@ -129,10 +120,10 @@ private function formauth(\Strong $auth, $req)
129120
* the request has already authenticated, the next middleware is called. Otherwise,
130121
* a 401 Authentication Required response is returned to the client.
131122
*
132-
* @param \Strong $auth
123+
* @param \Strong\Strong $auth
133124
* @param object $req
134125
*/
135-
private function httpauth(\Strong $auth, $req)
126+
private function httpAuth($auth, $req)
136127
{
137128
$res = $this->app->response();
138129
$authUser = $req->headers('PHP_AUTH_USER');

0 commit comments

Comments
 (0)