Skip to content

Commit 75d1d44

Browse files
committed
Updated StrongAuth to now take Strong upon initialization if Strong already present.
1 parent 33d930c commit 75d1d44

File tree

2 files changed

+73
-18
lines changed

2 files changed

+73
-18
lines changed

Middleware/README.markdown

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Slim Authentication and XSS Middlewares
2+
3+
## CsrfGuard
4+
5+
This is used to protect your website from CSRF attacks.
6+
7+
### How to use
8+
9+
use \Slim\Slim;
10+
use \Slim\Extras\Middleware\CsrfGuard;
11+
12+
$app = new Slim();
13+
$app->add(new CsrfGuard());
14+
15+
In your view template add this to any web forms you have created.
16+
17+
<input type="hidden" name="<?php echo $csrf_key; ?>" value="<?php echo $csrf_token; ?>">
18+
19+
## HttpBasic
20+
21+
This will provide you with basic user Authentication based on username and password set.
22+
23+
### How to use
24+
25+
use \Slim\Slim;
26+
use \Slim\Extras\Middleware\HttpBasicAuth;
27+
28+
$app = new Slim();
29+
$app->add(new HttpBasicAuth('theUsername', 'thePassword'));
30+
31+
32+
## Strong
33+
34+
### How to use
35+
36+
You will need to pass Strong a config with all your secured routes and any information that is needed
37+
for your Provider.
38+
39+
Here is some sample code for using PDO provider and securing some routes using regex.
40+
41+
use \Slim\Slim;
42+
use \Slim\Extras\Middleware\StrongAuth;
43+
44+
$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));

Middleware/StrongAuth.php

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
*
1212
* USAGE
1313
*
14+
* use Slim\Slim;
15+
* use Slim\Extras\Middleware\StrongAuth;
16+
*
1417
* $app = new Slim();
1518
* $app->add(new StrongAuth(array('provider' => 'PDO', 'dsn' => 'sqlite:memory')));
1619
*
@@ -39,11 +42,6 @@
3942

4043
class StrongAuth extends \Slim\Middleware
4144
{
42-
/**
43-
* @var string
44-
*/
45-
protected $realm;
46-
4745
/**
4846
* @var string
4947
*/
@@ -59,18 +57,20 @@ class StrongAuth extends \Slim\Middleware
5957
*/
6058
protected $settings = array(
6159
'login.url' => '/',
62-
'auth_type' => 'http',
60+
'auth.type' => 'http',
6361
);
6462

6563
/**
6664
* Constructor
6765
*
6866
* @param array $config Configuration for Strong and Login Details
67+
* @param \Strong $strong
6968
* @return void
7069
*/
71-
public function __construct(array $config = array())
70+
public function __construct(array $config = array(), \Strong $strong = null)
7271
{
7372
$this->config = array_merge($this->settings, $config);
73+
$this->auth = (!empty($strong)) ? $strong : \Strong::factory($this->config);
7474
}
7575

7676
/**
@@ -80,29 +80,26 @@ public function __construct(array $config = array())
8080
*/
8181
public function call()
8282
{
83-
$app = $this->app;
84-
$config = $this->config;
8583
$req = $this->app->request();
8684

8785
// Authentication Initialised
88-
$auth = Strong::factory($this->config);
89-
switch ($this->config['auth_type']) {
86+
switch ($this->config['auth.type']) {
9087
case 'form':
91-
$this->formauth($auth, $req);
88+
$this->formauth($this->auth, $req);
9289
break;
9390
default:
94-
$this->httpauth($auth, $req);
91+
$this->httpauth($this->auth, $req);
9592
break;
9693
}
9794
}
9895

9996
/**
10097
* Form based authentication
10198
*
102-
* @param Strong $auth
99+
* @param \Strong $auth
103100
* @param object $req
104101
*/
105-
private function formauth(Strong $auth, $req)
102+
private function formauth(\Strong $auth, $req)
106103
{
107104
$app = $this->app;
108105
$config = $this->config;
@@ -135,10 +132,10 @@ private function formauth(Strong $auth, $req)
135132
* the request has already authenticated, the next middleware is called. Otherwise,
136133
* a 401 Authentication Required response is returned to the client.
137134
*
138-
* @param Strong $auth
135+
* @param \Strong $auth
139136
* @param object $req
140137
*/
141-
private function httpauth(Strong $auth, $req)
138+
private function httpauth(\Strong $auth, $req)
142139
{
143140
$res = $this->app->response();
144141
$authUser = $req->headers('PHP_AUTH_USER');
@@ -148,7 +145,7 @@ private function httpauth(Strong $auth, $req)
148145
$this->next->call();
149146
} else {
150147
$res->status(401);
151-
$res->header('WWW-Authenticate', sprintf('Basic realm="%s"', $this->realm));
148+
$res->header('WWW-Authenticate', sprintf('Basic realm="%s"', $this->config['realm']));
152149
}
153150
}
154151
}

0 commit comments

Comments
 (0)