bde42 / oauth2-marvin
Marvin (42) OAuth 2.0 客户端提供商,用于 League OAuth2-Client
1.0.1
2015-11-05 22:39 UTC
Requires
- php: >=5.5.0
- league/oauth2-client: ~1.0
Requires (Dev)
- mockery/mockery: ~0.9
- phpunit/phpunit: ~4.0
- squizlabs/php_codesniffer: ~2.0
This package is not auto-updated.
Last update: 2024-09-28 18:47:43 UTC
README
此包为 PHP League 的 OAuth 2.0 客户端 提供了 42 OAuth 2.0 支持。
安装
要安装,请使用 composer
composer require bde42/oauth2-marvin
用法
用法与 The League 的 OAuth 客户端相同,使用 \BDE42\OAuth2\Client\Provider\Marvin
作为提供商。
授权码流
if (session('current_user') == null) { $provider = new \BDE42\OAuth2\Client\Provider\Marvin([ 'clientId' => 'your_client_id', 'clientSecret' => 'your_secret', 'redirectUri' => 'https://uri/to/redirect' ]); // If we don't have an authorization code then get one if (!isset($_GET['code'])) { $authUrl = $provider->getAuthorizationUrl(/*options*/); session(['oauth2state' => $provider->getState()]); return redirect($authUrl); // Check given state against previously stored one to mitigate CSRF attack } else if (empty($_GET['state']) || $_GET['state'] !== session('oauth2state')) { session(['oauth2state' => null]); exit('Invalid state'); } // Try to get an access token (using the authorization code grant) $token = $provider->getAccessToken('authorization_code', [ 'code' => $_GET['code'] ]); // Try to get an access token (using the authorization code grant) try { $user = $provider->getResourceOwner($token); } catch (Exception $e) { exit('Oh dear...'); } session(['current_user' => $user->getUserInfos()]); //Basic informations (uid, email, name and login) //$user->toArray() //Get all user informations whose you have the access authorization print_r ("NEW USER : ".json_encode(session('current_user'))); } else { print_r ("REGISTERED USER : ".json_encode(session('current_user'))); }
测试
$ ./vendor/bin/phpunit
贡献
有关详细信息,请参阅 CONTRIBUTING
致谢
许可协议
MIT 许可协议 (MIT)。有关更多信息,请参阅 许可文件