bshaffer/oauth2-server-httpfoundation-bridge

oauth2-server-php 对 HttpFoundation 的桥梁

v1.7.1 2024-01-20 14:40 UTC

This package is auto-updated.

Last update: 2024-08-22 18:52:56 UTC


README

oauth2-server-php 对 HttpFoundation 的桥梁。

oauth2-server-httpfoundation-bridge 是 oauth2-server-php 的包装器,它返回 Symfony\Component\HttpFoundation\Response 而不是 OAuth2\Response,并使用 Symfony\Component\HttpFoundation\Request 而不是 OAuth2\Request

如果您正在将 OAuth2 集成到 Silex、Symfony 或 Laravel 4 应用程序(或任何使用 HttpFoundation 的应用程序),这将使您的应用程序更加简洁。

安装

Composer 是安装此库的最佳方式。将此行添加到 composer.json 中

{
    "require": {
        "bshaffer/oauth2-server-httpfoundation-bridge": "v1.0",
        ...
    },
    ...
}

然后运行 composer.phar install

尽可能与 oauth2-server-php 库匹配标签。v1.1 是最新的标签。

创建请求

创建请求对象的方式与之前相同,但现在您使用的是 OAuth2\HttpFoundationBridge\Request

$request = OAuth2\HttpFoundationBridge\Request::createFromGlobals();
$app->run($request);

请求对象现在与 HttpFoundation 和 oauth2-server-php 都兼容

// getBaseUrl is unique to HttpFoundation
$baseUrl = $request->getBaseUrl();

// call oauth server
$server->grantAccessToken($request);

如果 HttpFoundation 请求已经存在,您可以使用静态 createFromRequest 函数来构建 OAuth2\HttpFoundationBridge\Request 实例

use OAuth2\HttpFoundationBridge\Request as BridgeRequest;

// in your controller layer, the $request object is passed in
public function execute(Request $request)
{
    //... (instantiate server/response objects)
    $bridgeRequest = BridgeRequest::createFromRequest($request);
    $server->grantAccessToken($bridgeRequest, $response);
}

创建响应

OAuth2\HttpFoundationBridge\Response 对象扩展了 Symfony\Component\HttpFoundation\JsonResponse 并实现了 OAuth2\ResponseInterface,允许您将其传递并从控制器中返回。在 Symfony 和 Silex 中,这将需要集成服务器。

use OAuth2\HttpFoundationBridge\Response as BridgeResponse;

// in your controller layer, the $request object is passed in
public function execute(Request $request)
{
    //... (instantiate server/response objects)
    $response = new BridgeResponse();
    return $server->handleTokenRequest($request, $response);
}

注意:此对象将返回 JSON。使用 OAuth2\ResponseInterface 实现自己的类以支持不同的内容类型。

示例

联系

请联系 Brent Shaffer (bshafs gmail com) 获取更多信息