/oauth-server

OAuth2 Server for Hyperf Framework

Primary LanguagePHP

OAuth2 Server for Hyperf Framework

installation

    composer require xtwoend/oauth-server

Run on terminal

    php bin/hyperf.php vendor:publish xtwoend/oauth-server
  
    php bin/hyperf.php migrate

    php bin/hyperf.php oauth:key

创建app_client

$userId = $request->input('user_id',1);
$length = 32; // 设置密钥长度
$clientSecret = bin2hex(random_bytes($length)); // 使用 random_bytes 函数生成随机的二进制字符串,并将其转换为十六进制表示形式
Db::table('oauth_clients')->insert([
	'user_id' => $userId,
	'project_id' => 0,
	'name' => 'default',
	'secret' => $clientSecret,
	'created_at' => date("Y-m-d H:i:s")
]);
return $clientSecret;

测试获取 token

curl --location --request POST 'http://127.0.0.1/oauth/token' \
--header 'User-Agent: Apifox/1.0.0 (https://apifox.com)' \
--header 'Content-Type: application/json' \
--header 'Accept: */*' \
--header 'Connection: keep-alive' \
--data-raw '{
    "grant_type":"client_credentials",
    "client_id":1,
    "client_secret":"6f818639a2944ac2f88df1174386f39e08e963a3fd8af64c5ca59e597ea5bddb"
}'