teltek / pumukit-oauth2-bundle
为PHP League OAuth2-Client提供的OAuth 2.0客户端提供者
1.0.0
2021-04-12 08:08 UTC
Requires
- php: >=7.2
- league/oauth2-client: 2.2.1
- pumukit/pumukit: >=3.6
This package is auto-updated.
Last update: 2024-09-08 13:54:21 UTC
README
此包为PHP League的OAuth 2.0客户端提供OAuth 2.0支持。
安装
要安装,请使用composer
composer require teltek/pumukit-oauth2-bundle
如果没有,请将以下内容添加到config/bundles.php
Pumukit\OAuth2Bundle\PumukitOAuth2Bundle::class => ['all' => true]
然后执行以下命令
php bin/console cache:clear php bin/console cache:clear --env=prod php bin/console assets:install
使用方法
使用方法与The League的OAuth客户端相同,使用Pumukit\OAuth2Bundle\Provider\Oam
作为提供者。
授权码流程
$provider = new Pumukit\OAuth2Bundle\Provider\Oam( 'clientId' => 'your client id', 'clientSecret' => 'your client secret', 'redirectUri' => 'your redirect uri', 'urlAuthorize' => 'your url authorize', 'urlAccessToken' => 'your url access token', 'urlResourceOwnerDetails' => 'your url resource owner details', ]); if (!isset($_GET['code'])) { $options['scope'] = array('Customer.Info','UserProfile.me'); $authorizationUrl = $provider->getAuthorizationUrl($options); $_SESSION['oauth2state'] = $provider->getState(); header('Location: '.$authorizationUrl); exit; } elseif (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['oauth2state'])) { unset($_SESSION['oauth2state']); exit('Invalid state'); } else { try { $accessToken = $provider->getAccessToken('authorization_code',['code' => $_GET['code']]); $resourceOwner = $provider->getResourceOwner($accessToken); ... } catch (IdentityProviderException $e) { exit($e->getMessage()); }
作用域
如果您想发送不同的作用域,必须编辑
$options['scope'] = array('Customer.Info','UserProfile.me');