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 *
3942
4043class 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