binhvd / oauth2-server-httpfoundation-bridge
oauth2-server-php 的 HttpFoundation 适配器
Requires
- php: >=7.2.0
- binhvd/oauth2-server-php: >=1.12.0
- symfony/http-foundation: >=5.0
This package is not auto-updated.
Last update: 2024-10-03 03:28:45 UTC
README
为 HttpFoundation 和 oauth2-server-php 提供桥梁。
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)