PHP code example of stru / stru-hyperf-oauth

1. Go to this page and download the library: Download stru/stru-hyperf-oauth library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

stru / stru-hyperf-oauth example snippets


php bin/hyperf.php vendor:publish stru/stru-hyperf-ouath

php bin/hyperf.php migrate

php bin/hyperf.php stru:client

该命令生成auth_code验证方式的客户端,需要手动输入name和回调地址

php bin/hyperf.php stru:keys

// 客户端----laravel做客户端代码示例

protected $url = 'http://192.168.10.1:8000/';           //客户端地址
protected $remoteUrl = 'http://192.168.10.10:9501/';    //oauth服务器地址
public function redirect(Request $request){
    $request->session()->put('state', $state = Str::random(40));

    $query = http_build_query([
        'client_id' => 1,
        'redirect_uri' => $this->url.'callback',
        'response_type' => 'code',
        'scope' => 'public',
        'state' => $state,
    ]);

    return redirect($this->remoteUrl.'oauth/authorize?'.$query);
}

public function callback(Request $request)
{
    $http = new Client();

    $response = $http->post($this->remoteUrl.'oauth/token', [
        'form_params' => [
            'grant_type' => 'authorization_code',
            'client_id' => 1,
            'client_secret' => 'QI7aUla1qVTVFqYRStqt5D56sR0s0L5HU0NS1YMG',
            'redirect_uri' => $this->url.'callback',
            'code' => $request->code,
        ],
    ]);

    return \json_decode((string) $response->getBody(), true);
}