From: <ja...@us...> - 2007-07-25 23:27:21
|
Revision: 16790 http://gallery.svn.sourceforge.net/gallery/?rev=16790&view=rev Author: jablko Date: 2007-07-25 16:27:22 -0700 (Wed, 25 Jul 2007) Log Message: ----------- Decrease HTTP and server auth plugin order weight to increase priority. Use lightweight event system. Modified Paths: -------------- branches/DEV_2_3/gallery2/modules/httpauth/HttpAuthSiteAdmin.inc branches/DEV_2_3/gallery2/modules/httpauth/TryLogout.inc branches/DEV_2_3/gallery2/modules/httpauth/classes/HttpAuthHelper.class branches/DEV_2_3/gallery2/modules/httpauth/classes/HttpAuthInterface_1_0.class branches/DEV_2_3/gallery2/modules/httpauth/classes/HttpAuthPlugin.class branches/DEV_2_3/gallery2/modules/httpauth/classes/ServerAuthPlugin.class branches/DEV_2_3/gallery2/modules/httpauth/module.inc branches/DEV_2_3/gallery2/modules/httpauth/templates/HttpAuthSiteAdmin.tpl branches/DEV_2_3/gallery2/modules/httpauth/templates/TryLogout.tpl branches/DEV_2_3/gallery2/modules/httpauth/test/phpunit/HttpAuthHelperTest.class branches/DEV_2_3/gallery2/modules/httpauth/test/phpunit/HttpAuthModuleTest.class branches/DEV_2_3/gallery2/modules/httpauth/test/phpunit/HttpAuthPluginTest.class branches/DEV_2_3/gallery2/modules/httpauth/test/phpunit/ServerAuthPluginTest.class Modified: branches/DEV_2_3/gallery2/modules/httpauth/HttpAuthSiteAdmin.inc =================================================================== --- branches/DEV_2_3/gallery2/modules/httpauth/HttpAuthSiteAdmin.inc 2007-07-25 22:56:15 UTC (rev 16789) +++ branches/DEV_2_3/gallery2/modules/httpauth/HttpAuthSiteAdmin.inc 2007-07-25 23:27:22 UTC (rev 16790) @@ -66,8 +66,9 @@ return array($ret, null); } - foreach (array('httpAuthPlugin', 'useGlobally', 'serverAuthPlugin', 'regexAuthPlugin', - 'authtypePattern', 'usernamePattern', 'usernameReplace', 'authName') as $key) { + foreach (array('httpAuthPlugin', 'authName', 'serverAuthPlugin', 'regexAuthPlugin', + 'authtypePattern', 'usernamePattern', 'usernameReplace', 'useGlobally') + as $key) { if (isset($form[$key])) { $ret = $module->setParameter($key, $form[$key]); if ($ret) { @@ -130,8 +131,9 @@ /* Load our default values if we didn't just come from this form */ if ($form['formName'] != 'HttpAuthSiteAdmin') { $form['formName'] = 'HttpAuthSiteAdmin'; - foreach (array('httpAuthPlugin', 'useGlobally', 'serverAuthPlugin', 'regexAuthPlugin', - 'authtypePattern', 'usernamePattern', 'usernameReplace', 'authName') as $key) { + foreach (array('httpAuthPlugin', 'authName', 'serverAuthPlugin', 'regexAuthPlugin', + 'authtypePattern', 'usernamePattern', 'usernameReplace', 'useGlobally') + as $key) { if (!empty($params[$key])) { $form[$key] = $params[$key]; } Modified: branches/DEV_2_3/gallery2/modules/httpauth/TryLogout.inc =================================================================== --- branches/DEV_2_3/gallery2/modules/httpauth/TryLogout.inc 2007-07-25 22:56:15 UTC (rev 16789) +++ branches/DEV_2_3/gallery2/modules/httpauth/TryLogout.inc 2007-07-25 23:27:22 UTC (rev 16790) @@ -39,16 +39,16 @@ /* Ask browser to authenticate with bogus authtype */ GalleryUtilities::setResponseHeader('HTTP/1.0 401 Unauthorized', false); - GalleryUtilities::setResponseHeader('WWW-Authenticate: Bogus'); + GalleryUtilities::setResponseHeader('WWW-Authenticate: Bogus', false); - /* Redirect using random username and password */ + /* Redirect with random username and password */ $TryLogout = array(); foreach (array('scriptUrl', 'hrefUrl') as $key) { $url = $urlGenerator->generateUrl(array('view' => 'httpauth.FinishLogout'), array('forceFullUrl' => true, 'htmlEntities' => $key == 'hrefUrl')); - $TryLogout[$key] = - HttpAuthHelper::addHttpAuthToUrl($url, '__LOGOUT__' . rand(), rand()); + $TryLogout[$key] = HttpAuthHelper::addHttpAuthToUrl( + $url, '__LOGOUT__' . rand(), rand()); } /* Render HTML body */ Modified: branches/DEV_2_3/gallery2/modules/httpauth/classes/HttpAuthHelper.class =================================================================== --- branches/DEV_2_3/gallery2/modules/httpauth/classes/HttpAuthHelper.class 2007-07-25 22:56:15 UTC (rev 16789) +++ branches/DEV_2_3/gallery2/modules/httpauth/classes/HttpAuthHelper.class 2007-07-25 23:27:22 UTC (rev 16790) @@ -124,17 +124,23 @@ return array(null, true); } - /* - * Use the Basic auth-type for tests. PHP does not define auth variables for arbitrary - * auth-types. - */ - list ($status, $headers, $body) = GalleryCoreApi::requestWebPage($urlGenerator->generateUrl( - array('view' => 'httpauth.HttpAuthWorks'), - array('forceFullUrl' => true, - 'htmlEntities' => false)), - 'GET', array('Authorization' => 'Basic ' . base64_encode('USERNAME:PASSWORD'))); + /* Use Basic authtype because AUTH_TYPE is not defined for arbitrary authtypes */ + list ($status, $headers, $body) = GalleryCoreApi::requestWebPage( + $urlGenerator->generateUrl(array('view' => 'httpauth.HttpAuthWorks'), + array('forceFullUrl' => true, + 'htmlEntities' => false)), + 'GET', + array('Authorization' => 'Basic ' . base64_encode('USERNAME:PASSWORD'))); - return array(null, trim($body) == "Basic\nUSERNAME\nPASSWORD"); + if (!preg_match('/^HTTP\/[0-9]\.[0-9] 200/', $status)) { + return array(null, false); + } + + if (trim($body) != "AUTHTYPE\nUSERNAME\nPASSWORD") { + return array(null, false); + } + + return array(null, true); } /** @@ -149,18 +155,24 @@ $username = GalleryUtilities::getServerVar('PHP_AUTH_USER'); $password = GalleryUtilities::getServerVar('PHP_AUTH_PW'); - $authorization = GalleryUtilities::getRequestVariables('authorization'); - if (empty($authorization)) { - /* IIS ISAPI PHP defines HTTP_AUTHORIZATION */ + if (empty($username)) { $authorization = GalleryUtilities::getServerVar('HTTP_AUTHORIZATION'); - } - if (!empty($authorization)) { + if (empty($authorization)) { + $authorization = GalleryUtilities::getRequestVariables('authorization'); + if (empty($authorization)) { + return array($authtype, $username, $password); + } + } + list ($authtype, $authdata) = explode(' ', $authorization); list ($username, $password) = explode(':', base64_decode($authdata)); + if (empty($username)) { + return array($authtype, $username, $password); + } } - /* AUTH_TYPE is often not defined, assume a default. */ - if (!empty($username) && empty($authtype)) { + /* AUTH_TYPE is often not defined, assume a default */ + if (empty($authtype)) { $authtype = 'Basic'; } @@ -170,10 +182,10 @@ /** * Common code from HttpAuthPlugin::getUser and ServerAuthPlugin::getUser. * - * Potentially filters authentication type and username with regular expressions before - * returning active user. + * Potentially filters authtype and username with regular expressions before returning active + * user. * - * @param string authentication type (Basic, Negotiate, etc.) + * @param string authtype (Basic, Negotiate, etc.) * @param string username * @return array object GalleryStatus a status code * object GalleryUser the active user or null @@ -186,7 +198,7 @@ if (!empty($params['regexAuthPlugin'])) { if (!preg_match($params['authtypePattern'], $authtype)) { - /* Reject authentication type */ + /* Reject authtype */ return array(null, null); } @@ -195,8 +207,8 @@ return array(null, null); } - $username = - preg_replace($params['usernamePattern'], $params['usernameReplace'], $username); + $username = preg_replace( + $params['usernamePattern'], $params['usernameReplace'], $username); } if (empty($username)) { @@ -223,7 +235,8 @@ } /** - * Regenerate the session if this request might the initial login (active auth vs passive auth). + * Regenerate the session if this request might be the initial login (active auth vs. passive + * auth). * @param object GalleryUser $authenticatedUser user authenticated for this request * @return object GalleryStatus a status code */ @@ -244,8 +257,8 @@ } /** - * Adds the given pair of username / password to the given URL as HTTP auth user:pass@hostname. - * @param string $url An absolute URL + * Add the given username / password pair to the given URL as HTTP auth user:pass@hostname. + * @param string $url an absolute URL * @param string $username * @param string $password */ @@ -257,7 +270,7 @@ } /** - * Removes user:pass from the given URL. + * Remove user:pass from the given URL. * @param string $url An absolute URL */ function stripHttpAuthFromUrl($url) { @@ -312,7 +325,7 @@ /** * @see HttpAuthInterface_1_0::requestAuthentication */ - function requestAuthentication($ignoreUseGloballyFlag=true) { + function requestAuthentication($force=false) { list ($ret, $params) = GalleryCoreApi::fetchAllPluginParameters('module', 'httpauth'); if ($ret) { return $ret; @@ -322,13 +335,13 @@ return null; } - if (!$ignoreUseGloballyFlag && empty($params['useGlobally'])) { + if (!$force && empty($params['useGlobally'])) { return null; } GalleryUtilities::setResponseHeader('HTTP/1.0 401 Unauthorized', false); - GalleryUtilities::setResponseHeader("WWW-Authenticate: Basic realm='$params[authName]'", - false); + GalleryUtilities::setResponseHeader( + "WWW-Authenticate: Basic realm='$params[authName]'", false); return null; } Modified: branches/DEV_2_3/gallery2/modules/httpauth/classes/HttpAuthInterface_1_0.class =================================================================== --- branches/DEV_2_3/gallery2/modules/httpauth/classes/HttpAuthInterface_1_0.class 2007-07-25 22:56:15 UTC (rev 16789) +++ branches/DEV_2_3/gallery2/modules/httpauth/classes/HttpAuthInterface_1_0.class 2007-07-25 23:27:22 UTC (rev 16790) @@ -19,9 +19,8 @@ */ /** - * This is an interface for the httpauth module. Modules that wish to opt-in for HTTPauth can - * utilize an implementation of this interface. - * + * This is an interface for the HTTP auth module. Modules that wish to opt-in for HTTP auth can + * use an implementation of this interface. * @see HttpAuthPlugin * @see ServerAuthPlugin * @package HttpAuth @@ -33,24 +32,21 @@ class HttpAuthInterface_1_0 { /** - * Checks if HTTP authentication is enabled. - * @return array object GalleryStatus a status code, - * bool true if HTTPAuth is enabled, - * bool true if ServerAuth is enabled - * bool true if it is enabled globally or - * false if it is only enabled for specific modules + * Check if HTTP auth is enabled. + * @return array object GalleryStatus a status code + * boolean true if HTTP auth is enabled + * boolean true if server auth is enabled + * boolean true if HTTP auth is enabled for all modules */ function getConfiguration() { return array(GalleryCoreApi::error(ERROR_UNIMPLEMENTED), null, null, null); } /** - * Registers the HTTP authentication Gallery authentication plugin. - * @param bool $enableHttpAuth Set to true to activate HTTP auth, false to deactivate. - * @param bool $enableServerAuth (optional) Set to true to activate Server auth, false to - * deactivate (defaults to false). - * @param bool $useGlobally (optional) Set to true to activate auth for all modules, false to - * require modules to opt-in for HTTP auth (default), + * Register HTTP auth plugins. + * @param boolean $enableHttpAuth true to enable HTTP auth + * @param boolean $enableServerAuth (optional) true to enable server auth + * @param boolean $useGlobally (optional) true to enable auth for all modules * @return object GalleryStatus a status code */ function setConfiguration($enableHttpAuth, $enableServerAuth=false, $useGlobally=false) { @@ -59,9 +55,11 @@ /** * Prompt the user via HTTP authentication for username / password. + * @param boolean $force (optional) request authentication regardless of + * whether HTTP auth is enabled for all modules * @return object GalleryStatus a status code */ - function requestAuthentication() { + function requestAuthentication($force=false) { return GalleryCoreApi::error(ERROR_UNIMPLEMENTED); } } Modified: branches/DEV_2_3/gallery2/modules/httpauth/classes/HttpAuthPlugin.class =================================================================== --- branches/DEV_2_3/gallery2/modules/httpauth/classes/HttpAuthPlugin.class 2007-07-25 22:56:15 UTC (rev 16789) +++ branches/DEV_2_3/gallery2/modules/httpauth/classes/HttpAuthPlugin.class 2007-07-25 23:27:22 UTC (rev 16790) @@ -23,8 +23,8 @@ /** * Get active user from username and password in HTTP headers. * - * HTTP authentication is handled between the user-agent and Gallery. Gallery is responsible to - * authenticate a user-supplied username / password pair. + * HTTP authentication is handled between the user-agent and Gallery. Gallery is responsible for + * authenticating the username / password pair. * * @package HttpAuth * @subpackage Classes @@ -38,6 +38,9 @@ */ function getUser() { list ($authtype, $username, $password) = HttpAuthHelper::getHttpAuth(); + if (empty($username) || strncmp($username, '__LOGOUT__', 10) === 0) { + return array(null, null); + } list ($ret, $user) = HttpAuthHelper::getUser($authtype, $username); if ($ret) { @@ -45,30 +48,25 @@ } /* - * We are effectively logging in on every request when we use this plugin. We can't - * post a login event each time we do this, but we need to post FailedLogin events. + * We're effectively logging in on every request with this plugin. We can't post a login + * event each time we do this, but we need to post FailedLogin events. */ - if (isset($user) && $user->isCorrectPassword($password)) { - $ret = HttpAuthHelper::regenerateSessionIfNecessary($user); - if ($ret) { - return array($ret, null); - } - - return array(null, $user); - } else if (!empty($username) && strpos($username, '__LOGOUT__') !== 0) { - /* - * Don't post an event if the username starts with __logout__ since that is used when we - * try to force the user-agent to clear its auth cache. - */ + if (!isset($user) || !$user->isCorrectPassword($password)) { $event = GalleryCoreApi::newEvent('Gallery::FailedLogin'); - $event->setData(array('userName' => $username)); - list ($ret, $ignored) = GalleryCoreApi::postEvent($event); + $event->setData(array('username' => $username, + 'password' => $password)); + list ($ret, $eventResults) = GalleryCoreApi::postEvent($event); if ($ret) { return array($ret, null); } } - return array(null, null); + $ret = HttpAuthHelper::regenerateSessionIfNecessary($user); + if ($ret) { + return array($ret, null); + } + + return array(null, $user); } } ?> Modified: branches/DEV_2_3/gallery2/modules/httpauth/classes/ServerAuthPlugin.class =================================================================== --- branches/DEV_2_3/gallery2/modules/httpauth/classes/ServerAuthPlugin.class 2007-07-25 22:56:15 UTC (rev 16789) +++ branches/DEV_2_3/gallery2/modules/httpauth/classes/ServerAuthPlugin.class 2007-07-25 23:27:22 UTC (rev 16790) @@ -47,17 +47,23 @@ $authtype = GalleryUtilities::getServerVar('REDIRECT_AUTH_TYPE'); $username = GalleryUtilities::getServerVar('REDIRECT_REMOTE_USER'); } + if (empty($username) || strncmp($username, '__LOGOUT__', 10) === 0) { + return array(null, null); + } list ($ret, $user) = HttpAuthHelper::getUser($authtype, $username); if ($ret) { return array($ret, null); } + if (!isset($user)) { + return array(null, null, array('view' => 'core.UserAdmin', + 'subView' => 'register.UserSelfRegistration', + 'return' => true)); + } - if (!empty($user)) { - $ret = HttpAuthHelper::regenerateSessionIfNecessary($user); - if ($ret) { - return array($ret, null); - } + $ret = HttpAuthHelper::regenerateSessionIfNecessary($user); + if ($ret) { + return array($ret, null); } return array(null, $user); Modified: branches/DEV_2_3/gallery2/modules/httpauth/module.inc =================================================================== --- branches/DEV_2_3/gallery2/modules/httpauth/module.inc 2007-07-25 22:56:15 UTC (rev 16789) +++ branches/DEV_2_3/gallery2/modules/httpauth/module.inc 2007-07-25 23:27:22 UTC (rev 16790) @@ -19,7 +19,7 @@ */ /** - * HTTP Auth Module. Login using HTTP authentication. + * HTTP Auth module. Login with HTTP authentication. * @package HttpAuth * @author Jack Bates <ms...@fr...> * @version $Revision$ @@ -33,10 +33,10 @@ $this->setName($gallery->i18n('HTTP Auth')); $this->setVersion('1.0.1'); /* Update upgrade() too! */ $this->_templateVersion = 1; - $this->setDescription($gallery->i18n('Login using HTTP authentication.')); + $this->setDescription($gallery->i18n('Login with HTTP authentication.')); $this->setGroup('authentication', $gallery->i18n('Authentication')); - $this->setCallbacks('getSiteAdminViews|registerEventListeners'); - $this->setRequiredCoreApi(array(7, 20)); + $this->setCallbacks('getSiteAdminViews'); + $this->setRequiredCoreApi(array(7, 34)); $this->setRequiredModuleApi(array(3, 6)); } @@ -45,19 +45,18 @@ * @todo Explicit rewrite module version check can be removed on next major module API version. */ function activate($postActivationEvent=true) { - /* Ensure the rewrite module is compatible, 'pattern' is optional since v1.1.8 of rewrite */ + /* Check that the URL rewrite module is compatible, 'pattern' is optional since 1.1.8 */ list ($ret, $modules) = GalleryCoreApi::fetchPluginList('module'); if ($ret) { return array($ret, null); } - if (isset($modules['rewrite'])) { list ($ret, $rewrite) = GalleryCoreApi::loadPlugin('module', 'rewrite', true); if ($ret) { return array($ret, null); } - if (version_compare($rewrite->getVersion(), '1.1.8', '<')) { + if (version_compare($rewrite->getVersion(), '1.1.8') < 0) { return array(GalleryCoreApi::error(ERROR_CONFIGURATION_REQUIRED), null); } } @@ -80,51 +79,40 @@ } if (!empty($params['httpAuthPlugin'])) { - $ret = GalleryCoreApi::registerFactoryImplementation( - 'GalleryAuthPlugin', 'HttpAuthPlugin', 'HttpAuthPlugin', - 'modules/httpauth/classes/HttpAuthPlugin.class', $this->getId(), null); + $ret = GalleryCoreApi::registerFactoryImplementation('GalleryAuthPlugin', + 'HttpAuthPlugin', 'HttpAuthPlugin', + 'modules/httpauth/classes/HttpAuthPlugin.class', 'httpauth', null, 3); if ($ret) { return $ret; } } if (!empty($params['serverAuthPlugin'])) { - $ret = GalleryCoreApi::registerFactoryImplementation( - 'GalleryAuthPlugin', 'ServerAuthPlugin', 'ServerAuthPlugin', - 'modules/httpauth/classes/ServerAuthPlugin.class', $this->getId(), null); + $ret = GalleryCoreApi::registerFactoryImplementation('GalleryAuthPlugin', + 'ServerAuthPlugin', 'ServerAuthPlugin', + 'modules/httpauth/classes/ServerAuthPlugin.class', 'httpauth', null, 3); if ($ret) { return $ret; } } - $ret = GalleryCoreApi::registerFactoryImplementation( - 'HttpAuthInterface_1_0', 'HttpAuthHelper', 'HttpAuthInterface', - 'modules/httpauth/classes/HttpAuthHelper.class', $this->getId(), null); + $ret = GalleryCoreApi::registerFactoryImplementation('GalleryEventListener', + 'HttpAuthModule', 'HttpAuthModule', + 'modules/httpauth/module.inc', 'httpauth', + array('Gallery::ActivatePlugin', + 'Gallery::Error', + 'Gallery::Logout')); if ($ret) { return $ret; } - } - /** - * @see GalleryModule::registerEventListeners - */ - function registerEventListeners() { - $listener = new HttpAuthModule(); - $ret = GalleryCoreApi::registerEventListener('Gallery::ActivatePlugin', $listener, true); + $ret = GalleryCoreApi::registerFactoryImplementation('HttpAuthInterface_1_0', + 'HttpAuthHelper', 'HttpAuthHelper', + 'modules/httpauth/classes/HttpAuthHelper.class', 'httpauth', null); if ($ret) { return $ret; } - $ret = GalleryCoreApi::registerEventListener('Gallery::Error', $listener, true); - if ($ret) { - return $ret; - } - - $ret = GalleryCoreApi::registerEventListener('Gallery::Logout', $listener, true); - if ($ret) { - return $ret; - } - return null; } @@ -136,11 +124,11 @@ case null: /* Initial install */ foreach (array('httpAuthPlugin' => true, - 'authName' => 'Gallery', - 'authtypePattern' => '//', - 'usernamePattern' => '/^(.+\\\\)?([^\\\\@]+)(@.+)?$/', - 'usernameReplace' => '$2', - 'useGlobally' => false) as $key => $value) { + 'authName' => 'Gallery', + 'authtypePattern' => '//', + 'usernamePattern' => '/^(.+\\\\)?([^\\\\@]+)(@.+)?$/', + 'usernameReplace' => '$2', + 'useGlobally' => false) as $key => $value) { $ret = $this->setParameter($key, $value); if ($ret) { return $ret; @@ -151,7 +139,7 @@ /* Add AuthFilterPlugin and RegexAuthFilterPlugin */ case '0.1.0': - /* Update to lighter event subsystem */ + /* Use lightweight event system */ case '0.1.1': /* Introduce auth plugins */ @@ -204,7 +192,7 @@ /* Simplify auth plugin system */ case '0.3.0': - /* Add support for the php-cgi server API */ + /* Add support for the PHP CGI server API */ $ret = $this->_activateRewriteRules(); if ($ret && !($ret->getErrorCode() & ERROR_CONFIGURATION_REQUIRED)) { return $ret; @@ -214,7 +202,7 @@ /* Add logout view to clear browsers' authentication cache */ case '0.5.0': - /* Adding HttpAuthInterface and new site admin option */ + /* Add HttpAuthInterface and new site admin option */ $ret = $this->setParameter('useGlobally', false); if ($ret) { return $ret; @@ -226,11 +214,17 @@ case '1.0.0': /* .mo file migration */ + case '1.0.1': + /* Decrease HTTP and server auth plugin order weight to increase priority */ + + case '1.0.2': + /* Use lightweight event system */ + case 'end of upgrade path': break; default: - return GalleryStatus::error(ERROR_BAD_PLUGIN, __FILE__, __LINE__, + return GalleryCoreApi::error(ERROR_BAD_PLUGIN, __FILE__, __LINE__, sprintf('Unknown module version %s', $currentVersion)); } } @@ -255,9 +249,7 @@ */ list ($ret, $rewriteApi) = GalleryCoreApi::newFactoryInstance('RewriteApi'); if ($ret) { - if ($gallery->getDebug()) { - $gallery->debug('Error in HttpAuthModule::getRewriteRules: ' . $ret->getAsText()); - } + $gallery->debug('Error in HttpAuthModule::getRewriteRules: ' . $ret->getAsText()); return $rules; } if (!isset($rewriteApi)) { @@ -266,20 +258,16 @@ list ($ret, $isCompatible) = $rewriteApi->isCompatibleWithApi(array(1, 1)); if ($ret) { - if ($gallery->getDebug()) { - $gallery->debug('Error in HttpAuthModule::getRewriteRules: ' . $ret->getAsText()); - } + $gallery->debug('Error in HttpAuthModule::getRewriteRules: ' . $ret->getAsText()); return $rules; } if (!$isCompatible) { return $rules; } - list ($ret, $activeRules) = $rewriteApi->fetchActiveRulesForModule($this->getId()); + list ($ret, $activeRules) = $rewriteApi->fetchActiveRulesForModule('httpauth'); if ($ret) { - if ($gallery->getDebug()) { - $gallery->debug('Error in HttpAuthModule::getRewriteRules: ' . $ret->getAsText()); - } + $gallery->debug('Error in HttpAuthModule::getRewriteRules: ' . $ret->getAsText()); return $rules; } if (!in_array('authorization', $activeRules)) { @@ -287,10 +275,7 @@ GalleryCoreApi::requireOnce('modules/httpauth/classes/HttpAuthHelper.class'); list ($ret, $success) = HttpAuthHelper::checkHttpAuth(); if ($ret) { - if ($gallery->getDebug()) { - $gallery->debug( - 'Error in HttpAuthModule::getRewriteRules: ' . $ret->getAsText()); - } + $gallery->debug('Error in HttpAuthModule::getRewriteRules: ' . $ret->getAsText()); return $rules; } if ($success) { @@ -303,25 +288,41 @@ 'comment' => $this->translate('Authorization Header'), 'help' => $this->translate( 'Pass the Authorization header to Gallery in a request variable.'), - 'conditions' => array(array('test' => 'HTTP:Authorization', - 'pattern' => '%authorization%'), - array('test' => 'QUERY_STRING', - 'pattern' => '!' . GalleryUtilities::prefixFormVariable( - 'authorization') . '=')), - 'options' => array('baseUrl' => '%{REQUEST_URI}'), - 'flags' => array('QSA'), 'keywords' => array( 'authorization' => array( 'pattern' => '(.+)', - 'help' => $this->translate('Authorization header.')))); + 'help' => $this->translate('Authorization header.'))), + 'conditions' => array( + array('test' => 'HTTP:Authorization', + 'pattern' => '%authorization%'), + array('test' => 'QUERY_STRING', + 'pattern' => '!' . GalleryUtilities::prefixFormVariable('authorization') + . '=')), + 'options' => array('baseUrl' => '%{REQUEST_URI}'), + 'flags' => array('QSA')); return $rules; } /** + * Is this event listener appropriate at this time? + * @return array $eventName => boolean + * @static + */ + function &_isAppropriate() { + static $isAppropriate; + return $isAppropriate; + } + + /** * @see GalleryEventListener::handleEvent */ function handleEvent($event) { + $isAppropriate = HttpAuthModule::_isAppropriate(); + if (empty($isAppropriate[$event->getEventName()]) && GalleryUtilities::isTest()) { + return array(null, null); + } + switch ($event->getEventName()) { case 'Gallery::ActivatePlugin': $data = $event->getData(); @@ -347,7 +348,6 @@ if ($ret) { return array($ret, null); } - return array(null, array('errorHandled' => true)); case 'Gallery::Logout': @@ -361,6 +361,8 @@ if (!empty($username) || !empty($remoteUser)) { return array(null, array('delegate' => array('view' => 'httpauth.TryLogout'))); } + + return array(null, null); } return array(null, null); @@ -387,7 +389,7 @@ return GalleryCoreApi::error(ERROR_CONFIGURATION_REQUIRED); } - list ($ret, $success) = $rewriteApi->activateRewriteRulesForModule($this->getId()); + list ($ret, $success) = $rewriteApi->activateRewriteRulesForModule('httpauth'); if ($ret) { return $ret; } Modified: branches/DEV_2_3/gallery2/modules/httpauth/templates/HttpAuthSiteAdmin.tpl =================================================================== --- branches/DEV_2_3/gallery2/modules/httpauth/templates/HttpAuthSiteAdmin.tpl 2007-07-25 22:56:15 UTC (rev 16789) +++ branches/DEV_2_3/gallery2/modules/httpauth/templates/HttpAuthSiteAdmin.tpl 2007-07-25 23:27:22 UTC (rev 16790) @@ -39,7 +39,7 @@ <p class="giDescription"> {capture assign="adminPluginsUrl"}{g->url arg1="view=core.SiteAdmin" arg2="subView=core.AdminPlugins" return=true}{/capture} - {g->text text="PHP Path Info rewrite doesn't support the rule to fall back on passing HTTP usernames and passwords to Gallery. You should uninstall and reinstall the URL rewrite module in the %sSite Admin Plugins option%s and choose either Apache mod_rewrite or ISAPI_Rewrite. Troubleshooting documentation is in the %sGallery Codex%s." arg1="<a href=\"$adminPluginsUrl\">" arg2="</a>" arg3="<a href=\"http://codex.gallery2.org/index.php/Gallery2:Modules:httpauth\">" arg4="</a>"} + {g->text text="PHP path info doesn't support the rule to fall back on passing HTTP usernames and passwords to Gallery. You should uninstall and reinstall the URL rewrite module in the %sSite Admin Plugins option%s and choose either Apache mod_rewrite or ISAPI_Rewrite. Troubleshooting documentation is in the %sGallery Codex%s." arg1="<a href=\"$adminPluginsUrl\">" arg2="</a>" arg3="<a href=\"http://codex.gallery2.org/index.php/Gallery2:Modules:httpauth\">" arg4="</a>"} </p> </div> {/if} @@ -125,7 +125,7 @@ <div class="gbBlock" id="cbUsernameRegex" {if empty($form.regexAuthPlugin)} style="display: none"{/if}> <p class="giDescription"> - {g->text text="Specify here a regular expression which the username must match for authentication to proceed and a string with which to replace it. See PHP %s documentation for more information." arg1="<a href=\"http://php.net/preg_replace\"> preg_replace </a>"} + {g->text text="Specify here a regular expression which the username must match for authentication to proceed and a string with which to replace it. See PHP %spreg_replace%s documentation." arg1="<a href=\"http://php.net/preg_replace\">" arg2="</a>"} </p> {g->text text="Username Pattern:"} Modified: branches/DEV_2_3/gallery2/modules/httpauth/templates/TryLogout.tpl =================================================================== --- branches/DEV_2_3/gallery2/modules/httpauth/templates/TryLogout.tpl 2007-07-25 22:56:15 UTC (rev 16789) +++ branches/DEV_2_3/gallery2/modules/httpauth/templates/TryLogout.tpl 2007-07-25 23:27:22 UTC (rev 16790) @@ -18,6 +18,6 @@ <div class="gbBlock"> <p class="giDescription"> - {g->text text="If you're not automatically redirected, %sclick here to finish logging out%s." arg1="<a href=\"`$TryLogout.hrefUrl`\">" arg2="</a>"} + <h3><a href="{$TryLogout.hrefUrl}"> {g->text text="Finish Logout"} </a></h3> </p> </div> Modified: branches/DEV_2_3/gallery2/modules/httpauth/test/phpunit/HttpAuthHelperTest.class =================================================================== --- branches/DEV_2_3/gallery2/modules/httpauth/test/phpunit/HttpAuthHelperTest.class 2007-07-25 22:56:15 UTC (rev 16789) +++ branches/DEV_2_3/gallery2/modules/httpauth/test/phpunit/HttpAuthHelperTest.class 2007-07-25 23:27:22 UTC (rev 16790) @@ -46,16 +46,16 @@ GalleryDataCache::put('GalleryPluginHelper::fetchPluginList(module)', $moduleStatus); /* Mock preGallery rewrite parser. Mock activate HTTP auth rewrite rules. */ - $this->_parserType =& HttpAuthHelperTestRewriteApi::_getParserType(); + $this->_parserType =& HttpAuthHelperTestRewriteApi::_parserType(); $this->_parserType = 'preGallery'; - $this->_activeRules =& HttpAuthHelperTestRewriteApi::_fetchActiveRulesForModule('httpauth'); - $this->_activeRules = array('authorization'); + $this->_activeRules =& HttpAuthHelperTestRewriteApi::_activeRules(); + $this->_activeRules = array(array('authorization')); $this->_markFactoryForCleanup(); $this->_registerFactoryImplementationForTest( 'RewriteApi', 'HttpAuthHelperTestRewriteApi', 'HttpAuthHelperTestRewriteApi', 'modules/httpauth/test/phpunit/HttpAuthHelperTest.class', 'test'); - /* Define HTTP auth works request */ + /* Define requests */ $this->_httpAuthWorksRequest = 'GET ' . $urlGenerator->generateUrl( array('view' => 'httpauth.HttpAuthWorks'), array('forceServerRelativeUrl' => true, @@ -87,22 +87,25 @@ . "\n\n\n\n"; /* Use HttpAuthHelperTestPlatform */ + $this->_platform =& $gallery->getPlatform(); $this->_platform = new HttpAuthHelperTestPlatform(); $this->_platform->_messages[$this->_httpAuthWorksRequest] = $this->_accessAuthorizationResponse; - $gallery->setPlatform($this->_platform); $ret = $this->_markPluginParametersForCleanup('module', 'httpauth'); if ($ret) { return $ret; } - /* Configure plugin */ - foreach (array('httpAuthPlugin' => true, 'regexAuthPlugin' => true, - 'authtypePattern' => '//', 'usernameReplace' => '$2', + /* Set plugin parameters */ + foreach (array('httpAuthPlugin' => true, + 'authName' => 'TEST_AUTH_NAME', + 'serverAuthPlugin' => false, + 'regexAuthPlugin' => true, + 'authtypePattern' => '//', 'usernamePattern' => '/^(.+\\\\)?([^\\\\@]+)(@.+)?$/', - 'serverAuthPlugin' => false, 'useGlobally' => true, - 'authName' => 'TEST_AUTH_NAME') as $key => $value) { + 'usernameReplace' => '$2', + 'useGlobally' => true) as $key => $value) { $ret = GalleryCoreApi::setPluginParameter('module', 'httpauth', $key, $value); if ($ret) { return $ret; @@ -116,60 +119,66 @@ } $this->_markForCleanup($this->_user); - list ($ret, $this->_authInterface) = - GalleryCoreApi::newFactoryInstance('HttpAuthInterface_1_0'); + list ($ret, $this->_authInterface) = GalleryCoreApi::newFactoryInstance( + 'HttpAuthInterface_1_0'); if ($ret) { return $ret; } - $this->assert(isset($this->_authInterface), 'Could not instantiate auth interface.'); + $this->assert(isset($this->_authInterface), 'Could not instantiate HTTP auth interface'); - /* Use HttpAuthModuleTestPhpVm */ + /* Use HttpAuthHelperTestPhpVm */ + $this->_phpVm =& $gallery->getPhpVm(); $this->_phpVm = new HttpAuthHelperTestPhpVm(); - $gallery->_phpVm =& $this->_phpVm; - /* Clear response headers */ - $this->_headers =& GalleryUtilities::_getResponseHeaders(); - $this->_headers = array(); + /* Reset response headers */ + $this->_responseHeaders =& GalleryUtilities::_getResponseHeaders(); + $this->_responseHeaders = null; + /* Use HttpAuthHelperTestSession */ $this->_saveSession = $gallery->_session; + $this->_session =& $gallery->getSession(); + $this->_session = new HttpAuthHelperTestSession(); $_SERVER['AUTH_TYPE'] = $_SERVER['PHP_AUTH_USER'] = $_SERVER['PHP_AUTH_PW'] = null; - GalleryUtilities::removeRequestVariable('authorization'); $_SERVER['HTTP_AUTHORIZATION'] = null; + GalleryUtilities::removeRequestVariable('authorization'); } function tearDown() { global $gallery; - GalleryDataCache::reset(); $gallery->_session = $this->_saveSession; + GalleryDataCache::reset(); $ret = parent::tearDown(); if ($ret) { $this->failWithStatus($ret); } - if (!empty($this->_resetFactory)) { - /* After the plugin parameters have been reset, reset the factory as well */ - list ($ret, $module) = GalleryCoreApi::loadPlugin('module', 'httpauth'); - if ($ret) { - $this->failWithStatus($ret); - } else { - $ret = GalleryCoreApi::unregisterFactoryImplementationsByModuleId('httpauth'); - if ($ret) { - $this->failWithStatus($ret); - } - $ret = $module->performFactoryRegistrations(); - if ($ret) { - $this->failWithStatus($ret); - } - } + if (empty($this->_resetFactory)) { + return null; + } - $storage =& $gallery->getStorage(); - $ret = $storage->commitTransaction(); - if ($ret) { - $this->failWithStatus($ret); - } + /* After plugin parameters have been reset, reset the factory as well */ + list ($ret, $module) = GalleryCoreApi::loadPlugin('module', 'httpauth'); + if ($ret) { + return $ret; } + + $ret = GalleryCoreApi::unregisterFactoryImplementationsByModuleId('httpauth'); + if ($ret) { + return $ret; + } + + $ret = $module->performFactoryRegistrations(); + if ($ret) { + return $ret; + } + + $storage =& $gallery->getStorage(); + $ret = $storage->commitTransaction(); + if ($ret) { + return $ret; + } } function testCheckConfiguration() { @@ -213,7 +222,7 @@ function testCheckConfigurationAuthorizationRuleDisabled() { $this->_platform->_messages[$this->_httpAuthWorksRequest] = $this->_missingAuthorizationResponse; - $this->_activeRules = array(); + $this->_activeRules = array(null, array()); list ($ret, $code) = HttpAuthHelper::checkConfiguration(); if ($ret) { @@ -300,11 +309,10 @@ } function testGetHttpAuthFromRequestVariable() { + $_SERVER['HTTP_AUTHORIZATION'] = 'OTHER ' . base64_encode('NOUSER:NOPASS'); GalleryUtilities::putRequestVariable( 'authorization', 'AUTHTYPE ' . base64_encode('USERNAME:PASSWORD')); - $_SERVER['HTTP_AUTHORIZATION'] = 'OTHER ' . base64_encode('NOUSER:NOPASS'); - list ($authtype, $username, $password) = HttpAuthHelper::getHttpAuth(); $this->assertEquals('AUTHTYPE', $authtype); $this->assertEquals('USERNAME', $username); @@ -335,8 +343,8 @@ $_SERVER['PHP_AUTH_PW'] = 'PASSWORD'; list ($authtype, $username, $password) = HttpAuthHelper::getHttpAuth(); - $this->assertEquals(null, $authtype, 'auth type'); - $this->assertEquals('', $username, 'user name'); + $this->assertEquals(null, $authtype, 'authtype'); + $this->assertEquals('', $username, 'username'); $this->assertEquals('PASSWORD', $password, 'password'); } @@ -409,8 +417,8 @@ } function testGetUserUsernameReplace() { - list ($ret, $user) = - HttpAuthHelper::getUser('Test', $this->_user->getUserName() . '@REALM.TLD'); + list ($ret, $user) = HttpAuthHelper::getUser( + 'Test', $this->_user->getUserName() . '@REALM.TLD'); if ($ret) { return $ret; } @@ -433,55 +441,58 @@ function testAddHttpAuthToUrl() { $url = 'http://www.example.com/gallery2/main.php?foo=bar&view=baz#some'; - $this->assertEquals('http://FAKEUSER:FAK...@ww.../gallery2/' . - 'main.php?foo=bar&view=baz#some', - HttpAuthHelper::addHttpAuthToUrl($url, 'FAKEUSER', 'FAKEPASS'), - 'URL with all components'); + $this->assertEquals( + 'http://FAKEUSER:FAK...@ww.../gallery2/main.php?foo=bar&view=baz#some', + HttpAuthHelper::addHttpAuthToUrl($url, 'FAKEUSER', 'FAKEPASS'), + 'URL with all components'); $url = 'webdav://www.example.com/gallery2/main.php?foo=bar&view=baz#some'; - $this->assertEquals('webdav://FAKEUSER:FAK...@ww.../gallery2/' . - 'main.php?foo=bar&view=baz#some', - HttpAuthHelper::addHttpAuthToUrl($url, 'FAKEUSER', 'FAKEPASS'), - 'non-standard protocol, no HTML entities'); + $this->assertEquals( + 'webdav://FAKEUSER:FAK...@ww.../gallery2/main.php?foo=bar&view=baz#some', + HttpAuthHelper::addHttpAuthToUrl($url, 'FAKEUSER', 'FAKEPASS'), + 'Non-standard protocol, no HTML entities'); $url = 'https://www.example.com/gallery2/main.php'; - $this->assertEquals('https://FAKEUSER:FAK...@ww.../gallery2/main.php', - HttpAuthHelper::addHttpAuthToUrl($url, 'FAKEUSER', 'FAKEPASS'), - 'no query string, anchor'); + $this->assertEquals( + 'https://FAKEUSER:FAK...@ww.../gallery2/main.php', + HttpAuthHelper::addHttpAuthToUrl($url, 'FAKEUSER', 'FAKEPASS'), + 'No query string, anchor'); $url = 'http://OLDUSER:OL...@ex.../gallery2/main.php?foo=bar&view=baz#some'; - $this->assertEquals('http://FAKEUSER:FAK...@ex.../gallery2/' . - 'main.php?foo=bar&view=baz#some', - HttpAuthHelper::addHttpAuthToUrl($url, 'FAKEUSER', 'FAKEPASS'), - 'URL with existing user:pass'); + $this->assertEquals( + 'http://FAKEUSER:FAK...@ex.../gallery2/main.php?foo=bar&view=baz#some', + HttpAuthHelper::addHttpAuthToUrl($url, 'FAKEUSER', 'FAKEPASS'), + 'URL with existing user:pass'); } function testStripHttpAuthFromUrl() { $url = 'http://OLDUSER:OL...@ex.../gallery2/main.php?foo=bar&view=baz#some'; - $this->assertEquals('http://example.com/gallery2/main.php?foo=bar&view=baz#some', - HttpAuthHelper::stripHttpAuthFromUrl($url), - 'URL with user:pass'); + $this->assertEquals( + 'http://example.com/gallery2/main.php?foo=bar&view=baz#some', + HttpAuthHelper::stripHttpAuthFromUrl($url), + 'URL with user:pass'); $url = 'http://www.example.com/gallery2/main.php?foo=bar&view=baz#some'; - $this->assertEquals('http://www.example.com/gallery2/main.php?foo=bar&view=baz#some', - HttpAuthHelper::stripHttpAuthFromUrl($url), - 'URL without user:pass'); + $this->assertEquals( + 'http://www.example.com/gallery2/main.php?foo=bar&view=baz#some', + HttpAuthHelper::stripHttpAuthFromUrl($url), + 'URL without user:pass'); $url = 'webdav://OLDUSER:OL...@ex.../gallery2/main.php?foo=bar&view=baz'; $this->assertEquals('webdav://example.com/gallery2/main.php?foo=bar&view=baz', - HttpAuthHelper::stripHttpAuthFromUrl($url), - 'URL without HTML entities, anchor, other protocol'); + HttpAuthHelper::stripHttpAuthFromUrl($url), + 'URL without HTML entities, anchor, other protocol'); } function testGetConfiguration() { - list ($ret, $authPluginEnabled, $serverPluginEnabled, $usedGlobally) = - $this->_authInterface->getConfiguration(); + list ($ret, $authPluginEnabled, $serverPluginEnabled, $globallyEnabled) = + $this->_authInterface->getConfiguration(); if ($ret) { return $ret; } $this->assertEquals(array(true, false, true), - array($authPluginEnabled, $serverPluginEnabled, $usedGlobally)); + array($authPluginEnabled, $serverPluginEnabled, $globallyEnabled)); } function testSetConfiguration() { @@ -496,14 +507,15 @@ return $ret; } - $this->assert(!(bool)$params['httpAuthPlugin'], 'wrong httpAuthPlugin value'); - $this->assert((bool)$params['serverAuthPlugin'], 'wrong serverAuthPlugin value'); - $this->assert(!(bool)$params['useGlobally'], 'wrong useGlobally value'); - list ($ret, $authPlugins) = - GalleryCoreApi::getAllFactoryImplementationIds('GalleryAuthPlugin'); + $this->assert(empty($params['httpAuthPlugin']), 'Wrong httpAuthPlugin value'); + $this->assert(!empty($params['serverAuthPlugin']), 'Wrong serverAuthPlugin value'); + $this->assert(empty($params['useGlobally']), 'Wrong useGlobally value'); + list ($ret, $authPlugins) = GalleryCoreApi::getAllFactoryImplementationIds( + 'GalleryAuthPlugin'); if ($ret) { return $ret; } + $this->assert(!isset($authPlugins['HttpAuthPlugin']), 'HttpAuthPlugin still registerd'); $this->assert(isset($authPlugins['ServerAuthPlugin']), 'ServerAuthPlugin not registered'); } @@ -520,9 +532,9 @@ return $ret; } - $this->assert((bool)$params['httpAuthPlugin'], 'wrong httpAuthPlugin value'); - $this->assert(!(bool)$params['serverAuthPlugin'], 'wrong serverAuthPlugin value'); - $this->assert(!(bool)$params['useGlobally'], 'wrong useGlobally value'); + $this->assert(!empty($params['httpAuthPlugin']), 'Wrong httpAuthPlugin value'); + $this->assert(empty($params['serverAuthPlugin']), 'Wrong serverAuthPlugin value'); + $this->assert(empty($params['useGlobally']), 'Wrong useGlobally value'); } function testRequestAuthentication() { @@ -536,12 +548,13 @@ return $ret; } - $this->assertEquals(array('status' => 'HTTP/1.0 401 Unauthorized', - 'www-authenticate' => "WWW-Authenticate: Basic realm='TEST_AUTH_NAME'"), - $this->_headers); + $expectedHeaders = array(); + $expectedHeaders['status'] = 'HTTP/1.0 401 Unauthorized'; + $expectedHeaders['www-authenticate'] = 'WWW-Authenticate: Basic realm=\'TEST_AUTH_NAME\''; + $this->assertEquals($expectedHeaders, $this->_responseHeaders); } - function testRequestAuthenticationDoNotIgnoreUseGloballyFlag() { + function testRequestAuthenticationRespectsUseGloballyFlag() { $ret = GalleryCoreApi::setPluginParameter('module', 'httpauth', 'useGlobally', false); if ($ret) { return $ret; @@ -552,7 +565,7 @@ return $ret; } - $this->assertEquals(array(), $this->_headers); + $this->assertEquals(array(), $this->_responseHeaders); } function testRequestAuthenticationIgnoredIfHttpAuthNotEnabled() { @@ -568,7 +581,7 @@ return $ret; } - $this->assertEquals(array(), $this->_headers); + $this->assertEquals(array(), $this->_responseHeaders); } function testRequestAuthenticationAlsoSentWhenAlreadyLoggedIn() { @@ -579,69 +592,60 @@ return $ret; } - $this->assertEquals(array('status' => 'HTTP/1.0 401 Unauthorized', - 'www-authenticate' => "WWW-Authenticate: Basic realm='TEST_AUTH_NAME'"), - $this->_headers); + $expectedHeaders = array(); + $expectedHeaders['status'] = 'HTTP/1.0 401 Unauthorized'; + $expectedHeaders['www-authenticate'] = 'WWW-Authenticate: Basic realm=\'TEST_AUTH_NAME\''; + $this->assertEquals($expectedHeaders, $this->_responseHeaders); } function testRegenerateSessionIfNecessary() { - global $gallery; - $gallery->_session = new HttpAuthHelperTestSession(); - $ret = HttpAuthHelper::regenerateSessionIfNecessary($this->_user); if ($ret) { return $ret; } - $this->assert($gallery->_session->regenerateWasCalled()); + $this->assertEquals(array(array()), $this->_session->_regenerateCalls); } function testRegenerateSessionIfNecessarySameUserAsFromSession() { - global $gallery; - $gallery->_session = new HttpAuthHelperTestSession($this->_user->getId()); - + $this->_session->_userId = $this->_user->getId(); $ret = HttpAuthHelper::regenerateSessionIfNecessary($this->_user); if ($ret) { return $ret; } - $this->assert(!$gallery->_session->regenerateWasCalled()); + $this->assertEquals(null, $this->_session->regenerateCalls); } function testRegenerateSessionIfNecessaryOtherUserThanFromSession() { - global $gallery; - $gallery->_session = new HttpAuthHelperTestSession('OTHER_USER'); - + $this->_session->_userId = 'OTHER_USER'; $ret = HttpAuthHelper::regenerateSessionIfNecessary($this->_user); if ($ret) { return $ret; } - $this->assert($gallery->_session->regenerateWasCalled()); + $this->assertEquals(array(array()), $this->_session->_regenerateCalls); } function testRegenerateSessionIfNecessaryNoAuthenticatedUser() { - global $gallery; - $gallery->_session = new HttpAuthHelperTestSession($this->_user->getId()); - + $this->_session->_userId = $this->_user->getId(); $ret = HttpAuthHelper::regenerateSessionIfNecessary(null); if ($ret) { return $ret; } - $this->assert(!$gallery->_session->regenerateWasCalled()); + $this->assertEquals(null, $this->_session->_regenerateCalls); } } class HttpAuthHelperTestRewriteApi { - var $foo; - function &_fetchActiveRulesForModule($moduleId) { + function &_activeRules() { static $activeRules; return $activeRules; } - function &_getParserType() { + function &_parserType() { static $parserType; return $parserType; } @@ -651,11 +655,11 @@ } function fetchActiveRulesForModule($moduleId) { - return array(null, HttpAuthHelperTestRewriteApi::_fetchActiveRulesForModule($moduleId)); + return HttpAuthHelperTestRewriteApi::_activeRules(); } function getParserType() { - return HttpAuthHelperTestRewriteApi::_getParserType(); + return HttpAuthHelperTestRewriteApi::_parserType(); } } @@ -712,27 +716,22 @@ } class HttpAuthHelperTestPhpVm extends GalleryPhpVm { + function header($header, $replace=null) { /* Avoid modifying actual header information */ } } class HttpAuthHelperTestSession { - function HttpAuthHelperTestSession($userId=null) { - $this->_userId = $userId; - $this->_regenerateWasCalled = false; - } + var $_regenerateCalls; + var $_userId; function getUserId() { return $this->_userId; } function regenerate() { - $this->_regenerateWasCalled = true; + $this->_regenerateCalls[] = array(); } - - function regenerateWasCalled() { - return $this->_regenerateWasCalled; - } } ?> Modified: branches/DEV_2_3/gallery2/modules/httpauth/test/phpunit/HttpAuthModuleTest.class =================================================================== --- branches/DEV_2_3/gallery2/modules/httpauth/test/phpunit/HttpAuthModuleTest.class 2007-07-25 22:56:15 UTC (rev 16789) +++ branches/DEV_2_3/gallery2/modules/httpauth/test/phpunit/HttpAuthModuleTest.class 2007-07-25 23:27:22 UTC (rev 16790) @@ -86,30 +86,31 @@ '')) . "\n\n\n\n"; /* Use HttpAuthModuleTestPlatform */ - $this->_platform =& new HttpAuthModuleTestPlatform(); + $this->_platform =& $gallery->getPlatform(); + $this->_platform = new HttpAuthModuleTestPlatform(); $this->_platform->_messages[$this->_httpAuthWorksRequest] = $this->_accessAuthorizationResponse; - $gallery->setPlatform($this->_platform); $ret = $this->_markPluginParametersForCleanup('module', 'httpauth'); if ($ret) { return $ret; } + /* Set plugin parameters */ foreach (array('httpAuthPlugin' => true, 'useGlobally' => true) as $key => $value) { - $ret = GalleryCoreApi::setPluginParameter('module', 'httpauth', $key, $value); + $ret = $this->_module->setParameter($key, $value); if ($ret) { return $ret; } } /* Use HttpAuthModuleTestPhpVm */ + $this->_phpVm =& $gallery->getPhpVm(); $this->_phpVm = new HttpAuthModuleTestPhpVm(); - $gallery->_phpVm =& $this->_phpVm; - /* Clear response headers */ - $this->_headers =& GalleryUtilities::_getResponseHeaders(); - $this->_headers = array(); + /* Reset response headers */ + $this->_responseHeaders =& GalleryUtilities::_getResponseHeaders(); + $this->_responseHeaders = array(); /* Reset auth related server and request variables */ unset($_SERVER['PHP_AUTH_USER']); @@ -126,7 +127,7 @@ GalleryDataCache::reset(); $ret = parent::tearDown(); if ($ret) { - $this->failWithStatus($ret); + return $ret; } } @@ -176,9 +177,10 @@ return $ret; } - $this->assertEquals(array('status' => 'HTTP/1.0 401 Unauthorized', - 'www-authenticate' => "WWW-Authenticate: Basic realm='$params[authName]'"), - $this->_headers); + $expectedHeaders = array(); + $expectedHeaders['status'] = 'HTTP/1.0 401 Unauthorized'; + $expectedHeaders['www-authenticate'] = "WWW-Authenticate: Basic realm='$params[authName]'"; + $this->assertEquals($expectedHeaders, $this->_responseHeaders); } function testHandleEventErrorPermissionDeniedIgnoredIfUseGloballyDisabled() { @@ -195,7 +197,7 @@ return $ret; } - $this->assertEquals(array(), $this->_headers); + $this->assertEquals(array(), $this->_responseHeaders); } function testHandleEventLogoutNoUsername() { @@ -255,7 +257,6 @@ } class HttpAuthModuleTestRewriteApi { - var $foo; function &_fetchActiveRulesForModule($moduleId) { static $activeRules; Modified: branches/DEV_2_3/gallery2/modules/httpauth/test/phpunit/HttpAuthPluginTest.class =================================================================== --- branches/DEV_2_3/gallery2/modules/httpauth/test/phpunit/HttpAuthPluginTest.class 2007-07-25 22:56:15 UTC (rev 16789) +++ branches/DEV_2_3/gallery2/modules/httpauth/test/phpunit/HttpAuthPluginTest.class 2007-07-25 23:27:22 UTC (rev 16790) @@ -46,15 +46,24 @@ return $ret; } $this->_markForCleanup($this->_user); + $_SERVER['AUTH_TYPE'] = 'Test'; $this->_plugin = new HttpAuthPlugin(); - $this->_saveSession = $gallery->_session; - $gallery->_session = new HttpAuthPluginTestSession(); + /* Use HttpAuthPluginTestSession */ + $this->_saveSession = $gallery->_session; + $this->_session =& $gallery->getSession(); + $this->_session = new HttpAuthPluginTestSession(); - $this->_listener = new NullEventListener(); - $this->_registerTestEventListener('Gallery::FailedLogin', $this->_listener); + /* Use NullEventListener */ + $this->_event =& NullEventListener::_event(); + $this->_event = null; + + $this->_markFactoryForCleanup(); + $this->_registerFactoryImplementationForTest('GalleryEventListener', + 'NullEventListener', 'NullEventListener', + 'lib/tools/phpunit/GalleryTestCase.class', 'test', array('Gallery::FailedLogin')); } function tearDown() { @@ -73,9 +82,6 @@ } function testValidUser() { - global $gallery; - $session =& $gallery->getSession(); - $_SERVER['PHP_AUTH_USER'] = $this->_user->getUserName(); $_SERVER['PHP_AUTH_PW'] = 'password'; @@ -85,8 +91,11 @@ } $this->assertEquals($this->_user, $user); - $this->assert(!isset($this->_listener->_event), 'There was a FailedLogin event!'); - $this->assert($session->regenerateWasCalled(), 'Session has not been regenerated!'); + + $this->assert(!isset($this->_event), 'There was a FailedLogin event!'); + + $this->assertEquals(array(array()), $this->_session->_regenerateCalls, + 'Session should have been regenerated'); } function testInvalidUser() { @@ -100,12 +109,12 @@ $this->assert(!isset($user)); - if (!isset($this->_listener->_event)) { - $this->assert(false, 'Listener didn\'t receive event!'); - } else { - $eventData = $this->_listener->_event->getData(); - $this->assertEquals($_SERVER['PHP_AUTH_USER'], $eventData['userName']); + if (!isset($this->_event)) { + return $this->fail('Should have handled an event'); } + + $data = $this->_event->getData(); + $this->assertEquals($_SERVER['PHP_AUTH_USER'], $data['userName']); } function testValidUserInvalidPassword() { @@ -119,12 +128,12 @@ $this->assert(!isset($user)); - if (!isset($this->_listener->_event)) { - $this->assert(false, 'Listener didn\'t receive event!'); - } else { - $eventData = $this->_listener->_event->getData(); - $this->assertEquals($this->_user->getUserName(), $eventData['userName']); + if (!isset($this->_event)) { + return $this->fail('Should have handled an event'); } + + $data = $this->_event->getData(); + $this->assertEquals($_SERVER['PHP_AUTH_USER'], $data['userName']); } function testNoFailedLoginEventForSpecialLogoutUsername() { @@ -137,28 +146,20 @@ } $this->assert(!isset($user)); - $this->assert(!isset($this->_listener->_event), 'There was a FailedLogin event!'); + + $this->assert(!isset($this->_event), 'There was a FailedLogin event!'); } } -/** - * Mock Session - */ class HttpAuthPluginTestSession { - function HttpAuthPluginTestSession() { - $this->_regenerateWasCalled = false; - } + var $_regenerateCalls; function getUserId() { return null; } function regenerate() { - $this->_regenerateWasCalled = true; + $this->_regenerateCalls[] = array(); } - - function regenerateWasCalled() { - return $this->_regenerateWasCalled; - } } ?> Modified: branches/DEV_2_3/gallery2/modules/httpauth/test/phpunit/ServerAuthPluginTest.class =================================================================== --- branches/DEV_2_3/gallery2/modules/httpauth/test/phpunit/ServerAuthPluginTest.class 2007-07-25 22:56:15 UTC (rev 16789) +++ branches/DEV_2_3/gallery2/modules/httpauth/test/phpunit/ServerAuthPluginTest.class 2007-07-25 23:27:22 UTC (rev 16790) @@ -34,11 +34,11 @@ } function setUp() { - global $gallery; $ret = parent::setUp(); if ($ret) { return $ret; } + global $gallery; /* Create test user */ list ($ret, $this->_user) = $this->_createRandomUser(); @@ -51,7 +51,8 @@ $this->_plugin = new ServerAuthPlugin(); $this->_saveSession = $gallery->_session; - $gallery->_session = new ServerAuthPluginTestSession(); + $this->_session =& $gallery->getSession(); + $this->_session = new ServerAuthPluginTestSession(); } function tearDown() { @@ -65,23 +66,20 @@ $ret = parent::tearDown(); if ($ret) { - $this->failWithStatus($ret); + return $ret; } } function testValidUser() { - global $gallery; - $session =& $gallery->getSession(); - $_SERVER['REMOTE_USER'] = $this->_user->getUserName(); list ($ret, $user) = $this->_plugin->getUser(); if ($ret) { - return ($ret); + return $ret; } $this->assertEquals($this->_user, $user); - $this->assert($session->regenerateWasCalled(), 'Session has not been regenerated!'); + $this->assert($this->_session->regenerateWasCalled(), 'Session has not been regenerated!'); } function testInvalidUser() { @@ -89,17 +87,15 @@ list ($ret, $user) = $this->_plugin->getUser(); if ($ret) { - return ($ret); + return $ret; } $this->assertEquals(null, $user); } } -/** - * Mock Session - */ class ServerAuthPluginTestSession { + function ServerAuthPluginTestSession() { $this->_regenerateWasCalled = false; } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |