reezy/api-sign

reezy api sign

This package's canonical repository appears to be gone and the package has been frozen as a result.

v0.1.0 2020-10-14 09:16 UTC

This package is not auto-updated.

Last update: 2025-04-26 01:41:02 UTC


README

请求签名验证组件,防止伪造或篡改请求。

安装依赖

composer require reezy/api-sign

使用

在需要的控制器或请求上加上中间件 APISignMiddleware

或者

$container->get(APISignManager::class)->check($request); 

请求参数签名

参与签名的部分为:

  • accessKey
  • 请求方法(method),请求方法使用大写
  • 路径(path),路径要以'/'开头
  • 查询参数(sortedQuery),按参数名升序排序
  • 请求时间(date),请求头中的 Date (GMT)
  • 请求头(signedHeaders),参与签名的请求头,按客户端指定顺序构造 name1=value1&name2=value2...
  • 请求体摘要(body),当媒体类型为application/x-www-form-urlencodedapplication/json请求时取md5参与签名

签名算法

// 构造签名串
signstr=accessKey|uppercase(method)|path|sortedQuery|date|signedHeaders|lowercase(md5(body))
// 计算签名
sign=hmac_hash(algo, signstr,secret)

配置

<?php
return [
    'android' => [
        'secret_key' => 'SECRET_KEY',
        // 签名有效时间,为0表示不限制
        'ttl' => 20, 
        'allow_headers' => []
    ],
    'ios' => [
        'secret_key' => 'SECRET_KEY',
        'ttl' => 20,
        'allow_headers' => []
    ],

    'header_keys' => [
        'sign_key' => 'x-sign',
        'date_key' => 'date',
        'algo_key' => 'x-sign-algo',
        'headers_key' => 'x-sign-headers',
        'access_key' => 'x-sign-key',
    ],
];

LICENSE

The Component is open-sourced software licensed under the Apache license.