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.
74 lines
2.0 KiB
74 lines
2.0 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'));
|
|
|
|
// 设置发行时间和过期时间
|
|
$now = time();
|
|
$token = $builder
|
|
->issuedAt($now) // iat: 发行时间
|
|
->expiresAt($now + config('jwt.token_ttl')) // exp: 过期时间
|
|
->withClaim('iss', 'xtt') // iss: 发行人
|
|
->withClaim('sub', 'xtoken') // sub: 主题
|
|
->withClaim('aud', 'ttc'); // aud: 受众
|
|
foreach ($claims as $key => $value) {
|
|
$token = $token->withClaim($key, $value);
|
|
}
|
|
|
|
return (string) $token->sign($signer, $key);
|
|
}
|
|
}
|