php管理和接口
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

116 lines
3.6 KiB

<?php
namespace app\api\controller;
use app\Request;
// for JWT
use Lcobucci\JWT\Builder;
// use Lcobucci\JWT\Signer\Hmac\Sha256;
// use Lcobucci\JWT\Signer\Key\InMemory;
use Lcobucci\JWT\Token;
// use DateTimeImmutable;
use Lcobucci\JWT\Configuration;
use Lcobucci\JWT\Signer\Hmac\Sha256;
use Lcobucci\JWT\Signer\Key\InMemory;
use DateTimeImmutable;
use Lcobucci\JWT\Token\Plain;
use Lcobucci\JWT\Validation\RequiredConstraintsViolated;
use Lcobucci\JWT\Validation\Constraint\SignedWith;
class Index extends ApiController
{
//
public function index(Request $request)
{
$arr=[];
$head = $request->header('mkpwd');
if ($head == '7xopjJClRxTHhtAm') {
// make user password
$data = $request->post();
// 用户密码
$salt = isset($data['salt'])?$data['salt']:makeSalt(6);
$arr['encpass']=password($data['upass'].$salt);
$arr['salt']=$salt;
} else {
$arr = ["ver" => "00", "date" => time()];
}
$ss = json_encode($arr);
return $ss;
}
/**
* 生成随机字符串
* make salt
* @param int $len
*/
protected function makeSalt(int $len){
$ss = "abcdefghigklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
$salt = '';
for ($i = 0; $i < $len; $i++) {
$salt .= $ss[mt_rand(0, strlen($ss) - 1)];
}
return $salt;
}
public function test(){
// return time();
// throwError('密码错误');
$claims=[
"id" => 1,
"nick_name" => 'zhangsan'
];
$signer = new Sha256();
$key = InMemory::plainText(config('jwt.secret'));
//
$config = Configuration::forSymmetricSigner($signer,$key);
//$builder = new Builder();
// 设置发行时间和过期时间
$now = new DateTimeImmutable(); // 当前时间
// 设置发行时间和过期时间
$secondsToAdd = (int) config('jwt.token_ttl');
$expiresAt = $now->add(new \DateInterval('PT' . $secondsToAdd . 'S'));
// $token = $builder
// ->issuedAt($now) // iat: 发行时间
// ->expiresAt($expiresAt) // exp: 过期时间
// ->withIssuer('iss', 'xtt') // iss: 发行人
// ->withSubject('sub', 'xtoken') // sub: 主题
// ->withAudience('aud', 'ttc'); // aud: 受众
$token = $config->builder()
// 签发人
->issuedBy('https://douyin.xingtongworld.com/')
// 受众
->permittedFor('https://douyin.xingtongworld.com/')
// JWT ID 编号 唯一标识
->identifiedBy($claims['id'])
// 签发时间
->issuedAt($now)
// 在1分钟后才可使用
// ->canOnlyBeUsedAfter($now->modify('+1 minute'))
// 过期时间1小时
->expiresAt($now->modify('+1 hour'))
// 自定义uid 额外参数
->withClaim('uid', $claims['id'])
->withClaim('name',$claims['nick_name'])
// 自定义header 参数
// ->withHeader('foo', 'bar')
// 生成token
->getToken($config->signer(), $config->signingKey());
// base64
return $token->toString();
// foreach ($claims as $key => $value) {
// $token = $token->withClaim($key, $value);
// }
// return (string) $token->sign($signer, $key);
}
}