2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Provides an interface to HTTP basic authentication.
|
2014-11-27 20:34:55 +01:00
|
|
|
*
|
2009-11-15 22:24:58 +01:00
|
|
|
* This utility class can be used to secure any request with basic authentication. To do so,
|
|
|
|
* {@link BasicAuth::requireLogin()} from your Controller's init() method or action handler method.
|
2014-11-27 20:34:55 +01:00
|
|
|
*
|
2009-11-15 22:24:58 +01:00
|
|
|
* It also has a function to protect your entire site. See {@link BasicAuth::protect_entire_site()}
|
2010-10-19 00:29:35 +02:00
|
|
|
* for more information. You can control this setting on controller-level by using {@link Controller->basicAuthEnabled}.
|
2014-11-27 20:34:55 +01:00
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2008-02-25 03:10:37 +01:00
|
|
|
* @subpackage security
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2010-05-25 06:20:54 +02:00
|
|
|
class BasicAuth {
|
2008-04-09 13:17:39 +02:00
|
|
|
/**
|
2013-03-21 19:48:54 +01:00
|
|
|
* @config
|
2010-10-19 00:29:35 +02:00
|
|
|
* @var Boolean Flag set by {@link self::protect_entire_site()}
|
2008-04-09 13:17:39 +02:00
|
|
|
*/
|
2009-11-15 22:41:13 +01:00
|
|
|
private static $entire_site_protected = false;
|
2014-11-27 20:34:55 +01:00
|
|
|
|
2010-10-19 00:29:35 +02:00
|
|
|
/**
|
2013-03-21 19:48:54 +01:00
|
|
|
* @config
|
2010-10-19 00:29:35 +02:00
|
|
|
* @var String|array Holds a {@link Permission} code that is required
|
2014-11-27 20:34:55 +01:00
|
|
|
* when calling {@link protect_site_if_necessary()}. Set this value through
|
2010-10-19 00:29:35 +02:00
|
|
|
* {@link protect_entire_site()}.
|
|
|
|
*/
|
|
|
|
private static $entire_site_protected_code = 'ADMIN';
|
2014-11-27 20:34:55 +01:00
|
|
|
|
2010-10-19 00:29:35 +02:00
|
|
|
/**
|
2013-03-21 19:48:54 +01:00
|
|
|
* @config
|
2010-10-19 00:29:35 +02:00
|
|
|
* @var String Message that shows in the authentication box.
|
|
|
|
* Set this value through {@link protect_entire_site()}.
|
|
|
|
*/
|
|
|
|
private static $entire_site_protected_message = "SilverStripe test website. Use your CMS login.";
|
2008-04-09 13:17:39 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Require basic authentication. Will request a username and password if none is given.
|
2014-11-27 20:34:55 +01:00
|
|
|
*
|
2009-03-22 23:59:14 +01:00
|
|
|
* Used by {@link Controller::init()}.
|
2014-11-27 20:34:55 +01:00
|
|
|
*
|
2010-10-19 00:29:35 +02:00
|
|
|
* @throws SS_HTTPResponse_Exception
|
2014-11-27 20:34:55 +01:00
|
|
|
*
|
2008-04-09 13:17:39 +02:00
|
|
|
* @param string $realm
|
2010-10-19 00:29:35 +02:00
|
|
|
* @param string|array $permissionCode Optional
|
2010-10-15 03:18:07 +02:00
|
|
|
* @param boolean $tryUsingSessionLogin If true, then the method with authenticate against the
|
2010-10-19 00:29:35 +02:00
|
|
|
* session log-in if those credentials are disabled.
|
2014-11-27 20:34:55 +01:00
|
|
|
* @return Member $member
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function requireLogin($realm, $permissionCode = null, $tryUsingSessionLogin = true) {
|
2010-11-30 06:13:09 +01:00
|
|
|
$isRunningTests = (class_exists('SapphireTest', false) && SapphireTest::is_running_test());
|
|
|
|
if(!Security::database_is_ready() || (Director::is_cli() && !$isRunningTests)) return true;
|
2014-11-27 20:34:55 +01:00
|
|
|
|
2015-03-17 00:07:21 +01:00
|
|
|
/*
|
|
|
|
* Enable HTTP Basic authentication workaround for PHP running in CGI mode with Apache
|
|
|
|
* Depending on server configuration the auth header may be in HTTP_AUTHORIZATION or
|
|
|
|
* REDIRECT_HTTP_AUTHORIZATION
|
2016-01-06 00:34:58 +01:00
|
|
|
*
|
2015-03-17 00:07:21 +01:00
|
|
|
* The follow rewrite rule must be in the sites .htaccess file to enable this workaround
|
|
|
|
* RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
|
|
|
|
*/
|
|
|
|
$authHeader = (isset($_SERVER['HTTP_AUTHORIZATION']) ? $_SERVER['HTTP_AUTHORIZATION'] :
|
|
|
|
(isset($_SERVER['REDIRECT_HTTP_AUTHORIZATION']) ? $_SERVER['REDIRECT_HTTP_AUTHORIZATION'] : null));
|
2014-11-27 20:34:55 +01:00
|
|
|
$matches = array();
|
2015-03-17 00:07:21 +01:00
|
|
|
if ($authHeader &&
|
|
|
|
preg_match('/Basic\s+(.*)$/i', $authHeader, $matches)) {
|
2014-11-27 20:34:55 +01:00
|
|
|
list($name, $password) = explode(':', base64_decode($matches[1]));
|
|
|
|
$_SERVER['PHP_AUTH_USER'] = strip_tags($name);
|
|
|
|
$_SERVER['PHP_AUTH_PW'] = strip_tags($password);
|
|
|
|
}
|
|
|
|
|
2010-10-15 03:18:07 +02:00
|
|
|
$member = null;
|
2007-07-19 12:40:28 +02:00
|
|
|
if(isset($_SERVER['PHP_AUTH_USER']) && isset($_SERVER['PHP_AUTH_PW'])) {
|
2008-02-25 03:10:37 +01:00
|
|
|
$member = MemberAuthenticator::authenticate(array(
|
2014-11-27 20:34:55 +01:00
|
|
|
'Email' => $_SERVER['PHP_AUTH_USER'],
|
2008-02-25 03:10:37 +01:00
|
|
|
'Password' => $_SERVER['PHP_AUTH_PW'],
|
|
|
|
), null);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-11-27 20:34:55 +01:00
|
|
|
|
2010-10-15 03:18:07 +02:00
|
|
|
if(!$member && $tryUsingSessionLogin) $member = Member::currentUser();
|
2014-11-27 20:34:55 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// If we've failed the authentication mechanism, then show the login form
|
2010-10-15 03:18:07 +02:00
|
|
|
if(!$member) {
|
2010-10-19 00:29:35 +02:00
|
|
|
$response = new SS_HTTPResponse(null, 401);
|
|
|
|
$response->addHeader('WWW-Authenticate', "Basic realm=\"$realm\"");
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
if(isset($_SERVER['PHP_AUTH_USER'])) {
|
2010-10-19 00:29:35 +02:00
|
|
|
$response->setBody(_t('BasicAuth.ERRORNOTREC', "That username / password isn't recognised"));
|
2007-07-19 12:40:28 +02:00
|
|
|
} else {
|
2010-10-19 00:29:35 +02:00
|
|
|
$response->setBody(_t('BasicAuth.ENTERINFO', "Please enter a username and password."));
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-11-27 20:34:55 +01:00
|
|
|
|
2010-10-19 00:29:35 +02:00
|
|
|
// Exception is caught by RequestHandler->handleRequest() and will halt further execution
|
|
|
|
$e = new SS_HTTPResponse_Exception(null, 401);
|
|
|
|
$e->setResponse($response);
|
|
|
|
throw $e;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-11-27 20:34:55 +01:00
|
|
|
|
2010-10-19 00:29:35 +02:00
|
|
|
if($permissionCode && !Permission::checkMember($member->ID, $permissionCode)) {
|
|
|
|
$response = new SS_HTTPResponse(null, 401);
|
|
|
|
$response->addHeader('WWW-Authenticate', "Basic realm=\"$realm\"");
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
if(isset($_SERVER['PHP_AUTH_USER'])) {
|
2010-10-19 00:29:35 +02:00
|
|
|
$response->setBody(_t('BasicAuth.ERRORNOTADMIN', "That user is not an administrator."));
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-11-27 20:34:55 +01:00
|
|
|
|
2010-10-19 00:29:35 +02:00
|
|
|
// Exception is caught by RequestHandler->handleRequest() and will halt further execution
|
|
|
|
$e = new SS_HTTPResponse_Exception(null, 401);
|
|
|
|
$e->setResponse($response);
|
|
|
|
throw $e;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-11-27 20:34:55 +01:00
|
|
|
|
2007-10-02 06:32:11 +02:00
|
|
|
return $member;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-11-27 20:34:55 +01:00
|
|
|
|
2009-11-15 22:24:58 +01:00
|
|
|
/**
|
|
|
|
* Enable protection of the entire site with basic authentication.
|
2014-11-27 20:34:55 +01:00
|
|
|
*
|
2009-11-15 22:24:58 +01:00
|
|
|
* This log-in uses the Member database for authentication, but doesn't interfere with the
|
|
|
|
* regular log-in form. This can be useful for test sites, where you want to hide the site
|
|
|
|
* away from prying eyes, but still be able to test the regular log-in features of the site.
|
2014-11-27 20:34:55 +01:00
|
|
|
*
|
2009-11-15 22:24:58 +01:00
|
|
|
* If you are including conf/ConfigureFromEnv.php in your _config.php file, you can also enable
|
|
|
|
* this feature by adding this line to your _ss_environment.php:
|
2014-11-27 20:34:55 +01:00
|
|
|
*
|
2009-11-15 22:24:58 +01:00
|
|
|
* define('SS_USE_BASIC_AUTH', true);
|
2014-11-27 20:34:55 +01:00
|
|
|
*
|
2010-10-19 00:29:35 +02:00
|
|
|
* @param boolean $protect Set this to false to disable protection.
|
|
|
|
* @param String $code {@link Permission} code that is required from the user.
|
|
|
|
* Defaults to "ADMIN". Set to NULL to just require a valid login, regardless
|
|
|
|
* of the permission codes a user has.
|
2009-11-15 22:24:58 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function protect_entire_site($protect = true, $code = 'ADMIN', $message = null) {
|
2013-03-21 19:48:54 +01:00
|
|
|
Config::inst()->update('BasicAuth', 'entire_site_protected', $protect);
|
|
|
|
Config::inst()->update('BasicAuth', 'entire_site_protected_code', $code);
|
|
|
|
Config::inst()->update('BasicAuth', 'entire_site_protected_message', $message);
|
2009-11-15 22:24:58 +01:00
|
|
|
}
|
2014-11-27 20:34:55 +01:00
|
|
|
|
2009-11-15 22:24:58 +01:00
|
|
|
/**
|
|
|
|
* Call {@link BasicAuth::requireLogin()} if {@link BasicAuth::protect_entire_site()} has been called.
|
2010-10-19 00:29:35 +02:00
|
|
|
* This is a helper function used by {@link Controller::init()}.
|
2014-11-27 20:34:55 +01:00
|
|
|
*
|
2010-10-19 00:29:35 +02:00
|
|
|
* If you want to enabled protection (rather than enforcing it),
|
|
|
|
* please use {@link protect_entire_site()}.
|
2009-11-15 22:24:58 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function protect_site_if_necessary() {
|
2013-03-21 19:48:54 +01:00
|
|
|
$config = Config::inst()->forClass('BasicAuth');
|
|
|
|
if($config->entire_site_protected) {
|
|
|
|
self::requireLogin($config->entire_site_protected_message, $config->entire_site_protected_code, false);
|
2009-11-15 22:24:58 +01:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2009-11-15 22:24:58 +01:00
|
|
|
|
2009-03-10 23:08:52 +01:00
|
|
|
}
|