Php 使用SecurityServiceProvider和Silex验证HMAC

Php 使用SecurityServiceProvider和Silex验证HMAC,php,symfony,hmac,silex,Php,Symfony,Hmac,Silex,在允许用户访问系统之前,我正在URL中发送一个hmac,希望该URL有效。没有用户数据库,因此它只是验证url参数以验证它是由正确的服务器生成的,用户无法更改这些参数以访问系统的其他部分 我想知道是否有人对SecurityServiceProvider做过类似的事情。我可以使用在路由和安全防火墙规则之后触发的before中间件来实现这一点。如果可能的话,我想在防火墙上停止这个请求。我决定在“之前”使用silex的中间件功能。我相信这可能不是最好的解决方法,但我的应用程序没有用户或身份验证,所以我

在允许用户访问系统之前,我正在URL中发送一个hmac,希望该URL有效。没有用户数据库,因此它只是验证url参数以验证它是由正确的服务器生成的,用户无法更改这些参数以访问系统的其他部分


我想知道是否有人对SecurityServiceProvider做过类似的事情。我可以使用在路由和安全防火墙规则之后触发的before中间件来实现这一点。如果可能的话,我想在防火墙上停止这个请求。

我决定在“之前”使用silex的中间件功能。我相信这可能不是最好的解决方法,但我的应用程序没有用户或身份验证,所以我只需要检查密钥是否正确并返回响应。如果有人对改进这一点有任何意见,我很乐意听取他们的意见

当外部系统生成url时,它们必须使用我的配置文件中定义的共享密钥,并且可以通过$app['config']['hmac_key']访问。hmac在路径中hmac之后的所有内容上生成。所以如果我有子文件夹domain.com/folder/hmac/arg1/arg2/arg3。before过滤器在hmac处拆分路由,并在此之后构建路径

// Before firing the controller check the hmac matches, otherwise return 403.
$app->before(function (Request $request) use ($app) {
    // 40 chars in a sha1 hmac hash
    if(!preg_match('/^[0-9a-f]{40}$/i', $request->get('hmac')))
        $app->abort(403, "Invalid key.");

    // break the route down to the arguments used in hmac
    $route = explode('hmac_', $request->get('_route'));
    $route = explode('_',$route[1]);
    // build the path used to generate hmac
    $path = array();
    foreach($route as $r)
        if($v = $request->get($r))
            $path[] = $v;

    $path = implode('/', $path);

    // Generate hmac hash from path and key
    $hmac = hash_hmac("sha1", $path, $app['config']['hmac_key']);

    // If the hmac's don't match return 403
    if($hmac !== $request->get('hmac'))
        $app->abort(403, "Invalid key.");
});

可能是我问题的答案。。。当我发送数据时,我通常使用发送的URL参数的排序规则生成hmac,然后比较哈希以确保它是可信的源。如何确保传递给您的数据是可信的?我正在使用url参数生成哈希。我正在对domain.com/folder/hmac/arg1/arg2/arg3的'arg1/arg2/arg3'进行哈希运算。我正在使用silex的路由,因此受信任的源不需要生成像arg1=blah&arg2=blah&arg3=blah这样的url(如果你是这么说的话),它只是/blah/blah/blah。不管怎样,散列这两个键实际上没有什么区别,因为键实际上比路径长。是的,这很好,我通常也会传递一个时间戳,这样键的寿命就有限了。