reb3r/oauth2-server-httpfoundation-bridge

oauth2-server-php 对 HttpFoundation 的桥梁

v2.4 2023-03-09 15:08 UTC

This package is auto-updated.

Last update: 2024-09-10 15:26:29 UTC


README

HttpFoundationoauth2-server-php 提供桥梁。

Build Status

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

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

安装

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

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

然后运行 composer.phar install

尽可能匹配 oauth2-server-php 库的标签。 v2.2 是最新的标签。

创建请求

创建请求对象的方式与之前相同,但现在您使用 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 实现自己的类以支持不同的内容类型。

示例

联系方式

请联系 Christian Reber (christian.reber fujitsu com) 获取更多信息

致谢

非常感谢 Brent Shaffer (https://github.com/bshaffer) 为原始版本,这是我分支的。