2020-01-07 17:27:55 +08:00
|
|
|
<?php
|
|
|
|
namespace catcher;
|
|
|
|
|
|
|
|
use catcher\exceptions\FailedException;
|
|
|
|
use catcher\exceptions\LoginFailedException;
|
|
|
|
use thans\jwt\facade\JWTAuth;
|
|
|
|
use think\facade\Session;
|
|
|
|
|
|
|
|
class CatchAuth
|
|
|
|
{
|
|
|
|
protected $auth;
|
|
|
|
|
|
|
|
protected $guard;
|
|
|
|
|
|
|
|
// 默认获取
|
|
|
|
protected $username = 'email';
|
|
|
|
// 校验字段
|
|
|
|
protected $password = 'password';
|
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->auth = config('catch.auth');
|
|
|
|
|
|
|
|
$this->guard = $this->auth['default']['guard'];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* set guard
|
|
|
|
*
|
|
|
|
* @time 2020年01月07日
|
|
|
|
* @param $guard
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function guard($guard): self
|
|
|
|
{
|
|
|
|
$this->guard = $guard;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2020-01-07 18:04:05 +08:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @time 2020年01月07日
|
|
|
|
* @param $condition
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2020-01-07 17:27:55 +08:00
|
|
|
public function attempt($condition)
|
|
|
|
{
|
|
|
|
$user = $this->authenticate($condition);
|
|
|
|
|
|
|
|
if (!$user) {
|
|
|
|
throw new LoginFailedException();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!password_verify($condition['password'], $user->password)) {
|
|
|
|
throw new LoginFailedException();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->{$this->getDriver()}($user);
|
|
|
|
}
|
|
|
|
|
2020-01-07 18:04:05 +08:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @time 2020年01月07日
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2020-01-07 17:27:55 +08:00
|
|
|
public function user()
|
|
|
|
{
|
|
|
|
switch ($this->getDriver()) {
|
|
|
|
case 'jwt':
|
|
|
|
$model = app($this->getProvider()['model']);
|
2020-01-07 18:04:05 +08:00
|
|
|
return $model->where($model->getPk(), JWTAuth::auth()[$this->jwtKey()])->find();
|
2020-01-07 17:27:55 +08:00
|
|
|
case 'session':
|
|
|
|
return Session::get($this->sessionUserKey(), null);
|
|
|
|
default:
|
|
|
|
throw new FailedException('user not found');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-07 18:04:05 +08:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @time 2020年01月07日
|
2020-01-20 14:38:50 +08:00
|
|
|
* @return mixed
|
2020-01-07 18:04:05 +08:00
|
|
|
*/
|
2020-01-07 17:27:55 +08:00
|
|
|
public function logout()
|
|
|
|
{
|
2020-01-20 14:38:50 +08:00
|
|
|
switch ($this->getDriver()) {
|
|
|
|
case 'jwt':
|
|
|
|
return true;
|
|
|
|
case 'session':
|
|
|
|
Session::delete($this->sessionUserKey());
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
throw new FailedException('user not found');
|
|
|
|
}
|
2020-01-07 17:27:55 +08:00
|
|
|
}
|
|
|
|
|
2020-01-07 18:04:05 +08:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @time 2020年01月07日
|
|
|
|
* @param $user
|
|
|
|
* @return string
|
|
|
|
*/
|
2020-01-07 17:27:55 +08:00
|
|
|
protected function jwt($user)
|
|
|
|
{
|
2020-01-07 18:04:05 +08:00
|
|
|
$token = JWTAuth::builder([$this->jwtKey() => $user->id]);
|
2020-01-07 17:27:55 +08:00
|
|
|
|
2020-01-07 18:04:05 +08:00
|
|
|
JWTAuth::setToken($token);
|
2020-01-07 17:27:55 +08:00
|
|
|
|
2020-01-07 18:04:05 +08:00
|
|
|
return $token;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @time 2020年01月07日
|
|
|
|
* @param $user
|
|
|
|
* @return void
|
|
|
|
*/
|
2020-01-07 17:27:55 +08:00
|
|
|
protected function session($user)
|
|
|
|
{
|
|
|
|
Session::set($this->sessionUserKey(), $user);
|
|
|
|
}
|
|
|
|
|
2020-01-07 18:04:05 +08:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @time 2020年01月07日
|
|
|
|
* @return string
|
|
|
|
*/
|
2020-01-07 17:27:55 +08:00
|
|
|
protected function sessionUserKey()
|
|
|
|
{
|
|
|
|
return $this->guard . '_user';
|
|
|
|
}
|
|
|
|
|
2020-01-07 18:04:05 +08:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @time 2020年01月07日
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function jwtKey()
|
|
|
|
{
|
|
|
|
return $this->guard . '_id';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @time 2020年01月07日
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2020-01-07 17:27:55 +08:00
|
|
|
protected function getDriver()
|
|
|
|
{
|
|
|
|
return $this->auth['guards'][$this->guard]['driver'];
|
|
|
|
}
|
|
|
|
|
2020-01-07 18:04:05 +08:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @time 2020年01月07日
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2020-01-07 17:27:55 +08:00
|
|
|
protected function getProvider()
|
|
|
|
{
|
|
|
|
return $this->auth['providers'][$this->auth['guards'][$this->guard]['provider']];
|
|
|
|
}
|
|
|
|
|
2020-01-07 18:04:05 +08:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @time 2020年01月07日
|
|
|
|
* @param $condition
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2020-01-07 17:27:55 +08:00
|
|
|
protected function authenticate($condition)
|
|
|
|
{
|
|
|
|
$provider = $this->getProvider();
|
|
|
|
|
|
|
|
return $this->{$provider['driver']}($condition);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @time 2020年01月07日
|
|
|
|
* @param $condition
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function database($condition): void
|
|
|
|
{}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @time 2020年01月07日
|
|
|
|
* @param $condition
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
protected function orm($condition)
|
|
|
|
{
|
|
|
|
return app($this->getProvider()['model'])->where($this->filter($condition))->find();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @time 2020年01月07日
|
|
|
|
* @param $condition
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function filter($condition): array
|
|
|
|
{
|
|
|
|
$where = [];
|
|
|
|
|
|
|
|
foreach ($condition as $field => $value) {
|
|
|
|
if ($field != $this->password) {
|
|
|
|
$where[$field] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $where;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @time 2020年01月07日
|
|
|
|
* @param $field
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function username($field): self
|
|
|
|
{
|
|
|
|
$this->username = $field;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @time 2020年01月07日
|
|
|
|
* @param $field
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function password($field): self
|
|
|
|
{
|
|
|
|
$this->password = $field;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
}
|