spomky-labs / oauth2-server-resource-owner-password-credentials-grant-type
Requires
Requires (Dev)
- phpunit/phpunit: ~4.5
- spomky-labs/oauth2-server-bearer-token: ~5.0.0
- spomky-labs/oauth2-server-client-manager-supervisor: ~5.0.0
- spomky-labs/oauth2-server-exception-manager: ~5.0.0
- spomky-labs/oauth2-server-password-client: ~5.0.0
- spomky-labs/oauth2-server-public-client: ~5.0.0
- spomky-labs/oauth2-server-scope-manager: ~5.0.0
- spomky-labs/oauth2-server-simple-string-access-token: ~5.0.0
Suggests
- spomky-labs/oauth2-server-refresh-token-grant-type: Refresh Token Grant Type
This package is not auto-updated.
Last update: 2015-08-15 06:32:28 UTC
README
此库为OAuth2服务器添加了一种新的授权类型:资源所有者密码凭据授权类型。
此授权类型使用资源所有者的用户名和密码直接获取访问令牌。客户端可以通过在请求体中包含参数issueRefreshToken=true
来获取刷新令牌(如果服务器支持)。
先决条件
此库需要OAuth2服务器。
安装
安装是一个简单的3步过程
- 下载并安装库
- 使用您的类扩展
- 将授权类型添加到您的OAuth2服务器
第1步:安装库
安装此库的首选方法是使用Composer
composer require "spomky-labs/oauth2-server-resource-owner-password-credentials-grant-type" "~4.0"
第2步:创建您的类
此授权类型需要一个资源所有者和一个资源所有者管理器。您可以在以下位置找到必须扩展的抽象类。
资源所有者
简单资源所有者此库提供了一个抽象类,以简化您的操作:OAuth2\ResourceOwner\OAuth2ResourceOwner
。
您可以扩展它或创建自己的类。您只需定义如何存储密码以及在请求时如何检查它。
请随意添加您需要的所有setter和getter。
以下示例中,我们存储密码的哈希(SHA-256)。
<?php
namespace ACME\MyOAuth2Server\ResourceOwner;
use OAuth2\ResourceOwner\OAuth2ResourceOwner;
class MyResourceOwner extends OAuth2ResourceOwner
{
protected $password;
public function setPassword($password)
{
$this->password = hash('sha256', $password);
return $this;
}
public function getPassword()
{
return $this->password;
}
}
带有刷新令牌扩展的资源所有者使用此授权类型,客户端可以请求刷新令牌(如果服务器支持此授权类型)并在令牌过期时获取新的访问令牌。默认情况下,此授权类型允许发布刷新令牌,但您可以根据客户端限制刷新令牌的发布。
- 通过服务器配置(见下文)
- 通过应用于资源所有者的扩展
如果您想使用此功能,则您的用户类只需实现OAuth2\ResourceOwner\IOAuth2IssueRefreshTokenExtension
。
以下示例中,只有机密客户端被允许获取刷新令牌
<?php
namespace ACME\MyOAuth2Server\ResourceOwner;
use ACME\MyOAuth2Server\ResourceOwner\MyResourceOwner;
use OAuth2\ResourceOwner\IOAuth2IssueRefreshTokenExtension;
use OAuth2\Client\IOAuth2ConfidentialClient;
class MyResourceOwnerWithExtension extends MyResourceOwner implements IOAuth2IssueRefreshTokenExtension
{
public function isRefreshTokenIssueAllowed(IOAuth2Client $client)
{
return ($client instanceof IOAuth2ConfidentialClient);
}
}
在以下示例中,默认情况下不允许任何客户端,除非它们被特别授权
<?php
namespace ACME\MyOAuth2Server\ResourceOwner;
use ACME\MyOAuth2Server\ResourceOwner\MyResourceOwner;
use OAuth2\ResourceOwner\IOAuth2IssueRefreshTokenExtension;
class MyResourceOwnerWithExtension extends MyResourceOwner implements IOAuth2IssueRefreshTokenExtension
{
protected $allowedClients = array();
public function setRefreshTokenIssueAllowed(IOAuth2Client $client, $refreshTokenAllowed)
{
$this->allowedClients[$client->getPublicId()] = $refreshTokenAllowed;
return $this;
}
public function isRefreshTokenIssueAllowed(IOAuth2Client $client)
{
return isset($this->allowedClients[$client->getPublicId()])?$this->allowedClients[$client->getPublicId()]:false;
}
}
资源所有者管理器
OAuth2服务器库提供了一个接口:OAuth2\ResourceOwner\IOAuth2ResourceOwnerManager
。
您必须实现此接口并定义如何验证资源所有者密码凭据以及获取资源所有者对象。
<?php
namespace ACME\MyOAuth2Server\ResourceOwner;
use OAuth2\ResourceOwner\IOAuth2ResourceOwner;
use OAuth2\ResourceOwner\IOAuth2ResourceOwnerManager;
class MyOAuth2ResourceOwnerManager extends OAuth2ResourceOwnerManager
{
public function addResourceOwner(IOAuth2ResourceOwner $user)
{
return $this->resourceOwners[$user->getUsername()] = $user;
}
public function removeResourceOwner(IOAuth2ResourceOwner $user)
{
if (isset($this->resourceOwners[$username]))
{
unset($this->resourceOwners[$username]);
}
}
public function checkResourceOwnerPasswordCredentials($username, $password)
{
$resourceOwner = $this->getResourceOwner($username)
if (null === $resourceOwner)
{
return false;
}
return hash('sha256', $password) === $resourceOwner->getPassword();
}
public function getResourceOwner($username)
{
if (isset($this->resourceOwners[$username]))
{
return $this->resourceOwners[$username];
}
return null;
}
}
第3步:将授权类型添加到您的OAuth2服务器
要使用此授权类型,只需扩展此库提供的抽象类。
<?php
namespace ACME\MyOAuth2Server
use ACME\MyOAuth2Server\ResourceOwner\MyOAuth2ResourceOwnerManager;
use OAuth2\Grant\OAuth2ResourceOwnerPasswordCredentialsGrantType;
class MyResourceOwnerGrantType extend OAuth2ResourceOwnerPasswordCredentialsGrantType
{
protected function getExceptionManager()
{
//Return the Exception Manager of your server
}
protected function getResourceOwnerManager()
{
//Return the resource Owner Manager you just have created
}
protected function getConfiguration()
{
//Return the configuration object of your server
}
}
现在您可以将MyResourceOwnerGrantType
实例添加到您的访问令牌端点。
配置
此授权类型添加了一个新的配置选项
allow_refresh_token_with_resource_owner_grant_type
(布尔值,默认=true)- 如果为true,则客户端请求时将发放刷新令牌。
- 如果为false,则不允许客户端使用访问令牌获取刷新令牌。注意,如果资源所有者实现了
OAuth2\ResourceOwner\IOAuth2IssueRefreshTokenExtension
,则此选项无效果。
示例
<?php
use OAuth2\Configuration\OAuth2Configuration;
$configuration = new OAuth2Configuration(array(
'allow_refresh_token_with_resource_owner_grant_type' => false,
));