PHP code example of larva / laravel-auth-signature-guard

1. Go to this page and download the library: Download larva/laravel-auth-signature-guard 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/ */

    

larva / laravel-auth-signature-guard example snippets


//认证配置
/config/auth.php

//增加一个看守器

'guards' => [
	'web'=>[],//原WEB的
	'api'=>[],//原API
	'signature'=>[//新增的
		'driver'=>'signature'
		'provider'=>'users',
	]
]
// 使用时在控制器中

class SDKController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth:signature');
    }
}

//按照以上配置,该控制器所有的方法都会经过签名看守器认证。


$appKey = '123456';
$params = [
    'app_id' => 3, 
    'timestamp' => 1555069945,
    'signature_method'=>'HMAC-SHA1',
    'signature_version'=>'1.0',
    'signature_nonce'=>'rakdienakdig',
    'key1'=>'val1',
    'key2'=>'val2'
];

//排序参数
//按照键名对关联数组进行升序排序
ksort($params);
//编码
$stringToSign = urlencode(http_build_query($params, null, '&', PHP_QUERY_RFC3986));
$stringToSign = str_replace(['+', '*'], ['%20', '%2A'], $stringToSign);
$stringToSign = preg_replace('/%7E/', '~', $stringToSign);

//签名
$params['signature'] = base64_encode(hash_hmac('sha1', $stringToSign, $appKey.'&', true));

// 你的HTTP 实例,
$res = $http->post('your api url/path',$params);

//其中参数中的时间戳和世界标准时间相差不能超过1分钟。

//签名计算例子 在tests 目录