Skip to content

Commit cf315ca

Browse files
AdminAdmin
authored andcommitted
Merge branch 'develop'
2 parents e80be97 + 96717b7 commit cf315ca

File tree

4 files changed

+126
-21
lines changed

4 files changed

+126
-21
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
}

Views/README.markdown

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,37 @@ library. You can use the Twig custom view in your Slim Framework application lik
1717
If you are not using Composer to autoload project dependencies, you must also set the Twig view's public static
1818
`$twigDirectory` property; this is the relative or absolute path to the directory that conatins the Twig library.
1919

20-
You may also set the public static `$twigOptions` property; this is an array of settings that customize the Twig
21-
library behavior.
20+
### Twig configuration
21+
22+
There are several public static properties you can use to customize the Twig library behavior.
23+
24+
####$twigOptions
25+
26+
An array of options to pass to the underlying Twig environment ([Twig docs](http://twig.sensiolabs.org/doc/api.html#environment-options)):
27+
28+
\Slim\Extras\Views\Twig::$twigOptions = array(
29+
'debug' => true
30+
);
31+
32+
33+
####$twigExtensions
34+
35+
An array contianing Twig extensions to load ([Twig docs](http://twig.sensiolabs.org/doc/advanced.html)):
36+
37+
\Slim\Extras\Views\Twig::$twigExtensions = array(
38+
new MyCustomExtension(),
39+
new ThirdPartyExtension()
40+
);
41+
42+
43+
####$twigTemplateDirs
44+
45+
An array of paths to directories containing Twig templates ([Twig docs](http://twig.sensiolabs.org/doc/api.html#twig-loader-filesystem)):
46+
47+
\Slim\Extras\Views\Twig::$twigTemplateDirs = array(
48+
realpath(PROJECT_DIR . '/templates'),
49+
realpath(PROJECT_DIR . '/some/other/templates')
50+
);
2251

2352
## Mustache
2453

Views/Twig.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ class Twig extends \Slim\View
4646
*/
4747
public static $twigDirectory = null;
4848

49+
/**
50+
* @var array Paths to directories to attempt to load Twig template from
51+
*/
52+
public static $twigTemplateDirs = array();
53+
4954
/**
5055
* @var array The options for the Twig environment, see
5156
* http://www.twig-project.org/book/03-Twig-for-Developers
@@ -62,6 +67,22 @@ class Twig extends \Slim\View
6267
*/
6368
private $twigEnvironment = null;
6469

70+
/**
71+
* Get a list of template directories
72+
*
73+
* Returns an array of templates defined by self::$twigTemplateDirs, falls
74+
* back to Slim\View's built-in getTemplatesDirectory method.
75+
*
76+
* @return array
77+
**/
78+
private function getTemplateDirs()
79+
{
80+
if (empty(self::$twigTemplateDirs)) {
81+
return array($this->getTemplatesDirectory());
82+
}
83+
return self::$twigTemplateDirs;
84+
}
85+
6586
/**
6687
* Render Twig Template
6788
*
@@ -92,7 +113,7 @@ public function getEnvironment()
92113
}
93114

94115
\Twig_Autoloader::register();
95-
$loader = new \Twig_Loader_Filesystem($this->getTemplatesDirectory());
116+
$loader = new \Twig_Loader_Filesystem($this->getTemplateDirs());
96117
$this->twigEnvironment = new \Twig_Environment(
97118
$loader,
98119
self::$twigOptions

0 commit comments

Comments
 (0)