1
0
mirror of https://gitee.com/koogua/course-tencent-cloud.git synced 2025-06-26 04:21:27 +08:00
koogua cbc2e5762a !11 阶段性合并
* 根据app需要作出相应调整
* 路由增加命名name,增加app应用管理
* 完成基本API,增加h5和小程序支付定义
2020-11-10 10:25:16 +08:00

73 lines
1.6 KiB
PHP

<?php
namespace App\Services\Auth;
use App\Models\User as UserModel;
use App\Services\Auth as AuthService;
use Lcobucci\JWT\Builder as JwtBuilder;
use Lcobucci\JWT\Parser as JwtParser;
use Lcobucci\JWT\Signer\Hmac\Sha256 as JwtSingerSha256;
use Lcobucci\JWT\Signer\Key as JwtSingerKey;
use Lcobucci\JWT\ValidationData as JwtValidationData;
class Api extends AuthService
{
public function saveAuthInfo(UserModel $user)
{
$builder = new JwtBuilder();
$config = $this->getConfig();
$expireTime = time() + $config->path('jwt.lifetime');
$builder->expiresAt($expireTime);
$builder->withClaim('user_id', $user->id);
$builder->withClaim('user_name', $user->name);
$singer = new JwtSingerSha256();
$key = new JwtSingerKey($config->path('jwt.key'));
$token = $builder->getToken($singer, $key);
return $token->__toString();
}
public function clearAuthInfo()
{
}
public function getAuthInfo()
{
$authToken = $this->request->getHeader('X-Token');
if (!$authToken) return null;
$config = $this->getConfig();
$parser = new JWTParser();
$token = $parser->parse($authToken);
$data = new JWTValidationData(time(), $config->path('jwt.leeway'));
if (!$token->validate($data)) {
return null;
}
$singer = new JwtSingerSha256();
if (!$token->verify($singer, $config->path('jwt.key'))) {
return null;
}
return [
'id' => $token->getClaim('user_id'),
'name' => $token->getClaim('user_name'),
];
}
}