Created
July 16, 2014 14:19
-
-
Save bradberger/4a1f05caa8cf51c30b64 to your computer and use it in GitHub Desktop.
Proper Anahita HTTP Authentication headers
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
// File anahita/anahita/src/plugins/system/anahita.php | |
/** | |
* Remebers handling | |
* | |
* @return void | |
*/ | |
public function onAfterInitialise() | |
{ | |
global $mainframe; | |
// No remember me for admin | |
if ($mainframe->isAdmin()) | |
return; | |
//if alredy logged in then forget it | |
if (!JFactory::getUser()->guest) | |
return; | |
jimport('joomla.utilities.utility'); | |
jimport('joomla.utilities.simplecrypt'); | |
$user = []; | |
$remember = JUtility::getHash('JLOGIN_REMEMBER'); | |
// for json requests obtain the username and password from the $_SERVER array | |
// else if the remember me cookie exists, decrypt and obtain the username and password from it | |
if (KRequest::format() == 'json') { | |
if (KRequest::has('server.PHP_AUTH_USER') && KRequest::has('server.PHP_AUTH_PW')) { | |
$user['username'] = KRequest::get('server.PHP_AUTH_USER', 'raw'); | |
$user['password'] = KRequest::get('server.PHP_AUTH_PW', 'raw'); | |
} else { | |
header('WWW-Authenticate: Basic realm="Anahita"'); | |
} | |
} else if (isset($_COOKIE[$remember]) && $_COOKIE[$remember] != '') { | |
$key = JUtility::getHash(KRequest::get('server.HTTP_USER_AGENT', 'raw')); | |
if ($key) { | |
$crypt = new JSimpleCrypt($key); | |
$cookie = $crypt->decrypt($_COOKIE[$remember]); | |
$user = (array)@unserialize($cookie); | |
} | |
} | |
if (!empty($user)) { | |
jimport('joomla.user.authentication'); | |
$authentication =& JAuthentication::getInstance(); | |
try { | |
$authResponse = $authentication->authenticate($user, []); | |
if ($authResponse->status === JAUTHENTICATE_STATUS_SUCCESS) { | |
KService::get('com://site/people.helper.person')->login($user, true); | |
} | |
} catch (RuntimeException $e) { | |
//only throws exception if we are using JSON format | |
//otherwise let the current app handle it | |
if (KRequest::format() == 'json') { | |
throw $e; | |
} | |
} | |
} | |
return; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment