chocofamilyme/oauth2-client

PHP OAuth2.0 客户端认证库

1.0 2020-11-26 07:50 UTC

This package is auto-updated.

Last update: 2024-09-26 15:52:46 UTC


README

OAuth 2.0 认证客户端。它提供多种授权类型,使用这些类型可以获取认证数据。

安装

composer require chocofamilyme/oauth2-client

使用方法

授权类型 "password"

<?php
use Chocofamilyme\OAuth2\Client\Authentication;
use Chocofamilyme\OAuth2\Client\GrandTypes\PasswordGrandType;

$grantType = new PasswordGrandType('client_id', 'username', 'password');
$oauth2 = new Authentication($grantType, 'http://oauth.server.com/auth/token');
$authData = $oauth2->getAuthData();
var_dump($authData);

授权类型 "client_credentials"

<?php
use Chocofamilyme\OAuth2\Client\Authentication;
use Chocofamilyme\OAuth2\Client\GrandTypes\ClientCredentialsGrandType;

$grantType = new ClientCredentialsGrandType('client_id', 'client_secret');
$oauth2 = new Authentication($grantType, 'http://oauth.server.com/auth/token');
$authData = $oauth2->getAuthData();
var_dump($authData);

扩展

  • 您也可以使用自己的授权类型扩展此库,请参阅 GrantTypeInterface
  • 您还可以使用自己的 HTTP 客户端,只需实现 HttpClientInterface 即可。
  • 如果您的 OAuth 2.0 服务器返回的响应格式不是此库可以理解的,只需编写自己的响应类,该类必须实现 ResponseInterface

然后每个自定义模块都可以在认证类的构造函数中使用。

new \Chocofamilyme\OAuth2\Client\Authentication(GrantTypeInterface $grantType, string $authTokenUrl, HttpClientInterface $httpClient = null, ResponseInterface $responseInterface = null)