This repository was archived by the owner on Nov 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathStrongAuth.php
More file actions
139 lines (130 loc) · 4.62 KB
/
StrongAuth.php
File metadata and controls
139 lines (130 loc) · 4.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
/**
* Strong Authentication
*
* Use this middleware with your Slim Framework application
* to require HTTP basic auth for all routes.
*
* @author Andrew Smith <a.smith@silentworks.co.uk>
* @version 1.0
* @copyright 2012 Andrew Smith
*
* USAGE
*
* $app = new \Slim\Slim();
* $app->add(new \Slim\Extras\Middleware\StrongAuth(array('provider' => 'PDO', 'pdo' => new PDO('sqlite:memory'))));
*
* MIT LICENSE
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
namespace Slim\Middleware;
class StrongAuth extends \Slim\Middleware
{
/**
* @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 $strong
* @return void
*/
public function __construct(array $config = array(), \Strong\Strong $strong = null)
{
$this->config = array_merge($this->settings, $config);
$this->auth = (!empty($strong)) ? $strong : \Strong\Strong::factory($this->config);
}
/**
* Call
*
* @return void
*/
public function call()
{
$req = $this->app->request();
// Authentication Initialised
switch ($this->config['auth.type']) {
case 'form':
$this->formAuth($this->auth, $req);
break;
default:
$this->httpAuth($this->auth, $req);
break;
}
}
/**
* Form based authentication
*
* @param \Strong\Strong $auth
* @param object $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']) && is_array($config['security.urls']) ? $config['security.urls'] : array();
foreach ($secured_urls as $surl) {
$patternAsRegex = $surl['path'];
if (substr($surl['path'], -1) === '/') {
$patternAsRegex = $patternAsRegex . '?';
}
$patternAsRegex = '@^' . $patternAsRegex . '$@';
if (preg_match($patternAsRegex, $req->getPathInfo())) {
if (!$auth->loggedIn()) {
if ($req->getPath() !== $config['login.url']) {
$app->redirect($config['login.url']);
}
}
}
}
});
$this->next->call();
}
/**
* HTTPAuth based authentication
*
* This method will check the HTTP request headers for previous authentication. If
* the request has already authenticated, the next middleware is called. Otherwise,
* a 401 Authentication Required response is returned to the client.
*
* @param \Strong\Strong $auth
* @param object $req
*/
private function httpAuth($auth, $req)
{
$res = $this->app->response();
$authUser = $req->headers('PHP_AUTH_USER');
$authPass = $req->headers('PHP_AUTH_PW');
if ($authUser && $authPass && $auth->login($authUser, $authPass)) {
$this->next->call();
} else {
$res->status(401);
$res->header('WWW-Authenticate', sprintf('Basic realm="%s"', $this->config['realm']));
}
}
}