radweb / oauth-token-encoding
OAuth 2 Token 响应的替代编码
Requires
- php: >=5.6.0
Requires (Dev)
- illuminate/http: >=4.1
- league/oauth2-server: ^4.1
- mockery/mockery: ^0.9.4
- phpunit/phpunit: ^4.8
- zendframework/zend-diactoros: ^1.1
Suggests
- illuminate/http: Required to use the Illuminate adaptor (>=4.1)
- league/oauth2-server: Required to use the Laravel middleware (^4.0)
- symfony/http-foundation: Required to use the Symfony HTTP Foundation Adaptor (~2.3|~3.0)
- zendframework/zend-diactoros: Required to use the PSR-7 adaptor (^1.1)
This package is not auto-updated.
Last update: 2024-09-12 00:30:17 UTC
README
OAuth 2 Token 编码器
OAuth 2 规范指定,令牌响应应为 JSON 格式。然而,XML 用户将是 XML 用户,因此存在一个草案规范扩展,该扩展定义了 OAuth 响应在 XML 和表单编码格式中的外观
https://tools.ietf.org/html/draft-richer-oauth-xml-01
{ "access_token":"2YotnFZFEjr1zCsicMWpAA", "token_type":"example", "expires_in":3600 }
<oauth> <access_token>2YotnFZFEjr1zCsicMWpAA</access_token> <token_type>example</token_type> <expires_in>3600</expires_in> </oauth>
access_token=2YotnFZFEjr1zCsicMWpAA&token_type=example&expires_in=3600
安装
composer require radweb\oauth-token-encoding
用法
存在一个基本的 Radweb\OAuthTokenEncoding\OAuthTokenEncoder
类,当提供 Accept
标头和一个表示 OAuth 令牌的数组时,将返回正确的 Content-Type
标头和正确编码的 OAuth 令牌。
还有为常见库提供的适配器,将返回正确的响应对象
OAuthTokenIlluminateAdaptor
用于 LaravelOAuthTokenSymfonyAdaptor
用于 SymfonyOAuthTokenPsrAdaptor
用于任何 PSR-7 兼容的库(尽管它使用Zend\Diactoros
包作为 PSR-7 响应的实现)
最后,如果您使用的是 League\OAuth2\Server
包,有一个兼容的 LeagueOAuthExceptionFormatter
类用于格式化该库的异常。如果您与 Laravel 一起使用它,还有 LaravelOAuthExceptionHandlingMiddleware
用于自动执行此操作。
基本用法
// grab the "Accept" header from your request and pass it in $accept = 'application/xml'; // create an access token $oauthToken = [ "access_token" => "2YotnFZFEjr1zCsicMWpAA", "token_type" => "example", "expires_in" => 3600, "refresh_token" => "tGzv3JOkF0XG5Qx2TlKWIA", "example_parameter" => "example_value", ]; $encoder = new OAuthTokenEncoder; list($contentType, $body) = $encoder->encode($accept, $oauthToken); // return a response using the given body & content type
与 League 的 OAuth 2 服务器一起
可以通过 League\OAuth2\Server\AuthorizationServer
的 issueAccessToken
方法返回的格式传递给编码器。
list($contentType, $body) = $encoder->encode($authorizationServer->issueAccessToken());
与 Laravel / Lumen 一起
给定一个 Illuminate\Http\Request
对象,适配器将检查请求的 Accept
标头,查找 application/json
、application/xml
或 application/x-www-form-urlencoded
。如果没有找到,它假定是 JSON。
$oauthToken = [ "access_token" => "2YotnFZFEjr1zCsicMWpAA", "token_type" => "example", "expires_in" => 3600, "refresh_token" => "tGzv3JOkF0XG5Qx2TlKWIA", "example_parameter" => "example_value", ]; // $request should be a Illuminate\Http\Request $adaptor = new OAuthTokenIlluminateAdaptor(new OAuthTokenEncoder, $request); // or.. $adaptor = OAuthTokenIlluminateAdaptor::make($request); $response = $adaptor->adapt($oauthToken); // $response is now an Illuminate\Http\Response
响应将包含正确编码的正文、正确的 Content-Type
标头和 Cache-Control: no-store
标头。
与 Laravel OAuth 2 服务器 一起
use \LucaDegasperi\OAuth2Server\Authorizer; use \Radweb\OAuthTokenEncoding\ResponseAdaptors\OAuthTokenIlluminateAdaptor; Route::post('oauth/token', function(Authorizer $authorizer, OAuthTokenIlluminateAdaptor $adaptor) { return $adaptor->adapt($authorizer->issueAccessToken()); });
可以通过 League\OAuth2\Server\AuthorizationServer
的 issueAccessToken
方法返回的格式传递给编码器。
与 PSR-7 一起
构建响应需要
zendframework/zend-diactoros
包。
给定一个 PSR-7 请求,适配器将检查请求的 Accept
标头,查找 application/json
、application/xml
或 application/x-www-form-urlencoded
。如果没有找到,它假定是 JSON。
$oauthToken = [ "access_token" => "2YotnFZFEjr1zCsicMWpAA", "token_type" => "example", "expires_in" => 3600, "refresh_token" => "tGzv3JOkF0XG5Qx2TlKWIA", "example_parameter" => "example_value", ]; // $request should be a PSR-7 compliant Request object $adaptor = new OAuthTokenPsrAdaptor(new OAuthTokenEncoder, $request); // or.. $adaptor = OAuthTokenPsrAdaptor::make($request); $response = $adaptor->adapt($oauthToken); // $response is now a PSR-7 compliant Response object
响应将包含正确编码的正文、正确的 Content-Type
标头和 Cache-Control: no-store
标头。
错误
如果您使用 Laravel OAuth 2 服务器,您可以使用 LaravelOAuthExceptionHandlingMiddleware
而不是该包中提供的那个。
{ "error": "invalid_client", "error_description": "Client authentication failed." }
<oauth> <error>invalid_client</error> <error_description>Client authentication failed.</error_description> </oauth>
error=invalid_client&error_description=Client+authentication+failed.