socialite
Socialite is an OAuth2 Authentication tool. It is inspired by laravel/socialite, you can easily use it without Laravel.
Top Related Projects
Laravel wrapper around OAuth 1 & OAuth 2 libraries.
Open source social sign on PHP Library. HybridAuth goal is to act as an abstract api between your application and various social apis and identities providers such as Facebook, Twitter and Google.
Easy integration with OAuth 2.0 service providers.
Quick Overview
Socialite is a PHP library that provides an easy-to-use interface for OAuth authentication with various social platforms. It simplifies the process of integrating social login functionality into web applications, supporting multiple providers such as Facebook, Google, Twitter, and more.
Pros
- Easy to use and integrate with existing PHP projects
- Supports a wide range of social media platforms and OAuth providers
- Extensible architecture allows for adding custom providers
- Well-documented and actively maintained
Cons
- Requires PHP 7.0 or higher, which may not be suitable for older projects
- Some providers may require additional configuration or API keys
- Limited to OAuth-based authentication methods
Code Examples
- Basic usage with GitHub provider:
use Overtrue\Socialite\SocialiteManager;
$config = [
'github' => [
'client_id' => 'your-github-app-id',
'client_secret' => 'your-github-app-secret',
'redirect' => 'http://localhost/socialite/callback.php',
],
];
$socialite = new SocialiteManager($config);
$url = $socialite->create('github')->redirect();
- Handling the callback:
$user = $socialite->create('github')->user();
echo $user->getId();
echo $user->getName();
echo $user->getEmail();
- Using a custom provider:
$socialite->extend('myProvider', function($config) {
return new MyCustomProvider($config);
});
$user = $socialite->create('myProvider')->user();
Getting Started
- Install Socialite using Composer:
composer require overtrue/socialite
- Set up your configuration:
$config = [
'github' => [
'client_id' => 'your-github-app-id',
'client_secret' => 'your-github-app-secret',
'redirect' => 'http://localhost/socialite/callback.php',
],
];
$socialite = new Overtrue\Socialite\SocialiteManager($config);
- Implement the authentication flow in your application:
// Redirect to provider
$url = $socialite->create('github')->redirect();
// Handle callback
$user = $socialite->create('github')->user();
Competitor Comparisons
Laravel wrapper around OAuth 1 & OAuth 2 libraries.
Pros of Socialite
- Tightly integrated with Laravel framework, offering seamless authentication
- Extensive documentation and community support within Laravel ecosystem
- Built-in support for popular OAuth providers like Facebook, Twitter, Google
Cons of Socialite
- Limited to Laravel applications, not suitable for other PHP frameworks
- Fewer supported providers out-of-the-box compared to Socialite
- Less flexibility for customization outside Laravel's conventions
Code Comparison
Socialite:
return Socialite::driver('github')->redirect();
Socialite:
$user = Socialite::driver('github')
->scopes(['read:user', 'public_repo'])
->user();
Both libraries provide a similar API for OAuth authentication, but Socialite is more tightly coupled with Laravel's ecosystem, while Socialite offers greater flexibility across different PHP projects.
Socialite is ideal for Laravel developers seeking a seamless social authentication solution within their framework. On the other hand, Socialite is better suited for developers working with various PHP frameworks or seeking more customization options and a wider range of supported providers.
Open source social sign on PHP Library. HybridAuth goal is to act as an abstract api between your application and various social apis and identities providers such as Facebook, Twitter and Google.
Pros of Hybridauth
- Supports a wider range of social networks and identity providers (60+)
- More mature project with a longer history and larger community
- Offers more detailed documentation and examples
Cons of Hybridauth
- Slightly more complex setup and configuration
- Less frequent updates and releases compared to Socialite
- Heavier codebase, which may impact performance for simpler use cases
Code Comparison
Socialite:
$user = Socialite::driver('github')->user();
Hybridauth:
$hybridauth = new Hybridauth($config);
$adapter = $hybridauth->authenticate('GitHub');
$user = $adapter->getUserProfile();
Both libraries aim to simplify social authentication in PHP applications, but they differ in their approach and scope. Socialite focuses on Laravel integration and offers a more streamlined API for common social providers. Hybridauth, on the other hand, provides a more comprehensive solution with support for a broader range of providers and more flexibility in configuration.
Socialite is generally easier to set up and use, especially for Laravel developers, while Hybridauth offers more advanced features and customization options. The choice between the two depends on the specific requirements of your project, such as the number of social providers needed and the level of integration with your existing framework.
Easy integration with OAuth 2.0 service providers.
Pros of oauth2-client
- More extensive provider support, including many niche providers
- Highly modular and extensible architecture
- Detailed documentation and examples for custom provider implementation
Cons of oauth2-client
- Steeper learning curve due to its more complex structure
- Requires more setup and configuration for each provider
- Less opinionated, which may lead to inconsistent implementations across projects
Code Comparison
oauth2-client:
$provider = new \League\OAuth2\Client\Provider\GenericProvider([
'clientId' => 'your-client-id',
'clientSecret' => 'your-client-secret',
'redirectUri' => 'https://example.com/callback-url',
'urlAuthorize' => 'https://example.com/oauth/authorize',
'urlAccessToken' => 'https://example.com/oauth/token',
'urlResourceOwnerDetails' => 'https://example.com/api/user'
]);
socialite:
$user = Socialite::driver('github')->user();
Summary
oauth2-client offers more flexibility and provider support but requires more setup and has a steeper learning curve. Socialite provides a simpler, more opinionated approach with easier implementation for common providers. Choose based on your project's specific needs and complexity requirements.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Socialite
Socialite æ¯ä¸ä¸ª OAuth2 认è¯å·¥å ·ã å®ççµææ¥æºäº laravel/socialite ï¼ ä½ å¯ä»¥å¾è½»æçå¨ä»»ä½ PHP 项ç®ä¸ä½¿ç¨å®ãè±æææ¡£
è¯¥å·¥å ·ç°å·²æ¯æå¹³å°æï¼Facebookï¼Githubï¼Googleï¼Linkedinï¼Outlookï¼QQï¼TAPDï¼æ¯ä»å®ï¼æ·å®ï¼ç¾åº¦ï¼ééï¼å¾®åï¼å¾®ä¿¡ï¼æé³ï¼é£ä¹¦ï¼Larkï¼è±ç£ï¼ä¼ä¸å¾®ä¿¡ï¼è ¾è®¯äºï¼Lineï¼Giteeï¼Codingã
å¦æä½ å欢æç项ç®å¹¶æ³æ¯ææï¼ç¹å»è¿é :heart:
çæ¬è¦æ±
PHP >= 8.0.2
å®è£
composer require "overtrue/socialite" -vvv
使ç¨æå
ç¨æ·åªéè¦å建ç¸åºé ç½®åéï¼ç¶åéè¿å·¥å ·ä¸ºå个平å°å建认è¯åºç¨ï¼å¹¶è½»æ¾è·å该平å°ç access_token åç¨æ·ç¸å ³ä¿¡æ¯ãå·¥å ·å®ç°é»è¾è¯¦è§åç §åå¤§å¹³å° OAuth2 ææ¡£ã
å·¥å ·ä½¿ç¨å¤§è´å为以ä¸å æ¥ï¼
- é 置平å°è®¾ç½®
- å建对åºå¹³å°åºç¨
- 让ç¨æ·è·³è½¬è³å¹³å°è®¤è¯
- æå¡å¨æ¶å°å¹³å°åè° Codeï¼ä½¿ç¨ Code æ¢åå¹³å°å¤ç¨æ·ä¿¡æ¯ï¼å æ¬ access_tokenï¼
为 Laravel ç¨æ·å建çæ´æ¹ä¾¿çæ´åçå ï¼ overtrue/laravel-socialite
authorize.php
: 让ç¨æ·è·³è½¬è³å¹³å°è®¤è¯
<?php
use Overtrue\Socialite\SocialiteManager;
$config = [
'github' => [
'client_id' => 'your-app-id',
'client_secret' => 'your-app-secret',
'redirect' => 'http://localhost/socialite/callback.php',
],
];
$socialite = new SocialiteManager($config);
$url = $socialite->create('github')->redirect();
return redirect($url);
callback.php
:
<?php
use Overtrue\Socialite\SocialiteManager;
$config = [
'github' => [
'client_id' => 'your-app-id',
'client_secret' => 'your-app-secret',
'redirect' => 'http://localhost/socialite/callback.php',
],
];
$socialite = new SocialiteManager($config);
$code = request()->query('code');
$user = $socialite->create('github')->userFromCode($code);
$user->getId(); // 1472352
$user->getNickname(); // "overtrue"
$user->getUsername(); // "overtrue"
$user->getName(); // "å®æ£è¶
"
$user->getEmail(); // "anzhengchao@gmail.com"
...
é ç½®
为æ¯ä¸ªå¹³å°è®¾ç½®ç¸åçé®å¼å¯¹åå°±è½å¼ç®±å³ç¨ï¼client_id
, client_secret
, redirect
.
示ä¾ï¼
$config = [
'weibo' => [
'client_id' => 'your-app-id',
'client_secret' => 'your-app-secret',
'redirect' => 'http://localhost/socialite/callback.php',
],
'facebook' => [
'client_id' => 'your-app-id',
'client_secret' => 'your-app-secret',
'redirect' => 'http://localhost/socialite/callback.php',
],
];
èªå®ä¹åºç¨å
ä½ å¯ä»¥ä½¿ç¨ä»»æä½ å欢çåå对æ¯ä¸ªå¹³å°è¿è¡å½åï¼æ¯å¦è¯´ foo
ï¼ éç¨å«åçæ¹æ³åéè¦å¨é
ç½®ä¸å¤è®¾ç½®ä¸ä¸ª provider
é®ï¼è¿æ ·æè½åè¯å·¥å
·å
å¦ä½æ£ç¡®æ¾å°ä½ æ³è¦çç¨åºï¼
$config = [
// 为 github åºç¨èµ·å«å为 foo
'foo' => [
'provider' => 'github', // <-- provider name
'client_id' => 'your-app-id',
'client_secret' => 'your-app-secret',
'redirect' => 'http://localhost/socialite/callback.php',
],
// å¦å¤ä¸ä¸ªååå«å bar ç github åºç¨
'bar' => [
'provider' => 'github', // <-- provider name
'client_id' => 'your-app-id',
'client_secret' => 'your-app-secret',
'redirect' => 'http://localhost/socialite/callback.php',
],
//...
];
$socialite = new SocialiteManager($config);
$appFoo = $socialite->create('foo');
$appBar = $socialite->create('bar');
æ©å±èªå®ä¹æå¡æä¾ç¨åº
ä½ å¯ä»¥å¾å®¹æçä»èªå®ä¹çæå¡æä¾ä¸å建åºç¨ï¼åªéè¦éµå¾ªå¦ä¸ä¸¤ç¹ï¼
-
使ç¨èªå®ä¹å建å¨
å¦ä¸ä»£ç æ示ï¼ä¸º foo åºç¨å®ä¹äºæå¡æä¾åï¼ä½æ¯å·¥å ·æ¬èº«è¿æªæ¯æï¼æ以使ç¨å建å¨
extend()
ï¼ä»¥éå å½æ°çå½¢å¼ä¸ºè¯¥æå¡æä¾å建ä¸ä¸ªå®ä¾ã
$config = [
'foo' => [
'provider' => 'myprovider', // <-- ä¸ä¸ªå·¥å
·è¿æªæ¯æçæå¡æä¾ç¨åº
'client_id' => 'your-app-id',
'client_secret' => 'your-app-secret',
'redirect' => 'http://localhost/socialite/callback.php',
],
];
$socialite = new SocialiteManager($config);
$socialite->extend('myprovider', function(array $config) {
return new MyCustomProvider($config);
});
$app = $socialite->create('foo');
- 使ç¨æå¡æä¾ç±»
[!IMPORTANT]
ðð» ä½ çèªå®ä¹æå¡æä¾ç±»å¿ é¡»å®ç°
Overtrue\Socialite\Contracts\ProviderInterface
æ¥å£
class MyCustomProvider implements \Overtrue\Socialite\Contracts\ProviderInterface
{
//...
}
æ¥ä¸æ¥ä¸º provider
设置该类å让工å
·å¯ä»¥æ¾å°è¯¥ç±»å¹¶å®ä¾åï¼
$config = [
'foo' => [
'provider' => MyCustomProvider::class, // <-- ç±»å
'client_id' => 'your-app-id',
'client_secret' => 'your-app-secret',
'redirect' => 'http://localhost/socialite/callback.php',
],
];
$socialite = new SocialiteManager($config);
$app = $socialite->create('foo');
å¹³å°
ä¸åçå¹³å°æä¸åçé ç½®æ¹æ³ï¼ä¸ºäºç¡®ä¿å·¥å ·çæ£å¸¸è¿è¡ï¼æ以请确ä¿ä½ æ使ç¨çå¹³å°çé ç½®é½æ¯å¦æ设置çã
æ¯ä»å®
请æå¦ä¸æ¹å¼é ç½®
$config = [
'alipay' => [
// è¿ä¸ªé®åè¿è½åå®æ¹ææ¡£é£æ ·å«å 'app_id'
'client_id' => 'your-app-id',
// è¯·æ ¹æ®å®æ¹ææ¡£ï¼å¨å®æ¹ç®¡çåå°é
ç½® RSA2
// 注æï¼ è¿æ¯ä½ èªå·±çç§é¥
// 注æï¼ ä¸å
许ç§é¥å
容æå
¶ä»å符
// å»ºè®®ï¼ ä¸ºäºä¿è¯å®å
¨ï¼ä½ å¯ä»¥å°ææ¬ä¿¡æ¯ä»ç£çæ件ä¸è¯»åï¼èä¸æ¯å¨è¿éææ
'rsa_private_key' => 'your-rsa-private-key',
// ç¡®ä¿è¿éçå¼ä¸ä½ å¨æå¡åå°ç»å®çå°åå¼ä¸è´
// è¿ä¸ªé®åè¿è½åå®æ¹ææ¡£é£æ ·å«å 'redirect_url'
'redirect' => 'http://localhost/socialite/callback.php',
// æ²ç®±æ¨¡å¼æ¥å
¥å°åè§ https://opendocs.alipay.com/open/220/105337#%E5%85%B3%E4%BA%8E%E6%B2%99%E7%AE%B1
'sandbox' => false,
]
...
];
$socialite = new SocialiteManager($config);
$user = $socialite->create('alipay')->userFromCode('here is auth code');
// 详è§ææ¡£åé¢ "User interface"
$user->getId(); // 1472352
$user->getNickname(); // "overtrue"
$user->getUsername(); // "overtrue"
$user->getName(); // "å®æ£è¶
"
...
æ¬å·¥å ·ææ¶åªæ¯æ RSA2 个人ç§é¥è®¤è¯æ¹å¼ã
éé
å¦ææ¡£æ示
注æï¼è¯¥å·¥å ·ä» æ¯æ QR code è¿æ¥å°ç¬¬ä¸æ¹ç½ç«ï¼ç¨æ¥è·åç¨æ·ä¿¡æ¯ï¼opeidï¼ unionid å nicknameï¼
$config = [
'dingtalk' => [
// or 'app_id'
'client_id' => 'your app id',
// or 'app_secret'
'client_secret' => 'your app secret',
// or 'redirect_url'
'redirect' => 'redirect URL'
]
];
$socialite = new SocialiteManager($config);
$user = $socialite->create('dingtalk')->userFromCode('here is auth code');
// 详è§ææ¡£åé¢ "User interface"
$user->getId(); // 1472352
$user->getNickname(); // "overtrue"
$user->getUsername(); // "overtrue"
$user->getName(); // "å®æ£è¶
"
...
æé³
注æï¼ ä½¿ç¨æé³æå¡æä¾çæ¶åï¼å¦æä½ æ³ç´æ¥ä½¿ç¨ access_token è·åç¨æ·ä¿¡æ¯æ¶ï¼è¯·å 设置 openidã å è°ç¨
withOpenId()
åè°ç¨userFromToken()
$config = [
'douyin' => [
'client_id' => 'your app id',
'client_secret' => 'your app secret',
'redirect' => 'redirect URL'
]
];
$socialite = new SocialiteManager($config);
$user = $socialite->create('douyin')->userFromCode('here is auth code');
$user = $socialite->create('douyin')->withOpenId('openId')->userFromToken('here is the access token');
头æ¡
注æï¼ ä½¿ç¨
头æ¡
æå¡æä¾çæ¶åï¼å¦æä½ æ³ç´æ¥ä½¿ç¨ access_token è·åç¨æ·ä¿¡æ¯æ¶ï¼è¯·å 设置 openidã å è°ç¨withOpenId()
åè°ç¨userFromToken()
$config = [
'toutiao' => [
'client_id' => 'your app id',
'client_secret' => 'your app secret',
'redirect' => 'redirect URL'
]
];
$socialite = new SocialiteManager($config);
$user = $socialite->create('toutiao')->userFromCode('here is auth code');
$user = $socialite->create('toutiao')->withOpenId('openId')->userFromToken('here is the access token');
西ç
注æï¼ ä½¿ç¨
西ç
æå¡æä¾çæ¶åï¼å¦æä½ æ³ç´æ¥ä½¿ç¨ access_token è·åç¨æ·ä¿¡æ¯æ¶ï¼è¯·å 设置 openidã å è°ç¨withOpenId()
åè°ç¨userFromToken()
$config = [
'xigua' => [
'client_id' => 'your app id',
'client_secret' => 'your app secret',
'redirect' => 'redirect URL'
]
];
$socialite = new SocialiteManager($config);
$user = $socialite->create('xigua')->userFromCode('here is auth code');
$user = $socialite->create('xigua')->withOpenId('openId')->userFromToken('here is the access token');
ç¾åº¦
å
¶ä»é
置没å¥åºå«ï¼å¨ç¨æ³ä¸ï¼å¯ä»¥å¾è½»æçéæ©éå®åç»å½é¡µé¢ç模å¼ï¼éè¿ withDisplay()
- pageï¼å ¨å±å½¢å¼çææé¡µé¢ (é»è®¤)ï¼éç¨äº web åºç¨ã
- popup: å¼¹æ¡å½¢å¼çææ页é¢ï¼éç¨äºæ¡é¢è½¯ä»¶åºç¨å web åºç¨ã
- dialog: æµ®å±å½¢å¼çææ页é¢ï¼åªè½ç¨äºç«å web åºç¨ã
- mobile: Iphone/Android çæºè½ç§»å¨ç»ç«¯ä¸ç¨çææ页é¢ï¼éç¨äº Iphone/Android çæºè½ç§»å¨ç»ç«¯ä¸çåºç¨ã
- tv: çµè§çè¶ å¤§æ¾ç¤ºå±ä½¿ç¨çææ页é¢ã
- pad: IPad/Android çæºè½å¹³æ¿çµè使ç¨çææ页é¢ã
$authUrl = $socialite->create('baidu')->withDisplay('mobile')->redirect();
popup
模å¼æ¯å·¥å
·å
é»è®¤ç使ç¨æ¨¡å¼ãbasic
æ¯é»è®¤ä½¿ç¨ç scopes å¼ã
é£ä¹¦
éè¿ä¸äºç®åçæ¹æ³é ç½® app_ticket å°±è½ä½¿ç¨å é¨åºç¨æ¨¡å¼
$config = [
'feishu' => [
// or 'app_id'
'client_id' => 'your app id',
// or 'app_secret'
'client_secret' => 'your app secret',
// or 'redirect_url'
'redirect' => 'redirect URL',
// å¦æä½ æ³ä½¿ç¨ä½¿ç¨å
é¨åºç¨çæ¹å¼è·å app_access_token
// 对è¿ä¸ªé®è®¾ç½®äº 'internal' å¼é£ä¹ä½ å·²ç»å¼å¯äºå
é¨åºç¨æ¨¡å¼
'app_mode' => 'internal'
]
];
$socialite = new SocialiteManager($config);
$feishuDriver = $socialite->create('feishu');
$feishuDriver->withInternalAppMode()->userFromCode('here is code');
$feishuDriver->withDefaultMode()->withAppTicket('app_ticket')->userFromCode('here is code');
Lark
éè¿ä¸äºç®åçæ¹æ³é ç½® app_ticket å°±è½ä½¿ç¨å é¨åºç¨æ¨¡å¼
$config = [
'lark' => [
// or 'app_id'
'client_id' => 'your app id',
// or 'app_secret'
'client_secret' => 'your app secret',
// or 'redirect_url'
'redirect' => 'redirect URL',
// å¦æä½ æ³ä½¿ç¨ä½¿ç¨å
é¨åºç¨çæ¹å¼è·å app_access_token
// 对è¿ä¸ªé®è®¾ç½®äº 'internal' å¼é£ä¹ä½ å·²ç»å¼å¯äºå
é¨åºç¨æ¨¡å¼
'app_mode' => 'internal'
]
];
$socialite = new SocialiteManager($config);
$larkDriver = $socialite->create('lark');
$larkDriver->withInternalAppMode()->userFromCode('here is code');
$larkDriver->withDefaultMode()->withAppTicket('app_ticket')->userFromCode('here is code');
æ·å®
å
¶ä»é
ç½®ä¸å
¶ä»å¹³å°çä¸æ ·ï¼ä½ è½éæ©ä½ æ³è¦å±ç¤ºçéå®å页é¢ç±»åéè¿ä½¿ç¨ withView()
$authUrl = $socialite->create('taobao')->withView('wap')->redirect();
web
模å¼æ¯å·¥å
·é»è®¤ä½¿ç¨çå±ç¤ºæ¹å¼ï¼ user_info
æ¯é»è®¤ä½¿ç¨ç scopes èå´å¼ã
微信
æ们æ¯æå¼æ¾å¹³å°ä»£è¡¨å ¬ä¼å·è¿è¡ç¬¬ä¸æ¹å¹³å°ç½é¡µææã
ä½ åªéè¦åä¸é¢è¿æ ·è¾å ¥ä½ çé ç½®ãå®æ¹è´¦å·ä¸éè¦ææã
...
[
'wechat' =>
[
'client_id' => 'client_id',
'client_secret' => 'client_secret',
'redirect' => 'redirect-url',
// å¼æ¾å¹³å° - 第ä¸æ¹å¹³å°æé
'component' => [
// or 'app_id', 'component_app_id' as key
'id' => 'component-app-id',
// or 'app_token', 'access_token', 'component_access_token' as key
'token' => 'component-access-token',
]
]
],
...
Coding
æ¨éè¦é¢å¤é
ç½® team_url
为æ¨çå¢éååï¼ä¾å¦ï¼
$config = [
'coding' => [
'team_url' => 'https://{your-team}.coding.net',
'client_id' => 'your app id',
'client_secret' => 'your app secret',
'redirect' => 'redirect URL',
]
];
PayPal
æ¨å¯è½éè¦è®¾ç½®responseTypeï¼å¯ä»¥ä½¿ç¨withResponseType
å½æ°è¿è¡è®¾ç½®ï¼é»è®¤æ¯code
è¿å¯ä»¥è®¾ç½®ä¸ºid_token
æcode
& id_token
https://developer.paypal.com/docs/log-in-with-paypal/integrate/generate-button/
$config = [
'paypal' => [
'client_id' => 'AT******************',
'client_secret' => 'EK**************',
'sandbox' => false,
'redirect_url'=>"nativexo://paypalpay",
],
];
å ¶ä»ä¸äºæå·§
Scopes
å¨éå®åç¨æ·ä¹åï¼æ¨è¿å¯ä»¥ä½¿ç¨ scopes()
æ¹æ³å¨è¯·æ±ä¸è®¾ç½® âèå´âãæ¤æ¹æ³å°è¦çææç°æçä½ç¨åï¼
$response = $socialite->create('github')
->scopes(['scope1', 'scope2'])->redirect();
Redirect URL
ä½ ä¹å¯ä»¥å¨æ设置' redirect_uri 'ï¼ä½ å¯ä»¥ä½¿ç¨ä»¥ä¸æ¹æ³æ¥æ¹å redirect_uri
URL:
$url = 'your callback url.';
$socialite->redirect($url);
// or
$socialite->withRedirectUrl($url)->redirect();
State
ä½ çåºç¨ç¨åºå¯ä»¥ä½¿ç¨ä¸ä¸ªç¶æåæ°æ¥ç¡®ä¿ååºå±äºåä¸ä¸ªç¨æ·åèµ·ç请æ±ï¼ä»èé²æ¢è·¨ç«è¯·æ±ä¼ªé (CSFR) æ»å»ãå½æ¶ææ»å»è 欺éªç¨æ·æ§è¡ä¸éè¦çæä½ (åªæç¨æ·ææå¨åä¿¡ä»»ç web åºç¨ç¨åºä¸æ§è¡) æ¶ï¼å°±ä¼åç CSFR æ»å»ï¼æææä½é½å°å¨ä¸æ¶åæè¦åç¨æ·çæ åµä¸å®æã
è¿éæä¸ä¸ªæç®åçä¾åï¼è¯´æäºå¦ä½æä¾ç¶æå¯ä»¥è®©ä½ çåºç¨ç¨åºæ´å®å ¨ãå¨æ¬ä¾ä¸ï¼æ们使ç¨ä¼è¯ ID ä½ä¸ºç¶æåæ°ï¼ä½æ¯æ¨å¯ä»¥ä½¿ç¨æ¨æ³è¦ä¸ºç¶æå建å¼çä»»ä½é»è¾ã
带ç state
åæ°çéå®å
<?php
session_start();
$config = [
//...
];
// Assign to state the hashing of the session ID
$state = hash('sha256', session_id());
$socialite = new SocialiteManager($config);
$url = $socialite->create('github')->withState($state)->redirect();
return redirect($url);
æ£éªåè°ç state
ä¸æ¦ç¨æ·ææä½ çåºç¨ç¨åºï¼ç¨æ·å°è¢«éå®ååä½ çåºç¨ç¨åºç redirect_uriãOAuth æå¡å¨å°ä¸å ä¿®æ¹å°è¿åç¶æåæ°ãæ£æ¥ redirect_uri ä¸æä¾çç¶ææ¯å¦ä¸åºç¨ç¨åºçæçç¶æç¸å¹é ï¼
<?php
session_start();
$state = request()->query('state');
$code = request()->query('code');
// Check the state received with current session id
if ($state != hash('sha256', session_id())) {
exit('State does not match!');
}
$user = $socialite->create('github')->userFromCode($code);
// authorized
æ¥çæ´å¤å
³äº state
åæ°çææ¡£
å ¶ä»çä¸äºåæ°
è¦å¨è¯·æ±ä¸å
å«ä»»ä½å¯éåæ°ï¼è°ç¨ with()
æ¹æ³ä¼ å
¥ä¸ä¸ªä½ æ³è¦è®¾ç½®çå
³èæ°ç»ï¼
$response = $socialite->create('google')
->with(['hd' => 'example.com'])->redirect();
User interface
æ åç user api
$user = $socialite->create('github')->userFromCode($code);
{
"id": 1472352,
"nickname": "overtrue",
"name": "å®æ£è¶
",
"email": "anzhengchao@gmail.com",
"avatar": "https://avatars.githubusercontent.com/u/1472352?v=3",
"raw": {
"login": "overtrue",
"id": 1472352,
"avatar_url": "https://avatars.githubusercontent.com/u/1472352?v=3",
"gravatar_id": "",
"url": "https://api.github.com/users/overtrue",
"html_url": "https://github.com/overtrue",
...
},
"token_response": {
"access_token": "5b1dc56d64fffbd052359f032716cc4e0a1cb9a0",
"token_type": "bearer",
"scope": "user:email"
}
}
ä½ å¯ä»¥åè¿æ ·ä»¥æ°ç»é®çå½¢å¼è·å user å±æ§ï¼
$user['id']; // 1472352
$user['nickname']; // "overtrue"
$user['name']; // "å®æ£è¶
"
$user['email']; // "anzhengchao@gmail.com"
...
æè
使ç¨è¯¥ User
对象çæ¹æ³ï¼
mixed $user->getId();
?string $user->getNickname();
?string $user->getName();
?string $user->getEmail();
?string $user->getAvatar();
?string $user->getRaw();
?string $user->getAccessToken();
?string $user->getRefreshToken();
?int $user->getExpiresIn();
?array $user->getTokenResponse();
ä» OAuth API ååºä¸åå¾åå§æ°æ®
$user->getRaw()
æ¹æ³ä¼è¿åä¸ä¸ª arrayã
å½ä½ ä½¿ç¨ userFromCode() æ³è¦è·å token ååºçåå§æ°æ®
$user->getTokenResponse()
æ¹æ³ä¼è¿åä¸ä¸ª array éé¢æ¯ååºä»è·å token æ¶å API è¿åçååºã
注æï¼å½ä½ 使ç¨
userFromCode()
æ¶ï¼è¿ä¸ªæ¹æ³åªè¿åä¸ä¸ª ææçæ°ç»ï¼å¦åå°è¿å nullï¼å 为userFromToken()
没æ token ç HTTP ååºã
éè¿ access token è·åç¨æ·ä¿¡æ¯
$accessToken = 'xxxxxxxxxxx';
$user = $socialite->userFromToken($accessToken);
Enjoy it! :heart:
åç §
- Alipay - ç¨æ·ä¿¡æ¯ææ
- DingTalk - æ«ç ç»å½ç¬¬ä¸æ¹ç½ç«
- Google - OpenID Connect
- Github - Authorizing OAuth Apps
- Facebook - Graph API
- Linkedin - Authenticating with OAuth 2.0
- å¾®å - OAuth 2.0 æææºå¶è¯´æ
- QQ - OAuth 2.0 ç»å½ QQ
- è ¾è®¯äº - OAuth2.0
- å¾®ä¿¡å ¬ä¼å¹³å° - OAuth ææ¡£
- 微信å¼æ¾å¹³å° - ç½ç«åºç¨å¾®ä¿¡ç»å½å¼åæå
- 微信å¼æ¾å¹³å° - ä»£å ¬ä¼å·åèµ·ç½é¡µææ
- ä¼ä¸å¾®ä¿¡ - OAuth ææ¡£
- ä¼ä¸å¾®ä¿¡ç¬¬ä¸æ¹åºç¨ - OAuth ææ¡£
- è±ç£ - OAuth 2.0 æææºå¶è¯´æ
- æé³ - ç½ç«åºç¨å¼åæå
- é£ä¹¦ - ææ说æ
- Lark - ææ说æ
- Tapd - ç¨æ·ææ说æ
- Line - OAuth 2.0
- Gitee - OAuthææ¡£
- PayPal - OAuthææ¡£
PHP æ©å±å å¼å
æ³ç¥éå¦ä½ä»é¶å¼å§æ建 PHP æ©å±å ï¼
è¯·å ³æ³¨æçå®æ课ç¨ï¼æä¼å¨æ¤è¯¾ç¨ä¸å享ä¸äºæ©å±å¼åç»éª ââ ãPHP æ©å±å å®ææç¨ - ä»å ¥é¨å°åå¸ã
License
MIT
Top Related Projects
Laravel wrapper around OAuth 1 & OAuth 2 libraries.
Open source social sign on PHP Library. HybridAuth goal is to act as an abstract api between your application and various social apis and identities providers such as Facebook, Twitter and Google.
Easy integration with OAuth 2.0 service providers.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot