2018-10-17 08:19:58 -04:00
|
|
|
<?php
|
2019-10-10 19:21:41 -04:00
|
|
|
|
2018-10-17 08:19:58 -04:00
|
|
|
/**
|
|
|
|
* @file /src/Core/Authentication.php
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Friendica\Core;
|
|
|
|
|
2019-05-13 01:36:09 -04:00
|
|
|
use Friendica\App;
|
2018-10-17 08:19:58 -04:00
|
|
|
use Friendica\BaseObject;
|
2019-07-23 20:03:08 -04:00
|
|
|
use Friendica\Network\HTTPException\ForbiddenException;
|
2018-10-17 08:19:58 -04:00
|
|
|
|
|
|
|
/**
|
2019-10-10 19:21:41 -04:00
|
|
|
* Handle Authentification, Session and Cookies
|
|
|
|
*/
|
2018-10-17 08:19:58 -04:00
|
|
|
class Authentication extends BaseObject
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @brief Calculate the hash that is needed for the "Friendica" cookie
|
|
|
|
*
|
|
|
|
* @param array $user Record from "user" table
|
|
|
|
*
|
|
|
|
* @return string Hashed data
|
2019-01-06 16:06:53 -05:00
|
|
|
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
|
2018-10-17 08:19:58 -04:00
|
|
|
*/
|
2018-10-17 15:30:41 -04:00
|
|
|
public static function getCookieHashForUser($user)
|
2018-10-17 08:19:58 -04:00
|
|
|
{
|
2019-10-10 19:21:41 -04:00
|
|
|
return hash_hmac(
|
|
|
|
"sha256",
|
|
|
|
hash_hmac("sha256", $user["password"], $user["privkey"]),
|
|
|
|
Config::get("system", "site_prvkey")
|
|
|
|
);
|
2018-10-17 08:19:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Set the "Friendica" cookie
|
|
|
|
*
|
2019-01-06 16:06:53 -05:00
|
|
|
* @param int $time
|
2018-10-17 08:19:58 -04:00
|
|
|
* @param array $user Record from "user" table
|
2019-01-06 16:06:53 -05:00
|
|
|
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
|
2018-10-17 08:19:58 -04:00
|
|
|
*/
|
2018-10-17 15:30:41 -04:00
|
|
|
public static function setCookie($time, $user = [])
|
2018-10-17 08:19:58 -04:00
|
|
|
{
|
|
|
|
if ($time != 0) {
|
|
|
|
$time = $time + time();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($user) {
|
2019-10-10 19:21:41 -04:00
|
|
|
$value = json_encode([
|
|
|
|
"uid" => $user["uid"],
|
2018-10-17 15:30:41 -04:00
|
|
|
"hash" => self::getCookieHashForUser($user),
|
2019-10-10 19:21:41 -04:00
|
|
|
"ip" => defaults($_SERVER, 'REMOTE_ADDR', '0.0.0.0')
|
|
|
|
]);
|
2018-10-17 08:19:58 -04:00
|
|
|
} else {
|
|
|
|
$value = "";
|
|
|
|
}
|
|
|
|
|
2019-08-15 11:23:35 -04:00
|
|
|
setcookie("Friendica", $value, $time, "/", "", (Config::get('system', 'ssl_policy') == App\BaseURL::SSL_POLICY_FULL), true);
|
2018-10-17 08:19:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Kills the "Friendica" cookie and all session data
|
|
|
|
*/
|
2018-10-17 15:30:41 -04:00
|
|
|
public static function deleteSession()
|
2018-10-17 08:19:58 -04:00
|
|
|
{
|
2018-10-17 15:30:41 -04:00
|
|
|
self::setCookie(-3600); // make sure cookie is deleted on browser close, as a security measure
|
2018-10-17 08:19:58 -04:00
|
|
|
session_unset();
|
|
|
|
session_destroy();
|
|
|
|
}
|
2019-05-13 01:36:09 -04:00
|
|
|
|
|
|
|
public static function twoFactorCheck($uid, App $a)
|
|
|
|
{
|
|
|
|
// Check user setting, if 2FA disabled return
|
|
|
|
if (!PConfig::get($uid, '2fa', 'verified')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check current path, if 2fa authentication module return
|
2019-07-23 20:03:08 -04:00
|
|
|
if ($a->argc > 0 && in_array($a->argv[0], ['2fa', 'view', 'help', 'api', 'proxy', 'logout'])) {
|
2019-05-13 01:36:09 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Case 1: 2FA session present and valid: return
|
|
|
|
if (Session::get('2fa')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Case 2: No valid 2FA session: redirect to code verification page
|
2019-07-23 20:03:08 -04:00
|
|
|
if ($a->isAjax()) {
|
|
|
|
throw new ForbiddenException();
|
|
|
|
} else {
|
|
|
|
$a->internalRedirect('2fa');
|
|
|
|
}
|
2019-05-13 01:36:09 -04:00
|
|
|
}
|
2018-10-17 08:19:58 -04:00
|
|
|
}
|