This repository has been archived by the owner on Sep 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HTTP Basic Authentication Middleware for Slim Framework
- Loading branch information
Showing
1 changed file
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
namespace Slim\Middleware; | ||
|
||
/** | ||
* HTTP Basic Authentication | ||
* | ||
* Use this middleware with your Slim Framework application | ||
* to require HTTP basic auth for all routes. | ||
* | ||
* @package Slim | ||
* @author Julio Napuri <[email protected]> | ||
*/ | ||
class HttpBasicAuth extends \Slim\Middleware | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
protected $realm; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $username; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $password; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $route; | ||
|
||
/** | ||
* Constructor | ||
* | ||
* @param string $username The HTTP Authentication username | ||
* @param string $password The HTTP Authentication password | ||
* @param string $realm The HTTP Authentication realm | ||
*/ | ||
public function __construct($username, $password, $realm = 'Protected Area', $route = '') | ||
{ | ||
$this->username = $username; | ||
$this->password = $password; | ||
$this->realm = $realm; | ||
$this->route = $route; | ||
} | ||
|
||
/** | ||
* Call | ||
* | ||
* 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. | ||
*/ | ||
public function call() | ||
{ | ||
$request = $this->app->request; | ||
|
||
if (false !== strpos($request->getPath(), $this->route)) { | ||
|
||
$req = $this->app->request(); | ||
$res = $this->app->response(); | ||
$authUser = $req->headers('PHP_AUTH_USER'); | ||
$authPass = $req->headers('PHP_AUTH_PW'); | ||
|
||
if ($authUser && $authPass && $authUser === $this->username && $authPass === $this->password) { | ||
$this->next->call(); | ||
} else { | ||
$res->status(401); | ||
$res->header('WWW-Authenticate', sprintf('Basic realm="%s"', $this->realm)); | ||
} | ||
|
||
return; | ||
} | ||
$this->next->call(); | ||
|
||
|
||
} | ||
} |