libcast / oauth2-authot
Authôt OAuth 2.0 客户端提供者,适用于 PHP League 的 OAuth 2.0 Client
v0.1.0
2016-06-23 12:28 UTC
Requires
- php: >=5.5.0
- league/oauth2-client: ^1.4
Requires (Dev)
- mockery/mockery: ~0.9
- phpunit/phpunit: ~4.0
This package is not auto-updated.
Last update: 2024-09-18 19:54:49 UTC
README
本软件包为 PHP League 的 OAuth 2.0 Client 提供Authôt OAuth 2.0 支持。
安装
要安装,请使用 composer
composer require libcast/oauth2-authot
使用方法
使用方法与 The League 的 OAuth 客户端相同,使用 \Libcast\OAuth2\Client\Provider\Authot
作为提供者。
授权码流程
<?php
$provider = new Libcast\OAuth2\Client\Provider\Authot([
'clientId' => '{authot-client-id}',
'clientSecret' => '{authot-client-secret}',
'redirectUri' => 'https://example.com/callback-url',
]);
if (!isset($_GET['code'])) {
// If we don't have an authorization code then get one
$authUrl = $provider->getAuthorizationUrl();
$_SESSION['oauth2state'] = $provider->getState();
header('Location: '.$authUrl);
exit;
// Check given state against previously stored one to mitigate CSRF attack
} elseif (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['oauth2state'])) {
unset($_SESSION['oauth2state']);
exit('Invalid state');
} else {
// Try to get an access token (using the authorization code grant)
$token = $provider->getAccessToken('authorization_code', [
'code' => $_GET['code']
]);
// Use this to interact with an API on the users behalf
echo $token->getToken();
}
测试
$ ./vendor/bin/phpunit tests
许可证
MIT 许可证 (MIT)。请参阅 许可证文件 获取更多信息。