Skip to content

Commit 74b8b7f

Browse files
author
Josh Lockhart
committed
Add HTTP basic auth middleware
1 parent 52312ce commit 74b8b7f

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

Middleware/HttpBasicAuth.php

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
/**
3+
* HTTP Basic Authentication
4+
*
5+
* Use this middleware with your Slim Framework application
6+
* to require HTTP basic auth for all routes.
7+
*
8+
* @author Josh Lockhart <[email protected]>
9+
* @version 1.0
10+
* @copyright 2012 Josh Lockhart
11+
*
12+
* USAGE
13+
*
14+
* $app = new Slim();
15+
* $app->add(new HttpBasicAuth('theUsername', 'thePassword'));
16+
*
17+
* MIT LICENSE
18+
*
19+
* Permission is hereby granted, free of charge, to any person obtaining
20+
* a copy of this software and associated documentation files (the
21+
* "Software"), to deal in the Software without restriction, including
22+
* without limitation the rights to use, copy, modify, merge, publish,
23+
* distribute, sublicense, and/or sell copies of the Software, and to
24+
* permit persons to whom the Software is furnished to do so, subject to
25+
* the following conditions:
26+
*
27+
* The above copyright notice and this permission notice shall be
28+
* included in all copies or substantial portions of the Software.
29+
*
30+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
31+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
32+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
33+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
34+
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
35+
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
36+
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
37+
*/
38+
class HttpBasicAuth extends Slim_Middleware {
39+
/**
40+
* @var string
41+
*/
42+
protected $realm;
43+
44+
/**
45+
* @var string
46+
*/
47+
protected $username;
48+
49+
/**
50+
* @var string
51+
*/
52+
protected $password;
53+
54+
/**
55+
* Constructor
56+
*
57+
* @param string $username The HTTP Authentication username
58+
* @param string $password The HTTP Authentication password
59+
* @param string $realm The HTTP Authentication realm
60+
* @return void
61+
*/
62+
public function __construct( $username, $password, $realm = 'Protected Area' ) {
63+
$this->username = $username;
64+
$this->password = $password;
65+
$this->realm = $realm;
66+
}
67+
68+
/**
69+
* Call
70+
*
71+
* This method will check the HTTP request headers for previous authentication. If
72+
* the request has already authenticated, the next middleware is called. Otherwise,
73+
* a 401 Authentication Required response is returned to the client.
74+
*
75+
* @return void
76+
*/
77+
public function call() {
78+
$req = $this->app->request();
79+
$res = $this->app->response();
80+
$authUser = $req->headers('PHP_AUTH_USER');
81+
$authPass = $req->headers('PHP_AUTH_PW');
82+
if ( $authUser && $authPass && $authUser === $this->username && $authPass === $this->password ) {
83+
$this->next->call();
84+
} else {
85+
$res->status(401);
86+
$res->header('WWW-Authenticate', sprintf('Basic realm="%s"', $this->realm));
87+
}
88+
}
89+
}

0 commit comments

Comments
 (0)