jampire / oauth2-appid
为PHP League OAuth2 Client提供的IBM App ID OAuth 2.0客户端提供程序
2.0.8
2020-06-04 18:12 UTC
Requires
- php: ^7.0
- league/oauth2-client: ^2.4
Requires (Dev)
- ext-json: *
- jakub-onderka/php-console-highlighter: ^0.4.0
- jakub-onderka/php-parallel-lint: ^1.0
- mockery/mockery: ^1.3
- phpunit/phpunit: ^5.7|^6.0|^8.4
- roave/security-advisories: dev-master
- squizlabs/php_codesniffer: ^2.3|^3.5
README
此包为PHP League的OAuth 2.0 Client提供IBM App ID OAuth 2.0支持。请参阅此页面以获取完整文档。
安装
要安装,请使用composer
composer require jampire/oauth2-appid
用法
用法与The League的OAuth客户端相同,使用\Jampire\OAuth2\Client\Provider\AppIdProvider
作为提供者。
使用baseAuthUri
指定IBM App ID基本服务器URL。您可以从IBM App ID服务的应用设置中的oAuthServerUrl
查找正确的值,去掉tenantId
部分,例如https://us-south.appid.cloud.ibm.com/oauth/v4
。
使用tenantId
指定IBM App ID租户ID。您可以从IBM App ID服务的应用设置中的tenantId
查找正确的值,例如abcd-efgh-1234-5678-mnop
。
所有其他值您都可以在IBM App ID服务的应用设置中找到。
不要忘记在IBM App ID白名单中注册您的重定向URL。请参阅IBM App ID 文档。
授权码流
<?php require_once __DIR__ . '/vendor/autoload.php'; use Jampire\OAuth2\Client\Provider\AppIdProvider; use Jampire\OAuth2\Client\Provider\AppIdException; session_start(); try { $provider = new AppIdProvider([ 'baseAuthUri' => '{baseAuthUri}', 'tenantId' => '{tenantId}', 'clientId' => '{clientId}', 'clientSecret' => '{clientSecret}', 'redirectUri' => '{redirectUri}', ]); } catch (AppIdException $e) { exit('Failed to create provider: ' . $e->getMessage()); } if (!isset($_GET['code'])) { // If we don't have an authorization code then get one // Fetch the authorization URL from the provider; this returns the // urlAuthorize option and generates and applies any necessary parameters // (e.g. state). $authorizationUrl = $provider->getAuthorizationUrl(); // Get the state generated for you and store it to the session. $_SESSION['oauth2state'] = $provider->getState(); // Redirect the user to the authorization URL. header('Location: ' . $authorizationUrl); exit; } if (empty($_GET['state']) || (isset($_SESSION['oauth2state']) && $_GET['state'] !== $_SESSION['oauth2state'])) { // Check given state against previously stored one to mitigate CSRF attack if (isset($_SESSION['oauth2state'])) { unset($_SESSION['oauth2state']); } exit('Invalid state'); } try { // Try to get an access token using the authorization code grant. $accessToken = $provider->getAccessToken('authorization_code', [ 'code' => $_GET['code'] ]); // We have an access token, which we may use in authenticated // requests against the service provider's API. echo '<b>Access Token:</b> ', $accessToken->getToken(), '<br>'; echo '<b>Refresh Token:</b> ', $accessToken->getRefreshToken(), '<br>'; echo '<b>Expired in:</b> ', $accessToken->getExpires(), '<br>'; echo '<b>Already expired?</b> ', ($accessToken->hasExpired() ? 'expired' : 'not expired'), '<br>'; // Using the access token, we may look up details about the // resource owner. $resourceOwner = $provider->getResourceOwner($accessToken); } catch (Exception $e) { // Failed to get the access token or user details. exit($e->getMessage()); }
示例
测试
$ ./vendor/bin/phpunit
贡献
请参阅CONTRIBUTING以获取详细信息。
致谢
许可
MIT许可(MIT)。请参阅许可文件以获取更多信息。