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.
 
 
 
 
 
 

82 lines
2.2 KiB

<?php
namespace app\api\controller;
use app\BaseController;
use app\Request;
// for jwt
use Lcobucci\JWT\Parser;
use Lcobucci\JWT\Builder;
use Lcobucci\JWT\Signer\Hmac\Sha256;
use Lcobucci\JWT\Signer\Key\InMemory;
use Lcobucci\JWT\Token;
class Index extends BaseController
{
//
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']);
$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'));
$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: 受众
foreach ($claims as $key => $value) {
$token = $token->withClaim($key, $value);
}
return (string) $token->sign($signer, $key);
}
}