Skip to content

dinofei/hyperf-accesstoken

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

安装

composer require bjyyb/hyperf-accesstoken:dev-main

发布配置文件

php bin/hyperf.php vendor:publish bjyyb/hyperf-accesstoken

使用:

生成 access_token和refresh_token

$tokenService = $this->container->get(\Bjyyb\AccessToken\Contract\TokenInterface::class);
$payload = [
    "user_id" => 1,
    "name" => "xx",
];
$result = $tokenService->iss($payload);
/** 返回如下格式数据:*/
[
    "access_token" => "xxx",
    "refresh_token" => "xxx",
];

验证 access_token

$tokenService = $this->container->get(\Bjyyb\AccessToken\Contract\TokenInterface::class);
$token = "xx";
$payload = $tokenService->verify($token);
/** 返回如下格式数据:*/
[
    "user_id" => 1,
    "name" => "xx",
];
/** 如果验证失败抛出异常 */

使用refresh_token 刷新 access_token

$tokenService = $this->container->get(\Bjyyb\AccessToken\Contract\TokenInterface::class);
$refreshToken = "xx";
$result = $tokenService->refresh($refreshToken);
/** 返回新的令牌:*/
[
    "access_token" => "xxx",
    "refresh_token" => "xxx",
];

删除 access_token和refresh_token

$tokenService = $this->container->get(\Bjyyb\AccessToken\Contract\TokenInterface::class);
$token = "xx";
$result = $tokenService->remove($token);

适配apiservice的token验证

安装jwt组件

composer require bjyyb/hyperf-jwt:dev-main

添加配置 config/jwt.php

[
'webtoken' => [
    // 签名算法
    'alg' => 'HS256',
    // 实体内容
    'payload' => [
    ],
    // 加密密钥
    'key' => 'abcd1234abcd',
    // 允许算法
    'allowed_algs' => ['HS256'],
],
];

解密token

$token = 'xxx';
$tokenService = $this->container->get(\Bjyyb\AccessToken\WebToken::class);
$data = $tokenService->verify($token);
var_dump($data);

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages