jvandemo/zendservice-oauth2

Zend Framework 2 的 Oauth2.0 服务

v1.0.0-alpha 2013-03-06 23:29 UTC

This package is not auto-updated.

Last update: 2024-09-14 15:05:58 UTC


README

Zend Framework 2 的 Oauth2 服务

状态

目前处于开发中,请勿在生产环境中使用...

待办事项

  • 编写更多和更好的文档
  • 提供更多单元测试

Composer

ZendService_Oauth2 现已在以下 Composer 包上可用:https://packagist.org.cn/packages/jvandemo/zendservice-oauth2

要将它包含到您的项目中,请将以下行添加到您的 composer.json

"require": {
	"jvandemo/zendservice-oauth2" : "dev-master"
}

实时演示

在以下网址有实时演示:http://jvandemo.my.phpcloud.com/ZendService_Oauth2/demos/ZendService/Oauth2/AuthorizationCode/index.php

演示页面的源代码包含在 demos 文件夹中。

快速演示

使用默认授权代码授予的示例代码

use ZendService\Oauth2\Client\Client;

// Create configuration
$config = array(

    // Oauth2 client options
    'client' => array(
        'client_id' => 'your_client_id',
	    'client_secret' => 'your_client_secret',
	    'authorization_url' => 'https://api.youwishtoconnect.to/authorize',
	    'access_token_url' => 'https://api.youwishtoconnect.to/access_token',
	    'redirect_uri' => 'http://www.yourwebsite.com/where_to_go_after_authorization',
	    'state' => 'somerandomstate',
    ),
    
    // Http client options
    'http' => array(
        'adapter'   => 'Zend\Http\Client\Adapter\Curl',
        'curloptions' => array(
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_SSL_VERIFYPEER => false,
        ),
    ),
);

// Create a new Oauth2.0 client and pass in the config
$client = new Client($config);

// Let the client build the authorization request for you:
$url = $client->getAuthorizationRequestUrl()

// Redirect the user to the authorization request url:
return $this->redirect()->toUrl($url);

第三方授权服务器将重定向您到您在 redirect_uri 中指定的 URL

// Grab the access token from the authorization response:
$code = $_GET['code'];

// Use the authorization code to get an access token: 
$accessToken = $client->getAccessToken(array(
	'code' => $code
));

访问令牌是一个非常轻量级的对象,可以存储在会话中或在您的后端系统中序列化以供以后使用,因此如果不需要,您不需要重复之前的步骤。

然后,每当您需要执行 Oauth2.0 请求时

// Create a client
$client = new Client($config);

// Get the token from your backend e.g. with unserialize
$accessToken = getAccessTokenFromYourBackend(); // Replace with your custom function

and perform GET requests
$response = $client->get('http://api.youwishtoconnect.to/some_endpoint', array('access_token' => $accessToken->getAccessToken()));

// or POST requests
$response = $client->post('http://api.youwishtoconnect.to/some_endpoint', array('access_token' => $accessToken->getAccessToken()));

// Some third parties e.g. Linkedin requires the token to be passed as `oauth2_access_token` parameter, so you can easily change it as required
$response = $client->get('http://api.youwishtoconnect.to/some_endpoint', array('oauth2_access_token' => $accessToken->getAccessToken()));

默认情况下,使用 Zend\Http\Client 执行请求,因此您会得到一个 'Zend\Http\Response' 对象

// Print the response body
echo $response->getBody()

主要功能

  • 提供了一个主要的 ZendService\Oauth2\Client\Client 类,用于简化使用
  • 默认配置下即可使用
    • 使用 AuthorizationCode 作为默认的授权授予
    • 使用 Zend\Http\Client 作为默认的 Http 客户端
    • 使用 ZendService\Oauth2\AccessToken\AccessToken 作为默认的 AccessToken 对象
    • 使用 ZendService\Oauth2\Client\Client 作为默认客户端实现
  • 支持自定义客户端实现以添加或更改行为
    • 可以扩展 ZendService\Oauth2\Client\AbstractClient
    • 必须实现 ZendService\Oauth2\Client\ClientInterface
  • 通过添加自定义授权授予来添加自定义 Oauth2.0 流
    • 可以扩展 ZendService\Oauth2\AuthorizationGrant\AbstractAuthorizationGrant
    • 必须实现 ZendService\Oauth2\AuthorizationGrant\AuthorizationGrantInterface
  • 支持自定义 Http 客户端
    • 必须实现 ZendService\Oauth2\Http\Client\ClientInterface

有建议吗?

联系我,写评论或者最好是:分支、实现并创建一个 pull request :-)