samuelthomas2774/oauth-client
Composer 安装命令:
composer require samuelthomas2774/oauth-client
包简介
An OAuth 2.0 Client library with built-in support for Facebook, Google, Microsoft, Yahoo, GitHub, LinkedIn & more.
README 文档
README
An OAuth 2.0 Client library with built-in support for Facebook, Google, Microsoft, Yahoo, GitHub, LinkedIn & more.
You can see the current source running on Heroku from examples/all-2 here: https://oauth-client-test.herokuapp.com
- Microsoft
- Yahoo
- GitHub
- Spotify
- Amazon
- Disqus
- TeamViewer
- WordPress.com
- Eventbrite
- Foursquare
- SpeechMore
- GitLab
- Mastodon
- Discord
- Slack
- DigitalOcean
- Gitter
- Deezer
- DeviantArt
- Twitch
- Vimeo
- Other, just create a new
OAuth2\\GenericOAuthProviderobject and include thebase_api_endpoint,authorise_endpointandtoken_endpointoptions
Requirements and installation
This requires at least PHP 7.1.
| Version | Supported |
|---|---|
| PHP 5.6 | No |
| PHP 7.0 | No |
| PHP 7.1 | Yes |
| PHP 7.2 | Yes |
| PHP 7.3 | Yes |
Composer
- Add
"samuelthomas2774/oauth-client": "~3.0.0"to your composer.json{ "require": { "samuelthomas2774/oauth-client": "~3.0.0" } } - Run Composer
This will automatically download the latest patch version.composer install
Usage
- Include
vendor/autoload.phpin all pages that need access to any provider
This will load any class in the src directory when used.require_once __DIR__ . '/vendor/autoload.php';
- Create a new
OAuth2\\OAuthobject with the parameters$client_id,$client_secretand$options
The$optionsarray must have at leastbase_api_endpoint,authorise_endpointandtoken_endpoint.use OAuth2\GenericOAuthProvider; $client_id = 'client-id'; $client_secret = 'client-secret'; $client = new GenericOAuthProvider($client_id, $client_secret, null, [ 'session_prefix' => 'facebook_', 'base_api_endpoint' => 'https://graph.facebook.com', 'authorise_endpoint' => 'https://facebook.com/dialog/oauth', 'token_endpoint' => 'oauth/access_token', // Relative to the base API URL ]);
- To get a link to the authorise page:
$redirect_url = 'https://example.com/facebook/code.php'; $scope = ['email', 'user_friends']; // Optional scope array // Returns an OAuth2\AuthoriseUrl object // This will be converted to a string automatically if needed // You can use this object to get the OAuth2\State object to store extra state data accessible with // $client->getRequestState() when the user accepts/rejects the authorise request $login_url = $client->generateAuthoriseUrlAndState($redirect_url, $scope); // Add data to the state object // Every time generateAuthoriseUrlAndState is called a new state object will be created with separate data, // so you can have multiple authorise links on the same page and have the authorise response page react differently // depending on which link was used $login_url->getState()->next_url = '/';
// You can also set the state parameter yourself (not recommended) // $login_url->getState() will still return a state object, but the attached data won't be saved // Use $login_url->getStateId(), $login_url->getState()->getId() or cast the state object to a string to get the // value of the state parameter $state = '...'; // Generate a random value and store in somewhere the next page can access (or set to null to not set a state parameter) $login_url = $client->generateAuthoriseUrl($state, $redirect_url, $scope);
- To get an access token from the code that was returned:
// Validates the request state and returns an OAuth2\AccessToken object $token = $client->getAccessTokenFromRequestCodeAndState();
// You can also validate the state and pass the redirect url and an optional requested scope array yourself // Only do this if you set the state parameter when you generated the authorise link $redirect_url = 'https://example.com/facebook/code.php'; // Must match the $redirect_url given to generateAuthoriseUrl exactly $requested_scope = ['email', 'user_friends']; // Optional requested scope array to include in the AccessToken object $token = $client->getAccessTokenFromRequestCode($redirect_url, $requested_scope);
- To get an access token a refresh token:
// Returns an object of data returned from the server // This may include a new refresh_token // $token can be a string (a refresh token) or an AccessToken object with a refresh token returned by all // getAccessToken* methods $new_token = $oauth->getAccessTokenFromRefreshToken($token);
- To make API requests with the access token:
try { $response = $client->api('GET', 'me' /*, $guzzle_options = [] /*, $auth = null */); } catch (Exception $error) { echo 'OAuth provider returned an error: ' . print_r($error, true); }
- To get/set the current access token:
You do not need to do this at the start of the script to get the access token from the session, this is done
automatically. Also, this function updates the access token in the session.
// Get $token = $client->getAccessToken(); // Set $client->setAccessToken($new_token); // Set without updating the access token in the session $client->setAccessToken($new_token, false);
Built-in providers
Any other providers please contact me at https://samuelelliott.ml/#contact and I'll add it as soon as possible.
| Provider | Class | Sign-up url |
|---|---|---|
| Amazon | OAuth2\Providers\Amazon\Amazon | https://developer.amazon.com/lwa/sp/overview.html |
| Deezer | OAuth2\Providers\Deezer\Deezer | https://developers.deezer.com/myapps/create |
| DeviantArt | OAuth2\Providers\DeviantArt\DeviantArt | https://www.deviantart.com/developers/register |
| DigitalOcean | OAuth2\Providers\DigitalOcean\DigitalOcean | https://cloud.digitalocean.com/account/api/applications/new |
Discord P |
OAuth2\Providers\Discord\Discord | https://discordapp.com/developers/applications/ |
| Disqus | OAuth2\Providers\Disqus\Disqus | https://disqus.com/api/applications/register/ |
| Eventbrite | OAuth2\Providers\Eventbrite\Eventbrite | https://www.eventbrite.co.uk/myaccount/apps/new/ |
Facebook P |
OAuth2\Providers\Facebook\Facebook | https://developers.facebook.com/apps/ |
| Foursquare | OAuth2\Providers\Foursquare\Foursquare | https://foursquare.com/developers/apps |
GitHub P |
OAuth2\Providers\GitHub\GitHub | https://github.com/settings/applications/new |
GitLab P I |
OAuth2\Providers\GitLab\GitLab | https://gitlab.com/profile/applications * |
| Gitter | OAuth2\Providers\Gitter\Gitter | https://developer.gitter.im/apps/new |
Google P |
OAuth2\Providers\Google\Google | https://console.developers.google.com |
| OAuth2\Providers\Instagram\Instagram | https://instagram.com/developer/clients/register/ | |
LinkedIn P |
OAuth2\Providers\Linkedin\Linkedin | https://www.linkedin.com/developer/apps/new |
Mastodon P I |
OAuth2\Providers\Mastodon\Mastodon | https://mastodon.social/settings/applications/new * |
| Microsoft | OAuth2\Providers\Microsoft\Microsoft | https://account.live.com/developers/applications/create |
| OAuth2\Providers\Pinterest\Pinterest | https://developers.pinterest.com/apps/ | |
| OAuth2\Providers\Reddit\Reddit | https://www.reddit.com/prefs/apps | |
Slack P |
OAuth2\Providers\Slack\Slack | https://api.slack.com/apps |
SpeechMore P |
OAuth2\Providers\SpeechMore\SpeechMore | https://speechmore.ml/settings/api-clients |
| Spotify | OAuth2\Providers\Spotify\Spotify | https://developer.spotify.com/my-applications/#!/applications/create |
| TeamViewer | OAuth2\Providers\TeamViewer\TeamViewer | https://login.teamviewer.com/nav/api |
| Twitch | OAuth2\Providers\Twitch\Twitch | https://dev.twitch.tv/dashboard/apps/create |
| Vimeo | OAuth2\Providers\Vimeo\Vimeo | https://developer.vimeo.com/apps/new |
| WordPress.com | OAuth2\Providers\WordPress\WordPress | https://developer.wordpress.com/apps/new/ |
Yahoo P |
OAuth2\Providers\Yahoo\Yahoo | https://developer.apps.yahoo.com/dashboard/createKey.html |
* This URL is for the default instance used
All the built-in providers implement OAuth2\UserProfilesInterface, which adds an extra method to get a
OAuth2\UserProfile object.
try { $user = $client->getUserProfile(); } catch (Exception $error) { echo 'OAuth provider returned an error: ' . print_r($error, true); }
Some also implement OAuth2\UserPicturesInterface (P), which adds a method to get the URL of the user's
picture/avatar.
try { $picture_url = $client->getUserPictureUrl(); } catch (Exception $error) { echo 'OAuth provider returned an error: ' . print_r($error, true); }
As GitLab and Mastodon support multiple instances they implement OAuth2\MultipleInstancesInterface, which adds an
option and method to set the instance's URL.
use OAuth2\Providers\GitLab\GitLab; $client = new GitLab($client_id, $client_secret, null, [ 'instance_url' => 'https://gitlab.fancy.org.uk', ]); $client->setInstanceUrl('https://gitlab.com');
Additional methods for providers
Facebook and Discord have additional methods.
Extending the OAuth2 class
You can extend the OAuth2 and other classes to add new functions and make existing functions work differently:
use OAuth2\Providers\Facebook\Facebook; class My_Extended_Facebook_Class extends Facebook { // Change the base_api_endpoint option to use a different API version public $base_api_endpoint = 'https://graph.facebook.com/v1.0/'; // Add a new function for getting the current user's ID public function getUserId(): string { $user = $this->getUserProfile(['id']); return $user->id; } }
You can then use the newly created class:
$client = new My_Extended_Facebook_Class('client-id', 'client-secret'); try { echo 'Your Facebook User ID (for this app) is: ' . htmlentities($client->getUserId()); } catch (Exception $exception) { echo 'Facebook returned an error: ' . $exception->getMessage(); }
samuelthomas2774/oauth-client 适用场景与选型建议
samuelthomas2774/oauth-client 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 111 次下载、GitHub Stars 达 12, 最近一次更新时间为 2015 年 07 月 10 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「client」 「oauth」 「oauth2」 「oauth2-client」 「oauth-client」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 samuelthomas2774/oauth-client 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 samuelthomas2774/oauth-client 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 samuelthomas2774/oauth-client 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
WeChat OAuth SDK
Ory-Hydra OAuth 2.0 Client Provider for The PHP League OAuth2-Client
Library for ORCID web services
A lightweight and powerful OAuth 2.0 authorization and resource server library with support for all the core specification grants. This library will allow you to secure your API with OAuth and allow your applications users to approve apps that want to access their data from your API.
Mock PSR-18 HTTP client
Email Toolkit Plugin for CakePHP
统计信息
- 总下载量: 111
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 12
- 点击次数: 25
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-07-10