il-k-honda-akamai-open/edgegrid-client

实现了由 https://developer.akamai.com/introduction/Client_Auth.html 指定的 Akamai {OPEN} EdgeGrid 身份验证

1.0.0 2017-09-01 07:23 UTC

README

License Build Status Code Coverage API Docs

Akamai {OPEN} EdgeGrid 身份验证的 PHP 客户端

注意:在版本 0.6.0 中,\Akamai\Open\EdgeGrid\Authentication 库本身已被移动到单独的 akamai-open/edgegrid-auth 包中。

此库在 Guzzle 上实现了 Akamai {OPEN} EdgeGrid 身份验证方案,既可以作为即插即用客户端,也可以作为中间件。

有关更多信息,请访问 Akamai {OPEN} 开发者社区

安装

此库需要 PHP 5.5+ 或 HHVM 3.5+ 来与内置的 Guzzle HTTP 客户端一起使用。

要安装,请使用 composer

$ composer require akamai-open/edgegrid-client

(单文件)替代安装

或者,从 发布页面 下载 PHAR 文件。

要使用它,只需将其包含在您的代码中即可

include 'akamai-open-edgegrid-client.phar';

// Library is ready to use

客户端使用

Akamai\Open\EdgeGrid\Client 扩展了 \GuzzleHttp\Client,并允许您在不干扰其他使用的情况下透明地签署 API 请求,这使得它成为即插即用客户端,唯一例外是您必须先调用 \Akamai\Open\EdgeGrid\Client->setAuth()(或向构造函数提供 \Akamai\Open\EdgeGrid\Authentication 的实例)再向 API 发送请求。

$client = new Akamai\Open\EdgeGrid\Client([
	'base_uri' => 'https://akaa-baseurl-xxxxxxxxxxx-xxxxxxxxxxxxx.luna.akamaiapis.net'
]);

$client->setAuth($client_token, $client_secret, $access_token);

// use $client just as you would \Guzzle\Http\Client
$response = $client->get('/billing-usage/v1/products');

使用凭据文件

我们建议使用 .edgerc 凭据文件。凭据可以使用开发者网站上的信息生成: https://developer.akamai.com/introduction/Prov_Creds.html

您的 .edgerc 应该看起来像这样

[default]
client_secret = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=
host = xxxxx.luna.akamaiapis.net/
access_token = xxxxx
client_token = xxxxx

要利用此功能,请使用工厂方法 \Akamai\Open\EdgeGrid\Client::createFromEdgeRcFile()

要使用 default 凭据创建客户端,在 Web 服务器用户的主目录中的 .edgerc 文件中或当前工作目录中

$client = \Akamai\Open\EdgeGrid\Client::createFromEdgeRcFile();

// use $client just as you would \Guzzle\Http\Client
$response = $client->get('/billing-usage/v1/products');

或者,指定凭据部分和/或 .edgerc 位置

$client = \Akamai\Open\EdgeGrid\Client::createFromEdgeRcFile('example', '../config/.edgerc');

// use $client just as you would \Guzzle\Http\Client
$response = $client->get('/billing-usage/v1/products');

命令行界面

为了帮助测试、探索和调试,此库具有一个 CLI,类似于 httpie,并提供有限的功能模拟,如以下文档所述。

如果您通过 composer 安装,CLI 工具作为 vendor/bin/http 提供,或者您可以直接执行 PHAR 文件。

# Composer installed
$ ./vendor/bin/http --help

# For Windows
> php ./vendor/bin/http --help

# PHAR download
php akamai-open-edgegrid-client.phar --help

参数

参数类似于 httpie

  • --auth-type={edgegrid,basic,digest} — 设置认证类型(默认:无)
  • --auth user:--a user: — 设置要使用的 .edgerc 部分。与 httpie-edgegrid 不同,冒号是可选的

您还可以指定 HTTP 方法(HEAD|GET|POST|PUT|DELETE - 不区分大小写)。

