You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
87 lines
2.4 KiB
PHTML
87 lines
2.4 KiB
PHTML
|
2 months ago
|
<?php
|
||
|
|
|
||
|
|
namespace App\Services\Auth;
|
||
|
|
|
||
|
|
use App\Models\User;
|
||
|
|
use App\Utils;
|
||
|
|
use App\Services\Config;
|
||
|
|
use App\Services\LoginSessionRevocationService;
|
||
|
|
|
||
|
|
class Cookie extends Base
|
||
|
|
{
|
||
|
|
public function login($uid, $time)
|
||
|
|
{
|
||
|
|
$user = User::find($uid);
|
||
|
|
if ($user === null) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
$sessionVersion = LoginSessionRevocationService::currentVersion($user);
|
||
|
|
$key = LoginSessionRevocationService::cookieKey($user->pass, $sessionVersion);
|
||
|
|
$expire_in = $time+time();
|
||
|
|
Utils\Cookie::set([
|
||
|
|
"uid" => $uid,
|
||
|
|
"email" => $user->email,
|
||
|
|
"key" => $key,
|
||
|
|
"ip" => md5($_SERVER["REMOTE_ADDR"].Config::get('key').$uid.$expire_in),
|
||
|
|
"expire_in" => $expire_in,
|
||
|
|
LoginSessionRevocationService::COOKIE_VERSION_NAME => $sessionVersion,
|
||
|
|
], $expire_in);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function getUser()
|
||
|
|
{
|
||
|
|
$uid = Utils\Cookie::get('uid');
|
||
|
|
$key = Utils\Cookie::get('key');
|
||
|
|
$ip = Utils\Cookie::get('ip');
|
||
|
|
$sessionVersion = Utils\Cookie::get(LoginSessionRevocationService::COOKIE_VERSION_NAME);
|
||
|
|
|
||
|
|
$expire_in = Utils\Cookie::get('expire_in');
|
||
|
|
|
||
|
|
if ($uid == null) {
|
||
|
|
$user = new User();
|
||
|
|
$user->isLogin = false;
|
||
|
|
return $user;
|
||
|
|
}
|
||
|
|
|
||
|
|
if ($ip != md5($_SERVER["REMOTE_ADDR"].Config::get('key').$uid.$expire_in) && Config::get('enable_login_bind_ip')=='true') {
|
||
|
|
$user = new User();
|
||
|
|
$user->isLogin = false;
|
||
|
|
return $user;
|
||
|
|
}
|
||
|
|
|
||
|
|
if ($expire_in<time()) {
|
||
|
|
$user = new User();
|
||
|
|
$user->isLogin = false;
|
||
|
|
return $user;
|
||
|
|
}
|
||
|
|
|
||
|
|
$user = User::find($uid);
|
||
|
|
if ($user == null) {
|
||
|
|
$user = new User();
|
||
|
|
$user->isLogin = false;
|
||
|
|
return $user;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (!LoginSessionRevocationService::cookieKeyMatches($user, $key, $sessionVersion)) {
|
||
|
|
$user->isLogin = false;
|
||
|
|
return $user;
|
||
|
|
}
|
||
|
|
$user->isLogin = true;
|
||
|
|
return $user;
|
||
|
|
}
|
||
|
|
|
||
|
|
public function logout()
|
||
|
|
{
|
||
|
|
$time = time() - 1000;
|
||
|
|
Utils\Cookie::set([
|
||
|
|
"uid" => null,
|
||
|
|
"email" => null,
|
||
|
|
"key" => null,
|
||
|
|
"ip" => null,
|
||
|
|
"expire_in" => null,
|
||
|
|
LoginSessionRevocationService::COOKIE_VERSION_NAME => null,
|
||
|
|
], $time);
|
||
|
|
}
|
||
|
|
}
|