| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- <?php
- namespace App\Services;
- use App\Utils\CacheKey;
- use App\Utils\Helper;
- use Firebase\JWT\JWT;
- use Firebase\JWT\Key;
- use App\Models\User;
- use Illuminate\Support\Facades\Cache;
- use Illuminate\Http\Request;
- class AuthService
- {
- private $user;
- public function __construct(User $user)
- {
- $this->user = $user;
- }
- public function generateAuthData(Request $request)
- {
- $guid = Helper::guid();
- $authData = JWT::encode([
- 'id' => $this->user->id,
- 'session' => $guid,
- ], config('app.key'), 'HS256');
- self::addSession($this->user->id, $guid, [
- 'ip' => $request->ip(),
- 'login_at' => time(),
- 'ua' => $request->userAgent()
- ]);
- return [
- 'token' => $this->user->token,
- 'is_admin' => $this->user->is_admin,
- 'auth_data' => $authData
- ];
- }
- public static function decryptAuthData($jwt)
- {
- try {
- if (!Cache::has($jwt)) {
- $data = (array)JWT::decode($jwt, new Key(config('app.key'), 'HS256'));
- if (!self::checkSession($data['id'], $data['session'])) return false;
- $user = User::select([
- 'id',
- 'email',
- 'is_admin',
- 'is_staff'
- ])
- ->find($data['id']);
- if (!$user) return false;
- Cache::put($jwt, $user->toArray(), 3600);
- }
- return Cache::get($jwt);
- } catch (\Exception $e) {
- return false;
- }
- }
- private static function checkSession($userId, $session)
- {
- $sessions = (array)Cache::get(CacheKey::get("USER_SESSIONS", $userId)) ?? [];
- if (!in_array($session, array_keys($sessions))) return false;
- return true;
- }
- private static function addSession($userId, $guid, $meta)
- {
- $cacheKey = CacheKey::get("USER_SESSIONS", $userId);
- $sessions = (array)Cache::get($cacheKey, []);
- $sessions[$guid] = $meta;
- if (!Cache::put(
- $cacheKey,
- $sessions
- )) return false;
- return true;
- }
- public function getSessions()
- {
- return (array)Cache::get(CacheKey::get("USER_SESSIONS", $this->user->id), []);
- }
- public function removeSession($sessionId)
- {
- $cacheKey = CacheKey::get("USER_SESSIONS", $this->user->id);
- $sessions = (array)Cache::get($cacheKey, []);
- unset($sessions[$sessionId]);
- if (!Cache::put(
- $cacheKey,
- $sessions
- )) return false;
- return true;
- }
- public function removeAllSession()
- {
- $cacheKey = CacheKey::get("USER_SESSIONS", $this->user->id);
- return Cache::forget($cacheKey);
- }
- }
|