Files
RomhackPlaza/app/Auth/XenForoGuard.php

193 lines
3.9 KiB
PHP
Raw Normal View History

2026-05-20 18:25:15 +02:00
<?php
namespace App\Auth;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Http\Request;
2026-06-23 19:24:38 +02:00
/**
* Xenforo authentification bridge.
*/
2026-05-20 18:25:15 +02:00
class XenForoGuard implements Guard
{
2026-06-23 19:24:38 +02:00
/**
* Authenticated user.
* @var XenForoUser|null
*/
2026-05-20 18:25:15 +02:00
private ?XenForoUser $user = null;
public function __construct(private readonly Request $request) {}
2026-06-23 19:24:38 +02:00
/**
* Check if user is logged in.
* @return bool
*/
2026-05-20 18:25:15 +02:00
public function check(): bool
{
return $this->user() !== null;
}
2026-06-23 19:24:38 +02:00
/**
* Check if user is a guest.
* @return bool
*/
2026-05-20 18:25:15 +02:00
public function guest(): bool
{
return ! $this->check();
}
2026-06-23 19:24:38 +02:00
/**
* Get user ID.
* @return mixed
*/
2026-05-20 18:25:15 +02:00
public function id(): mixed
{
return $this->user()?->getAuthIdentifier();
}
2026-06-23 19:24:38 +02:00
/**
* If user is defined.
* @return bool
*/
2026-05-20 18:25:15 +02:00
public function hasUser(): bool
{
return $this->user !== null;
}
2026-06-23 19:24:38 +02:00
/**
* Login user.
* @return XenForoUser|null
*/
2026-05-20 18:25:15 +02:00
public function user(): ?XenForoUser
{
if ($this->hasUser())
return $this->user;
$user = $this->getFromSession();
if( $user )
return $user;
$user = $this->getFromCookie();
if( $user )
return $user;
return null;
}
private function getFromSession(): ?XenForoUser
{
2026-05-20 18:25:15 +02:00
$sessionId = $this->request->cookie('xf_session');
if(!$sessionId)
return null;
$xfSession = \DB::connection('xenforo')
->table('session')
->where('session_id', $sessionId)
->value('session_data');
if(!$xfSession)
return null;
$sessionData = unserialize($xfSession);
if (!$sessionData || !isset($sessionData['userId']) || !$sessionData['userId'])
return null;
$xfUser = \DB::connection('xenforo')
->table('user')
->where('user_id', $sessionData['userId'])
->first();
if(!$xfUser)
return null;
return $this->user = new XenForoUser($xfUser);
}
private function isCorrectCookieKey(string $key, $record): bool
{
$known = $record->remember_key;
if( !$known )
return false;
$check = hash('sha256', $key, true);
return hash_equals($known, $check);
}
private function getFromCookie(): ?XenForoUser
{
$cookie = $this->request->cookie('xf_user');
if(!$cookie)
return null;
$parts = explode(',', $cookie);
if( count( $parts ) !== 2 )
return null;
[$userId, $key] = $parts;
$userId = (int) $userId;
if( !$userId || !$key )
return null;
$remembers = \DB::connection('xenforo')
->table('user_remember')
->where('user_id', $userId)
->get();
if( !$remembers )
return null;
$valid = false;
foreach( $remembers as $remember )
{
if( $this->isCorrectCookieKey($key, $remember) && $remember->expiry_date >= time() ){
$valid = true;
break;
}
}
if( !$valid )
return null;
$xfUser = \DB::connection('xenforo')
->table('user')
->where('user_id', $userId)
->first();
if(!$xfUser)
return null;
return $this->user = new XenForoUser($xfUser);
}
2026-06-23 19:24:38 +02:00
/**
* Unused.
*
* @param array $credentials
*
* @return bool
*/
2026-05-20 18:25:15 +02:00
public function validate(array $credentials = []): bool
{
return false;
}
public function setUser(mixed $user): void
{
$this->user = $user;
}
2026-06-23 19:24:38 +02:00
/**
* Unused.
* @return void
*/
2026-06-08 16:25:52 +02:00
public function logout(): void
{
redirect('/');
}
2026-05-20 18:25:15 +02:00
}