vdbelt / oauth2-revolut
OAuth 2.0 客户端的 Revolut 提供商
v1.0.4
2022-03-22 12:54 UTC
Requires
- lcobucci/jwt: ^4.0
- league/oauth2-client: ^2.4.1
Requires (Dev)
- ext-json: *
- mockery/mockery: ^1.3
- phpunit/phpunit: ^7.5 || ^8.0 || ^9.0
- squizlabs/php_codesniffer: ^3.5
README
此软件包为 PHP League 的 OAuth 2.0 客户端 提供Revolut OAuth 2.0 支持。
安装
要安装,请使用 composer
composer require vdbelt/oauth2-revolut
用法
用法与 The League 的 OAuth 客户端相同,使用 \League\OAuth2\Client\Provider\Revolut 作为提供者。
生成密钥对
按照Revolut API 文档中描述的步骤开始生成密钥对。
openssl genrsa -out privatekey.pem 1024
openssl req -new -x509 -key privatekey.pem -out publickey.cer -days 1825
通过 Revolut for Business API 设置页面上传公钥,并将私钥存储在安全的地方。
授权码流
$provider = new League\OAuth2\Client\Provider\Revolut([ 'clientId' => '{revolut-client-id}', 'privateKey' => 'file://{revolut-private-key-path}', 'redirectUri' => 'https://example.com/callback-url' // equal to redirect URI provided to Revolut 'isSandbox' => false ]); 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'] ]); // Store the token somewhere safe. Note that the token is valid for 40 minutes. // After 40 minutes, you can request a new access token based on the refresh token (valid for 90 days): if($token->hasExpired()) { $newToken = $provider->getAccessToken('refresh_token', [ 'refresh_token' => $token->getRefreshToken() ]); } // Use this to interact with the API on the users behalf echo $token->getToken(); }
测试
$ ./vendor/bin/phpunit
贡献
有关详细信息,请参阅贡献指南。
鸣谢
许可证
MIT 许可证(MIT)。有关更多信息,请参阅许可证文件。