167 lines
3.6 KiB
PHP
167 lines
3.6 KiB
PHP
<?php
|
|
namespace App\Domains\Auth\Services;
|
|
|
|
use App\Core\Service;
|
|
use Illuminate\Contracts\Auth\Guard;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\Request;
|
|
use Tymon\JWTAuth\Exceptions\JWTException;
|
|
use Dipper\Foundation\Repository\Repository;
|
|
|
|
class AuthService extends Service
|
|
{
|
|
protected $guardName;
|
|
protected $guard;
|
|
protected $repository;
|
|
|
|
protected $token;
|
|
protected $user;
|
|
|
|
/**
|
|
* 记住密码时的过期时间
|
|
*/
|
|
const REMEMBER_TTL = 30 * 24 * 60;
|
|
|
|
/**
|
|
* 构造函数
|
|
*
|
|
* @return void
|
|
*/
|
|
public function __construct($guard, Repository $repository)
|
|
{
|
|
$this->guardName = $guard;
|
|
$this->guard = Auth::guard($guard);
|
|
$this->repository = $repository;
|
|
}
|
|
|
|
public function login($username, $password, $remember = null)
|
|
{
|
|
$credentials = ['password' => $password];
|
|
|
|
if (filter_var($username, FILTER_VALIDATE_EMAIL)) {
|
|
$credentials['email'] = $username;
|
|
} elseif (ctype_digit($username) && strlen($username) == 11) {
|
|
$credentials['mobile'] = $username;
|
|
} else {
|
|
$credentials['username'] = $username;
|
|
}
|
|
|
|
if ($remember) {
|
|
$this->guard->setTTL(self::REMEMBER_TTL);
|
|
}
|
|
|
|
if (!$this->guard->validate($credentials)) {
|
|
return err('帐号或密码不正确');
|
|
}
|
|
|
|
$user = $this->guard->getLastAttempted();
|
|
|
|
$token = $this->guard->login($user);
|
|
|
|
$this->setToken($token);
|
|
$this->setUser($user);
|
|
|
|
return $token;
|
|
}
|
|
|
|
public function logout()
|
|
{
|
|
try {
|
|
$this->guard->logout();
|
|
} catch (JWTException $e) {
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
public function authenticate()
|
|
{
|
|
if ($this->user) {
|
|
$newToken = $this->guard->login($this->user);
|
|
$this->setToken($newToken);
|
|
return $this->user;
|
|
}
|
|
|
|
if (!$token = $this->getToken()) {
|
|
return null;
|
|
}
|
|
|
|
$newToken = '';
|
|
|
|
try {
|
|
// 超出TTL两倍时长则视为超时
|
|
$range = $this->guard->factory()->getTTL() * 60;
|
|
$payload = $this->guard->manager()->getJWTProvider()->decode($token);
|
|
|
|
if ($payload && $payload['exp'] + $range >= time()) {
|
|
$user = $this->repository->find($payload['sub']);
|
|
if ($user) {
|
|
$newToken = $this->guard->login($user);
|
|
$this->setToken($newToken);
|
|
$this->setUser($user);
|
|
}
|
|
}
|
|
} catch (\Exception $e) {
|
|
// TokenBlacklistedException
|
|
}
|
|
|
|
return $user;
|
|
}
|
|
|
|
/**
|
|
* Get the value of guardName
|
|
*/
|
|
public function getGuardName()
|
|
{
|
|
return $this->guardName;
|
|
}
|
|
|
|
/**
|
|
* Get the value of guard
|
|
*/
|
|
public function getGuard()
|
|
{
|
|
return $this->guard;
|
|
}
|
|
|
|
/**
|
|
* Get the value of token
|
|
*/
|
|
public function getToken()
|
|
{
|
|
return $this->token ?: Request::get('access_token', Request::bearerToken());
|
|
}
|
|
|
|
/**
|
|
* Set the value of token
|
|
*
|
|
* @return self
|
|
*/
|
|
public function setToken($token)
|
|
{
|
|
$this->token = $token;
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Get the value of user
|
|
*/
|
|
public function getUser()
|
|
{
|
|
return $this->user;
|
|
}
|
|
|
|
/**
|
|
* Set the value of user
|
|
*
|
|
* @return self
|
|
*/
|
|
public function setUser($user)
|
|
{
|
|
$this->user = $user;
|
|
|
|
return $this;
|
|
}
|
|
}
|