PDO
概观
PDO存储类使用 PHP 的扩展。这允许连接到MySQL,SQLite,PostgreSQL 。
安装
PDO是默认安装的php 5.1+
,这个库已经是必需的了,所以你会很好的去。
开始使用
一旦完成, 连接到您选择的数据库服务器。
// connection for MySQL$pdo = new PDO('mysql:host=localhost;dbname=test', $user, $pass); // connection for SQLite $pdo = new PDO('sqlite:/opt/databases/mydb.sq3'); // connection for SQLite in memory $pdo = new PDO('sqlite::memory:');
然后,使用Pdo
存储类创建存储对象:
$storage = new OAuth2\Storage\Pdo($pdo); // now you can perform storage functions, such as the one below $storage->setClientDetails($client_id, $client_secret, $redirect_uri);
用法
PDO存储引擎实现了该库中支持的所有标准存储接口。请参阅了解更多信息。
蒙戈
概观
使用文档数据库在OAuth中存储和检索对象。
安装
首先,安装 for PHP您可以使用PECL:
$ sudo pecl install mongo
或者手动安装。 从github 的代码。
$ tar zxvf mongodb-mongodb-php-driver-.tar.gz$ cd mongodb-mongodb-php-driver- $ phpize$ ./configure$ make all$ sudo make install
接下来,mongo.so
在您的php.ini
文件中启用扩展名:
extension=mongo.so
开始使用
一旦完成, 连接到mongo服务器。
$mongo = new \MongoClient();
现在,使用Mongo
存储类创建存储对象:
$storage = new OAuth2\Storage\Mongo($mongo); // now you can perform storage functions, such as the one below $storage->setClientDetails($client_id, $client_secret, $redirect_uri);
用法
Mongo存储引擎实现了该库中支持的所有标准存储接口。请参阅了解更多信息。
Redis的
概观
使用键值存储系统在OAuth中存储和检索对象。
安装
首先,安装PHP 的
$ composer require predis/predis:dev-master
开始使用
一旦完成, 来连接到redis服务器。
$predis = new \Predis\Client();
然后,使用Redis
存储类创建存储对象:
$storage = new OAuth2\Storage\Redis($predis); // now you can perform storage functions, such as the one below $storage->setClientDetails($client_id, $client_secret, $redirect_uri);
用法
Redis存储引擎实现了该库中支持的所有标准存储接口。请参阅了解更多信息。
卡桑德拉
概观
使用数据库管理系统在OAuth中存储和检索对象。
安装
首先,为PHP 安装
$ composer require thobbs/phpcassa:dev-master
开始使用
一旦完成, 实例以连接到cassandra服务器。
$servers = array('127.0.0.1:9160'); $cassandra = new \phpcassa\Connection\ConnectionPool('oauth2_server', $servers);
然后,使用Cassandra
存储类创建存储对象:
$storage = new OAuth2\Storage\Cassandra($cassandra); // now you can perform storage functions, such as the one below $storage->setClientDetails($client_id, $client_secret, $redirect_uri);
用法
Cassandra存储引擎实现了该库中支持的所有标准存储接口。请参阅了解更多信息。
DynamoDB
概观
使用 NoSQL数据库服务在OAuth中存储和检索对象。
安装
首先,您需要安装
$ composer require aws/aws-sdk-php:dev-master
开始使用
如果您尚未创建~/.aws/credentials
文件,则这是启动DynamoDB并运行的最简单方法。
// @see http://docs.aws.amazon.com/aws-sdk-php/guide/latest/credentials.html#credential-profiles$config = array( 'profile' => 'default', 'region' => Aws\Common\Enum\Region::US_EAST_1, // Your region may differ );
或者,您可以将您的客户端配置为直接使用您的凭据运行
// These credentials are found in your AWS management console$config = array( 'key' => 'my-aws-key', 'secret' => 'my-aws-secret', 'region' => Aws\Common\Enum\Region::US_EAST_1, // Your region may differ );
接下来,通过创建您的配置数组并使用以下factory
方法来实例化AWS客户端:
$dynamo = Aws\DynamoDb\DynamoDbClient::factory($config);
最后,使用DynamoDB
存储类创建存储对象:
$storage = new OAuth2\Storage\DynamoDB($dynamo); // now you can perform storage functions, such as the one below $storage->setClientDetails($client_id, $client_secret, $redirect_uri);
要查看默认表结构的示例,请查看此库中的函数,或者使用DynamoDB的管理UI自行创建表。
用法
DynamoDB存储引擎实现了此库中支持的所有标准存储接口。请参阅了解更多信息。
自定义存储
概观
使用这个库的一个很大的好处是可以自定义你自己的存储。如果其中一个内置存储对象不符合您的需求,则可以实现下面的一个或多个接口以获取自定义存储功能。
如何自定义存储
首先,找出你想要实现的存储接口。至少,每一个的OAuth2服务器需要存储对象实施AccessTokenInterface
和ClientCredentialsInterface
,因此,如果您不能存储在内置存储类的这些使用,您的自定义存储可以用这些启动。之后,您将需要根据您的实施其他接口。例如,如果您希望服务器支持authorization_code
授予类型(最常见),并且希望自定义存储执行该操作,则还需要AuthorizationCodeInterface
在存储对象上实现 。
术语“实现”是指您的自定义存储PHP类中的类声明。例如,要为访问令牌,客户端和授权代码授权类型使用自定义存储,您的类将如下所示:
class MyCustomStorage implements OAuth2\Storage\AccessTokenInterface, OAuth2\Storage\ClientCredentialsInterface, OAuth2\Storage\AuthorizationCodeInterface { // method implementation here... }
一旦完成,您将需要编写这些接口所需的所有方法。例如,ClientCredentialsInterface
指定getClientDetails
接受$client_id
参数并返回客户端数据数组的方法。
从那里,你将传递这个新的存储对象到服务器类:
$customStorage = new MyCustomStorage();$server = new OAuth2\Server($customStorage);
不知道你需要什么方法?没问题,只需弹出打开界面类,并!如果这不是你的风格,PHP会抛出一个有用的错误消息,让你知道你的类仍然需要实现的方法,如果它丢失了。
包含在该库中的所有存储对象与“自定义存储”对象没有区别。唯一真正的区别是它们默认包含在库中,并且它们实现了所有的接口(你不需要实现所有的存储接口,只是你想要定制的接口)。
有关所有可能的存储接口,请参阅下面的接口列表,并查看 类,以获得为实现“定制”目的而实现的存储对象的示例 - 与Cassandra存储引擎进行接口。
接口
为了实现基本的访问令牌。所必需的 ,除非使用 。
ID:“access_token”
和是必需的,用于收集有关发出请求的客户端的信息。
ID:“客户端”
验证客户的凭证。这对于所有请求都是必需的,以便验证发出请求的客户端。
ID:“client_credentials”
用于实施
id:“authorization_code”
用于实现。
id:“refresh_token”
用于实现。
id:“user_credentials”
必需。
id:“jwt_bearer”
使用来实现作用域。
ID:“范围”
用于实施。
id:“public_key”
使用的界面。这需要公用密钥接口(见下),并用于验证访问令牌而不使用数据库系统。
ID:无
使用多个存储
几乎所有类型的存储都可以使用该库附带的每个存储对象。但是,您可能想要针对不同的事物使用不同类型的商店。例如,如果你想存储你的客户MySQL
,你的访问令牌MongoDB
和你的范围Memory
?
clients ---> ||MySQL Database|| access tokens ---> { { MongoDB }} scopes ---> (( Memory ))
这不是问题!您只需在创建OAuth2服务器对象时单独指定每个存储。首先创建你的存储对象:
// instantiate all your storage objects$clientStorage = new OAuth2\Storage\PDO(array( 'dsn' => 'mysql:host=localhost;dbname=test', 'username' => $user, 'password' => $pass )); $tokenStorage = new OAuth2\Storage\Mongo(array( 'host' => 'localhost', 'port' => 27017 )); $scopeStorage = new OAuth2\Storage\Memory(array( 'supported_scopes' => array( 'one-scope', 'two-scope', 'red-scope', 'blue-scope', ); ));
完成此操作后,创建OAuth服务器,并将存储对象作为关联数组传递,并将其id作为关键字:
// add your storage objects to the OAuth server$server = new OAuth2\Server(array( 'client_credentials' => $clientStorage, 'access_token' => $tokenStorage, 'scope' => $scopeStorage, ));
你也可以在创建后添加它们,如果你喜欢:
$server->addStorage($clientStorage, 'client_credentials'); $server->addStorage($tokenStorage, 'access_token'); $server->addStorage($scopeStorage, 'scope');
由于您指定了存储ID,因此库仅将这些存储用于指定的功能。
对于存储器及其ID的列表,向下滚动到界面 上的。