diff options
Diffstat (limited to 'webmail/plugins/http_authentication')
5 files changed, 201 insertions, 0 deletions
diff --git a/webmail/plugins/http_authentication/config.inc.php.dist b/webmail/plugins/http_authentication/config.inc.php.dist new file mode 100644 index 0000000..0d798a5 --- /dev/null +++ b/webmail/plugins/http_authentication/config.inc.php.dist @@ -0,0 +1,9 @@ +<?php + +// HTTP Basic Authentication Plugin options +// ---------------------------------------- +// Default mail host to log-in using user/password from HTTP Authentication. +// This is useful if the users are free to choose arbitrary mail hosts (or +// from a list), but have one host they usually want to log into. +// Unlike $rcmail_config['default_host'] this must be a string! +$rcmail_config['http_authentication_host'] = ''; diff --git a/webmail/plugins/http_authentication/http_authentication.php b/webmail/plugins/http_authentication/http_authentication.php new file mode 100644 index 0000000..a94b612 --- /dev/null +++ b/webmail/plugins/http_authentication/http_authentication.php @@ -0,0 +1,92 @@ +<?php + +/** + * HTTP Basic Authentication + * + * Make use of an existing HTTP authentication and perform login with the existing user credentials + * + * Configuration: + * // redirect the client to this URL after logout. This page is then responsible to clear HTTP auth + * $rcmail_config['logout_url'] = 'http://server.tld/logout.html'; + * + * See logout.html (in this directory) for an example how HTTP auth can be cleared. + * + * For other configuration options, see config.inc.php.dist! + * + * @version @package_version@ + * @license GNU GPLv3+ + * @author Thomas Bruederli + */ +class http_authentication extends rcube_plugin +{ + + function init() + { + $this->add_hook('startup', array($this, 'startup')); + $this->add_hook('authenticate', array($this, 'authenticate')); + $this->add_hook('logout_after', array($this, 'logout')); + } + + function startup($args) + { + if (!empty($_SERVER['PHP_AUTH_USER']) && !empty($_SERVER['PHP_AUTH_PW'])) { + $rcmail = rcmail::get_instance(); + $rcmail->add_shutdown_function(array('http_authentication', 'shutdown')); + + // handle login action + if (empty($args['action']) && empty($_SESSION['user_id'])) { + $args['action'] = 'login'; + } + // Set user password in session (see shutdown() method for more info) + else if (!empty($_SESSION['user_id']) && empty($_SESSION['password'])) { + $_SESSION['password'] = $rcmail->encrypt($_SERVER['PHP_AUTH_PW']); + } + } + + return $args; + } + + function authenticate($args) + { + // Load plugin's config file + $this->load_config(); + + $host = rcmail::get_instance()->config->get('http_authentication_host'); + if (is_string($host) && trim($host) !== '') + $args['host'] = rcube_idn_to_ascii(rcube_parse_host($host)); + + // Allow entering other user data in login form, + // e.g. after log out (#1487953) + if (!empty($args['user'])) { + return $args; + } + + if (!empty($_SERVER['PHP_AUTH_USER']) && !empty($_SERVER['PHP_AUTH_PW'])) { + $args['user'] = $_SERVER['PHP_AUTH_USER']; + $args['pass'] = $_SERVER['PHP_AUTH_PW']; + } + + $args['cookiecheck'] = false; + $args['valid'] = true; + + return $args; + } + + function logout($args) + { + // redirect to configured URL in order to clear HTTP auth credentials + if (!empty($_SERVER['PHP_AUTH_USER']) && $args['user'] == $_SERVER['PHP_AUTH_USER']) { + if ($url = rcmail::get_instance()->config->get('logout_url')) { + header("Location: $url", true, 307); + } + } + } + + function shutdown() + { + // There's no need to store password (even if encrypted) in session + // We'll set it back on startup (#1486553) + rcmail::get_instance()->session->remove('password'); + } +} + diff --git a/webmail/plugins/http_authentication/logout.html b/webmail/plugins/http_authentication/logout.html new file mode 100644 index 0000000..0a78a62 --- /dev/null +++ b/webmail/plugins/http_authentication/logout.html @@ -0,0 +1,29 @@ +<!DOCTYPE html> +<html> +<head> +<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> +<title>Logout</title> +<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js"></script> +<script type="text/javascript"> + +// as seen on http://stackoverflow.com/questions/31326/is-there-a-browser-equivalent-to-ies-clearauthenticationcache +$(document).ready(function(){ + if (document.all && document.execCommand) { + document.execCommand("ClearAuthenticationCache", "false"); + } + else { + $.ajax({ + url: location.href, + type: 'POST', + username: '__LOGOUT__', + password: '***********' + }); + } +}); + +</script> +</head> +<body> +<h1>You've successully been logged out!</h1> + +</body>
\ No newline at end of file diff --git a/webmail/plugins/http_authentication/package.xml b/webmail/plugins/http_authentication/package.xml new file mode 100644 index 0000000..aab3819 --- /dev/null +++ b/webmail/plugins/http_authentication/package.xml @@ -0,0 +1,48 @@ +<?xml version="1.0" encoding="UTF-8"?> +<package xmlns="http://pear.php.net/dtd/package-2.0" xmlns:tasks="http://pear.php.net/dtd/tasks-1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" packagerversion="1.9.0" version="2.0" xsi:schemaLocation="http://pear.php.net/dtd/tasks-1.0 + http://pear.php.net/dtd/tasks-1.0.xsd + http://pear.php.net/dtd/package-2.0 + http://pear.php.net/dtd/package-2.0.xsd"> + <name>http_authentication</name> + <channel>pear.roundcube.net</channel> + <summary>HTTP Basic Authentication</summary> + <description>Make use of an existing HTTP authentication and perform login with the existing user credentials.</description> + <lead> + <name>Thomas Bruederli</name> + <user>thomasb</user> + <email>roundcube@gmail.com</email> + <active>yes</active> + </lead> + <date>2012-09-18</date> + <version> + <release>1.5</release> + <api>1.5</api> + </version> + <stability> + <release>stable</release> + <api>stable</api> + </stability> + <license uri="http://www.gnu.org/licenses/gpl.html">GNU GPLv3+</license> + <notes>-</notes> + <contents> + <dir baseinstalldir="/" name="/"> + <file name="http_authentication.php" role="php"> + <tasks:replace from="@name@" to="name" type="package-info"/> + <tasks:replace from="@package_version@" to="version" type="package-info"/> + </file> + <file name="logout.html" role="data"></file> + </dir> + <!-- / --> + </contents> + <dependencies> + <required> + <php> + <min>5.2.1</min> + </php> + <pearinstaller> + <min>1.7.0</min> + </pearinstaller> + </required> + </dependencies> + <phprelease/> +</package> diff --git a/webmail/plugins/http_authentication/tests/HttpAuthentication.php b/webmail/plugins/http_authentication/tests/HttpAuthentication.php new file mode 100644 index 0000000..c172368 --- /dev/null +++ b/webmail/plugins/http_authentication/tests/HttpAuthentication.php @@ -0,0 +1,23 @@ +<?php + +class HttpAuthentication_Plugin extends PHPUnit_Framework_TestCase +{ + + function setUp() + { + include_once dirname(__FILE__) . '/../http_authentication.php'; + } + + /** + * Plugin object construction test + */ + function test_constructor() + { + $rcube = rcube::get_instance(); + $plugin = new http_authentication($rcube->api); + + $this->assertInstanceOf('http_authentication', $plugin); + $this->assertInstanceOf('rcube_plugin', $plugin); + } +} + |