最后,您可以使用以下语法轻松指定头和 JSON 主体字段

  • Header-Name:value — 头和值用冒号分隔
  • jsonKey=value — 在 POSTPUT 主体中发送 {"jsonKey": "value"}。这还将自动设置 Content-TypeAccept 头为 application/json
  • jsonKey:=[1,2,3] — 允许您指定原始JSON数据,在正文中发送 {"jsonKey": [1, 2, 3]}

限制

  • 无法发送 multipart/mime(文件上传)数据
  • 不支持客户端证书
  • 无法验证服务器证书
  • 输出无法自定义,所有HTTP和正文数据(请求和响应)都会显示
  • 响应没有语法高亮(尽管JSON已格式化)

Guzzle中间件

此包提供三种不同的中间件处理器

  • \Akamai\Open\EdgeGrid\Handler\Authentication - 提供透明的API请求签名
  • \Akamai\Open\EdgeGrid\Handler\Verbose - 简单输出(或记录)响应
  • \Akamai\Open\EdgeGrid\Handler\Debug - 简单输出(或记录)错误

当使用 Client 时,所有三个都可以透明地添加,或者添加到标准的 \GuzzleHttp\Client,或者通过添加它们作为处理器。

透明使用

要启用 Authentication,请调用 Client->setAuthentication(),或将 \Akamai\EdgeGrid\Authentication 实例传递给 Client->__construct()

要启用 Verbose,请调用 Client->setInstanceVerbose()Client::setVerbose(),并传递 true|resource|[resource output, resource error] 之一。默认为 [STDOUT, STDERR]。

要启用 Debug,请调用 Client->setInstanceDebug()Client::setDebug(),或将 debug 配置选项设置为 true|resource。默认为 STDERR

中间件

认证处理器

// Create the Authentication Handler
$auth = \Akamai\Open\EdgeGrid\Handler\Authentication::createFromEdgeRcFile();
// or:
$auth = new \Akamai\Open\EdgeGrid\Handler\Authentication;
$auth->setAuth($client_token, $client_secret, $access_token);

// Create the handler stack
$handlerStack = \GuzzleHttp\HandlerStack::create();

// Add the Auth handler to the stack
$handlerStack->push($auth);

// Add the handler to a regular \GuzzleHttp\Client
$guzzle = new \GuzzleHttp\Client([
    "handler" => $handlerStack
]);

详细处理器

// Create the handler stack
$handlerStack = HandlerStack::create();

// Add the Auth handler to the stack
$handlerStack->push(new \Akamai\Open\EdgeGrid\Handler\Verbose());

// Add the handler to a regular \GuzzleHttp\Client
$guzzle = new \GuzzleHttp\Client([
    "handler" => $handlerStack
]);

调试处理器

// Create the handler stack
$handlerStack = HandlerStack::create();

// Add the Auth handler to the stack
$handlerStack->push(new \Akamai\Open\EdgeGrid\Handler\Debug());

// Add the handler to a regular \GuzzleHttp\Client
$guzzle = new \GuzzleHttp\Client([
    "handler" => $handlerStack
]);

使用PHP 5.3(不推荐)

PHP 5.3自2014年8月14日起已停止维护,存在 已知的 安全漏洞,因此我们不推荐使用它。然而,我们理解许多仍在积极支持的长周期(LTS)发行版仍在使用PHP 5.3,因此我们提供了以下信息。

签名器本身与PHP 5.3兼容,并已移动到akamai-open/edgegrid-auth 包。

作者

Davey Shafik dshafik@akamai.com

许可证

版权所有 2016 Akamai Technologies, Inc. 保留所有权利。

根据Apache许可证版本2.0(“许可证”)许可;除非符合许可证规定,否则不得使用此文件。您可以在https://apache.ac.cn/licenses/LICENSE-2.0 获取许可证副本。

除非适用法律要求或书面同意,否则在许可证下分发的软件按“原样”基础分发,不提供任何明示或暗示的保证或条件。有关许可证的具体语言、权限和限制,请参阅许可证。